• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 import android.car.drivingstate.CarUxRestrictions;
24 import android.content.Context;
25 import android.os.UserHandle;
26 import android.os.UserManager;
27 import android.widget.Toast;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.ClickableWhileDisabledPreference;
31 import com.android.car.settings.common.FragmentController;
32 import com.android.car.settings.common.PreferenceController;
33 import com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment;
34 
35 /**
36  * Controller which determines if factory clear (aka "factory reset") should be displayed based on
37  * user status.
38  */
39 public class FactoryResetEntryPreferenceController
40         extends PreferenceController<ClickableWhileDisabledPreference> {
41 
42     private final UserManager mUserManager;
43 
FactoryResetEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44     public FactoryResetEntryPreferenceController(Context context, String preferenceKey,
45             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
46         super(context, preferenceKey, fragmentController, uxRestrictions);
47         mUserManager = UserManager.get(context);
48     }
49 
50     @Override
getPreferenceType()51     protected Class<ClickableWhileDisabledPreference> getPreferenceType() {
52         return ClickableWhileDisabledPreference.class;
53     }
54 
55     @Override
onCreateInternal()56     protected void onCreateInternal() {
57         super.onCreateInternal();
58         getPreference().setDisabledClickListener(p ->
59                 Toast.makeText(getContext(), getContext().getString(R.string.action_unavailable),
60                         Toast.LENGTH_LONG).show());
61     }
62 
63     @Override
handlePreferenceClicked(ClickableWhileDisabledPreference preference)64     protected boolean handlePreferenceClicked(ClickableWhileDisabledPreference preference) {
65         if (mUserManager.hasUserRestriction(DISALLOW_FACTORY_RESET)) {
66             getFragmentController().showDialog(ActionDisabledByAdminDialogFragment.newInstance(
67                     DISALLOW_FACTORY_RESET, UserHandle.USER_SYSTEM),
68                     DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG);
69             return true;
70         }
71         return super.handlePreferenceClicked(preference);
72     }
73 
74     @Override
getAvailabilityStatus()75     public int getAvailabilityStatus() {
76         return shouldDisable() ? AVAILABLE_FOR_VIEWING : AVAILABLE;
77     }
78 
shouldDisable()79     private boolean shouldDisable() {
80         if (!mUserManager.isAdminUser() && !isDemoUser()) {
81             // Disable for non-admin and non-demo users.
82             return true;
83         }
84         UserHandle userHandle = UserHandle.of(getContext().getUserId());
85         return mUserManager.hasBaseUserRestriction(DISALLOW_FACTORY_RESET, userHandle);
86     }
87 
isDemoUser()88     private boolean isDemoUser() {
89         return UserManager.isDeviceInDemoMode(getContext()) && mUserManager.isDemoUser();
90     }
91 }
92