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.settings.network;
18 
19 import static com.android.settingslib.wifi.WifiEnterpriseRestrictionUtils.isChangeWifiStateAllowed;
20 
21 import android.content.Context;
22 import android.net.wifi.WifiManager;
23 
24 import androidx.lifecycle.Lifecycle;
25 import androidx.lifecycle.LifecycleObserver;
26 import androidx.lifecycle.OnLifecycleEvent;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settings.widget.GenericSwitchController;
32 import com.android.settings.wifi.WifiEnabler;
33 import com.android.settingslib.RestrictedSwitchPreference;
34 import com.android.settingslib.core.AbstractPreferenceController;
35 
36 import com.google.common.annotations.VisibleForTesting;
37 
38 /**
39  * This controller helps to manage the state of wifi switch preference.
40  */
41 public class WifiSwitchPreferenceController extends AbstractPreferenceController implements
42         LifecycleObserver {
43 
44     public static final String KEY = "main_toggle_wifi";
45 
46     @VisibleForTesting
47     boolean mIsChangeWifiStateAllowed;
48     @VisibleForTesting
49     WifiEnabler mWifiEnabler;
50 
51     private RestrictedSwitchPreference mPreference;
52 
WifiSwitchPreferenceController(Context context, Lifecycle lifecycle)53     public WifiSwitchPreferenceController(Context context, Lifecycle lifecycle) {
54         super(context);
55         if (lifecycle == null) {
56             throw new IllegalArgumentException("Lifecycle must be set");
57         }
58 
59         lifecycle.addObserver(this);
60         mIsChangeWifiStateAllowed = isChangeWifiStateAllowed(context);
61     }
62 
63     @Override
getPreferenceKey()64     public String getPreferenceKey() {
65         return KEY;
66     }
67 
68     @Override
isAvailable()69     public boolean isAvailable() {
70         return true;
71     }
72 
73     @Override
displayPreference(PreferenceScreen screen)74     public void displayPreference(PreferenceScreen screen) {
75         super.displayPreference(screen);
76         mPreference = screen.findPreference(getPreferenceKey());
77         if (mPreference == null) return;
78 
79         mPreference.setChecked(isWifiEnabled());
80         if (!mIsChangeWifiStateAllowed) {
81             mPreference.setEnabled(false);
82             mPreference.setSummary(R.string.not_allowed_by_ent);
83         }
84     }
85 
86     /** Lifecycle.Event.ON_START */
87     @OnLifecycleEvent(Lifecycle.Event.ON_START)
onStart()88     public void onStart() {
89         // Don't use WifiEnabler when user is not allowed to change Wi-Fi state,
90         // Because the preference needs to be disabled when the user is not allowed to change the
91         // Wi-Fi state, but WifiEnabler will enable the preference when the Wi-Fi state changes.
92         if (mPreference != null && mIsChangeWifiStateAllowed) {
93             mWifiEnabler = new WifiEnabler(mContext, new GenericSwitchController(mPreference),
94                     FeatureFactory.getFeatureFactory().getMetricsFeatureProvider());
95         }
96     }
97 
98     /** Lifecycle.Event.ON_STOP */
99     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
onStop()100     public void onStop() {
101         if (mWifiEnabler != null) {
102             mWifiEnabler.teardownSwitchController();
103         }
104     }
105 
106     /** Lifecycle.Event.ON_RESUME */
107     @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
onResume()108     public void onResume() {
109         if (mWifiEnabler != null) {
110             mWifiEnabler.resume(mContext);
111         }
112     }
113 
114     /** Lifecycle.Event.ON_PAUSE */
115     @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
onPause()116     public void onPause() {
117         if (mWifiEnabler != null) {
118             mWifiEnabler.pause();
119         }
120     }
121 
isWifiEnabled()122     private boolean isWifiEnabled() {
123         WifiManager wifiManager = mContext.getSystemService(WifiManager.class);
124         if (wifiManager == null) return false;
125         return wifiManager.isWifiEnabled();
126     }
127 }
128