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.profiles;
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 import com.android.settingslib.utils.StringUtil;
27 
28 /**
29  * Provides common Profiles-related ConfirmationDialogFragments to ensure consistency
30  */
31 public final class ProfilesDialogProvider {
32 
33     /** Argument key to store the profile info that the device is trying to make an admin. */
34     static final String KEY_PROFILE_TO_MAKE_ADMIN = "PROFILE_TO_MAKE_ADMIN";
35     /** Argument key to store the profile type that the device is trying to remove. */
36     static final String KEY_PROFILE_TYPE = "PROFILE_TYPE";
37     /** {@link KEY_PROFILE_TYPE} value when removing the last admin on the device. */
38     static final String LAST_ADMIN = "LAST_ADMIN";
39     /** {@link KEY_PROFILE_TYPE} value when removing the last profile on the device. */
40     static final String LAST_PROFILE = "LAST_PROFILE";
41     /**
42      * Default {@link KEY_PROFILE_TYPE} value when removing a profile that is neither
43      * {@link LAST_ADMIN} nor {@link LAST_PROFILE}.
44      */
45     static final String ANY_PROFILE = "ANY_PROFILE";
46 
ProfilesDialogProvider()47     private ProfilesDialogProvider() {
48     }
49 
50     /** Gets a confirmation dialog fragment to confirm or reject adding a new profile. */
getConfirmCreateNewProfileDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)51     public static ConfirmationDialogFragment getConfirmCreateNewProfileDialogFragment(
52             Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
53             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
54 
55         String message = context.getString(R.string.user_add_user_message_setup)
56                 .concat(System.lineSeparator())
57                 .concat(System.lineSeparator())
58                 .concat(context.getString(R.string.user_add_user_message_update));
59 
60         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
61                 .setTitle(R.string.user_add_user_title)
62                 .setMessage(message)
63                 .setPositiveButton(android.R.string.ok, confirmListener)
64                 .setNegativeButton(android.R.string.cancel, rejectListener)
65                 .build();
66 
67         return dialogFragment;
68     }
69 
70     /** Gets a confirmation dialog fragment to confirm or reject exiting retail mode. */
getConfirmExitRetailModeDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)71     public static ConfirmationDialogFragment getConfirmExitRetailModeDialogFragment(Context context,
72             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
73             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
74         return new ConfirmationDialogFragment.Builder(context)
75                 .setTitle(R.string.exit_retail_mode_dialog_title)
76                 .setMessage(R.string.exit_retail_mode_dialog_body)
77                 .setPositiveButton(
78                         R.string.exit_retail_mode_dialog_confirmation_button_text, confirmListener)
79                 .setNegativeButton(android.R.string.cancel, rejectListener)
80                 .build();
81     }
82 
83     /**
84      * Gets a confirmation dialog fragment to indicate the maximum allowed number of profiles is
85      * reached.
86      */
getMaxProfilesLimitReachedDialogFragment( Context context, int maxProfileLimit)87     public static ConfirmationDialogFragment getMaxProfilesLimitReachedDialogFragment(
88             Context context, int maxProfileLimit) {
89         return new ConfirmationDialogFragment.Builder(context)
90                 .setTitle(R.string.user_limit_reached_title)
91                 .setMessage(StringUtil.getIcuPluralsString(context, maxProfileLimit,
92                         R.string.user_limit_reached_message))
93                 .setPositiveButton(android.R.string.ok, /* confirmListener= */ null)
94                 .build();
95     }
96 
97     /** Gets a confirmation dialog fragment to confirm or reject making a profile an admin. */
getConfirmGrantAdminDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener, UserInfo userToMakeAdmin)98     public static ConfirmationDialogFragment getConfirmGrantAdminDialogFragment(Context context,
99             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
100             @Nullable ConfirmationDialogFragment.RejectListener rejectListener,
101             UserInfo userToMakeAdmin) {
102 
103         String message = context.getString(R.string.grant_admin_permissions_message)
104                 .concat(System.lineSeparator())
105                 .concat(System.lineSeparator())
106                 .concat(context.getString(R.string.action_not_reversible_message));
107 
108         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
109                 .setTitle(R.string.grant_admin_permissions_title)
110                 .setMessage(message)
111                 .setPositiveButton(R.string.confirm_grant_admin, confirmListener)
112                 .setNegativeButton(android.R.string.cancel, rejectListener)
113                 .addArgumentParcelable(KEY_PROFILE_TO_MAKE_ADMIN, userToMakeAdmin)
114                 .build();
115 
116         return dialogFragment;
117     }
118 
119     /**
120      * Gets a confirmation dialog fragment to confirm or reject removing the last profile on the
121      * device.
122      */
getConfirmRemoveLastProfileDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)123     public static ConfirmationDialogFragment getConfirmRemoveLastProfileDialogFragment(
124             Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
125             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
126 
127         String message = context.getString(R.string.delete_last_user_delete_warning)
128                 .concat(System.lineSeparator())
129                 .concat(System.lineSeparator())
130                 .concat(context.getString(R.string.delete_last_user_system_setup_required_message));
131 
132         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
133                 .setTitle(R.string.delete_last_user_dialog_title)
134                 .setMessage(message)
135                 .setPositiveButton(R.string.delete_button, confirmListener)
136                 .setNegativeButton(android.R.string.cancel, rejectListener)
137                 .addArgumentString(KEY_PROFILE_TYPE, LAST_PROFILE)
138                 .build();
139 
140         return dialogFragment;
141     }
142 
143     /**
144      * Gets a confirmation dialog fragment to confirm or reject removing the last admin profile on
145      * the device.
146      */
getConfirmRemoveLastAdminDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)147     public static ConfirmationDialogFragment getConfirmRemoveLastAdminDialogFragment(
148             Context context,
149             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
150             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
151 
152         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
153                 .setTitle(R.string.choose_new_admin_title)
154                 .setMessage(R.string.choose_new_admin_message)
155                 .setPositiveButton(R.string.choose_new_admin_label, confirmListener)
156                 .setNegativeButton(android.R.string.cancel, rejectListener)
157                 .addArgumentString(KEY_PROFILE_TYPE, LAST_ADMIN)
158                 .build();
159 
160         return dialogFragment;
161     }
162 
163     /**
164      * Gets a confirmation dialog fragment to confirm or reject removing a profile that is neither
165      * the last admin nor the last profile on the device.
166      */
getConfirmRemoveProfileDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)167     public static ConfirmationDialogFragment getConfirmRemoveProfileDialogFragment(Context context,
168             @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
169             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
170 
171         ConfirmationDialogFragment dialogFragment = new ConfirmationDialogFragment.Builder(context)
172                 .setTitle(R.string.delete_user_dialog_title)
173                 .setMessage(R.string.delete_user_dialog_message)
174                 .setPositiveButton(R.string.delete_button, confirmListener)
175                 .setNegativeButton(android.R.string.cancel, rejectListener)
176                 .addArgumentString(KEY_PROFILE_TYPE, ANY_PROFILE)
177                 .build();
178 
179         return dialogFragment;
180     }
181 }
182