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 exiting retail mode. */
getConfirmExitRetailModeDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)70     public static ConfirmationDialogFragment getConfirmExitRetailModeDialogFragment(Context context,
71             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
72             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
73         return new ConfirmationDialogFragment.Builder(context)
74                 .setTitle(R.string.exit_retail_mode_dialog_title)
75                 .setMessage(R.string.exit_retail_mode_dialog_body)
76                 .setPositiveButton(
77                         R.string.exit_retail_mode_dialog_confirmation_button_text, confirmListener)
78                 .setNegativeButton(android.R.string.cancel, rejectListener)
79                 .build();
80     }
81 
82     /**
83      * Gets a confirmation dialog fragment to indicate the maximum allowed number of users is
84      * reached.
85      */
getMaxUsersLimitReachedDialogFragment(Context context, int maxUserLimit)86     public static ConfirmationDialogFragment getMaxUsersLimitReachedDialogFragment(Context context,
87             int maxUserLimit) {
88         return new ConfirmationDialogFragment.Builder(context)
89                 .setTitle(R.string.user_limit_reached_title)
90                 .setMessage(context.getResources().getQuantityString(
91                         R.plurals.user_limit_reached_message, maxUserLimit, maxUserLimit))
92                 .setPositiveButton(android.R.string.ok, /* confirmListener= */ null)
93                 .build();
94     }
95 
96     /** 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)97     public static ConfirmationDialogFragment getConfirmGrantAdminDialogFragment(Context context,
98             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
99             @Nullable ConfirmationDialogFragment.RejectListener rejectListener,
100             UserInfo userToMakeAdmin) {
101 
102         String message = context.getString(R.string.grant_admin_permissions_message)
103                 .concat(System.lineSeparator())
104                 .concat(System.lineSeparator())
105                 .concat(context.getString(R.string.action_not_reversible_message));
106 
107         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
108                 .setTitle(R.string.grant_admin_permissions_title)
109                 .setMessage(message)
110                 .setPositiveButton(R.string.confirm_grant_admin, confirmListener)
111                 .setNegativeButton(android.R.string.cancel, rejectListener)
112                 .addArgumentParcelable(KEY_USER_TO_MAKE_ADMIN, userToMakeAdmin)
113                 .build();
114 
115         return dialogFragment;
116     }
117 
118     /**
119      * Gets a confirmation dialog fragment to confirm or reject removing the last user on the
120      * device.
121      */
getConfirmRemoveLastUserDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)122     public static ConfirmationDialogFragment getConfirmRemoveLastUserDialogFragment(Context context,
123             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
124             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
125 
126         String message = context.getString(R.string.delete_last_user_admin_created_message)
127                 .concat(System.lineSeparator())
128                 .concat(System.lineSeparator())
129                 .concat(context.getString(R.string.delete_last_user_system_setup_required_message));
130 
131         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
132                 .setTitle(R.string.delete_last_user_dialog_title)
133                 .setMessage(message)
134                 .setPositiveButton(R.string.delete_button, confirmListener)
135                 .setNegativeButton(android.R.string.cancel, rejectListener)
136                 .addArgumentString(KEY_USER_TYPE, LAST_USER)
137                 .build();
138 
139         return dialogFragment;
140     }
141 
142     /**
143      * Gets a confirmation dialog fragment to confirm or reject removing the last admin user on the
144      * device.
145      */
getConfirmRemoveLastAdminDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)146     public static ConfirmationDialogFragment getConfirmRemoveLastAdminDialogFragment(
147             Context context,
148             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
149             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
150 
151         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
152                 .setTitle(R.string.choose_new_admin_title)
153                 .setMessage(R.string.choose_new_admin_message)
154                 .setPositiveButton(R.string.choose_new_admin_label, confirmListener)
155                 .setNegativeButton(android.R.string.cancel, rejectListener)
156                 .addArgumentString(KEY_USER_TYPE, LAST_ADMIN)
157                 .build();
158 
159         return dialogFragment;
160     }
161 
162     /**
163      * Gets a confirmation dialog fragment to confirm or reject removing a user that is neither the
164      * last admin nor the last user on the device.
165      */
getConfirmRemoveUserDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)166     public static ConfirmationDialogFragment getConfirmRemoveUserDialogFragment(Context context,
167             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
168             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
169 
170         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
171                 .setTitle(R.string.delete_user_dialog_title)
172                 .setMessage(R.string.delete_user_dialog_message)
173                 .setPositiveButton(R.string.delete_button, confirmListener)
174                 .setNegativeButton(android.R.string.cancel, rejectListener)
175                 .addArgumentString(KEY_USER_TYPE, ANY_USER)
176                 .build();
177 
178         return dialogFragment;
179     }
180 }
181