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.wifi;
18 
19 import static android.os.UserManager.DISALLOW_ADD_WIFI_CONFIG;
20 
21 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm;
22 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm;
23 
24 import android.car.drivingstate.CarUxRestrictions;
25 import android.content.Context;
26 import android.net.wifi.WifiManager;
27 
28 import androidx.preference.Preference;
29 
30 import com.android.car.settings.common.FragmentController;
31 
32 /**
33  * Controls preference for adding wifi.
34  */
35 public class AddWifiPreferenceController extends WifiBasePreferenceController<Preference> {
36 
AddWifiPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     public AddWifiPreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
39         super(context, preferenceKey, fragmentController, uxRestrictions);
40     }
41 
42     @Override
getPreferenceType()43     protected Class<Preference> getPreferenceType() {
44         return Preference.class;
45     }
46 
47     @Override
onCreateInternal()48     protected void onCreateInternal() {
49         super.onCreateInternal();
50 
51         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> {
52             WifiUtil.runClickableWhileDisabled(getContext(), DISALLOW_ADD_WIFI_CONFIG,
53                     getFragmentController());
54         });
55     }
56 
57     @Override
getDefaultAvailabilityStatus()58     public int getDefaultAvailabilityStatus() {
59         // If the base controller is not available, so is this controller.
60         int superStatus = super.getDefaultAvailabilityStatus();
61         if (superStatus != AVAILABLE) return superStatus;
62 
63         // Check the DISALLOW_ADD_WIFI_CONFIG restriction.
64         Context context = getContext();
65         if (hasUserRestrictionByUm(context, DISALLOW_ADD_WIFI_CONFIG)
66                 || hasUserRestrictionByDpm(context, DISALLOW_ADD_WIFI_CONFIG)) {
67             return AVAILABLE_FOR_VIEWING;
68         }
69 
70         return AVAILABLE;
71     }
72 
73     @Override
onWifiStateChanged(int state)74     public void onWifiStateChanged(int state) {
75         switch (state) {
76             case WifiManager.WIFI_STATE_DISABLED:
77                 getPreference().setVisible(false);
78                 break;
79             default:
80                 getPreference().setVisible(true);
81         }
82     }
83 }