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

Commit 47312183 authored by Kenny Root's avatar Kenny Root
Browse files

AdbService: move source of truth for enabled

This makes the AdbService the source of truth for whether ADB is enabled
on the system or not. All UsbManager checks will now query the
AdbService.

Test: make
Bug: 63820489
Change-Id: I2965a15b5fa9f747fe0ae4dc791b5e357e78253f
parent c96dba72
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -18,4 +18,5 @@ package android.debug;

/** @hide */
interface IAdbTransport {
    void onAdbEnabled(boolean enabled);
}
+81 −2
Original line number Diff line number Diff line
@@ -15,15 +15,21 @@
 */
package com.android.server.adb;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.debug.AdbManagerInternal;
import android.debug.IAdbManager;
import android.debug.IAdbTransport;
import android.hardware.usb.UsbManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.ArrayMap;
import android.util.Slog;

@@ -86,12 +92,32 @@ public class AdbService extends IAdbManager.Stub {
        AdbHandler(Looper looper) {
            super(looper);
            try {
                // TODO: add initialization here.
                /*
                 * Use the normal bootmode persistent prop to maintain state of adb across
                 * all boot modes.
                 */
                mAdbEnabled = containsFunction(
                        SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY, ""),
                        UsbManager.USB_FUNCTION_ADB);

                // register observer to listen for settings changes
                mContentResolver.registerContentObserver(
                        Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
                        false, new AdbSettingsObserver());
            } catch (Exception e) {
                Slog.e(TAG, "Error initializing AdbHandler", e);
            }
        }

        private boolean containsFunction(String functions, String function) {
            int index = functions.indexOf(function);
            if (index < 0) return false;
            if (index > 0 && functions.charAt(index - 1) != ',') return false;
            int charAfter = index + function.length();
            if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false;
            return true;
        }

        public void sendMessage(int what, boolean arg) {
            removeMessages(what);
            Message m = Message.obtain(this, what);
@@ -102,15 +128,39 @@ public class AdbService extends IAdbManager.Stub {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // TODO: handle messages here.
                case MSG_ENABLE_ADB:
                    setAdbEnabled(msg.arg1 == 1);
                    break;
            }
        }
    }

    private class AdbSettingsObserver extends ContentObserver {
        AdbSettingsObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            boolean enable = (Settings.Global.getInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, 0) > 0);
            mHandler.sendMessage(MSG_ENABLE_ADB, enable);
        }
    }

    private static final String TAG = "AdbService";
    private static final boolean DEBUG = false;

    private static final int MSG_ENABLE_ADB = 1;

    /**
     * The persistent property which stores whether adb is enabled or not.
     * May also contain vendor-specific default functions for testing purposes.
     */
    private static final String USB_PERSISTENT_CONFIG_PROPERTY = "persist.sys.usb.config";

    private final Context mContext;
    private final ContentResolver mContentResolver;
    private final AdbService.AdbHandler mHandler;
    private final ArrayMap<IBinder, IAdbTransport> mTransports = new ArrayMap<>();

@@ -118,6 +168,7 @@ public class AdbService extends IAdbManager.Stub {

    private AdbService(Context context) {
        mContext = context;
        mContentResolver = context.getContentResolver();

        mHandler = new AdbHandler(FgThread.get().getLooper());

@@ -130,6 +181,32 @@ public class AdbService extends IAdbManager.Stub {
     */
    public void systemReady() {
        if (DEBUG) Slog.d(TAG, "systemReady");

        // make sure the ADB_ENABLED setting value matches the current state
        try {
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, mAdbEnabled ? 1 : 0);
        } catch (SecurityException e) {
            // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't be changed.
            Slog.d(TAG, "ADB_ENABLED is restricted.");
        }
    }

    private void setAdbEnabled(boolean enable) {
        if (DEBUG) Slog.d(TAG, "setAdbEnabled(" + enable + "), mAdbEnabled=" + mAdbEnabled);

        if (enable == mAdbEnabled) {
            return;
        }
        mAdbEnabled = enable;

        for (IAdbTransport transport : mTransports.values()) {
            try {
                transport.onAdbEnabled(enable);
            } catch (RemoteException e) {
                Slog.w(TAG, "Unable to send onAdbEnabled to transport " + transport.toString());
            }
        }
    }

    @Override
@@ -142,6 +219,8 @@ public class AdbService extends IAdbManager.Stub {
            if (args == null || args.length == 0 || "-a".equals(args[0])) {
                pw.println("ADB Manager State:");
                pw.increaseIndent();
                pw.print("ADB enabled: ");
                pw.println(mAdbEnabled);
                pw.print("Number of registered transports: ");
                pw.println(mTransports.size());
            } else {
+29 −57
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.debug.AdbManagerInternal;
import android.debug.IAdbTransport;
import android.hardware.usb.UsbAccessory;
@@ -202,19 +201,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        sBlackListedInterfaces.add(UsbConstants.USB_CLASS_WIRELESS_CONTROLLER);
    }

    private class AdbSettingsObserver extends ContentObserver {
        public AdbSettingsObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            boolean enable = (Settings.Global.getInt(mContentResolver,
                    Settings.Global.ADB_ENABLED, 0) > 0);
            mHandler.sendMessage(MSG_ENABLE_ADB, enable);
        }
    }

    /*
     * Listens for uevent messages from the kernel to monitor the USB state
     */
@@ -367,11 +353,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        mUEventObserver = new UsbUEventObserver();
        mUEventObserver.startObserving(USB_STATE_MATCH);
        mUEventObserver.startObserving(ACCESSORY_START_MATCH);

        // register observer to listen for settings changes
        mContentResolver.registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
                false, new AdbSettingsObserver());
    }

    UsbProfileGroupSettingsManager getCurrentSettings() {
@@ -491,7 +472,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        private NotificationManager mNotificationManager;

        protected long mScreenUnlockedFunctions;
        protected boolean mAdbEnabled;
        protected boolean mBootCompleted;
        protected boolean mCurrentFunctionsApplied;
        protected boolean mUseUsbNotification;
@@ -522,13 +502,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
            mCurrentUser = ActivityManager.getCurrentUser();
            mScreenLocked = true;

            /*
             * Use the normal bootmode persistent prop to maintain state of adb across
             * all boot modes.
             */
            mAdbEnabled = UsbHandlerLegacy.containsFunction(getSystemProperty(
                    USB_PERSISTENT_CONFIG_PROPERTY, ""), UsbManager.USB_FUNCTION_ADB);

            mSettings = getPinnedSharedPrefs(mContext);
            if (mSettings == null) {
                Slog.e(TAG, "Couldn't load shared preferences");
@@ -629,8 +602,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser

        private void setAdbEnabled(boolean enable) {
            if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
            if (enable != mAdbEnabled) {
                mAdbEnabled = enable;

            if (enable) {
                setSystemProperty(USB_PERSISTENT_CONFIG_PROPERTY, UsbManager.USB_FUNCTION_ADB);
@@ -640,10 +611,9 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser

            setEnabledFunctions(mCurrentFunctions, true);
            updateAdbNotification(false);
            }

            if (mDebuggingManager != null) {
                mDebuggingManager.setAdbEnabled(mAdbEnabled);
                mDebuggingManager.setAdbEnabled(enable);
            }
        }

@@ -808,6 +778,11 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
            AdbTransport(UsbHandler handler) {
                mHandler = handler;
            }

            @Override
            public void onAdbEnabled(boolean enabled) {
                mHandler.sendMessage(MSG_ENABLE_ADB, enabled);
            }
        }

        /**
@@ -818,7 +793,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
            if (functions == UsbManager.FUNCTION_NONE) {
                return getChargingFunctions();
            }
            if (mAdbEnabled) {
            if (isAdbEnabled()) {
                return functions | UsbManager.FUNCTION_ADB;
            }
            return functions;
@@ -1044,17 +1019,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                    mUsbDeviceManager.getCurrentSettings().accessoryAttached(mCurrentAccessory);
                }
                if (mDebuggingManager != null) {
                    mDebuggingManager.setAdbEnabled(mAdbEnabled);
                }

                // make sure the ADB_ENABLED setting value matches the current state
                try {
                    putGlobalSettings(mContentResolver, Settings.Global.ADB_ENABLED,
                            mAdbEnabled ? 1 : 0);
                } catch (SecurityException e) {
                    // If UserManager.DISALLOW_DEBUGGING_FEATURES is on, that this setting can't
                    // be changed.
                    Slog.d(TAG, "ADB_ENABLED is restricted.");
                    mDebuggingManager.setAdbEnabled(isAdbEnabled());
                }

                updateUsbNotification(false);
@@ -1205,12 +1170,16 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
            }
        }

        protected boolean isAdbEnabled() {
            return LocalServices.getService(AdbManagerInternal.class).isAdbEnabled();
        }

        protected void updateAdbNotification(boolean force) {
            if (mNotificationManager == null) return;
            final int id = SystemMessage.NOTE_ADB_ACTIVE;
            final int titleRes = com.android.internal.R.string.adb_active_notification_title;

            if (mAdbEnabled && mConnected) {
            if (isAdbEnabled() && mConnected) {
                if ("0".equals(getSystemProperty("persist.adb.notify", ""))) return;

                if (force && mAdbNotificationShown) {
@@ -1264,7 +1233,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        protected long getChargingFunctions() {
            // if ADB is enabled, reset functions to ADB
            // else enable MTP as usual.
            if (mAdbEnabled) {
            if (isAdbEnabled()) {
                return UsbManager.FUNCTION_ADB;
            } else {
                return UsbManager.FUNCTION_MTP;
@@ -1333,7 +1302,6 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                    mHideUsbNotification);
            dump.write("audio_accessory_connected", UsbHandlerProto.AUDIO_ACCESSORY_CONNECTED,
                    mAudioAccessoryConnected);
            dump.write("adb_enabled", UsbHandlerProto.ADB_ENABLED, mAdbEnabled);

            try {
                writeStringIfNotNull(dump, "kernel_state", UsbHandlerProto.KERNEL_STATE,
@@ -1460,7 +1428,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                            + overrideFunctions.second);
                    if (!overrideFunctions.second.equals("")) {
                        String newFunction;
                        if (mAdbEnabled) {
                        if (isAdbEnabled()) {
                            newFunction = addFunction(overrideFunctions.second,
                                    UsbManager.USB_FUNCTION_ADB);
                        } else {
@@ -1471,7 +1439,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                        setSystemProperty(getPersistProp(false), newFunction);
                    }
                    return overrideFunctions.first;
                } else if (mAdbEnabled) {
                } else if (isAdbEnabled()) {
                    String newFunction = addFunction(UsbManager.USB_FUNCTION_NONE,
                            UsbManager.USB_FUNCTION_ADB);
                    setSystemProperty(getPersistProp(false), newFunction);
@@ -1577,7 +1545,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
            if (functions == null) {
                functions = "";
            }
            if (mAdbEnabled) {
            if (isAdbEnabled()) {
                functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
            } else {
                functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
@@ -1864,7 +1832,7 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
                     * Dont force to default when the configuration is already set to default.
                     */
                    if (msg.arg1 != 1) {
                        setEnabledFunctions(UsbManager.FUNCTION_NONE, !mAdbEnabled);
                        setEnabledFunctions(UsbManager.FUNCTION_NONE, !isAdbEnabled());
                    }
                    break;
                default:
@@ -2096,6 +2064,10 @@ public class UsbDeviceManager implements ActivityTaskManagerInternal.ScreenObser
        }
    }

    private void onAdbEnabled(boolean enabled) {
        mHandler.sendMessage(MSG_ENABLE_ADB, enabled);
    }

    /**
     * Write the state to a dump stream.
     */