1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.users;
18 
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.RadioButton;
25 import android.widget.RadioGroup;
26 
27 import com.android.settingslib.R;
28 
29 import java.util.function.Consumer;
30 
31 /**
32  * This class encapsulates a Dialog for choosing whether to grant admin privileges.
33  */
34 public class GrantAdminDialogController {
35 
36     /**
37      * Creates a dialog with option to grant user admin privileges.
38      */
createDialog(Activity activity, Consumer<Boolean> successCallback, Runnable cancelCallback)39     public Dialog createDialog(Activity activity,
40             Consumer<Boolean> successCallback, Runnable cancelCallback) {
41         LayoutInflater inflater = LayoutInflater.from(activity);
42         View content = inflater.inflate(R.layout.grant_admin_dialog_content, null);
43         RadioGroup radioGroup = content.findViewById(R.id.choose_admin);
44         RadioButton radioButton = radioGroup.findViewById(R.id.grant_admin_yes);
45         radioButton.setChecked(true);
46         Dialog dlg = new AlertDialog.Builder(activity)
47                 .setView(content)
48                 .setTitle(R.string.user_grant_admin_title)
49                 .setMessage(R.string.user_grant_admin_message)
50                 .setPositiveButton(android.R.string.ok,
51                         (dialog, which) ->  {
52                             if (successCallback != null) {
53                                 successCallback.accept(radioButton.isChecked());
54                             }
55                         })
56                 .setNegativeButton(android.R.string.cancel, (dialog, which) -> {
57                     if (cancelCallback != null) {
58                         cancelCallback.run();
59                     }
60                 })
61                 .setOnCancelListener(dialog -> {
62                     if (cancelCallback != null) {
63                         cancelCallback.run();
64                     }
65                 })
66                 .create();
67 
68         return dlg;
69     }
70 
71 }
72