• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.settings.AllInOneTetherSettings.DEDUP_POSTFIX;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.net.wifi.SoftApConfiguration;
24 import android.util.FeatureFlagUtils;
25 import android.util.Log;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.ListPreference;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.FeatureFlags;
33 
34 public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController {
35 
36     private static final String TAG = "WifiTetherApBandPref";
37     private static final String PREF_KEY = "wifi_tether_network_ap_band";
38 
39     private String[] mBandEntries;
40     private String[] mBandSummaries;
41     private int mBandIndex;
42 
WifiTetherApBandPreferenceController(Context context, OnTetherConfigUpdateListener listener)43     public WifiTetherApBandPreferenceController(Context context,
44             OnTetherConfigUpdateListener listener) {
45         super(context, listener);
46         updatePreferenceEntries();
47     }
48 
49     @Override
updateDisplay()50     public void updateDisplay() {
51         final SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
52         if (config == null) {
53             mBandIndex = SoftApConfiguration.BAND_2GHZ;
54             Log.d(TAG, "Updating band index to BAND_2GHZ because no config");
55         } else if (is5GhzBandSupported()) {
56             mBandIndex = validateSelection(config.getBand());
57             Log.d(TAG, "Updating band index to " + mBandIndex);
58         } else {
59             mWifiManager.setSoftApConfiguration(
60                     new SoftApConfiguration.Builder(config).setBand(SoftApConfiguration.BAND_2GHZ)
61                         .build());
62             mBandIndex = SoftApConfiguration.BAND_2GHZ;
63             Log.d(TAG, "5Ghz not supported, updating band index to 2GHz");
64         }
65         ListPreference preference =
66                 (ListPreference) mPreference;
67         preference.setEntries(mBandSummaries);
68         preference.setEntryValues(mBandEntries);
69 
70         if (!is5GhzBandSupported()) {
71             preference.setEnabled(false);
72             preference.setSummary(R.string.wifi_ap_choose_2G);
73         } else {
74             preference.setValue(Integer.toString(config.getBand()));
75             preference.setSummary(getConfigSummary());
76         }
77     }
78 
getConfigSummary()79     String getConfigSummary() {
80         switch (mBandIndex) {
81             case SoftApConfiguration.BAND_2GHZ:
82                 return mBandSummaries[0];
83             case SoftApConfiguration.BAND_5GHZ:
84                 return mBandSummaries[1];
85             default:
86                 return mContext.getString(R.string.wifi_ap_prefer_5G);
87         }
88     }
89 
90     @Override
getPreferenceKey()91     public String getPreferenceKey() {
92         return FeatureFlagUtils.isEnabled(mContext, FeatureFlags.TETHER_ALL_IN_ONE)
93                 ? PREF_KEY + DEDUP_POSTFIX : PREF_KEY;
94     }
95 
96     @Override
onPreferenceChange(Preference preference, Object newValue)97     public boolean onPreferenceChange(Preference preference, Object newValue) {
98         mBandIndex = validateSelection(Integer.parseInt((String) newValue));
99         Log.d(TAG, "Band preference changed, updating band index to " + mBandIndex);
100         preference.setSummary(getConfigSummary());
101         mListener.onTetherConfigUpdated(this);
102         return true;
103     }
104 
validateSelection(int band)105     private int validateSelection(int band) {
106         // unsupported states:
107         // 1: BAND_5GHZ only - include 2GHZ since some of countries doesn't support 5G hotspot
108         // 2: no 5 GHZ support means we can't have BAND_5GHZ - default to 2GHZ
109         if (SoftApConfiguration.BAND_5GHZ == band) {
110             if (!is5GhzBandSupported()) {
111                 return SoftApConfiguration.BAND_2GHZ;
112             }
113             return SoftApConfiguration.BAND_5GHZ | SoftApConfiguration.BAND_2GHZ;
114         }
115 
116         return band;
117     }
118 
119     @VisibleForTesting
updatePreferenceEntries()120     void updatePreferenceEntries() {
121         Resources res = mContext.getResources();
122         int entriesRes = R.array.wifi_ap_band;
123         int summariesRes = R.array.wifi_ap_band_summary;
124         mBandEntries = res.getStringArray(entriesRes);
125         mBandSummaries = res.getStringArray(summariesRes);
126     }
127 
is5GhzBandSupported()128     private boolean is5GhzBandSupported() {
129         final String countryCode = mWifiManager.getCountryCode();
130         if (!mWifiManager.is5GHzBandSupported() || countryCode == null) {
131             return false;
132         }
133         return true;
134     }
135 
getBandIndex()136     public int getBandIndex() {
137         return mBandIndex;
138     }
139 }
140