• 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.net.wifi.WifiConfiguration;
24 import android.support.v7.preference.ListPreference;
25 import android.support.v7.preference.Preference;
26 import android.support.v7.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 
30 public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController {
31 
32     private static final String PREF_KEY = "wifi_tether_network_ap_band";
33     private static final String[] BAND_VALUES =
34             {String.valueOf(AP_BAND_2GHZ), String.valueOf(AP_BAND_5GHZ)};
35 
36     private final String[] mBandEntries;
37     private int mBandIndex;
38 
WifiTetherApBandPreferenceController(Context context, OnTetherConfigUpdateListener listener)39     public WifiTetherApBandPreferenceController(Context context,
40             OnTetherConfigUpdateListener listener) {
41         super(context, listener);
42         mBandEntries = mContext.getResources().getStringArray(R.array.wifi_ap_band_config_full);
43         final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
44         if (config == null) {
45             mBandIndex = 0;
46         } else if (is5GhzBandSupported()) {
47             mBandIndex = config.apBand;
48         } else {
49             config.apBand = 0;
50             mWifiManager.setWifiApConfiguration(config);
51             mBandIndex = config.apBand;
52         }
53     }
54 
55     @Override
displayPreference(PreferenceScreen screen)56     public void displayPreference(PreferenceScreen screen) {
57         super.displayPreference(screen);
58         ListPreference preference = (ListPreference) mPreference;
59         if (!is5GhzBandSupported()) {
60             preference.setEnabled(false);
61             preference.setSummary(R.string.wifi_ap_choose_2G);
62         } else {
63             preference.setEntries(mBandEntries);
64             preference.setEntryValues(BAND_VALUES);
65             preference.setSummary(mBandEntries[mBandIndex]);
66             preference.setValue(String.valueOf(mBandIndex));
67         }
68     }
69 
70     @Override
getPreferenceKey()71     public String getPreferenceKey() {
72         return PREF_KEY;
73     }
74 
75     @Override
onPreferenceChange(Preference preference, Object newValue)76     public boolean onPreferenceChange(Preference preference, Object newValue) {
77         mBandIndex = Integer.parseInt((String) newValue);
78         preference.setSummary(mBandEntries[mBandIndex]);
79         mListener.onTetherConfigUpdated();
80         return true;
81     }
82 
is5GhzBandSupported()83     private boolean is5GhzBandSupported() {
84         final String countryCode = mWifiManager.getCountryCode();
85         if (!mWifiManager.isDualBandSupported() || countryCode == null) {
86             return false;
87         }
88         return true;
89     }
90 
getBandIndex()91     public int getBandIndex() {
92         return mBandIndex;
93     }
94 }
95