Merge "Allow TetherService to take a callback to receiver provision results."
This commit is contained in:
@@ -4,6 +4,7 @@ package com.android.settings;
|
|||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -25,7 +26,8 @@ public class HotspotOffReceiver extends BroadcastReceiver {
|
|||||||
if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) {
|
if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) {
|
||||||
if (DEBUG) Log.d(TAG, "TetherService.cancelRecheckAlarmIfNecessary called");
|
if (DEBUG) Log.d(TAG, "TetherService.cancelRecheckAlarmIfNecessary called");
|
||||||
// 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, TetherUtil.TETHERING_WIFI);
|
TetherService.cancelRecheckAlarmIfNecessary(
|
||||||
|
context, ConnectivityManager.TETHERING_WIFI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -31,13 +31,16 @@ import android.content.IntentFilter;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.ResultReceiver;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.ArrayMap;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.settingslib.TetherUtil;
|
import com.android.settingslib.TetherUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class TetherService extends Service {
|
public class TetherService extends Service {
|
||||||
private static final String TAG = "TetherService";
|
private static final String TAG = "TetherService";
|
||||||
@@ -57,9 +60,9 @@ public class TetherService extends Service {
|
|||||||
private static final String KEY_TETHERS = "currentTethers";
|
private static final String KEY_TETHERS = "currentTethers";
|
||||||
|
|
||||||
private int mCurrentTypeIndex;
|
private int mCurrentTypeIndex;
|
||||||
private boolean mEnableWifiAfterCheck;
|
|
||||||
private boolean mInProvisionCheck;
|
private boolean mInProvisionCheck;
|
||||||
private ArrayList<Integer> mCurrentTethers;
|
private ArrayList<Integer> mCurrentTethers;
|
||||||
|
private ArrayMap<Integer, List<ResultReceiver>> mPendingCallbacks;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
@@ -77,23 +80,42 @@ public class TetherService extends Service {
|
|||||||
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
|
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
|
||||||
mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
|
mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
|
||||||
mCurrentTypeIndex = 0;
|
mCurrentTypeIndex = 0;
|
||||||
|
mPendingCallbacks = new ArrayMap<>(3);
|
||||||
|
mPendingCallbacks.put(ConnectivityManager.TETHERING_WIFI, new ArrayList<ResultReceiver>());
|
||||||
|
mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>());
|
||||||
|
mPendingCallbacks.put(
|
||||||
|
ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if (intent.hasExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE)) {
|
if (intent.hasExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE)) {
|
||||||
int type = intent.getIntExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE,
|
int type = intent.getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
|
||||||
TetherUtil.TETHERING_INVALID);
|
ConnectivityManager.TETHERING_INVALID);
|
||||||
|
ResultReceiver callback =
|
||||||
|
intent.getParcelableExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK);
|
||||||
|
if (callback != null) {
|
||||||
|
List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
|
||||||
|
if (callbacksForType != null) {
|
||||||
|
callbacksForType.add(callback);
|
||||||
|
} else {
|
||||||
|
// Invalid tether type. Just ignore this request and report failure.
|
||||||
|
callback.send(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, null);
|
||||||
|
stopSelf();
|
||||||
|
return START_NOT_STICKY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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(TetherUtil.EXTRA_REM_TETHER_TYPE)) {
|
if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) {
|
||||||
if (!mInProvisionCheck) {
|
if (!mInProvisionCheck) {
|
||||||
int type = intent.getIntExtra(TetherUtil.EXTRA_REM_TETHER_TYPE,
|
int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE,
|
||||||
TetherUtil.TETHERING_INVALID);
|
ConnectivityManager.TETHERING_INVALID);
|
||||||
int index = mCurrentTethers.indexOf(type);
|
int index = mCurrentTethers.indexOf(type);
|
||||||
if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
|
if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
@@ -114,19 +136,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(TetherUtil.EXTRA_SET_ALARM, false)
|
if (intent.getBooleanExtra(ConnectivityManager.EXTRA_SET_ALARM, false)
|
||||||
&& mCurrentTethers.size() == 1) {
|
&& mCurrentTethers.size() == 1) {
|
||||||
scheduleAlarm();
|
scheduleAlarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intent.getBooleanExtra(TetherUtil.EXTRA_ENABLE_WIFI_TETHER, false)) {
|
if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) {
|
||||||
mEnableWifiAfterCheck = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
||||||
|
if (DEBUG) Log.d(TAG, "Stopping self. startid: " + startId);
|
||||||
stopSelf();
|
stopSelf();
|
||||||
return START_NOT_STICKY;
|
return START_NOT_STICKY;
|
||||||
}
|
}
|
||||||
@@ -173,12 +192,6 @@ public class TetherService extends Service {
|
|||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableWifiTetheringIfNeeded() {
|
|
||||||
if (!TetherUtil.isWifiTetherEnabled(this)) {
|
|
||||||
TetherUtil.setWifiTethering(true, this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void disableWifiTethering() {
|
private void disableWifiTethering() {
|
||||||
TetherUtil.setWifiTethering(false, this);
|
TetherUtil.setWifiTethering(false, this);
|
||||||
}
|
}
|
||||||
@@ -221,14 +234,14 @@ public class TetherService extends Service {
|
|||||||
|
|
||||||
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(TetherUtil.EXTRA_ADD_TETHER_TYPE, type);
|
intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type);
|
||||||
intent.putExtra(TetherUtil.EXTRA_SET_ALARM, true);
|
intent.putExtra(ConnectivityManager.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(TetherUtil.EXTRA_RUN_PROVISION, true);
|
intent.putExtra(ConnectivityManager.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);
|
||||||
@@ -248,7 +261,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(TetherUtil.EXTRA_REM_TETHER_TYPE, type);
|
intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type);
|
||||||
context.startService(intent);
|
context.startService(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,6 +277,20 @@ public class TetherService extends Service {
|
|||||||
if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck");
|
if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void fireCallbacksForType(int type, int result) {
|
||||||
|
List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
|
||||||
|
if (callbacksForType == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int errorCode = result == RESULT_OK ? ConnectivityManager.TETHER_ERROR_NO_ERROR :
|
||||||
|
ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
|
||||||
|
for (ResultReceiver callback : callbacksForType) {
|
||||||
|
if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback");
|
||||||
|
callback.send(errorCode, null);
|
||||||
|
}
|
||||||
|
callbacksForType.clear();
|
||||||
|
}
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
@@ -278,24 +305,21 @@ public class TetherService extends Service {
|
|||||||
}
|
}
|
||||||
int checkType = mCurrentTethers.get(mCurrentTypeIndex);
|
int checkType = mCurrentTethers.get(mCurrentTypeIndex);
|
||||||
mInProvisionCheck = false;
|
mInProvisionCheck = false;
|
||||||
if (intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT) == RESULT_OK) {
|
int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT);
|
||||||
if (checkType == TetherUtil.TETHERING_WIFI && mEnableWifiAfterCheck) {
|
if (result != RESULT_OK) {
|
||||||
enableWifiTetheringIfNeeded();
|
|
||||||
mEnableWifiAfterCheck = false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch (checkType) {
|
switch (checkType) {
|
||||||
case TetherUtil.TETHERING_WIFI:
|
case ConnectivityManager.TETHERING_WIFI:
|
||||||
disableWifiTethering();
|
disableWifiTethering();
|
||||||
break;
|
break;
|
||||||
case TetherUtil.TETHERING_BLUETOOTH:
|
case ConnectivityManager.TETHERING_BLUETOOTH:
|
||||||
disableBtTethering();
|
disableBtTethering();
|
||||||
break;
|
break;
|
||||||
case TetherUtil.TETHERING_USB:
|
case ConnectivityManager.TETHERING_USB:
|
||||||
disableUsbTethering();
|
disableUsbTethering();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fireCallbacksForType(checkType, result);
|
||||||
|
|
||||||
if (++mCurrentTypeIndex >= mCurrentTethers.size()) {
|
if (++mCurrentTypeIndex >= mCurrentTethers.size()) {
|
||||||
// We are done with all checks, time to die.
|
// We are done with all checks, time to die.
|
||||||
|
@@ -47,10 +47,10 @@ 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;
|
||||||
|
|
||||||
import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH;
|
import static android.net.ConnectivityManager.TETHERING_BLUETOOTH;
|
||||||
import static com.android.settingslib.TetherUtil.TETHERING_INVALID;
|
import static android.net.ConnectivityManager.TETHERING_INVALID;
|
||||||
import static com.android.settingslib.TetherUtil.TETHERING_USB;
|
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||||
import static com.android.settingslib.TetherUtil.TETHERING_WIFI;
|
import static android.net.ConnectivityManager.TETHERING_WIFI;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Displays preferences for Tethering.
|
* Displays preferences for Tethering.
|
||||||
|
Reference in New Issue
Block a user