Save WifiAp config changes when hotspot is enabled

We need to properly handle asynchronous events when WifiAp for hotspot
is enabled and the user changes the config.  This CL changes the
behavior to:
1 - mark local boolean indicating a restart is needed
2 - trigger the WifiAp to stop
3 - save the new config
4 - when the stop is complete (and the boolean is set), restart the
WifiAp.

Also fix a few style issues in the file.

BUG:27460194
Change-Id: I7e49ab9c49e125017cf83866a396701e440bf383
This commit is contained in:
Rebecca Silberstein
2016-03-04 15:47:43 -08:00
parent 52fdd6a058
commit ffb4fbb1a7

View File

@@ -42,6 +42,7 @@ import android.os.UserManager;
import android.support.v14.preference.SwitchPreference; import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.util.Log;
import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.settings.wifi.WifiApDialog; import com.android.settings.wifi.WifiApDialog;
@@ -65,6 +66,8 @@ public class TetherSettings extends RestrictedSettingsFragment
private static final int DIALOG_AP_SETTINGS = 1; private static final int DIALOG_AP_SETTINGS = 1;
private static final String TAG = "TetheringSettings";
private SwitchPreference mUsbTether; private SwitchPreference mUsbTether;
private WifiApEnabler mWifiApEnabler; private WifiApEnabler mWifiApEnabler;
@@ -95,6 +98,8 @@ public class TetherSettings extends RestrictedSettingsFragment
private WifiConfiguration mWifiConfig = null; private WifiConfiguration mWifiConfig = null;
private ConnectivityManager mCm; private ConnectivityManager mCm;
private boolean mRestartWifiApAfterConfigChange;
private boolean mUsbConnected; private boolean mUsbConnected;
private boolean mMassStorageActive; private boolean mMassStorageActive;
@@ -141,7 +146,7 @@ public class TetherSettings extends RestrictedSettingsFragment
mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS); mUsbTether = (SwitchPreference) findPreference(USB_TETHER_SETTINGS);
mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING); mBluetoothTether = (SwitchPreference) findPreference(ENABLE_BLUETOOTH_TETHERING);
mCm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); mCm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mUsbRegexs = mCm.getTetherableUsbRegexs(); mUsbRegexs = mCm.getTetherableUsbRegexs();
mWifiRegexs = mCm.getTetherableWifiRegexs(); mWifiRegexs = mCm.getTetherableWifiRegexs();
@@ -183,6 +188,8 @@ public class TetherSettings extends RestrictedSettingsFragment
mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY); mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
mRestartWifiApAfterConfigChange = false;
if (mWifiConfig == null) { if (mWifiConfig == null) {
final String s = activity.getString( final String s = activity.getString(
com.android.internal.R.string.wifi_tether_configure_ssid_default); com.android.internal.R.string.wifi_tether_configure_ssid_default);
@@ -222,6 +229,12 @@ public class TetherSettings extends RestrictedSettingsFragment
updateState(available.toArray(new String[available.size()]), updateState(available.toArray(new String[available.size()]),
active.toArray(new String[active.size()]), active.toArray(new String[active.size()]),
errored.toArray(new String[errored.size()])); errored.toArray(new String[errored.size()]));
if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED
&& mRestartWifiApAfterConfigChange) {
mRestartWifiApAfterConfigChange = false;
Log.d(TAG, "Restarting WifiAp due to prior config change.");
startTethering(TETHERING_WIFI);
}
} else if (action.equals(Intent.ACTION_MEDIA_SHARED)) { } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
mMassStorageActive = true; mMassStorageActive = true;
updateState(); updateState();
@@ -391,8 +404,9 @@ public class TetherSettings extends RestrictedSettingsFragment
} }
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter == null) if (adapter == null) {
return; return;
}
int btState = adapter.getState(); int btState = adapter.getState();
if (btState == BluetoothAdapter.STATE_TURNING_OFF) { if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
mBluetoothTether.setEnabled(false); mBluetoothTether.setEnabled(false);
@@ -402,8 +416,8 @@ public class TetherSettings extends RestrictedSettingsFragment
mBluetoothTether.setSummary(R.string.bluetooth_turning_on); mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
} else { } else {
BluetoothPan bluetoothPan = mBluetoothPan.get(); BluetoothPan bluetoothPan = mBluetoothPan.get();
if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null && if (btState == BluetoothAdapter.STATE_ON && bluetoothPan != null
bluetoothPan.isTetheringOn()) { && bluetoothPan.isTetheringOn()) {
mBluetoothTether.setChecked(true); mBluetoothTether.setChecked(true);
mBluetoothTether.setEnabled(true); mBluetoothTether.setEnabled(true);
int bluetoothTethered = bluetoothPan.getConnectedDevices().size(); int bluetoothTethered = bluetoothPan.getConnectedDevices().size();
@@ -506,11 +520,12 @@ public class TetherSettings extends RestrictedSettingsFragment
* TODO: update config on a running access point when framework support is added * TODO: update config on a running access point when framework support is added
*/ */
if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) { if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
mWifiManager.setWifiApEnabled(null, false); Log.d("TetheringSettings",
mWifiManager.setWifiApEnabled(mWifiConfig, true); "Wifi AP config changed while enabled, stop and restart");
} else { mRestartWifiApAfterConfigChange = true;
mWifiManager.setWifiApConfiguration(mWifiConfig); mCm.stopTethering(TETHERING_WIFI);
} }
mWifiManager.setWifiApConfiguration(mWifiConfig);
int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig); int index = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT), mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
mWifiConfig.SSID, mWifiConfig.SSID,