Merge changes from topic "usb-adb-separation"

* changes:
  Update USB tests for ADB split
  Move AdbDebuggingManager to AdbService
  AdbService: move source of truth for enabled
  Add empty AdbHandler
  Add systemReady call for AdbService
  Add function to query ADB state
  Register USB as an ADB transport type
  Add ADB transport skeleton
  Add empty AdbManagerInternal for system server
  Add empty AdbService to SystemServer
  Move ADB debugging manager to core
  Rename to AdbDebuggingManager
  Move UsbDebuggingManager to new package
diff --git a/Android.bp b/Android.bp
index 56d6557..b715b73 100644
--- a/Android.bp
+++ b/Android.bp
@@ -150,6 +150,8 @@
         "core/java/android/content/pm/dex/ISnapshotRuntimeProfileCallback.aidl",
         "core/java/android/content/pm/permission/IRuntimePermissionPresenter.aidl",
         "core/java/android/database/IContentObserver.aidl",
+        "core/java/android/debug/IAdbManager.aidl",
+        "core/java/android/debug/IAdbTransport.aidl",
         ":libcamera_client_aidl",
         ":libcamera_client_framework_aidl",
         "core/java/android/hardware/IConsumerIrService.aidl",
diff --git a/api/system-current.txt b/api/system-current.txt
index 1649fc7..10cf633 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -100,6 +100,7 @@
     field public static final java.lang.String MANAGE_AUTO_FILL = "android.permission.MANAGE_AUTO_FILL";
     field public static final java.lang.String MANAGE_CARRIER_OEM_UNLOCK_STATE = "android.permission.MANAGE_CARRIER_OEM_UNLOCK_STATE";
     field public static final java.lang.String MANAGE_CA_CERTIFICATES = "android.permission.MANAGE_CA_CERTIFICATES";
+    field public static final java.lang.String MANAGE_DEBUGGING = "android.permission.MANAGE_DEBUGGING";
     field public static final java.lang.String MANAGE_DEVICE_ADMINS = "android.permission.MANAGE_DEVICE_ADMINS";
     field public static final java.lang.String MANAGE_IPSEC_TUNNELS = "android.permission.MANAGE_IPSEC_TUNNELS";
     field public static final java.lang.String MANAGE_ROLE_HOLDERS = "android.permission.MANAGE_ROLE_HOLDERS";
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index 77cebc8..508ea3b 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -48,6 +48,8 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ShortcutManager;
 import android.content.res.Resources;
+import android.debug.AdbManager;
+import android.debug.IAdbManager;
 import android.hardware.ConsumerIrManager;
 import android.hardware.ISerialManager;
 import android.hardware.SensorManager;
@@ -583,6 +585,15 @@
                 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
             }});
 
+        registerService(Context.ADB_SERVICE, AdbManager.class,
+                new CachedServiceFetcher<AdbManager>() {
+                    @Override
+                    public AdbManager createService(ContextImpl ctx)
+                                throws ServiceNotFoundException {
+                        IBinder b = ServiceManager.getServiceOrThrow(Context.ADB_SERVICE);
+                        return new AdbManager(ctx, IAdbManager.Stub.asInterface(b));
+                    }});
+
         registerService(Context.SERIAL_SERVICE, SerialManager.class,
                 new CachedServiceFetcher<SerialManager>() {
             @Override
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index 713cc8b..e378800 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -3967,6 +3967,18 @@
 
     /**
      * Use with {@link #getSystemService(String)} to retrieve a {@link
+     * Use with {@link #getSystemService} to retrieve a {@link
+     * android.debug.AdbManager} for access to ADB debug functions.
+     *
+     * @see #getSystemService(String)
+     * @see android.debug.AdbManager
+     *
+     * @hide
+     */
+    public static final String ADB_SERVICE = "adb";
+
+    /**
+     * Use with {@link #getSystemService(String)} to retrieve a {@link
      * android.hardware.SerialManager} for access to serial ports.
      *
      * @see #getSystemService(String)
diff --git a/core/java/android/debug/AdbManager.java b/core/java/android/debug/AdbManager.java
new file mode 100644
index 0000000..ae3d794
--- /dev/null
+++ b/core/java/android/debug/AdbManager.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.debug;
+
+import android.annotation.SystemService;
+import android.content.Context;
+
+/**
+ * This class allows the control of ADB-related functions. Currently only ADB over USB is
+ * supported, and none of the API is public.
+ *
+ * @hide
+ */
+@SystemService(Context.ADB_SERVICE)
+public class AdbManager {
+    private static final String TAG = "AdbManager";
+
+    private final Context mContext;
+    private final IAdbManager mService;
+
+    /**
+     * {@hide}
+     */
+    public AdbManager(Context context, IAdbManager service) {
+        mContext = context;
+        mService = service;
+    }
+}
diff --git a/core/java/android/debug/AdbManagerInternal.java b/core/java/android/debug/AdbManagerInternal.java
new file mode 100644
index 0000000..4469f0f
--- /dev/null
+++ b/core/java/android/debug/AdbManagerInternal.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.debug;
+
+/**
+ * This class allows the control of ADB-related functions that should only be called from the system
+ * server.
+ *
+ * @hide Only should be called from the system server.
+ */
+public abstract class AdbManagerInternal {
+    /**
+     * Registers a ADB transport mechanism.
+     *
+     * @param transport ADB transport interface to register
+     */
+    public abstract void registerTransport(IAdbTransport transport);
+
+    /**
+     * Unregisters a previously registered ADB transport mechanism.
+     *
+     * @param transport previously-added ADB transport interface to be removed
+     */
+    public abstract void unregisterTransport(IAdbTransport transport);
+
+    /**
+     * Returns {@code true} if ADB debugging is enabled.
+     */
+    public abstract boolean isAdbEnabled();
+}
diff --git a/core/java/android/debug/IAdbManager.aidl b/core/java/android/debug/IAdbManager.aidl
new file mode 100644
index 0000000..79e0794
--- /dev/null
+++ b/core/java/android/debug/IAdbManager.aidl
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.debug;
+
+/**
+ * Interface to communicate remotely with the {@code AdbService} in the system server.
+ *
+ * @hide
+ */
+interface IAdbManager {
+    /**
+     * Allow ADB debugging from the attached host. If {@code alwaysAllow} is
+     * {@code true}, add {@code publicKey} to list of host keys that the
+     * user has approved.
+     *
+     * @param alwaysAllow if true, add permanently to list of allowed keys
+     * @param publicKey RSA key in mincrypt format and Base64-encoded
+     */
+    void allowDebugging(boolean alwaysAllow, String publicKey);
+
+    /**
+     * Deny ADB debugging from the attached host.
+     */
+    void denyDebugging();
+
+    /**
+     * Clear all public keys installed for secure ADB debugging.
+     */
+    void clearDebuggingKeys();
+}
diff --git a/core/java/android/debug/IAdbTransport.aidl b/core/java/android/debug/IAdbTransport.aidl
new file mode 100644
index 0000000..77211fc93
--- /dev/null
+++ b/core/java/android/debug/IAdbTransport.aidl
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.debug;
+
+/** @hide */
+interface IAdbTransport {
+    void onAdbEnabled(boolean enabled);
+}
diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl
index 6d9c913..f4e776c 100644
--- a/core/java/android/hardware/usb/IUsbManager.aidl
+++ b/core/java/android/hardware/usb/IUsbManager.aidl
@@ -111,17 +111,6 @@
      */
     ParcelFileDescriptor getControlFd(long function);
 
-    /* Allow USB debugging from the attached host. If alwaysAllow is true, add the
-     * the public key to list of host keys that the user has approved.
-     */
-    void allowUsbDebugging(boolean alwaysAllow, String publicKey);
-
-    /* Deny USB debugging from the attached host */
-    void denyUsbDebugging();
-
-    /* Clear public keys installed for secure USB debugging */
-    void clearUsbDebuggingKeys();
-
     /* Gets the list of USB ports. */
     UsbPort[] getPorts();
 
diff --git a/core/proto/android/service/adb.proto b/core/proto/android/service/adb.proto
new file mode 100644
index 0000000..0060813
--- /dev/null
+++ b/core/proto/android/service/adb.proto
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+syntax = "proto2";
+package android.service.adb;
+
+option java_multiple_files = true;
+option java_outer_classname = "AdbServiceProto";
+
+import "frameworks/base/libs/incident/proto/android/privacy.proto";
+
+message AdbServiceDumpProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    optional AdbDebuggingManagerProto debugging_manager = 1;
+}
+
+message AdbDebuggingManagerProto {
+    option (android.msg_privacy).dest = DEST_AUTOMATIC;
+
+    optional bool connected_to_adb = 1;
+    optional string last_key_recevied = 2 [ (android.privacy).dest = DEST_EXPLICIT ];
+    optional string user_keys = 3 [ (android.privacy).dest = DEST_LOCAL ];
+    optional string system_keys = 4 [ (android.privacy).dest = DEST_LOCAL ];
+}
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index e728ead..caee5f0 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -1791,6 +1791,12 @@
     <permission android:name="android.permission.MANAGE_USB"
         android:protectionLevel="signature|privileged" />
 
+    <!-- @SystemApi Allows an application to manage Android Debug Bridge settings.
+         <p>Not for use by third-party applications.
+         @hide -->
+    <permission android:name="android.permission.MANAGE_DEBUGGING"
+        android:protectionLevel="signature|privileged" />
+
     <!-- @SystemApi Allows an application to access the MTP USB kernel driver.
          For use only by the device side MTP implementation.
          @hide -->
diff --git a/data/etc/privapp-permissions-platform.xml b/data/etc/privapp-permissions-platform.xml
index 8964ee3..2604365 100644
--- a/data/etc/privapp-permissions-platform.xml
+++ b/data/etc/privapp-permissions-platform.xml
@@ -256,6 +256,7 @@
         <permission name="android.permission.DELETE_PACKAGES"/>
         <permission name="android.permission.FORCE_STOP_PACKAGES"/>
         <permission name="android.permission.LOCAL_MAC_ADDRESS"/>
+        <permission name="android.permission.MANAGE_DEBUGGING"/>
         <permission name="android.permission.MANAGE_DEVICE_ADMINS"/>
         <permission name="android.permission.MANAGE_FINGERPRINT"/>
         <permission name="android.permission.MANAGE_USB"/>
@@ -376,6 +377,7 @@
         <permission name="android.permission.GET_APP_OPS_STATS"/>
         <permission name="android.permission.INTERACT_ACROSS_USERS"/>
         <permission name="android.permission.MANAGE_ACTIVITY_STACKS"/>
+        <permission name="android.permission.MANAGE_DEBUGGING"/>
         <permission name="android.permission.MANAGE_USB"/>
         <permission name="android.permission.MANAGE_USERS"/>
         <permission name="android.permission.MASTER_CLEAR"/>
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index d9b2063..b2bb883 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -216,6 +216,9 @@
     <!-- Permission necessary to change car audio volume through CarAudioManager -->
     <uses-permission android:name="android.car.permission.CAR_CONTROL_AUDIO_VOLUME" />
 
+    <!-- Permission to control Android Debug Bridge (ADB) -->
+    <uses-permission android:name="android.permission.MANAGE_DEBUGGING" />
+
     <uses-permission android:name="android.permission.HIDE_NON_SYSTEM_OVERLAY_WINDOWS" />
 
     <protected-broadcast android:name="com.android.settingslib.action.REGISTER_SLICE_RECEIVER" />
@@ -376,9 +379,9 @@
             android:excludeFromRecents="true">
         </activity>
 
-        <!-- started from UsbDebuggingManager -->
+        <!-- started from AdbDebuggingManager -->
         <activity android:name=".usb.UsbDebuggingActivity"
-            android:permission="android.permission.MANAGE_USB"
+            android:permission="android.permission.MANAGE_DEBUGGING"
             android:theme="@style/Theme.SystemUI.Dialog.Alert"
             android:finishOnCloseSystemDialogs="true"
             android:excludeFromRecents="true">
diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java
index 4102e63..ed2ad79 100644
--- a/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/usb/UsbDebuggingActivity.java
@@ -23,7 +23,7 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.hardware.usb.IUsbManager;
+import android.debug.IAdbManager;
 import android.hardware.usb.UsbManager;
 import android.os.Bundle;
 import android.os.IBinder;
@@ -153,12 +153,12 @@
         boolean allow = (which == AlertDialog.BUTTON_POSITIVE);
         boolean alwaysAllow = allow && mAlwaysAllow.isChecked();
         try {
-            IBinder b = ServiceManager.getService(USB_SERVICE);
-            IUsbManager service = IUsbManager.Stub.asInterface(b);
+            IBinder b = ServiceManager.getService(ADB_SERVICE);
+            IAdbManager service = IAdbManager.Stub.asInterface(b);
             if (allow) {
-                service.allowUsbDebugging(alwaysAllow, mKey);
+                service.allowDebugging(alwaysAllow, mKey);
             } else {
-                service.denyUsbDebugging();
+                service.denyDebugging();
             }
         } catch (Exception e) {
             Log.e(TAG, "Unable to notify Usb service", e);
diff --git a/services/usb/java/com/android/server/usb/UsbDebuggingManager.java b/services/core/java/com/android/server/adb/AdbDebuggingManager.java
similarity index 84%
rename from services/usb/java/com/android/server/usb/UsbDebuggingManager.java
rename to services/core/java/com/android/server/adb/AdbDebuggingManager.java
index 3b08505..ccead6c 100644
--- a/services/usb/java/com/android/server/usb/UsbDebuggingManager.java
+++ b/services/core/java/com/android/server/adb/AdbDebuggingManager.java
@@ -10,11 +10,11 @@
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions an
+ * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
-package com.android.server.usb;
+package com.android.server.adb;
 
 import static com.android.internal.util.dump.DumpUtils.writeStringIfNotNull;
 
@@ -37,7 +37,7 @@
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.service.usb.UsbDebuggingManagerProto;
+import android.service.adb.AdbDebuggingManagerProto;
 import android.util.Base64;
 import android.util.Slog;
 
@@ -53,8 +53,12 @@
 import java.security.MessageDigest;
 import java.util.Arrays;
 
-public class UsbDebuggingManager {
-    private static final String TAG = "UsbDebuggingManager";
+/**
+ * Provides communication to the Android Debug Bridge daemon to allow, deny, or clear public keysi
+ * that are authorized to connect to the ADB service itself.
+ */
+public class AdbDebuggingManager {
+    private static final String TAG = "AdbDebuggingManager";
     private static final boolean DEBUG = false;
 
     private static final String ADBD_SOCKET = "adbd";
@@ -64,22 +68,22 @@
 
     private final Context mContext;
     private final Handler mHandler;
-    private UsbDebuggingThread mThread;
+    private AdbDebuggingThread mThread;
     private boolean mAdbEnabled = false;
     private String mFingerprints;
 
-    public UsbDebuggingManager(Context context) {
-        mHandler = new UsbDebuggingHandler(FgThread.get().getLooper());
+    public AdbDebuggingManager(Context context) {
+        mHandler = new AdbDebuggingHandler(FgThread.get().getLooper());
         mContext = context;
     }
 
-    class UsbDebuggingThread extends Thread {
+    class AdbDebuggingThread extends Thread {
         private boolean mStopped;
         private LocalSocket mSocket;
         private OutputStream mOutputStream;
         private InputStream mInputStream;
 
-        UsbDebuggingThread() {
+        AdbDebuggingThread() {
             super(TAG);
         }
 
@@ -138,7 +142,8 @@
                     if (buffer[0] == 'P' && buffer[1] == 'K') {
                         String key = new String(Arrays.copyOfRange(buffer, 2, count));
                         Slog.d(TAG, "Received public key: " + key);
-                        Message msg = mHandler.obtainMessage(UsbDebuggingHandler.MESSAGE_ADB_CONFIRM);
+                        Message msg = mHandler.obtainMessage(
+                                AdbDebuggingHandler.MESSAGE_ADB_CONFIRM);
                         msg.obj = key;
                         mHandler.sendMessage(msg);
                     } else {
@@ -188,8 +193,7 @@
                 if (!mStopped && mOutputStream != null) {
                     try {
                         mOutputStream.write(msg.getBytes());
-                    }
-                    catch (IOException ex) {
+                    } catch (IOException ex) {
                         Slog.e(TAG, "Failed to write response:", ex);
                     }
                 }
@@ -197,7 +201,7 @@
         }
     }
 
-    class UsbDebuggingHandler extends Handler {
+    class AdbDebuggingHandler extends Handler {
         private static final int MESSAGE_ADB_ENABLED = 1;
         private static final int MESSAGE_ADB_DISABLED = 2;
         private static final int MESSAGE_ADB_ALLOW = 3;
@@ -205,26 +209,28 @@
         private static final int MESSAGE_ADB_CONFIRM = 5;
         private static final int MESSAGE_ADB_CLEAR = 6;
 
-        public UsbDebuggingHandler(Looper looper) {
+        AdbDebuggingHandler(Looper looper) {
             super(looper);
         }
 
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case MESSAGE_ADB_ENABLED:
-                    if (mAdbEnabled)
+                    if (mAdbEnabled) {
                         break;
+                    }
 
                     mAdbEnabled = true;
 
-                    mThread = new UsbDebuggingThread();
+                    mThread = new AdbDebuggingThread();
                     mThread.start();
 
                     break;
 
                 case MESSAGE_ADB_DISABLED:
-                    if (!mAdbEnabled)
+                    if (!mAdbEnabled) {
                         break;
+                    }
 
                     mAdbEnabled = false;
 
@@ -236,7 +242,7 @@
                     break;
 
                 case MESSAGE_ADB_ALLOW: {
-                    String key = (String)msg.obj;
+                    String key = (String) msg.obj;
                     String fingerprints = getFingerprints(key);
 
                     if (!fingerprints.equals(mFingerprints)) {
@@ -270,7 +276,7 @@
                         }
                         break;
                     }
-                    String key = (String)msg.obj;
+                    String key = (String) msg.obj;
                     String fingerprints = getFingerprints(key);
                     if ("".equals(fingerprints)) {
                         if (mThread != null) {
@@ -317,8 +323,9 @@
         for (int i = 0; i < digest.length; i++) {
             sb.append(hex.charAt((digest[i] >> 4) & 0xf));
             sb.append(hex.charAt(digest[i] & 0xf));
-            if (i < digest.length - 1)
+            if (i < digest.length - 1) {
                 sb.append(":");
+            }
         }
         return sb.toString();
     }
@@ -413,16 +420,14 @@
             if (!keyFile.exists()) {
                 keyFile.createNewFile();
                 FileUtils.setPermissions(keyFile.toString(),
-                    FileUtils.S_IRUSR | FileUtils.S_IWUSR |
-                    FileUtils.S_IRGRP, -1, -1);
+                        FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP, -1, -1);
             }
 
             FileOutputStream fo = new FileOutputStream(keyFile, true);
             fo.write(key.getBytes());
             fo.write('\n');
             fo.close();
-        }
-        catch (IOException ex) {
+        } catch (IOException ex) {
             Slog.e(TAG, "Error writing key:" + ex);
         }
     }
@@ -434,24 +439,40 @@
         }
     }
 
+    /**
+     * When {@code enabled} is {@code true}, this allows ADB debugging and starts the ADB hanler
+     * thread. When {@code enabled} is {@code false}, this disallows ADB debugging and shuts
+     * down the handler thread.
+     */
     public void setAdbEnabled(boolean enabled) {
-        mHandler.sendEmptyMessage(enabled ? UsbDebuggingHandler.MESSAGE_ADB_ENABLED
-                                          : UsbDebuggingHandler.MESSAGE_ADB_DISABLED);
+        mHandler.sendEmptyMessage(enabled ? AdbDebuggingHandler.MESSAGE_ADB_ENABLED
+                                          : AdbDebuggingHandler.MESSAGE_ADB_DISABLED);
     }
 
-    public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
-        Message msg = mHandler.obtainMessage(UsbDebuggingHandler.MESSAGE_ADB_ALLOW);
+    /**
+     * Allows the debugging from the endpoint identified by {@code publicKey} either once or
+     * always if {@code alwaysAllow} is {@code true}.
+     */
+    public void allowDebugging(boolean alwaysAllow, String publicKey) {
+        Message msg = mHandler.obtainMessage(AdbDebuggingHandler.MESSAGE_ADB_ALLOW);
         msg.arg1 = alwaysAllow ? 1 : 0;
         msg.obj = publicKey;
         mHandler.sendMessage(msg);
     }
 
-    public void denyUsbDebugging() {
-        mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_DENY);
+    /**
+     * Denies debugging connection from the device that last requested to connect.
+     */
+    public void denyDebugging() {
+        mHandler.sendEmptyMessage(AdbDebuggingHandler.MESSAGE_ADB_DENY);
     }
 
-    public void clearUsbDebuggingKeys() {
-        mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_CLEAR);
+    /**
+     * Clears all previously accepted ADB debugging public keys. Any subsequent request will need
+     * to pass through {@link #allowUsbDebugging(boolean, String)} again.
+     */
+    public void clearDebuggingKeys() {
+        mHandler.sendEmptyMessage(AdbDebuggingHandler.MESSAGE_ADB_CLEAR);
     }
 
     /**
@@ -460,19 +481,19 @@
     public void dump(DualDumpOutputStream dump, String idName, long id) {
         long token = dump.start(idName, id);
 
-        dump.write("connected_to_adb", UsbDebuggingManagerProto.CONNECTED_TO_ADB, mThread != null);
-        writeStringIfNotNull(dump, "last_key_received", UsbDebuggingManagerProto.LAST_KEY_RECEIVED,
+        dump.write("connected_to_adb", AdbDebuggingManagerProto.CONNECTED_TO_ADB, mThread != null);
+        writeStringIfNotNull(dump, "last_key_received", AdbDebuggingManagerProto.LAST_KEY_RECEVIED,
                 mFingerprints);
 
         try {
-            dump.write("user_keys", UsbDebuggingManagerProto.USER_KEYS,
+            dump.write("user_keys", AdbDebuggingManagerProto.USER_KEYS,
                     FileUtils.readTextFile(new File("/data/misc/adb/adb_keys"), 0, null));
         } catch (IOException e) {
             Slog.e(TAG, "Cannot read user keys", e);
         }
 
         try {
-            dump.write("system_keys", UsbDebuggingManagerProto.SYSTEM_KEYS,
+            dump.write("system_keys", AdbDebuggingManagerProto.SYSTEM_KEYS,
                     FileUtils.readTextFile(new File("/adb_keys"), 0, null));
         } catch (IOException e) {
             Slog.e(TAG, "Cannot read system keys", e);
diff --git a/services/core/java/com/android/server/adb/AdbService.java b/services/core/java/com/android/server/adb/AdbService.java
new file mode 100644
index 0000000..e5ab8fc
--- /dev/null
+++ b/services/core/java/com/android/server/adb/AdbService.java
@@ -0,0 +1,311 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+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.service.adb.AdbServiceDumpProto;
+import android.util.ArrayMap;
+import android.util.ArraySet;
+import android.util.Slog;
+import android.util.proto.ProtoOutputStream;
+
+
+import com.android.internal.util.DumpUtils;
+import com.android.internal.util.IndentingPrintWriter;
+import com.android.internal.util.dump.DualDumpOutputStream;
+import com.android.server.FgThread;
+import com.android.server.LocalServices;
+import com.android.server.SystemService;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.Collections;
+
+/**
+ * The Android Debug Bridge (ADB) service. This controls the availability of ADB and authorization
+ * of devices allowed to connect to ADB.
+ */
+public class AdbService extends IAdbManager.Stub {
+    /**
+     * Manages the service lifecycle for {@code AdbService} in {@code SystemServer}.
+     */
+    public static class Lifecycle extends SystemService {
+        private AdbService mAdbService;
+
+        public Lifecycle(Context context) {
+            super(context);
+        }
+
+        @Override
+        public void onStart() {
+            mAdbService = new AdbService(getContext());
+            publishBinderService(Context.ADB_SERVICE, mAdbService);
+        }
+
+        @Override
+        public void onBootPhase(int phase) {
+            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
+                mAdbService.systemReady();
+            } else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
+                mAdbService.bootCompleted();
+            }
+        }
+    }
+
+    private class AdbManagerInternalImpl extends AdbManagerInternal {
+        @Override
+        public void registerTransport(IAdbTransport transport) {
+            mTransports.put(transport.asBinder(), transport);
+        }
+
+        @Override
+        public void unregisterTransport(IAdbTransport transport) {
+            mTransports.remove(transport.asBinder());
+        }
+
+        @Override
+        public boolean isAdbEnabled() {
+            return mAdbEnabled;
+        }
+    }
+
+    private final class AdbHandler extends Handler {
+        AdbHandler(Looper looper) {
+            super(looper);
+            try {
+                /*
+                 * 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);
+            m.arg1 = (arg ? 1 : 0);
+            sendMessage(m);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_ENABLE_ADB:
+                    setAdbEnabled(msg.arg1 == 1);
+                    break;
+                case MSG_BOOT_COMPLETED:
+                    if (mDebuggingManager != null) {
+                        mDebuggingManager.setAdbEnabled(mAdbEnabled);
+                    }
+                    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;
+    private static final int MSG_BOOT_COMPLETED = 2;
+
+    /**
+     * 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<>();
+
+    private boolean mAdbEnabled;
+    private AdbDebuggingManager mDebuggingManager;
+
+    private AdbService(Context context) {
+        mContext = context;
+        mContentResolver = context.getContentResolver();
+
+        boolean secureAdbEnabled = SystemProperties.getBoolean("ro.adb.secure", false);
+        boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
+        if (secureAdbEnabled && !dataEncrypted) {
+            mDebuggingManager = new AdbDebuggingManager(context);
+        }
+
+        mHandler = new AdbHandler(FgThread.get().getLooper());
+
+        LocalServices.addService(AdbManagerInternal.class, new AdbManagerInternalImpl());
+    }
+
+    /**
+     * Called in response to {@code SystemService.PHASE_ACTIVITY_MANAGER_READY} from {@code
+     * SystemServer}.
+     */
+    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.");
+        }
+    }
+
+    /**
+     * Callend in response to {@code SystemService.PHASE_BOOT_COMPLETED} from {@code SystemServer}.
+     */
+    public void bootCompleted() {
+        if (DEBUG) Slog.d(TAG, "boot completed");
+        mHandler.sendEmptyMessage(MSG_BOOT_COMPLETED);
+    }
+
+    @Override
+    public void allowDebugging(boolean alwaysAllow, String publicKey) {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING, null);
+        if (mDebuggingManager != null) {
+            mDebuggingManager.allowDebugging(alwaysAllow, publicKey);
+        }
+    }
+
+    @Override
+    public void denyDebugging() {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING, null);
+        if (mDebuggingManager != null) {
+            mDebuggingManager.denyDebugging();
+        }
+    }
+
+    @Override
+    public void clearDebuggingKeys() {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_DEBUGGING, null);
+        if (mDebuggingManager != null) {
+            mDebuggingManager.clearDebuggingKeys();
+        } else {
+            throw new RuntimeException("Cannot clear ADB debugging keys, "
+                    + "AdbDebuggingManager not enabled");
+        }
+    }
+
+    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());
+            }
+        }
+
+        if (mDebuggingManager != null) {
+            mDebuggingManager.setAdbEnabled(enable);
+        }
+    }
+
+    @Override
+    public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
+        if (!DumpUtils.checkDumpPermission(mContext, TAG, writer)) return;
+
+        final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
+        final long ident = Binder.clearCallingIdentity();
+        try {
+            ArraySet<String> argsSet = new ArraySet<>();
+            Collections.addAll(argsSet, args);
+
+            boolean dumpAsProto = false;
+            if (argsSet.contains("--proto")) {
+                dumpAsProto = true;
+            }
+
+            if (argsSet.size() == 0 || argsSet.contains("-a") || dumpAsProto) {
+                DualDumpOutputStream dump;
+                if (dumpAsProto) {
+                    dump = new DualDumpOutputStream(new ProtoOutputStream(fd));
+                } else {
+                    pw.println("ADB MANAGER STATE (dumpsys adb):");
+
+                    dump = new DualDumpOutputStream(new IndentingPrintWriter(pw, "  "));
+                }
+
+                if (mDebuggingManager != null) {
+                    mDebuggingManager.dump(dump, "debugging_manager",
+                            AdbServiceDumpProto.DEBUGGING_MANAGER);
+                }
+
+                dump.flush();
+            } else {
+                pw.println("Dump current ADB state");
+                pw.println("  No commands available");
+            }
+        } finally {
+            Binder.restoreCallingIdentity(ident);
+        }
+    }
+}
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index bbc4f44..b1b5a7a 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -243,6 +243,8 @@
             "com.android.server.timezonedetector.TimeZoneDetectorService$Lifecycle";
     private static final String ACCESSIBILITY_MANAGER_SERVICE_CLASS =
             "com.android.server.accessibility.AccessibilityManagerService$Lifecycle";
+    private static final String ADB_SERVICE_CLASS =
+            "com.android.server.adb.AdbService$Lifecycle";
 
     private static final String PERSISTENT_DATA_BLOCK_PROP = "ro.frp.pst";
 
@@ -1372,6 +1374,15 @@
                 traceEnd();
             }
 
+            // Start ADB Debugging Service
+            traceBeginAndSlog("StartAdbService");
+            try {
+                mSystemServiceManager.startService(ADB_SERVICE_CLASS);
+            } catch (Throwable e) {
+                Slog.e(TAG, "Failure starting AdbService");
+            }
+            traceEnd();
+
             if (!isWatch) {
                 traceBeginAndSlog("StartSerialService");
                 try {
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index 9918395..27b8cdf 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -34,7 +34,8 @@
 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;
 import android.hardware.usb.UsbConfiguration;
 import android.hardware.usb.UsbConstants;
@@ -178,7 +179,6 @@
     private final boolean mHasUsbAccessory;
     @GuardedBy("mLock")
     private String[] mAccessoryStrings;
-    private UsbDebuggingManager mDebuggingManager;
     private final UEventObserver mUEventObserver;
 
     private static Set<Integer> sBlackListedInterfaces;
@@ -199,19 +199,6 @@
         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
      */
@@ -284,24 +271,18 @@
         }
         mControlFds.put(UsbManager.FUNCTION_PTP, ptpFd);
 
-        boolean secureAdbEnabled = SystemProperties.getBoolean("ro.adb.secure", false);
-        boolean dataEncrypted = "1".equals(SystemProperties.get("vold.decrypt"));
-        if (secureAdbEnabled && !dataEncrypted) {
-            mDebuggingManager = new UsbDebuggingManager(context);
-        }
-
         if (halNotPresent) {
             /**
              * Initialze the legacy UsbHandler
              */
             mHandler = new UsbHandlerLegacy(FgThread.get().getLooper(), mContext, this,
-                    mDebuggingManager, alsaManager, settingsManager);
+                    alsaManager, settingsManager);
         } else {
             /**
              * Initialize HAL based UsbHandler
              */
             mHandler = new UsbHandlerHal(FgThread.get().getLooper(), mContext, this,
-                    mDebuggingManager, alsaManager, settingsManager);
+                    alsaManager, settingsManager);
         }
 
         if (nativeIsStartRequested()) {
@@ -364,11 +345,6 @@
         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() {
@@ -482,13 +458,11 @@
         private int mMidiDevice;
 
         private final Context mContext;
-        private final UsbDebuggingManager mDebuggingManager;
         private final UsbAlsaManager mUsbAlsaManager;
         private final UsbSettingsManager mSettingsManager;
         private NotificationManager mNotificationManager;
 
         protected long mScreenUnlockedFunctions;
-        protected boolean mAdbEnabled;
         protected boolean mBootCompleted;
         protected boolean mCurrentFunctionsApplied;
         protected boolean mUseUsbNotification;
@@ -506,11 +480,9 @@
         protected static final String USB_PERSISTENT_CONFIG_PROPERTY = "persist.sys.usb.config";
 
         UsbHandler(Looper looper, Context context, UsbDeviceManager deviceManager,
-                UsbDebuggingManager debuggingManager, UsbAlsaManager alsaManager,
-                UsbSettingsManager settingsManager) {
+                UsbAlsaManager alsaManager, UsbSettingsManager settingsManager) {
             super(looper);
             mContext = context;
-            mDebuggingManager = debuggingManager;
             mUsbDeviceManager = deviceManager;
             mUsbAlsaManager = alsaManager;
             mSettingsManager = settingsManager;
@@ -519,13 +491,6 @@
             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");
@@ -626,22 +591,15 @@
 
         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);
-                } else {
-                    setSystemProperty(USB_PERSISTENT_CONFIG_PROPERTY, "");
-                }
-
-                setEnabledFunctions(mCurrentFunctions, true);
-                updateAdbNotification(false);
+            if (enable) {
+                setSystemProperty(USB_PERSISTENT_CONFIG_PROPERTY, UsbManager.USB_FUNCTION_ADB);
+            } else {
+                setSystemProperty(USB_PERSISTENT_CONFIG_PROPERTY, "");
             }
 
-            if (mDebuggingManager != null) {
-                mDebuggingManager.setAdbEnabled(mAdbEnabled);
-            }
+            setEnabledFunctions(mCurrentFunctions, true);
+            updateAdbNotification(false);
         }
 
         protected boolean isUsbTransferAllowed() {
@@ -799,6 +757,19 @@
             setEnabledFunctions(mScreenUnlockedFunctions, false);
         }
 
+        private static class AdbTransport extends IAdbTransport.Stub {
+            private final UsbHandler mHandler;
+
+            AdbTransport(UsbHandler handler) {
+                mHandler = handler;
+            }
+
+            @Override
+            public void onAdbEnabled(boolean enabled) {
+                mHandler.sendMessage(MSG_ENABLE_ADB, enabled);
+            }
+        }
+
         /**
          * Returns the functions that are passed down to the low level driver once adb and
          * charging are accounted for.
@@ -807,7 +778,7 @@
             if (functions == UsbManager.FUNCTION_NONE) {
                 return getChargingFunctions();
             }
-            if (mAdbEnabled) {
+            if (isAdbEnabled()) {
                 return functions | UsbManager.FUNCTION_ADB;
             }
             return functions;
@@ -964,6 +935,9 @@
                     mNotificationManager = (NotificationManager)
                             mContext.getSystemService(Context.NOTIFICATION_SERVICE);
 
+                    LocalServices.getService(
+                            AdbManagerInternal.class).registerTransport(new AdbTransport(this));
+
                     // Ensure that the notification channels are set up
                     if (isTv()) {
                         // TV-specific notification channel
@@ -1029,19 +1003,6 @@
                 if (mCurrentAccessory != null) {
                     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.");
-                }
 
                 updateUsbNotification(false);
                 updateAdbNotification(false);
@@ -1191,12 +1152,16 @@
             }
         }
 
+        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) {
@@ -1250,7 +1215,7 @@
         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;
@@ -1319,7 +1284,6 @@
                     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,
@@ -1362,9 +1326,8 @@
         private boolean mUsbDataUnlocked;
 
         UsbHandlerLegacy(Looper looper, Context context, UsbDeviceManager deviceManager,
-                UsbDebuggingManager debuggingManager, UsbAlsaManager alsaManager,
-                UsbSettingsManager settingsManager) {
-            super(looper, context, deviceManager, debuggingManager, alsaManager, settingsManager);
+                UsbAlsaManager alsaManager, UsbSettingsManager settingsManager) {
+            super(looper, context, deviceManager, alsaManager, settingsManager);
             try {
                 readOemUsbOverrideConfig(context);
                 // Restore default functions.
@@ -1446,7 +1409,7 @@
                             + overrideFunctions.second);
                     if (!overrideFunctions.second.equals("")) {
                         String newFunction;
-                        if (mAdbEnabled) {
+                        if (isAdbEnabled()) {
                             newFunction = addFunction(overrideFunctions.second,
                                     UsbManager.USB_FUNCTION_ADB);
                         } else {
@@ -1457,7 +1420,7 @@
                         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);
@@ -1563,7 +1526,7 @@
             if (functions == null) {
                 functions = "";
             }
-            if (mAdbEnabled) {
+            if (isAdbEnabled()) {
                 functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
             } else {
                 functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB);
@@ -1752,9 +1715,8 @@
         protected boolean mCurrentUsbFunctionsRequested;
 
         UsbHandlerHal(Looper looper, Context context, UsbDeviceManager deviceManager,
-                UsbDebuggingManager debuggingManager, UsbAlsaManager alsaManager,
-                UsbSettingsManager settingsManager) {
-            super(looper, context, deviceManager, debuggingManager, alsaManager, settingsManager);
+                UsbAlsaManager alsaManager, UsbSettingsManager settingsManager) {
+            super(looper, context, deviceManager, alsaManager, settingsManager);
             try {
                 ServiceNotification serviceNotification = new ServiceNotification();
 
@@ -1850,7 +1812,7 @@
                      * 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:
@@ -2061,25 +2023,8 @@
         mHandler.sendMessage(MSG_SET_SCREEN_UNLOCKED_FUNCTIONS, functions);
     }
 
-    public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
-        if (mDebuggingManager != null) {
-            mDebuggingManager.allowUsbDebugging(alwaysAllow, publicKey);
-        }
-    }
-
-    public void denyUsbDebugging() {
-        if (mDebuggingManager != null) {
-            mDebuggingManager.denyUsbDebugging();
-        }
-    }
-
-    public void clearUsbDebuggingKeys() {
-        if (mDebuggingManager != null) {
-            mDebuggingManager.clearUsbDebuggingKeys();
-        } else {
-            throw new RuntimeException("Cannot clear Usb Debugging keys, "
-                    + "UsbDebuggingManager not enabled");
-        }
+    private void onAdbEnabled(boolean enabled) {
+        mHandler.sendMessage(MSG_ENABLE_ADB, enabled);
     }
 
     /**
@@ -2091,10 +2036,6 @@
         if (mHandler != null) {
             mHandler.dump(dump, "handler", UsbDeviceManagerProto.HANDLER);
         }
-        if (mDebuggingManager != null) {
-            mDebuggingManager.dump(dump, "debugging_manager",
-                    UsbDeviceManagerProto.DEBUGGING_MANAGER);
-        }
 
         dump.end(token);
     }
diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java
index e0f3685..f9abedf 100644
--- a/services/usb/java/com/android/server/usb/UsbService.java
+++ b/services/usb/java/com/android/server/usb/UsbService.java
@@ -489,24 +489,6 @@
     }
 
     @Override
-    public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
-        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
-        mDeviceManager.allowUsbDebugging(alwaysAllow, publicKey);
-    }
-
-    @Override
-    public void denyUsbDebugging() {
-        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
-        mDeviceManager.denyUsbDebugging();
-    }
-
-    @Override
-    public void clearUsbDebuggingKeys() {
-        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
-        mDeviceManager.clearUsbDebuggingKeys();
-    }
-
-    @Override
     public UsbPort[] getPorts() {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
 
diff --git a/tests/UsbTests/src/com/android/server/usb/UsbHandlerTest.java b/tests/UsbTests/src/com/android/server/usb/UsbHandlerTest.java
index 23311b0..4b93ca3 100644
--- a/tests/UsbTests/src/com/android/server/usb/UsbHandlerTest.java
+++ b/tests/UsbTests/src/com/android/server/usb/UsbHandlerTest.java
@@ -59,8 +59,6 @@
     @Mock
     private UsbDeviceManager mUsbDeviceManager;
     @Mock
-    private UsbDebuggingManager mUsbDebuggingManager;
-    @Mock
     private UsbAlsaManager mUsbAlsaManager;
     @Mock
     private UsbSettingsManager mUsbSettingsManager;
@@ -89,9 +87,8 @@
         Intent mBroadcastedIntent;
 
         MockUsbHandler(Looper looper, Context context, UsbDeviceManager deviceManager,
-                UsbDebuggingManager debuggingManager, UsbAlsaManager alsaManager,
-                UsbSettingsManager settingsManager) {
-            super(looper, context, deviceManager, debuggingManager, alsaManager, settingsManager);
+                UsbAlsaManager alsaManager, UsbSettingsManager settingsManager) {
+            super(looper, context, deviceManager, alsaManager, settingsManager);
             mUseUsbNotification = false;
             mIsUsbTransferAllowed = true;
             mCurrentUsbFunctionsReceived = true;
@@ -144,8 +141,8 @@
         when(mSharedPreferences.edit()).thenReturn(mEditor);
 
         mUsbHandler = new MockUsbHandler(FgThread.get().getLooper(),
-                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbDebuggingManager,
-                mUsbAlsaManager, mUsbSettingsManager);
+                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbAlsaManager,
+                mUsbSettingsManager);
     }
 
     @SmallTest
@@ -190,8 +187,7 @@
         assertEquals(mUsbHandler.getEnabledFunctions(), UsbManager.FUNCTION_NONE);
         assertEquals(mMockProperties.get(UsbDeviceManager.UsbHandler
                 .USB_PERSISTENT_CONFIG_PROPERTY), UsbManager.USB_FUNCTION_ADB);
-        verify(mUsbDebuggingManager).setAdbEnabled(true);
-        assertTrue(mUsbHandler.mAdbEnabled);
+        assertTrue(mUsbHandler.isAdbEnabled());
 
         mUsbHandler.handleMessage(mUsbHandler.obtainMessage(MSG_UPDATE_STATE, 1, 1));
 
@@ -208,16 +204,15 @@
         mMockProperties.put(UsbDeviceManager.UsbHandler.USB_PERSISTENT_CONFIG_PROPERTY,
                 UsbManager.USB_FUNCTION_ADB);
         mUsbHandler = new MockUsbHandler(FgThread.get().getLooper(),
-                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbDebuggingManager,
-                mUsbAlsaManager, mUsbSettingsManager);
+                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbAlsaManager,
+                mUsbSettingsManager);
 
         sendBootCompleteMessages(mUsbHandler);
         mUsbHandler.handleMessage(mUsbHandler.obtainMessage(MSG_ENABLE_ADB, 0));
         assertEquals(mUsbHandler.getEnabledFunctions(), UsbManager.FUNCTION_NONE);
-        assertFalse(mUsbHandler.mAdbEnabled);
+        assertFalse(mUsbHandler.isAdbEnabled());
         assertEquals(mMockProperties.get(UsbDeviceManager.UsbHandler
                 .USB_PERSISTENT_CONFIG_PROPERTY), "");
-        verify(mUsbDebuggingManager).setAdbEnabled(false);
     }
 
     @SmallTest
@@ -232,14 +227,13 @@
     public void bootCompletedAdbEnabled() {
         mMockProperties.put(UsbDeviceManager.UsbHandler.USB_PERSISTENT_CONFIG_PROPERTY, "adb");
         mUsbHandler = new MockUsbHandler(FgThread.get().getLooper(),
-                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbDebuggingManager,
-                mUsbAlsaManager, mUsbSettingsManager);
+                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbAlsaManager,
+                mUsbSettingsManager);
 
         sendBootCompleteMessages(mUsbHandler);
         assertEquals(mUsbHandler.getEnabledFunctions(), UsbManager.FUNCTION_NONE);
         assertEquals(mMockGlobalSettings.get(Settings.Global.ADB_ENABLED).intValue(), 1);
-        assertTrue(mUsbHandler.mAdbEnabled);
-        verify(mUsbDebuggingManager).setAdbEnabled(true);
+        assertTrue(mUsbHandler.isAdbEnabled());
     }
 
     @SmallTest
@@ -321,8 +315,8 @@
                 UsbDeviceManager.UNLOCKED_CONFIG_PREF, mUsbHandler.mCurrentUser), ""))
                 .thenReturn(UsbManager.USB_FUNCTION_MTP);
         mUsbHandler = new MockUsbHandler(FgThread.get().getLooper(),
-                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbDebuggingManager,
-                mUsbAlsaManager, mUsbSettingsManager);
+                InstrumentationRegistry.getContext(), mUsbDeviceManager, mUsbAlsaManager,
+                mUsbSettingsManager);
         sendBootCompleteMessages(mUsbHandler);
         mUsbHandler.handleMessage(mUsbHandler.obtainMessage(MSG_UPDATE_SCREEN_LOCK, 1));
         mUsbHandler.handleMessage(mUsbHandler.obtainMessage(MSG_UPDATE_SCREEN_LOCK, 0));
@@ -335,4 +329,4 @@
         handler.handleMessage(handler.obtainMessage(MSG_BOOT_COMPLETED));
         handler.handleMessage(handler.obtainMessage(MSG_SYSTEM_READY));
     }
-}
\ No newline at end of file
+}