• 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 android.net.wifi.WifiConfiguration.AP_BAND_2GHZ;
20 import static android.net.wifi.WifiConfiguration.AP_BAND_5GHZ;
21 
22 import android.content.Context;
23 import android.content.res.Resources;
24 import android.icu.text.ListFormatter;
25 import android.net.wifi.WifiConfiguration;
26 import android.support.v7.preference.Preference;
27 import android.util.Log;
28 
29 import com.android.settings.R;
30 import com.android.settings.widget.HotspotApBandSelectionPreference;
31 
32 public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController {
33 
34     private static final String TAG = "WifiTetherApBandPref";
35     private static final String PREF_KEY = "wifi_tether_network_ap_band";
36     public static final String[] BAND_VALUES =
37             {String.valueOf(AP_BAND_2GHZ), String.valueOf(AP_BAND_5GHZ)};
38 
39     private final String[] mBandEntries;
40     private final 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         Resources res = mContext.getResources();
47         mBandEntries = res.getStringArray(R.array.wifi_ap_band_config_full);
48         mBandSummaries = res.getStringArray(R.array.wifi_ap_band_summary_full);
49     }
50 
51     @Override
updateDisplay()52     public void updateDisplay() {
53         final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
54         if (config == null) {
55             mBandIndex = 0;
56             Log.d(TAG, "Updating band index to 0 because no config");
57         } else if (is5GhzBandSupported()) {
58             mBandIndex = config.apBand;
59             Log.d(TAG, "Updating band index to " + mBandIndex);
60         } else {
61             config.apBand = 0;
62             mWifiManager.setWifiApConfiguration(config);
63             mBandIndex = config.apBand;
64             Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex);
65         }
66         HotspotApBandSelectionPreference preference =
67                 (HotspotApBandSelectionPreference) mPreference;
68 
69         if (!is5GhzBandSupported()) {
70             preference.setEnabled(false);
71             preference.setSummary(R.string.wifi_ap_choose_2G);
72         } else {
73             preference.setExistingConfigValue(config.apBand);
74             preference.setSummary(getConfigSummary());
75         }
76     }
77 
getConfigSummary()78     String getConfigSummary() {
79         if (mBandIndex == WifiConfiguration.AP_BAND_ANY) {
80             return ListFormatter.getInstance().format((Object[]) mBandSummaries);
81         }
82         return mBandSummaries[mBandIndex];
83     }
84 
85     @Override
getPreferenceKey()86     public String getPreferenceKey() {
87         return PREF_KEY;
88     }
89 
90     @Override
onPreferenceChange(Preference preference, Object newValue)91     public boolean onPreferenceChange(Preference preference, Object newValue) {
92         mBandIndex = (Integer) newValue;
93         Log.d(TAG, "Band preference changed, updating band index to " + mBandIndex);
94         preference.setSummary(getConfigSummary());
95         mListener.onTetherConfigUpdated();
96         return true;
97     }
98 
is5GhzBandSupported()99     private boolean is5GhzBandSupported() {
100         final String countryCode = mWifiManager.getCountryCode();
101         if (!mWifiManager.isDualBandSupported() || countryCode == null) {
102             return false;
103         }
104         return true;
105     }
106 
getBandIndex()107     public int getBandIndex() {
108         return mBandIndex;
109     }
110 }
111