Updated Tethering and portable hotspot to use switches.

Bug: 17020102
Change-Id: I5da1745d4ea9545f8960c015ca6858fb0a34de0d
This commit is contained in:
PauloftheWest
2014-08-24 11:59:35 -07:00
parent 8fe710682a
commit 656c88cb71
3 changed files with 39 additions and 39 deletions

View File

@@ -33,7 +33,7 @@ import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.CheckBoxPreference;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
@@ -41,7 +41,7 @@ import android.widget.Toast;
public class WifiApEnabler {
private final Context mContext;
private final CheckBoxPreference mCheckBox;
private final SwitchPreference mSwitch;
private final CharSequence mOriginalSummary;
private WifiManager mWifiManager;
@@ -66,17 +66,17 @@ public class WifiApEnabler {
ConnectivityManager.EXTRA_ERRORED_TETHER);
updateTetherState(available.toArray(), active.toArray(), errored.toArray());
} else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
enableWifiCheckBox();
enableWifiSwitch();
}
}
};
public WifiApEnabler(Context context, CheckBoxPreference checkBox) {
public WifiApEnabler(Context context, SwitchPreference switchPreference) {
mContext = context;
mCheckBox = checkBox;
mOriginalSummary = checkBox.getSummary();
checkBox.setPersistent(false);
mSwitch = switchPreference;
mOriginalSummary = switchPreference.getSummary();
switchPreference.setPersistent(false);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -90,21 +90,21 @@ public class WifiApEnabler {
public void resume() {
mContext.registerReceiver(mReceiver, mIntentFilter);
enableWifiCheckBox();
enableWifiSwitch();
}
public void pause() {
mContext.unregisterReceiver(mReceiver);
}
private void enableWifiCheckBox() {
private void enableWifiSwitch() {
boolean isAirplaneMode = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
if(!isAirplaneMode) {
mCheckBox.setEnabled(true);
mSwitch.setEnabled(true);
} else {
mCheckBox.setSummary(mOriginalSummary);
mCheckBox.setEnabled(false);
mSwitch.setSummary(mOriginalSummary);
mSwitch.setEnabled(false);
}
}
@@ -122,9 +122,9 @@ public class WifiApEnabler {
if (mWifiManager.setWifiApEnabled(null, enable)) {
/* Disable here, enabled on receiving success broadcast */
mCheckBox.setEnabled(false);
mSwitch.setEnabled(false);
} else {
mCheckBox.setSummary(R.string.wifi_error);
mSwitch.setSummary(R.string.wifi_error);
}
/**
@@ -147,7 +147,7 @@ public class WifiApEnabler {
public void updateConfigSummary(WifiConfiguration wifiConfig) {
String s = mContext.getString(
com.android.internal.R.string.wifi_tether_configure_ssid_default);
mCheckBox.setSummary(String.format(
mSwitch.setSummary(String.format(
mContext.getString(R.string.wifi_tether_enabled_subtext),
(wifiConfig == null) ? s : wifiConfig.SSID));
}
@@ -173,38 +173,38 @@ public class WifiApEnabler {
WifiConfiguration wifiConfig = mWifiManager.getWifiApConfiguration();
updateConfigSummary(wifiConfig);
} else if (wifiErrored) {
mCheckBox.setSummary(R.string.wifi_error);
mSwitch.setSummary(R.string.wifi_error);
}
}
private void handleWifiApStateChanged(int state) {
switch (state) {
case WifiManager.WIFI_AP_STATE_ENABLING:
mCheckBox.setSummary(R.string.wifi_tether_starting);
mCheckBox.setEnabled(false);
mSwitch.setSummary(R.string.wifi_tether_starting);
mSwitch.setEnabled(false);
break;
case WifiManager.WIFI_AP_STATE_ENABLED:
/**
* Summary on enable is handled by tether
* broadcast notice
*/
mCheckBox.setChecked(true);
mSwitch.setChecked(true);
/* Doesnt need the airplane check */
mCheckBox.setEnabled(true);
mSwitch.setEnabled(true);
break;
case WifiManager.WIFI_AP_STATE_DISABLING:
mCheckBox.setSummary(R.string.wifi_tether_stopping);
mCheckBox.setEnabled(false);
mSwitch.setSummary(R.string.wifi_tether_stopping);
mSwitch.setEnabled(false);
break;
case WifiManager.WIFI_AP_STATE_DISABLED:
mCheckBox.setChecked(false);
mCheckBox.setSummary(mOriginalSummary);
enableWifiCheckBox();
mSwitch.setChecked(false);
mSwitch.setSummary(mOriginalSummary);
enableWifiSwitch();
break;
default:
mCheckBox.setChecked(false);
mCheckBox.setSummary(R.string.wifi_error);
enableWifiCheckBox();
mSwitch.setChecked(false);
mSwitch.setSummary(R.string.wifi_error);
enableWifiSwitch();
}
}
}