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

Commit ba4af702 authored by Mark Chien's avatar Mark Chien Committed by Automerger Merge Worker
Browse files

Merge "Replace ConnectivityManager as TetheringManager" am: 40643684

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Settings/+/1344637

Change-Id: I9dcb7ee1f1e2c0dfbaf14cf9636b0b153e636cb7
parents 3548a5eb 40643684
Loading
Loading
Loading
Loading
+19 −18
Original line number Diff line number Diff line
@@ -16,13 +16,19 @@

package com.android.settings.network;

import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
import static android.net.TetheringManager.TETHERING_INVALID;
import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.TetheringConstants;
import android.net.TetheringManager;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.os.UserHandle;
@@ -52,23 +58,19 @@ public class TetherProvisioningActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mResultReceiver = (ResultReceiver)getIntent().getParcelableExtra(
                ConnectivityManager.EXTRA_PROVISION_CALLBACK);
        mResultReceiver = (ResultReceiver) getIntent().getParcelableExtra(EXTRA_PROVISION_CALLBACK);

        final int tetherType = getIntent().getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
                ConnectivityManager.TETHERING_INVALID);
        final int tetherType = getIntent().getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);

        final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID,
                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
        final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
        final int subId = SubscriptionManager.getActiveDataSubscriptionId();
        if (tetherSubId != subId) {
            Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
            mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
            mResultReceiver.send(TETHER_ERROR_PROVISIONING_FAILED, null);
            finish();
            return;
        }
        String[] provisionApp = getIntent().getStringArrayExtra(
                TetheringConstants.EXTRA_RUN_PROVISION);
        String[] provisionApp = getIntent().getStringArrayExtra(EXTRA_RUN_PROVISION);
        if (provisionApp == null || provisionApp.length < 2) {
            final Resources res = Utils.getResourcesForSubId(this, subId);
            provisionApp = res.getStringArray(
@@ -77,8 +79,8 @@ public class TetherProvisioningActivity extends Activity {
        final Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClassName(provisionApp[0], provisionApp[1]);
        intent.putExtra(EXTRA_TETHER_TYPE, tetherType);
        intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
        intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, mResultReceiver);
        intent.putExtra(EXTRA_SUBSCRIPTION_INDEX, subId);
        intent.putExtra(EXTRA_PROVISION_CALLBACK, mResultReceiver);
        if (DEBUG) {
            Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]);
        }
@@ -86,7 +88,7 @@ public class TetherProvisioningActivity extends Activity {
        if (getPackageManager().queryIntentActivities(intent,
                PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
            Log.e(TAG, "Provisioning app is configured, but not available.");
            mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
            mResultReceiver.send(TETHER_ERROR_PROVISIONING_FAILED, null);
            finish();
            return;
        }
@@ -99,9 +101,8 @@ public class TetherProvisioningActivity extends Activity {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == PROVISION_REQUEST) {
            if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode);
            int result = resultCode == Activity.RESULT_OK ?
                    TetheringManager.TETHER_ERROR_NO_ERROR :
                    TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
            int result = resultCode == Activity.RESULT_OK
                    ? TETHER_ERROR_NO_ERROR : TETHER_ERROR_PROVISIONING_FAILED;
            mResultReceiver.send(result, null);
            finish();
        }
+35 −63
Original line number Diff line number Diff line
@@ -16,13 +16,24 @@

package com.android.settings.wifi.tether;

import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
import static android.net.TetheringConstants.EXTRA_REM_TETHER_TYPE;
import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
import static android.net.TetheringManager.TETHERING_ETHERNET;
import static android.net.TetheringManager.TETHERING_INVALID;
import static android.net.TetheringManager.TETHERING_USB;
import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

import android.app.Activity;
import android.app.Service;
import android.app.usage.UsageStatsManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile.ServiceListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -31,7 +42,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.TetheringManager;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.telephony.SubscriptionManager;
@@ -89,17 +100,16 @@ public class TetherService extends Service {
        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>());
        mPendingCallbacks.put(TETHERING_WIFI, new ArrayList<ResultReceiver>());
        mPendingCallbacks.put(TETHERING_USB, new ArrayList<ResultReceiver>());
        mPendingCallbacks.put(TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
        mPendingCallbacks.put(TETHERING_ETHERNET, new ArrayList<ResultReceiver>());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.hasExtra(EXTRA_SUBID)) {
            final int tetherSubId = intent.getIntExtra(EXTRA_SUBID,
                    SubscriptionManager.INVALID_SUBSCRIPTION_ID);
            final int tetherSubId = intent.getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
            final int subId = getTetherServiceWrapper().getActiveDataSubscriptionId();
            if (tetherSubId != subId) {
                Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
@@ -109,18 +119,16 @@ public class TetherService extends Service {
                return START_NOT_STICKY;
            }
        }
        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 (intent.hasExtra(EXTRA_ADD_TETHER_TYPE)) {
            int type = intent.getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);
            ResultReceiver callback = intent.getParcelableExtra(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);
                    callback.send(TETHER_ERROR_UNKNOWN_IFACE, null);
                    stopSelf();
                    return START_NOT_STICKY;
                }
@@ -132,10 +140,9 @@ public class TetherService extends Service {
            }
        }

        if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) {
        if (intent.hasExtra(EXTRA_REM_TETHER_TYPE)) {
            if (!mInProvisionCheck) {
                int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE,
                        ConnectivityManager.TETHERING_INVALID);
                int type = intent.getIntExtra(EXTRA_REM_TETHER_TYPE, TETHERING_INVALID);
                int index = mCurrentTethers.indexOf(type);
                if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
                if (index >= 0) {
@@ -146,7 +153,7 @@ public class TetherService extends Service {
            }
        }

        if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) {
        if (intent.getBooleanExtra(EXTRA_RUN_PROVISION, false)) {
            startProvisioning(mCurrentTypeIndex);
        } else if (!mInProvisionCheck) {
            // If we aren't running any provisioning, no reason to stay alive.
@@ -207,32 +214,9 @@ public class TetherService extends Service {
        return buffer.toString();
    }

    private void disableWifiTethering() {
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.stopTethering(ConnectivityManager.TETHERING_WIFI);
    }

    private void disableUsbTethering() {
        ConnectivityManager cm =
                (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        cm.setUsbTethering(false);
    }

    private void disableBtTethering() {
        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            adapter.getProfileProxy(this, new ServiceListener() {
                @Override
                public void onServiceDisconnected(int profile) { }

                @Override
                public void onServiceConnected(int profile, BluetoothProfile proxy) {
                    ((BluetoothPan) proxy).setBluetoothTethering(false);
                    adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
                }
            }, BluetoothProfile.PAN);
        }
    private void disableTethering(final int tetheringType) {
        final TetheringManager tm = (TetheringManager) getSystemService(Context.TETHERING_SERVICE);
        tm.stopTethering(tetheringType);
    }

    private void startProvisioning(int index) {
@@ -255,7 +239,7 @@ public class TetherService extends Service {
        Intent intent = new Intent(provisionAction);
        int type = mCurrentTethers.get(index);
        intent.putExtra(TETHER_CHOICE, type);
        intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
        intent.putExtra(EXTRA_SUBSCRIPTION_INDEX, subId);
        intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND
                | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);

@@ -285,8 +269,8 @@ public class TetherService extends Service {
        if (callbacksForType == null) {
            return;
        }
        int errorCode = result == RESULT_OK ? ConnectivityManager.TETHER_ERROR_NO_ERROR :
                ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
        int errorCode = result == RESULT_OK ? TETHER_ERROR_NO_ERROR :
                TETHER_ERROR_PROVISIONING_FAILED;
        for (ResultReceiver callback : callbacksForType) {
          if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback");
          callback.send(errorCode, null);
@@ -309,19 +293,7 @@ public class TetherService extends Service {
                int checkType = mCurrentTethers.get(mCurrentTypeIndex);
                mInProvisionCheck = false;
                int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT);
                if (result != RESULT_OK) {
                    switch (checkType) {
                        case ConnectivityManager.TETHERING_WIFI:
                            disableWifiTethering();
                            break;
                        case ConnectivityManager.TETHERING_BLUETOOTH:
                            disableBtTethering();
                            break;
                        case ConnectivityManager.TETHERING_USB:
                            disableUsbTethering();
                            break;
                    }
                }
                if (result != RESULT_OK) disableTethering(checkType);
                fireCallbacksForType(checkType, result);

                if (++mCurrentTypeIndex >= mCurrentTethers.size()) {
+17 −17
Original line number Diff line number Diff line
@@ -16,15 +16,15 @@

package com.android.settings.wifi.tether;

import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE;
import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK;
import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION;
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;
import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR;
import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
import static android.net.TetheringManager.TETHERING_INVALID;
import static android.net.TetheringManager.TETHERING_USB;
import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;

import static org.mockito.Matchers.any;
@@ -47,7 +47,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.TetheringManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.ResultReceiver;
@@ -88,7 +88,7 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
    private ProvisionReceiver mProvisionReceiver;
    private Receiver mResultReceiver;

    @Mock private ConnectivityManager mConnectivityManager;
    @Mock private TetheringManager mTetheringManager;
    @Mock private PackageManager mPackageManager;
    @Mock private WifiManager mWifiManager;
    @Mock private SharedPreferences mPrefs;
@@ -208,9 +208,9 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
        runProvisioningForType(TETHERING_WIFI);

        assertTrue(waitForProvisionRequest(TETHERING_WIFI));
        assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISION_FAILED));
        assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISIONING_FAILED));

        verify(mConnectivityManager).stopTethering(ConnectivityManager.TETHERING_WIFI);
        verify(mTetheringManager).stopTethering(TETHERING_WIFI);
    }

    public void testFailureStopsTethering_Usb() {
@@ -219,9 +219,9 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
        runProvisioningForType(TETHERING_USB);

        assertTrue(waitForProvisionRequest(TETHERING_USB));
        assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISION_FAILED));
        assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISIONING_FAILED));

        verify(mConnectivityManager).setUsbTethering(eq(false));
        verify(mTetheringManager).stopTethering(TETHERING_USB);
    }

    public void testIgnoreOutdatedRequest() {
@@ -345,8 +345,8 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {

        @Override
        public Object getSystemService(String name) {
            if (CONNECTIVITY_SERVICE.equals(name)) {
                return mConnectivityManager;
            if (TETHERING_SERVICE.equals(name)) {
                return mTetheringManager;
            } else if (WIFI_SERVICE.equals(name)) {
                return mWifiManager;
            }