Move some Tethering code to SettingsLib to share

Change-Id: I43a356f2a14af4a15d1827919c39bbd1a04d3a94
This commit is contained in:
Jason Monk
2015-02-11 13:30:41 -05:00
parent b8331548d1
commit 4896c01729
5 changed files with 65 additions and 135 deletions

View File

@@ -23,6 +23,7 @@ import android.view.View;
import android.widget.ListView; import android.widget.ListView;
import com.android.settings.Settings.TetherSettingsActivity; import com.android.settings.Settings.TetherSettingsActivity;
import com.android.settingslib.TetherUtil;
import java.util.List; import java.util.List;
@@ -64,7 +65,7 @@ public class CreateShortcut extends LauncherActivity {
for (int i = activities.size() - 1; i >= 0; i--) { for (int i = activities.size() - 1; i >= 0; i--) {
ResolveInfo info = activities.get(i); ResolveInfo info = activities.get(i);
if (info.activityInfo.name.endsWith(TetherSettingsActivity.class.getSimpleName())) { if (info.activityInfo.name.endsWith(TetherSettingsActivity.class.getSimpleName())) {
if (!TetherSettings.showInShortcuts(this)) { if (!TetherUtil.isTetheringSupported(this)) {
activities.remove(i); activities.remove(i);
} }
} }

View File

@@ -6,6 +6,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import com.android.settingslib.TetherUtil;
/** /**
* This receiver catches when quick settings turns off the hotspot, so we can * This receiver catches when quick settings turns off the hotspot, so we can
* cancel the alarm in that case. All other cancels are handled in tethersettings. * cancel the alarm in that case. All other cancels are handled in tethersettings.
@@ -18,7 +20,7 @@ public class HotspotOffReceiver extends BroadcastReceiver {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) { if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) {
// The hotspot has been turned off, we don't need to recheck tethering. // The hotspot has been turned off, we don't need to recheck tethering.
TetherService.cancelRecheckAlarmIfNecessary(context, TetherSettings.WIFI_TETHERING); TetherService.cancelRecheckAlarmIfNecessary(context, TetherUtil.TETHERING_WIFI);
} }
} }
} }

View File

@@ -37,6 +37,7 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.settings.wifi.WifiApEnabler; import com.android.settings.wifi.WifiApEnabler;
import com.android.settingslib.TetherUtil;
import java.util.ArrayList; import java.util.ArrayList;
@@ -44,12 +45,6 @@ public class TetherService extends Service {
private static final String TAG = "TetherService"; private static final String TAG = "TetherService";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
public static final String EXTRA_SET_ALARM = "extraSetAlarm";
public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
public static final String EXTRA_ENABLE_WIFI_TETHER = "extraEnableWifiTether";
private static final String EXTRA_RESULT = "EntitlementResult"; private static final String EXTRA_RESULT = "EntitlementResult";
// Activity results to match the activity provision protocol. // Activity results to match the activity provision protocol.
@@ -88,15 +83,17 @@ public class TetherService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA_ADD_TETHER_TYPE)) { if (intent.hasExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE)) {
int type = intent.getIntExtra(EXTRA_ADD_TETHER_TYPE, TetherSettings.INVALID); int type = intent.getIntExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE,
TetherUtil.TETHERING_INVALID);
if (!mCurrentTethers.contains(type)) { if (!mCurrentTethers.contains(type)) {
if (DEBUG) Log.d(TAG, "Adding tether " + type); if (DEBUG) Log.d(TAG, "Adding tether " + type);
mCurrentTethers.add(type); mCurrentTethers.add(type);
} }
} }
if (intent.hasExtra(EXTRA_REM_TETHER_TYPE)) { if (intent.hasExtra(TetherUtil.EXTRA_REM_TETHER_TYPE)) {
int type = intent.getIntExtra(EXTRA_REM_TETHER_TYPE, TetherSettings.INVALID); int type = intent.getIntExtra(TetherUtil.EXTRA_REM_TETHER_TYPE,
TetherUtil.TETHERING_INVALID);
if (DEBUG) Log.d(TAG, "Removing tether " + type); if (DEBUG) Log.d(TAG, "Removing tether " + type);
int index = mCurrentTethers.indexOf(type); int index = mCurrentTethers.indexOf(type);
if (index >= 0) { if (index >= 0) {
@@ -112,16 +109,16 @@ public class TetherService extends Service {
// Only set the alarm if we have one tether, meaning the one just added, // Only set the alarm if we have one tether, meaning the one just added,
// to avoid setting it when it was already set previously for another // to avoid setting it when it was already set previously for another
// type. // type.
if (intent.getBooleanExtra(EXTRA_SET_ALARM, false) if (intent.getBooleanExtra(TetherUtil.EXTRA_SET_ALARM, false)
&& mCurrentTethers.size() == 1) { && mCurrentTethers.size() == 1) {
scheduleAlarm(); scheduleAlarm();
} }
if (intent.getBooleanExtra(EXTRA_ENABLE_WIFI_TETHER, false)) { if (intent.getBooleanExtra(TetherUtil.EXTRA_ENABLE_WIFI_TETHER, false)) {
mEnableWifiAfterCheck = true; mEnableWifiAfterCheck = true;
} }
if (intent.getBooleanExtra(EXTRA_RUN_PROVISION, false)) { if (intent.getBooleanExtra(TetherUtil.EXTRA_RUN_PROVISION, false)) {
startProvisioning(mCurrentTypeIndex); startProvisioning(mCurrentTypeIndex);
} else if (!mInProvisionCheck) { } else if (!mInProvisionCheck) {
// If we aren't running any provisioning, no reason to stay alive. // If we aren't running any provisioning, no reason to stay alive.
@@ -172,14 +169,13 @@ public class TetherService extends Service {
} }
private void enableWifiTetheringIfNeeded() { private void enableWifiTetheringIfNeeded() {
if (!isHotspotEnabled(this)) { if (!TetherUtil.isWifiTetherEnabled(this)) {
new WifiApEnabler(this, null).setSoftapEnabled(true); TetherUtil.setWifiTethering(true, this);
} }
} }
private void disableWifiTethering() { private void disableWifiTethering() {
WifiApEnabler enabler = new WifiApEnabler(this, null); TetherUtil.setWifiTethering(false, this);
enabler.setSoftapEnabled(false);
} }
private void disableUsbTethering() { private void disableUsbTethering() {
@@ -216,21 +212,16 @@ public class TetherService extends Service {
mInProvisionCheck = true; mInProvisionCheck = true;
} }
private static boolean isHotspotEnabled(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
return wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED;
}
public static void scheduleRecheckAlarm(Context context, int type) { public static void scheduleRecheckAlarm(Context context, int type) {
Intent intent = new Intent(context, TetherService.class); Intent intent = new Intent(context, TetherService.class);
intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); intent.putExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE, type);
intent.putExtra(EXTRA_SET_ALARM, true); intent.putExtra(TetherUtil.EXTRA_SET_ALARM, true);
context.startService(intent); context.startService(intent);
} }
private void scheduleAlarm() { private void scheduleAlarm() {
Intent intent = new Intent(this, TetherService.class); Intent intent = new Intent(this, TetherService.class);
intent.putExtra(EXTRA_RUN_PROVISION, true); intent.putExtra(TetherUtil.EXTRA_RUN_PROVISION, true);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
@@ -250,7 +241,7 @@ public class TetherService extends Service {
*/ */
public static void cancelRecheckAlarmIfNecessary(final Context context, int type) { public static void cancelRecheckAlarmIfNecessary(final Context context, int type) {
Intent intent = new Intent(context, TetherService.class); Intent intent = new Intent(context, TetherService.class);
intent.putExtra(EXTRA_REM_TETHER_TYPE, type); intent.putExtra(TetherUtil.EXTRA_REM_TETHER_TYPE, type);
context.startService(intent); context.startService(intent);
} }
@@ -276,19 +267,19 @@ public class TetherService extends Service {
mInProvisionCheck = false; mInProvisionCheck = false;
int checkType = mCurrentTethers.get(mCurrentTypeIndex); int checkType = mCurrentTethers.get(mCurrentTypeIndex);
if (intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT) == RESULT_OK) { if (intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT) == RESULT_OK) {
if (checkType == TetherSettings.WIFI_TETHERING && mEnableWifiAfterCheck) { if (checkType == TetherUtil.TETHERING_WIFI && mEnableWifiAfterCheck) {
enableWifiTetheringIfNeeded(); enableWifiTetheringIfNeeded();
mEnableWifiAfterCheck = false; mEnableWifiAfterCheck = false;
} }
} else { } else {
switch (checkType) { switch (checkType) {
case TetherSettings.WIFI_TETHERING: case TetherUtil.TETHERING_WIFI:
disableWifiTethering(); disableWifiTethering();
break; break;
case TetherSettings.BLUETOOTH_TETHERING: case TetherUtil.TETHERING_BLUETOOTH:
disableBtTethering(); disableBtTethering();
break; break;
case TetherSettings.USB_TETHERING: case TetherUtil.TETHERING_USB:
disableUsbTethering(); disableUsbTethering();
break; break;
} }

View File

@@ -16,6 +16,11 @@
package com.android.settings; package com.android.settings;
import static com.android.settingslib.TetherUtil.TETHERING_INVALID;
import static com.android.settingslib.TetherUtil.TETHERING_WIFI;
import static com.android.settingslib.TetherUtil.TETHERING_USB;
import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH;
import android.app.Activity; import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
@@ -43,6 +48,7 @@ import android.widget.TextView;
import com.android.settings.wifi.WifiApDialog; import com.android.settings.wifi.WifiApDialog;
import com.android.settings.wifi.WifiApEnabler; import com.android.settings.wifi.WifiApEnabler;
import com.android.settingslib.TetherUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@@ -93,13 +99,8 @@ public class TetherSettings extends SettingsPreferenceFragment
private boolean mBluetoothEnableForTether; private boolean mBluetoothEnableForTether;
public static final int INVALID = -1;
public static final int WIFI_TETHERING = 0;
public static final int USB_TETHERING = 1;
public static final int BLUETOOTH_TETHERING = 2;
/* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */ /* One of INVALID, WIFI_TETHERING, USB_TETHERING or BLUETOOTH_TETHERING */
private int mTetherChoice = INVALID; private int mTetherChoice = TETHERING_INVALID;
/* Stores the package name and the class name of the provisioning app */ /* Stores the package name and the class name of the provisioning app */
private String[] mProvisionApp; private String[] mProvisionApp;
@@ -454,10 +455,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean enable = (Boolean) value; boolean enable = (Boolean) value;
if (enable) { if (enable) {
startProvisioningIfNecessary(WIFI_TETHERING); startProvisioningIfNecessary(TETHERING_WIFI);
} else { } else {
if (isProvisioningNeeded(mProvisionApp)) { if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), WIFI_TETHERING); TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
} }
mWifiApEnabler.setSoftapEnabled(false); mWifiApEnabler.setSoftapEnabled(false);
} }
@@ -465,16 +466,13 @@ public class TetherSettings extends SettingsPreferenceFragment
} }
public static boolean isProvisioningNeededButUnavailable(Context context) { public static boolean isProvisioningNeededButUnavailable(Context context) {
String[] provisionApp = context.getResources().getStringArray( return (TetherUtil.isProvisioningNeeded(context)
com.android.internal.R.array.config_mobile_hotspot_provision_app); && !isIntentAvailable(context));
return (isProvisioningNeeded(provisionApp)
&& !isIntentAvailable(context, provisionApp));
} }
private static boolean isIntentAvailable(Context context, String[] provisionApp) { private static boolean isIntentAvailable(Context context) {
if (provisionApp.length < 2) { String[] provisionApp = context.getResources().getStringArray(
throw new IllegalArgumentException("provisionApp length should at least be 2"); com.android.internal.R.array.config_mobile_hotspot_provision_app);
}
final PackageManager packageManager = context.getPackageManager(); final PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN); Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(provisionApp[0], provisionApp[1]); intent.setClassName(provisionApp[0], provisionApp[1]);
@@ -483,18 +481,9 @@ public class TetherSettings extends SettingsPreferenceFragment
PackageManager.MATCH_DEFAULT_ONLY).size() > 0); PackageManager.MATCH_DEFAULT_ONLY).size() > 0);
} }
private static boolean isProvisioningNeeded(String[] provisionApp) {
if (SystemProperties.getBoolean("net.tethering.noprovisioning", false)
|| provisionApp == null) {
return false;
}
return (provisionApp.length == 2);
}
private void startProvisioningIfNecessary(int choice) { private void startProvisioningIfNecessary(int choice) {
mTetherChoice = choice; mTetherChoice = choice;
if (isProvisioningNeeded(mProvisionApp)) { if (TetherUtil.isProvisioningNeeded(getActivity())) {
Intent intent = new Intent(Intent.ACTION_MAIN); Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(mProvisionApp[0], mProvisionApp[1]); intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
intent.putExtra(TETHER_CHOICE, mTetherChoice); intent.putExtra(TETHER_CHOICE, mTetherChoice);
@@ -514,24 +503,24 @@ public class TetherSettings extends SettingsPreferenceFragment
//BT and USB need switch turned off on failure //BT and USB need switch turned off on failure
//Wifi tethering is never turned on until afterwards //Wifi tethering is never turned on until afterwards
switch (mTetherChoice) { switch (mTetherChoice) {
case BLUETOOTH_TETHERING: case TETHERING_BLUETOOTH:
mBluetoothTether.setChecked(false); mBluetoothTether.setChecked(false);
break; break;
case USB_TETHERING: case TETHERING_USB:
mUsbTether.setChecked(false); mUsbTether.setChecked(false);
break; break;
} }
mTetherChoice = INVALID; mTetherChoice = TETHERING_INVALID;
} }
} }
} }
private void startTethering() { private void startTethering() {
switch (mTetherChoice) { switch (mTetherChoice) {
case WIFI_TETHERING: case TETHERING_WIFI:
mWifiApEnabler.setSoftapEnabled(true); mWifiApEnabler.setSoftapEnabled(true);
break; break;
case BLUETOOTH_TETHERING: case TETHERING_BLUETOOTH:
// turn on Bluetooth first // turn on Bluetooth first
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.getState() == BluetoothAdapter.STATE_OFF) { if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
@@ -545,7 +534,7 @@ public class TetherSettings extends SettingsPreferenceFragment
mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext); mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
} }
break; break;
case USB_TETHERING: case TETHERING_USB:
setUsbTethering(true); setUsbTethering(true);
break; break;
default: default:
@@ -574,10 +563,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean newState = mUsbTether.isChecked(); boolean newState = mUsbTether.isChecked();
if (newState) { if (newState) {
startProvisioningIfNecessary(USB_TETHERING); startProvisioningIfNecessary(TETHERING_USB);
} else { } else {
if (isProvisioningNeeded(mProvisionApp)) { if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), USB_TETHERING); TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
} }
setUsbTethering(newState); setUsbTethering(newState);
} }
@@ -585,10 +574,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean bluetoothTetherState = mBluetoothTether.isChecked(); boolean bluetoothTetherState = mBluetoothTether.isChecked();
if (bluetoothTetherState) { if (bluetoothTetherState) {
startProvisioningIfNecessary(BLUETOOTH_TETHERING); startProvisioningIfNecessary(TETHERING_BLUETOOTH);
} else { } else {
if (isProvisioningNeeded(mProvisionApp)) { if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), BLUETOOTH_TETHERING); TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
} }
boolean errored = false; boolean errored = false;
@@ -652,17 +641,4 @@ public class TetherSettings extends SettingsPreferenceFragment
public int getHelpResource() { public int getHelpResource() {
return R.string.help_url_tether; return R.string.help_url_tether;
} }
/**
* Checks whether this screen will have anything to show on this device. This is called by
* the shortcut picker for Settings shortcuts (home screen widget).
* @param context a context object for getting a system service.
* @return whether Tether & portable hotspot should be shown in the shortcuts picker.
*/
public static boolean showInShortcuts(Context context) {
final ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
final boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
return !isSecondaryUser && cm.isTetheringSupported();
}
} }

View File

@@ -16,28 +16,20 @@
package com.android.settings.wifi; package com.android.settings.wifi;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log; import com.android.settings.R;
import android.widget.Toast; import com.android.settingslib.TetherUtil;
import java.util.ArrayList;
public class WifiApEnabler { public class WifiApEnabler {
private final Context mContext; private final Context mContext;
@@ -74,10 +66,8 @@ public class WifiApEnabler {
public WifiApEnabler(Context context, SwitchPreference switchPreference) { public WifiApEnabler(Context context, SwitchPreference switchPreference) {
mContext = context; mContext = context;
mSwitch = switchPreference; mSwitch = switchPreference;
mOriginalSummary = switchPreference != null ? switchPreference.getSummary() : ""; mOriginalSummary = switchPreference.getSummary();
if (switchPreference != null) { switchPreference.setPersistent(false);
switchPreference.setPersistent(false);
}
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -110,43 +100,13 @@ public class WifiApEnabler {
} }
public void setSoftapEnabled(boolean enable) { public void setSoftapEnabled(boolean enable) {
final ContentResolver cr = mContext.getContentResolver(); if (TetherUtil.setWifiTethering(enable, mContext)) {
/** /* Disable here, enabled on receiving success broadcast */
* Disable Wifi if enabling tethering mSwitch.setEnabled(false);
*/
int wifiState = mWifiManager.getWifiState();
if (enable && ((wifiState == WifiManager.WIFI_STATE_ENABLING) ||
(wifiState == WifiManager.WIFI_STATE_ENABLED))) {
mWifiManager.setWifiEnabled(false);
Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 1);
}
if (mWifiManager.setWifiApEnabled(null, enable)) {
if (mSwitch != null) {
/* Disable here, enabled on receiving success broadcast */
mSwitch.setEnabled(false);
}
} else { } else {
if (mSwitch != null) { mSwitch.setSummary(R.string.wifi_error);
mSwitch.setSummary(R.string.wifi_error);
}
} }
/**
* If needed, restore Wifi on tether disable
*/
if (!enable) {
int wifiSavedState = 0;
try {
wifiSavedState = Settings.Global.getInt(cr, Settings.Global.WIFI_SAVED_STATE);
} catch (Settings.SettingNotFoundException e) {
;
}
if (wifiSavedState == 1) {
mWifiManager.setWifiEnabled(true);
Settings.Global.putInt(cr, Settings.Global.WIFI_SAVED_STATE, 0);
}
}
} }
public void updateConfigSummary(WifiConfiguration wifiConfig) { public void updateConfigSummary(WifiConfiguration wifiConfig) {