1 /*
2  * Copyright (C) 2017 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.wifi.tether;
18 
19 import android.content.Context;
20 import android.net.wifi.SoftApConfiguration;
21 import android.net.wifi.WifiManager;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.Preference;
25 import androidx.preference.TwoStatePreference;
26 
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.overlay.FeatureFactory;
29 import com.android.settings.wifi.repository.WifiHotspotRepository;
30 
31 public class WifiTetherAutoOffPreferenceController extends BasePreferenceController implements
32         Preference.OnPreferenceChangeListener {
33 
34     private final WifiManager mWifiManager;
35     private boolean mSettingsOn;
36     @VisibleForTesting
37     boolean mNeedShutdownSecondarySap;
38 
WifiTetherAutoOffPreferenceController(Context context, String preferenceKey)39     public WifiTetherAutoOffPreferenceController(Context context, String preferenceKey) {
40         super(context, preferenceKey);
41         WifiHotspotRepository wifiHotspotRepository = FeatureFactory.getFeatureFactory()
42                 .getWifiFeatureProvider().getWifiHotspotRepository();
43         if (wifiHotspotRepository.isSpeedFeatureAvailable() && wifiHotspotRepository.isDualBand()) {
44             mNeedShutdownSecondarySap = true;
45         }
46         mWifiManager = context.getSystemService(WifiManager.class);
47     }
48 
49     @Override
getAvailabilityStatus()50     public int getAvailabilityStatus() {
51         return AVAILABLE;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         SoftApConfiguration softApConfiguration = mWifiManager.getSoftApConfiguration();
57         mSettingsOn = softApConfiguration.isAutoShutdownEnabled();
58 
59         ((TwoStatePreference) preference).setChecked(mSettingsOn);
60     }
61 
62     @Override
onPreferenceChange(Preference preference, Object newValue)63     public boolean onPreferenceChange(Preference preference, Object newValue) {
64         boolean settingsOn = (Boolean) newValue;
65         SoftApConfiguration.Builder configBuilder =
66                 new SoftApConfiguration.Builder(mWifiManager.getSoftApConfiguration());
67         configBuilder.setAutoShutdownEnabled(settingsOn);
68         if (mNeedShutdownSecondarySap) {
69             configBuilder.setBridgedModeOpportunisticShutdownEnabled(settingsOn);
70         }
71         mSettingsOn = settingsOn;
72         return mWifiManager.setSoftApConfiguration(configBuilder.build());
73     }
74 
isEnabled()75     public boolean isEnabled() {
76         return mSettingsOn;
77     }
78 }
79