From 8a934767dccd943685bc5750481d6aff394f8ae8 Mon Sep 17 00:00:00 2001 From: Jeremy Klein Date: Fri, 22 Jan 2016 16:14:05 -0800 Subject: [PATCH] Allow TetherService to take a callback to receiver provision results. See ag/850685 for corresponding framework changes. This is being done as part of exposing a cleaner tethering API which will be used by Quick Settings and Settings. BUG: 26247383 Change-Id: I061b8b8b2b0c5bbe98f50deb051e2bfb8e773d61 --- .../android/settings/HotspotOffReceiver.java | 4 +- src/com/android/settings/TetherService.java | 88 ++++++++++++------- src/com/android/settings/TetherSettings.java | 8 +- 3 files changed, 63 insertions(+), 37 deletions(-) diff --git a/src/com/android/settings/HotspotOffReceiver.java b/src/com/android/settings/HotspotOffReceiver.java index f3c3feede31..3db0ee92367 100644 --- a/src/com/android/settings/HotspotOffReceiver.java +++ b/src/com/android/settings/HotspotOffReceiver.java @@ -4,6 +4,7 @@ package com.android.settings; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.util.Log; @@ -25,7 +26,8 @@ public class HotspotOffReceiver extends BroadcastReceiver { if (wifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_DISABLED) { if (DEBUG) Log.d(TAG, "TetherService.cancelRecheckAlarmIfNecessary called"); // 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); } } } diff --git a/src/com/android/settings/TetherService.java b/src/com/android/settings/TetherService.java index 346a175d2b6..d4944efe50a 100644 --- a/src/com/android/settings/TetherService.java +++ b/src/com/android/settings/TetherService.java @@ -31,13 +31,16 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.os.IBinder; +import android.os.ResultReceiver; import android.os.SystemClock; import android.text.TextUtils; +import android.util.ArrayMap; import android.util.Log; import com.android.settingslib.TetherUtil; import java.util.ArrayList; +import java.util.List; public class TetherService extends Service { private static final String TAG = "TetherService"; @@ -57,9 +60,9 @@ public class TetherService extends Service { private static final String KEY_TETHERS = "currentTethers"; private int mCurrentTypeIndex; - private boolean mEnableWifiAfterCheck; private boolean mInProvisionCheck; private ArrayList mCurrentTethers; + private ArrayMap> mPendingCallbacks; @Override public IBinder onBind(Intent intent) { @@ -77,23 +80,42 @@ public class TetherService extends Service { SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, "")); mCurrentTypeIndex = 0; + mPendingCallbacks = new ArrayMap<>(3); + mPendingCallbacks.put(ConnectivityManager.TETHERING_WIFI, new ArrayList()); + mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList()); + mPendingCallbacks.put( + ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { - if (intent.hasExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE)) { - int type = intent.getIntExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE, - TetherUtil.TETHERING_INVALID); + if (intent.hasExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE)) { + int type = intent.getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, + ConnectivityManager.TETHERING_INVALID); + ResultReceiver callback = + intent.getParcelableExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK); + if (callback != null) { + List 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 (DEBUG) Log.d(TAG, "Adding tether " + type); mCurrentTethers.add(type); } } - if (intent.hasExtra(TetherUtil.EXTRA_REM_TETHER_TYPE)) { + if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) { if (!mInProvisionCheck) { - int type = intent.getIntExtra(TetherUtil.EXTRA_REM_TETHER_TYPE, - TetherUtil.TETHERING_INVALID); + int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, + ConnectivityManager.TETHERING_INVALID); int index = mCurrentTethers.indexOf(type); if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index); 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, // to avoid setting it when it was already set previously for another // type. - if (intent.getBooleanExtra(TetherUtil.EXTRA_SET_ALARM, false) + if (intent.getBooleanExtra(ConnectivityManager.EXTRA_SET_ALARM, false) && mCurrentTethers.size() == 1) { scheduleAlarm(); } - if (intent.getBooleanExtra(TetherUtil.EXTRA_ENABLE_WIFI_TETHER, false)) { - mEnableWifiAfterCheck = true; - } - - if (intent.getBooleanExtra(TetherUtil.EXTRA_RUN_PROVISION, false)) { + if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) { startProvisioning(mCurrentTypeIndex); } else if (!mInProvisionCheck) { // If we aren't running any provisioning, no reason to stay alive. + if (DEBUG) Log.d(TAG, "Stopping self. startid: " + startId); stopSelf(); return START_NOT_STICKY; } @@ -173,12 +192,6 @@ public class TetherService extends Service { return buffer.toString(); } - private void enableWifiTetheringIfNeeded() { - if (!TetherUtil.isWifiTetherEnabled(this)) { - TetherUtil.setWifiTethering(true, this); - } - } - private void disableWifiTethering() { TetherUtil.setWifiTethering(false, this); } @@ -221,14 +234,14 @@ public class TetherService extends Service { public static void scheduleRecheckAlarm(Context context, int type) { Intent intent = new Intent(context, TetherService.class); - intent.putExtra(TetherUtil.EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(TetherUtil.EXTRA_SET_ALARM, true); + intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(ConnectivityManager.EXTRA_SET_ALARM, true); context.startService(intent); } private void scheduleAlarm() { 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); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); @@ -248,7 +261,7 @@ public class TetherService extends Service { */ public static void cancelRecheckAlarmIfNecessary(final Context context, int type) { 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); } @@ -264,6 +277,20 @@ public class TetherService extends Service { if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck"); } + private void fireCallbacksForType(int type, int result) { + List 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() { @Override public void onReceive(Context context, Intent intent) { @@ -278,24 +305,21 @@ public class TetherService extends Service { } int checkType = mCurrentTethers.get(mCurrentTypeIndex); mInProvisionCheck = false; - if (intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT) == RESULT_OK) { - if (checkType == TetherUtil.TETHERING_WIFI && mEnableWifiAfterCheck) { - enableWifiTetheringIfNeeded(); - mEnableWifiAfterCheck = false; - } - } else { + int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT); + if (result != RESULT_OK) { switch (checkType) { - case TetherUtil.TETHERING_WIFI: + case ConnectivityManager.TETHERING_WIFI: disableWifiTethering(); break; - case TetherUtil.TETHERING_BLUETOOTH: + case ConnectivityManager.TETHERING_BLUETOOTH: disableBtTethering(); break; - case TetherUtil.TETHERING_USB: + case ConnectivityManager.TETHERING_USB: disableUsbTethering(); break; } } + fireCallbacksForType(checkType, result); if (++mCurrentTypeIndex >= mCurrentTethers.size()) { // We are done with all checks, time to die. diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java index ee8900e499e..4b5f23d26af 100644 --- a/src/com/android/settings/TetherSettings.java +++ b/src/com/android/settings/TetherSettings.java @@ -47,10 +47,10 @@ import com.android.settingslib.TetherUtil; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicReference; -import static com.android.settingslib.TetherUtil.TETHERING_BLUETOOTH; -import static com.android.settingslib.TetherUtil.TETHERING_INVALID; -import static com.android.settingslib.TetherUtil.TETHERING_USB; -import static com.android.settingslib.TetherUtil.TETHERING_WIFI; +import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; +import static android.net.ConnectivityManager.TETHERING_INVALID; +import static android.net.ConnectivityManager.TETHERING_USB; +import static android.net.ConnectivityManager.TETHERING_WIFI; /* * Displays preferences for Tethering.