• 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.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 
27 /**
28  * Provides common Profiles-related ConfirmationDialogFragments to ensure consistency
29  */
30 public final class ProfilesDialogProvider {
31 
32     /** Argument key to store the profile info that the device is trying to make an admin. */
33     static final String KEY_PROFILE_TO_MAKE_ADMIN = "PROFILE_TO_MAKE_ADMIN";
34     /** Argument key to store the profile type that the device is trying to remove. */
35     static final String KEY_PROFILE_TYPE = "PROFILE_TYPE";
36     /** {@link KEY_PROFILE_TYPE} value when removing the last admin on the device. */
37     static final String LAST_ADMIN = "LAST_ADMIN";
38     /** {@link KEY_PROFILE_TYPE} value when removing the last profile on the device. */
39     static final String LAST_PROFILE = "LAST_PROFILE";
40     /**
41      * Default {@link KEY_PROFILE_TYPE} value when removing a profile that is neither
42      * {@link LAST_ADMIN} nor {@link LAST_PROFILE}.
43      */
44     static final String ANY_PROFILE = "ANY_PROFILE";
45 
ProfilesDialogProvider()46     private ProfilesDialogProvider() {
47     }
48 
49     /** Gets a confirmation dialog fragment to confirm or reject adding a new profile. */
getConfirmCreateNewProfileDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)50     public static ConfirmationDialogFragment getConfirmCreateNewProfileDialogFragment(
51             Context context, @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 profiles is
84      * reached.
85      */
getMaxProfilesLimitReachedDialogFragment( Context context, int maxProfileLimit)86     public static ConfirmationDialogFragment getMaxProfilesLimitReachedDialogFragment(
87             Context context, int maxProfileLimit) {
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, maxProfileLimit, maxProfileLimit))
92                 .setPositiveButton(android.R.string.ok, /* confirmListener= */ null)
93                 .build();
94     }
95 
96     /** 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)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_PROFILE_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 profile on the
120      * device.
121      */
getConfirmRemoveLastProfileDialogFragment( Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)122     public static ConfirmationDialogFragment getConfirmRemoveLastProfileDialogFragment(
123             Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener,
124             @Nullable ConfirmationDialogFragment.RejectListener rejectListener) {
125 
126         String message = context.getString(R.string.delete_last_user_delete_warning)
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_PROFILE_TYPE, LAST_PROFILE)
137                 .build();
138 
139         return dialogFragment;
140     }
141 
142     /**
143      * Gets a confirmation dialog fragment to confirm or reject removing the last admin profile on
144      * the 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_PROFILE_TYPE, LAST_ADMIN)
157                 .build();
158 
159         return dialogFragment;
160     }
161 
162     /**
163      * Gets a confirmation dialog fragment to confirm or reject removing a profile that is neither
164      * the last admin nor the last profile on the device.
165      */
getConfirmRemoveProfileDialogFragment(Context context, @Nullable ConfirmationDialogFragment.ConfirmListener confirmListener, @Nullable ConfirmationDialogFragment.RejectListener rejectListener)166     public static ConfirmationDialogFragment getConfirmRemoveProfileDialogFragment(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_PROFILE_TYPE, ANY_PROFILE)
176                 .build();
177 
178         return dialogFragment;
179     }
180 }
181