1 /*
2  * Copyright (C) 2018 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.system;
18 
19 import static android.os.UserManager.DISALLOW_FACTORY_RESET;
20 
21 import static com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment.DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG;
22 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm;
23 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm;
24 
25 import android.car.drivingstate.CarUxRestrictions;
26 import android.content.Context;
27 import android.widget.Toast;
28 
29 import androidx.preference.Preference;
30 
31 import com.android.car.settings.R;
32 import com.android.car.settings.common.FragmentController;
33 import com.android.car.settings.common.PreferenceController;
34 import com.android.car.settings.enterprise.EnterpriseUtils;
35 
36 /**
37  * Controller which determines if factory clear (aka "factory reset") should be displayed based on
38  * user status.
39  */
40 public class FactoryResetEntryPreferenceController
41         extends PreferenceController<Preference> {
42 
FactoryResetEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public FactoryResetEntryPreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
45         super(context, preferenceKey, fragmentController, uxRestrictions);
46     }
47 
48     @Override
getPreferenceType()49     protected Class<Preference> getPreferenceType() {
50         return Preference.class;
51     }
52 
53     @Override
onCreateInternal()54     protected void onCreateInternal() {
55         super.onCreateInternal();
56         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> {
57             if (hasUserRestrictionByDpm(getContext(), DISALLOW_FACTORY_RESET)) {
58                 showActionDisabledByAdminDialog();
59             } else {
60                 Toast.makeText(getContext(), getContext().getString(R.string.action_unavailable),
61                         Toast.LENGTH_LONG).show();
62             }
63         });
64     }
65 
66     @Override
getDefaultAvailabilityStatus()67     public int getDefaultAvailabilityStatus() {
68         if (!isAlwaysAvailableForUser()
69                 || hasUserRestrictionByUm(getContext(), DISALLOW_FACTORY_RESET)
70                 || hasUserRestrictionByDpm(getContext(), DISALLOW_FACTORY_RESET)) {
71             return AVAILABLE_FOR_VIEWING;
72         }
73         return AVAILABLE;
74     }
75 
isAlwaysAvailableForUser()76     private boolean isAlwaysAvailableForUser() {
77         return EnterpriseUtils.isAdminUser(getContext())
78                 || EnterpriseUtils.isDemoUser(getContext());
79     }
80 
showActionDisabledByAdminDialog()81     private void showActionDisabledByAdminDialog() {
82         getFragmentController().showDialog(
83                 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(),
84                         DISALLOW_FACTORY_RESET),
85                 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG);
86     }
87 }
88