1 /*
2  * Copyright (C) 2020 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.car.hardware.power.PowerComponent.WIFI;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 import android.net.wifi.WifiManager;
24 import android.widget.Toast;
25 
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.ColoredSwitchPreference;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.PowerPolicyListener;
32 
33 /**
34  * Enables/disables Wifi state via ColoredSwitchPreference.
35  */
36 public class WifiStateSwitchPreferenceController extends
37         WifiBasePreferenceController<ColoredSwitchPreference> {
38 
39     @VisibleForTesting
40     final PowerPolicyListener mPowerPolicyListener;
41 
42     private boolean mIsPowerPolicyOn = true;
43     private boolean mIsWifiEnabled;
44 
WifiStateSwitchPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)45     public WifiStateSwitchPreferenceController(Context context, String preferenceKey,
46             FragmentController fragmentController,
47             CarUxRestrictions uxRestrictions) {
48         super(context, preferenceKey, fragmentController, uxRestrictions);
49         mPowerPolicyListener = new PowerPolicyListener(context, WIFI, isOn -> {
50             // refresh power state
51             mIsPowerPolicyOn = isOn;
52             refreshUi();
53         });
54     }
55 
56     @Override
getPreferenceType()57     protected Class<ColoredSwitchPreference> getPreferenceType() {
58         return ColoredSwitchPreference.class;
59     }
60 
61     @Override
updateState(ColoredSwitchPreference preference)62     protected void updateState(ColoredSwitchPreference preference) {
63         updateSwitchPreference();
64     }
65 
66     @Override
handlePreferenceChanged(ColoredSwitchPreference preference, Object newValue)67     protected boolean handlePreferenceChanged(ColoredSwitchPreference preference,
68             Object newValue) {
69         boolean wifiEnabled = (Boolean) newValue;
70         getCarWifiManager().setWifiEnabled(wifiEnabled);
71         return true;
72     }
73 
74     @Override
onCreateInternal()75     protected void onCreateInternal() {
76         super.onCreateInternal();
77         getPreference().setContentDescription(
78                 getContext().getString(R.string.wifi_state_switch_content_description));
79         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> {
80             if (mIsPowerPolicyOn) {
81                 // ActionDisabledByAdminDialog will be shown if DISALLOW_CONFIG_WIFI
82                 // is set by a device admin; otherwise, a default Toast will be shown
83                 WifiUtil.runClickableWhileDisabled(getContext(), getFragmentController());
84             } else {
85                 Toast.makeText(getContext(),
86                         getContext().getString(R.string.power_component_disabled),
87                         Toast.LENGTH_LONG).show();
88             }
89         });
90     }
91 
92     @Override
onStartInternal()93     protected void onStartInternal() {
94         super.onStartInternal();
95         onWifiStateChanged(getCarWifiManager().getWifiState());
96     }
97 
98     @Override
onResumeInternal()99     protected void onResumeInternal() {
100         mPowerPolicyListener.handleCurrentPolicy();
101     }
102 
103     @Override
onDestroyInternal()104     protected void onDestroyInternal() {
105         mPowerPolicyListener.release();
106     }
107 
108     @Override
onWifiEntriesChanged()109     public void onWifiEntriesChanged() {
110         // intentional no-op
111     }
112 
113     @Override
onWifiStateChanged(int state)114     public void onWifiStateChanged(int state) {
115         mIsWifiEnabled = (state == WifiManager.WIFI_STATE_ENABLED
116                 || state == WifiManager.WIFI_STATE_ENABLING);
117         refreshUi();
118     }
119 
updateSwitchPreference()120     private void updateSwitchPreference() {
121         getPreference().setChecked(mIsWifiEnabled);
122     }
123 
124     @Override
getDefaultAvailabilityStatus()125     public int getDefaultAvailabilityStatus() {
126         if (mIsPowerPolicyOn) {
127             return WifiUtil.getAvailabilityStatus(getContext());
128         }
129         return AVAILABLE_FOR_VIEWING;
130     }
131 }
132