Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8a934767 authored by Jeremy Klein's avatar Jeremy Klein
Browse files

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
parent 20c98177
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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);
            }
        }
    }
+56 −32
Original line number Diff line number Diff line
@@ -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<Integer> mCurrentTethers;
    private ArrayMap<Integer, List<ResultReceiver>> 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<ResultReceiver>());
        mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>());
        mPendingCallbacks.put(
                ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
    }

    @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<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 (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<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() {
        @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.
+4 −4
Original line number Diff line number Diff line
@@ -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.