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.content.res.Resources; 21 import android.net.wifi.WifiConfiguration; 22 import android.util.Log; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.ListPreference; 26 import androidx.preference.Preference; 27 28 import com.android.settings.R; 29 30 public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController { 31 32 private static final String TAG = "WifiTetherApBandPref"; 33 private static final String PREF_KEY = "wifi_tether_network_ap_band"; 34 35 private String[] mBandEntries; 36 private String[] mBandSummaries; 37 private int mBandIndex; 38 private boolean isDualMode; 39 WifiTetherApBandPreferenceController(Context context, OnTetherConfigUpdateListener listener)40 public WifiTetherApBandPreferenceController(Context context, 41 OnTetherConfigUpdateListener listener) { 42 super(context, listener); 43 isDualMode = mWifiManager.isDualModeSupported(); 44 updatePreferenceEntries(); 45 } 46 47 @Override updateDisplay()48 public void updateDisplay() { 49 final WifiConfiguration config = mWifiManager.getWifiApConfiguration(); 50 if (config == null) { 51 mBandIndex = 0; 52 Log.d(TAG, "Updating band index to 0 because no config"); 53 } else if (is5GhzBandSupported()) { 54 mBandIndex = validateSelection(config.apBand); 55 Log.d(TAG, "Updating band index to " + mBandIndex); 56 } else { 57 config.apBand = 0; 58 mWifiManager.setWifiApConfiguration(config); 59 mBandIndex = config.apBand; 60 Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex); 61 } 62 ListPreference preference = 63 (ListPreference) mPreference; 64 preference.setEntries(mBandSummaries); 65 preference.setEntryValues(mBandEntries); 66 67 if (!is5GhzBandSupported()) { 68 preference.setEnabled(false); 69 preference.setSummary(R.string.wifi_ap_choose_2G); 70 } else { 71 preference.setValue(Integer.toString(config.apBand)); 72 preference.setSummary(getConfigSummary()); 73 } 74 } 75 getConfigSummary()76 String getConfigSummary() { 77 if (mBandIndex == WifiConfiguration.AP_BAND_ANY) { 78 return mContext.getString(R.string.wifi_ap_prefer_5G); 79 } 80 return mBandSummaries[mBandIndex]; 81 } 82 83 @Override getPreferenceKey()84 public String getPreferenceKey() { 85 return PREF_KEY; 86 } 87 88 @Override onPreferenceChange(Preference preference, Object newValue)89 public boolean onPreferenceChange(Preference preference, Object newValue) { 90 mBandIndex = validateSelection(Integer.parseInt((String) newValue)); 91 Log.d(TAG, "Band preference changed, updating band index to " + mBandIndex); 92 preference.setSummary(getConfigSummary()); 93 mListener.onTetherConfigUpdated(); 94 return true; 95 } 96 validateSelection(int band)97 private int validateSelection(int band) { 98 // Reset the band to 2.4 GHz if we get a weird config back to avoid a crash. 99 final boolean isDualMode = mWifiManager.isDualModeSupported(); 100 101 // unsupported states: 102 // 1: no dual mode means we can't have AP_BAND_ANY - default to 5GHZ 103 // 2: no 5 GHZ support means we can't have AP_BAND_5GHZ - default to 2GHZ 104 // 3: With Dual mode support we can't have AP_BAND_5GHZ - default to ANY 105 if (!isDualMode && WifiConfiguration.AP_BAND_ANY == band) { 106 return WifiConfiguration.AP_BAND_5GHZ; 107 } else if (!is5GhzBandSupported() && WifiConfiguration.AP_BAND_5GHZ == band) { 108 return WifiConfiguration.AP_BAND_2GHZ; 109 } else if (isDualMode && WifiConfiguration.AP_BAND_5GHZ == band) { 110 return WifiConfiguration.AP_BAND_ANY; 111 } 112 113 return band; 114 } 115 116 @VisibleForTesting updatePreferenceEntries()117 void updatePreferenceEntries() { 118 Resources res = mContext.getResources(); 119 int entriesRes = R.array.wifi_ap_band_config_full; 120 int summariesRes = R.array.wifi_ap_band_summary_full; 121 // change the list options if this is a dual mode device 122 if (isDualMode) { 123 entriesRes = R.array.wifi_ap_band_dual_mode; 124 summariesRes = R.array.wifi_ap_band_dual_mode_summary; 125 } 126 mBandEntries = res.getStringArray(entriesRes); 127 mBandSummaries = res.getStringArray(summariesRes); 128 } 129 is5GhzBandSupported()130 private boolean is5GhzBandSupported() { 131 final String countryCode = mWifiManager.getCountryCode(); 132 if (!mWifiManager.isDualBandSupported() || countryCode == null) { 133 return false; 134 } 135 return true; 136 } 137 getBandIndex()138 public int getBandIndex() { 139 return mBandIndex; 140 } 141 } 142