• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car.settings.users;
18 
19 import android.content.Context;
20 import android.content.pm.UserInfo;
21 
22 import androidx.annotation.Nullable;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.ConfirmationDialogFragment;
26 
27 /**
28  * Provides common Users-related ConfirmationDialogFragments to ensure consistency
29  */
30 public final class UsersDialogProvider {
31 
32     /** Argument key to store the user info that the device is trying to make an admin. */
33     static final String KEY_USER_TO_MAKE_ADMIN = "USER_TO_MAKE_ADMIN";
34     /** Argument key to store the user type that the device is trying to remove. */
35     static final String KEY_USER_TYPE = "USER_TYPE";
36     /** {@link KEY_USER_TYPE} value when removing the last admin on the device. */
37     static final String LAST_ADMIN = "LAST_ADMIN";
38     /** {@link KEY_USER_TYPE} value when removing the last user on the device. */
39     static final String LAST_USER = "LAST_USER";
40     /**
41      * Default {@link KEY_USER_TYPE} value when removing a user that is neither {@link LAST_ADMIN}
42      * nor {@link LAST_USER}.
43      */
44     static final String ANY_USER = "ANY_USER";
45 
UsersDialogProvider()46     private UsersDialogProvider() {
47     }
48 
49     /** Gets a confirmation dialog fragment to confirm or reject adding a new user. */
getConfirmCreateNewUserDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)50     public static ConfirmationDialogFragment getConfirmCreateNewUserDialogFragment(Context context,
51             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
52             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
53 
54         String message = context.getString(R.string.user_add_user_message_setup)
55                 .concat(System.lineSeparator())
56                 .concat(System.lineSeparator())
57                 .concat(context.getString(R.string.user_add_user_message_update));
58 
59         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
60                 .setTitle(R.string.user_add_user_title)
61                 .setMessage(message)
62                 .setPositiveButton(android.R.string.ok, confirmListener)
63                 .setNegativeButton(android.R.string.cancel, rejectListener)
64                 .build();
65 
66         return dialogFragment;
67     }
68 
69     /** Gets a confirmation dialog fragment to confirm or reject making a user an admin. */
getConfirmGrantAdminDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener, UserInfo userToMakeAdmin)70     public static ConfirmationDialogFragment getConfirmGrantAdminDialogFragment(Context context,
71             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
72             @Nullable ConfirmationDialogFragment.RejectListener rejectListener,
73             UserInfo userToMakeAdmin) {
74 
75         String message = context.getString(R.string.grant_admin_permissions_message)
76                 .concat(System.lineSeparator())
77                 .concat(System.lineSeparator())
78                 .concat(context.getString(R.string.action_not_reversible_message));
79 
80         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
81                 .setTitle(R.string.grant_admin_permissions_title)
82                 .setMessage(message)
83                 .setPositiveButton(R.string.confirm_grant_admin, confirmListener)
84                 .setNegativeButton(android.R.string.cancel, rejectListener)
85                 .addArgumentParcelable(KEY_USER_TO_MAKE_ADMIN, userToMakeAdmin)
86                 .build();
87 
88         return dialogFragment;
89     }
90 
91     /**
92      * Gets a confirmation dialog fragment to confirm or reject removing the last user on the
93      * device.
94      */
getConfirmRemoveLastUserDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)95     public static ConfirmationDialogFragment getConfirmRemoveLastUserDialogFragment(Context context,
96             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
97             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
98 
99         String message = context.getString(R.string.delete_last_user_admin_created_message)
100                 .concat(System.lineSeparator())
101                 .concat(System.lineSeparator())
102                 .concat(context.getString(R.string.delete_last_user_system_setup_required_message));
103 
104         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
105                 .setTitle(R.string.delete_last_user_dialog_title)
106                 .setMessage(message)
107                 .setPositiveButton(R.string.delete_button, confirmListener)
108                 .setNegativeButton(android.R.string.cancel, rejectListener)
109                 .addArgumentString(KEY_USER_TYPE, LAST_USER)
110                 .build();
111 
112         return dialogFragment;
113     }
114 
115     /**
116      * Gets a confirmation dialog fragment to confirm or reject removing the last admin user on the
117      * device.
118      */
getConfirmRemoveLastAdminDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)119     public static ConfirmationDialogFragment getConfirmRemoveLastAdminDialogFragment(
120             Context context,
121             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
122             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
123 
124         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
125                 .setTitle(R.string.choose_new_admin_title)
126                 .setMessage(R.string.choose_new_admin_message)
127                 .setPositiveButton(R.string.choose_new_admin_label, confirmListener)
128                 .setNegativeButton(android.R.string.cancel, rejectListener)
129                 .addArgumentString(KEY_USER_TYPE, LAST_ADMIN)
130                 .build();
131 
132         return dialogFragment;
133     }
134 
135     /**
136      * Gets a confirmation dialog fragment to confirm or reject removing a user that is neither the
137      * last admin nor the last user on the device.
138      */
getConfirmRemoveUserDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)139     public static ConfirmationDialogFragment getConfirmRemoveUserDialogFragment(Context context,
140             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
141             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
142 
143         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
144                 .setTitle(R.string.delete_user_dialog_title)
145                 .setMessage(R.string.delete_user_dialog_message)
146                 .setPositiveButton(R.string.delete_button, confirmListener)
147                 .setNegativeButton(android.R.string.cancel, rejectListener)
148                 .addArgumentString(KEY_USER_TYPE, ANY_USER)
149                 .build();
150 
151         return dialogFragment;
152     }
153 }
154