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_DEBUGGING_FEATURES;
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 
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.os.UserManager;
27 
28 import com.android.car.settings.common.FragmentController;
29 import com.android.car.settings.common.Logger;
30 import com.android.car.settings.common.PreferenceController;
31 import com.android.car.settings.development.DevelopmentSettingsUtil;
32 import com.android.car.settings.enterprise.EnterpriseUtils;
33 import com.android.car.ui.preference.CarUiPreference;
34 
35 
36 /** Controls the visibility of the developer options setting. */
37 public class DeveloperOptionsEntryPreferenceController
38         extends PreferenceController<CarUiPreference> {
39 
40     private static final Logger LOG = new Logger(DeveloperOptionsEntryPreferenceController.class);
41 
DeveloperOptionsEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)42     public DeveloperOptionsEntryPreferenceController(Context context, String preferenceKey,
43             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
44         super(context, preferenceKey, fragmentController, uxRestrictions);
45     }
46 
47     @Override
getPreferenceType()48     protected Class<CarUiPreference> getPreferenceType() {
49         return CarUiPreference.class;
50     }
51 
52     @Override
getAvailabilityStatus()53     protected int getAvailabilityStatus() {
54         if (DevelopmentSettingsUtil.isDevelopmentSettingsEnabled(getContext(),
55                 getContext().getSystemService(UserManager.class))) {
56             return AVAILABLE;
57         }
58 
59         if (hasUserRestrictionByDpm(getContext(), DISALLOW_DEBUGGING_FEATURES)) {
60             return AVAILABLE_FOR_VIEWING;
61         }
62 
63         return CONDITIONALLY_UNAVAILABLE;
64     }
65 
66     @Override
onCreateInternal()67     protected void onCreateInternal() {
68         super.onCreateInternal();
69         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> {
70             if (hasUserRestrictionByDpm(getContext(), DISALLOW_DEBUGGING_FEATURES)) {
71                 showActionDisabledByAdminDialog();
72             }
73         });
74     }
75 
76     @Override
handlePreferenceClicked(CarUiPreference preference)77     protected boolean handlePreferenceClicked(CarUiPreference preference) {
78         // We need to make sure the developer options module is enabled for the following reasons:
79         //  - To enable developer options by default on eng builds
80         //  - To enable developer options for all admin users when any admin user enables it
81         // This is because on first launch per user, the developer options module may be disabled
82         // while the setting is enabled, so we need to enable the module
83         LOG.d("handlePreferenceClicked");
84         if (!DevelopmentSettingsUtil.isDeveloperOptionsModuleEnabled(getContext())) {
85             LOG.i("Inconsistent state: developer options enabled, but developer options module "
86                     + "disabled. Enabling module...");
87             DevelopmentSettingsUtil.setDevelopmentSettingsEnabled(getContext(), /* enable= */
88                     true);
89         }
90 
91         getContext().startActivity(preference.getIntent());
92         return true;
93     }
94 
showActionDisabledByAdminDialog()95     private void showActionDisabledByAdminDialog() {
96         getFragmentController().showDialog(
97                 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(),
98                         DISALLOW_DEBUGGING_FEATURES),
99                 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG);
100     }
101 }
102