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

View File

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

View File

@@ -16,6 +16,11 @@
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.Dialog;
import android.bluetooth.BluetoothAdapter;
@@ -43,6 +48,7 @@ import android.widget.TextView;
import com.android.settings.wifi.WifiApDialog;
import com.android.settings.wifi.WifiApEnabler;
import com.android.settingslib.TetherUtil;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
@@ -93,13 +99,8 @@ public class TetherSettings extends SettingsPreferenceFragment
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 */
private int mTetherChoice = INVALID;
private int mTetherChoice = TETHERING_INVALID;
/* Stores the package name and the class name of the provisioning app */
private String[] mProvisionApp;
@@ -454,10 +455,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean enable = (Boolean) value;
if (enable) {
startProvisioningIfNecessary(WIFI_TETHERING);
startProvisioningIfNecessary(TETHERING_WIFI);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), WIFI_TETHERING);
if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_WIFI);
}
mWifiApEnabler.setSoftapEnabled(false);
}
@@ -465,16 +466,13 @@ public class TetherSettings extends SettingsPreferenceFragment
}
public static boolean isProvisioningNeededButUnavailable(Context context) {
String[] provisionApp = context.getResources().getStringArray(
com.android.internal.R.array.config_mobile_hotspot_provision_app);
return (isProvisioningNeeded(provisionApp)
&& !isIntentAvailable(context, provisionApp));
return (TetherUtil.isProvisioningNeeded(context)
&& !isIntentAvailable(context));
}
private static boolean isIntentAvailable(Context context, String[] provisionApp) {
if (provisionApp.length < 2) {
throw new IllegalArgumentException("provisionApp length should at least be 2");
}
private static boolean isIntentAvailable(Context context) {
String[] provisionApp = context.getResources().getStringArray(
com.android.internal.R.array.config_mobile_hotspot_provision_app);
final PackageManager packageManager = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(provisionApp[0], provisionApp[1]);
@@ -483,18 +481,9 @@ public class TetherSettings extends SettingsPreferenceFragment
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) {
mTetherChoice = choice;
if (isProvisioningNeeded(mProvisionApp)) {
if (TetherUtil.isProvisioningNeeded(getActivity())) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
intent.putExtra(TETHER_CHOICE, mTetherChoice);
@@ -514,24 +503,24 @@ public class TetherSettings extends SettingsPreferenceFragment
//BT and USB need switch turned off on failure
//Wifi tethering is never turned on until afterwards
switch (mTetherChoice) {
case BLUETOOTH_TETHERING:
case TETHERING_BLUETOOTH:
mBluetoothTether.setChecked(false);
break;
case USB_TETHERING:
case TETHERING_USB:
mUsbTether.setChecked(false);
break;
}
mTetherChoice = INVALID;
mTetherChoice = TETHERING_INVALID;
}
}
}
private void startTethering() {
switch (mTetherChoice) {
case WIFI_TETHERING:
case TETHERING_WIFI:
mWifiApEnabler.setSoftapEnabled(true);
break;
case BLUETOOTH_TETHERING:
case TETHERING_BLUETOOTH:
// turn on Bluetooth first
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
@@ -545,7 +534,7 @@ public class TetherSettings extends SettingsPreferenceFragment
mBluetoothTether.setSummary(R.string.bluetooth_tethering_available_subtext);
}
break;
case USB_TETHERING:
case TETHERING_USB:
setUsbTethering(true);
break;
default:
@@ -574,10 +563,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean newState = mUsbTether.isChecked();
if (newState) {
startProvisioningIfNecessary(USB_TETHERING);
startProvisioningIfNecessary(TETHERING_USB);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), USB_TETHERING);
if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_USB);
}
setUsbTethering(newState);
}
@@ -585,10 +574,10 @@ public class TetherSettings extends SettingsPreferenceFragment
boolean bluetoothTetherState = mBluetoothTether.isChecked();
if (bluetoothTetherState) {
startProvisioningIfNecessary(BLUETOOTH_TETHERING);
startProvisioningIfNecessary(TETHERING_BLUETOOTH);
} else {
if (isProvisioningNeeded(mProvisionApp)) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), BLUETOOTH_TETHERING);
if (TetherUtil.isProvisioningNeeded(getActivity())) {
TetherService.cancelRecheckAlarmIfNecessary(getActivity(), TETHERING_BLUETOOTH);
}
boolean errored = false;
@@ -652,17 +641,4 @@ public class TetherSettings extends SettingsPreferenceFragment
public int getHelpResource() {
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;
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.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settingslib.TetherUtil;
import java.util.ArrayList;
public class WifiApEnabler {
private final Context mContext;
@@ -74,10 +66,8 @@ public class WifiApEnabler {
public WifiApEnabler(Context context, SwitchPreference switchPreference) {
mContext = context;
mSwitch = switchPreference;
mOriginalSummary = switchPreference != null ? switchPreference.getSummary() : "";
if (switchPreference != null) {
mOriginalSummary = switchPreference.getSummary();
switchPreference.setPersistent(false);
}
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -110,43 +100,13 @@ public class WifiApEnabler {
}
public void setSoftapEnabled(boolean enable) {
final ContentResolver cr = mContext.getContentResolver();
/**
* Disable Wifi if enabling tethering
*/
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) {
if (TetherUtil.setWifiTethering(enable, mContext)) {
/* Disable here, enabled on receiving success broadcast */
mSwitch.setEnabled(false);
}
} else {
if (mSwitch != null) {
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) {