Merge "UsbDebuggingManager: Add interface to clear secure adb keys"
diff --git a/core/java/android/hardware/usb/IUsbManager.aidl b/core/java/android/hardware/usb/IUsbManager.aidl
index 8286686..9bc967f 100644
--- a/core/java/android/hardware/usb/IUsbManager.aidl
+++ b/core/java/android/hardware/usb/IUsbManager.aidl
@@ -95,4 +95,7 @@
 
     /* Deny USB debugging from the attached host */
     void denyUsbDebugging();
+
+    /* Clear public keys installed for secure USB debugging */
+    void clearUsbDebuggingKeys();
 }
diff --git a/services/java/com/android/server/usb/UsbDebuggingManager.java b/services/java/com/android/server/usb/UsbDebuggingManager.java
index 1bb3a2c..93d3114 100644
--- a/services/java/com/android/server/usb/UsbDebuggingManager.java
+++ b/services/java/com/android/server/usb/UsbDebuggingManager.java
@@ -151,6 +151,7 @@
         private static final int MESSAGE_ADB_ALLOW = 3;
         private static final int MESSAGE_ADB_DENY = 4;
         private static final int MESSAGE_ADB_CONFIRM = 5;
+        private static final int MESSAGE_ADB_CLEAR = 6;
 
         public UsbDebuggingHandler(Looper looper) {
             super(looper);
@@ -214,6 +215,10 @@
                     showConfirmationDialog(key, mFingerprints);
                     break;
                 }
+
+                case MESSAGE_ADB_CLEAR:
+                    deleteKeyFile();
+                    break;
             }
         }
     }
@@ -257,17 +262,25 @@
         }
     }
 
-    private void writeKey(String key) {
+    private File getUserKeyFile() {
         File dataDir = Environment.getDataDirectory();
         File adbDir = new File(dataDir, ADB_DIRECTORY);
 
         if (!adbDir.exists()) {
             Slog.e(TAG, "ADB data directory does not exist");
-            return;
+            return null;
         }
 
+        return new File(adbDir, ADB_KEYS_FILE);
+    }
+
+    private void writeKey(String key) {
         try {
-            File keyFile = new File(adbDir, ADB_KEYS_FILE);
+            File keyFile = getUserKeyFile();
+
+            if (keyFile == null) {
+                return;
+            }
 
             if (!keyFile.exists()) {
                 keyFile.createNewFile();
@@ -286,6 +299,12 @@
         }
     }
 
+    private void deleteKeyFile() {
+        File keyFile = getUserKeyFile();
+        if (keyFile != null) {
+            keyFile.delete();
+        }
+    }
 
     public void setAdbEnabled(boolean enabled) {
         mHandler.sendEmptyMessage(enabled ? UsbDebuggingHandler.MESSAGE_ADB_ENABLED
@@ -303,6 +322,9 @@
         mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_DENY);
     }
 
+    public void clearUsbDebuggingKeys() {
+        mHandler.sendEmptyMessage(UsbDebuggingHandler.MESSAGE_ADB_CLEAR);
+    }
 
     public void dump(FileDescriptor fd, PrintWriter pw) {
         pw.println("  USB Debugging State:");
diff --git a/services/java/com/android/server/usb/UsbDeviceManager.java b/services/java/com/android/server/usb/UsbDeviceManager.java
index 8fa6de5..87aa8cce 100644
--- a/services/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/java/com/android/server/usb/UsbDeviceManager.java
@@ -864,6 +864,15 @@
         }
     }
 
+    public void clearUsbDebuggingKeys() {
+        if (mDebuggingManager != null) {
+            mDebuggingManager.clearUsbDebuggingKeys();
+        } else {
+            throw new RuntimeException("Cannot clear Usb Debugging keys, "
+                        + "UsbDebuggingManager not enabled");
+        }
+    }
+
     public void dump(FileDescriptor fd, PrintWriter pw) {
         if (mHandler != null) {
             mHandler.dump(fd, pw);
diff --git a/services/java/com/android/server/usb/UsbService.java b/services/java/com/android/server/usb/UsbService.java
index 3918d15..36669b1 100644
--- a/services/java/com/android/server/usb/UsbService.java
+++ b/services/java/com/android/server/usb/UsbService.java
@@ -255,6 +255,12 @@
     }
 
     @Override
+    public void clearUsbDebuggingKeys() {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
+        mDeviceManager.clearUsbDebuggingKeys();
+    }
+
+    @Override
     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
         final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");