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_NETWORK_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 /** Controller which determines if network reset should be displayed based on user status. */
37 public class ResetNetworkEntryPreferenceController extends PreferenceController<Preference> {
38 
ResetNetworkEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)39     public ResetNetworkEntryPreferenceController(Context context, String preferenceKey,
40             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
41         super(context, preferenceKey, fragmentController, uxRestrictions);
42     }
43 
44     @Override
getPreferenceType()45     protected Class<Preference> getPreferenceType() {
46         return Preference.class;
47     }
48 
49     @Override
onCreateInternal()50     protected void onCreateInternal() {
51         super.onCreateInternal();
52         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> {
53             if (hasUserRestrictionByDpm(getContext(), DISALLOW_NETWORK_RESET)) {
54                 showActionDisabledByAdminDialog();
55             } else {
56                 // Currently, there is no use case when AVAILABLE_FOR_VIEWING other than restricted
57                 // by DPM
58                 Toast.makeText(getContext(), getContext().getString(R.string.action_unavailable),
59                         Toast.LENGTH_LONG).show();
60             }
61         });
62     }
63 
64     @Override
getAvailabilityStatus()65     public int getAvailabilityStatus() {
66         if (!isAlwaysAvailableForUser()
67                 || hasUserRestrictionByUm(getContext(), DISALLOW_NETWORK_RESET)) {
68             return DISABLED_FOR_PROFILE;
69         }
70 
71         if (hasUserRestrictionByDpm(getContext(), DISALLOW_NETWORK_RESET)) {
72             return AVAILABLE_FOR_VIEWING;
73         }
74         return AVAILABLE;
75     }
76 
isAlwaysAvailableForUser()77     private boolean isAlwaysAvailableForUser() {
78         return EnterpriseUtils.isAdminUser(getContext());
79     }
80 
showActionDisabledByAdminDialog()81     private void showActionDisabledByAdminDialog() {
82         getFragmentController().showDialog(
83                 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(),
84                         DISALLOW_NETWORK_RESET),
85                 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG);
86     }
87 }
88