Get alias for Bluetooth devices.

Bluetooth devices can be renamed by the user.  Make the
input system aware of the user-specified name and transparently
pass it down to applications.  This enables the keyboard
layout picker Settings UI to use device names that are
consistent with what the user set in the Bluetooth UI.

Bug: 6363157
Change-Id: I8eea26ce2c69c2a3f09c8de02e9e847610e0419c
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 02c4d5a..729c3f3 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -726,6 +726,7 @@
         final StatusBarManagerService statusBarF = statusBar;
         final DreamManagerService dreamyF = dreamy;
         final InputManagerService inputManagerF = inputManager;
+        final BluetoothService bluetoothF = bluetooth;
 
         // We now tell the activity manager it is okay to run third party
         // code.  It will call back into us once it has gotten to the state
@@ -838,7 +839,7 @@
                     reportWtf("making DreamManagerService ready", e);
                 }
                 try {
-                    if (inputManagerF != null) inputManagerF.systemReady();
+                    if (inputManagerF != null) inputManagerF.systemReady(bluetoothF);
                 } catch (Throwable e) {
                     reportWtf("making InputManagerService ready", e);
                 }
diff --git a/services/java/com/android/server/input/InputManagerService.java b/services/java/com/android/server/input/InputManagerService.java
index a4ed31c..189a9c7 100644
--- a/services/java/com/android/server/input/InputManagerService.java
+++ b/services/java/com/android/server/input/InputManagerService.java
@@ -26,6 +26,8 @@
 import org.xmlpull.v1.XmlSerializer;
 
 import android.Manifest;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -56,6 +58,7 @@
 import android.os.RemoteException;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
+import android.server.BluetoothService;
 import android.util.Log;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -106,6 +109,7 @@
     private final Callbacks mCallbacks;
     private final InputManagerHandler mHandler;
     private boolean mSystemReady;
+    private BluetoothService mBluetoothService;
 
     // Persistent data store.  Must be locked each time during use.
     private final PersistentDataStore mDataStore = new PersistentDataStore();
@@ -167,6 +171,7 @@
             int repeat, int token);
     private static native void nativeCancelVibrate(int ptr, int deviceId, int token);
     private static native void nativeReloadKeyboardLayouts(int ptr);
+    private static native void nativeReloadDeviceAliases(int ptr);
     private static native String nativeDump(int ptr);
     private static native void nativeMonitor(int ptr);
 
@@ -217,12 +222,12 @@
         updateShowTouchesFromSettings();
     }
 
-    public void systemReady() {
+    public void systemReady(BluetoothService bluetoothService) {
         if (DEBUG) {
             Slog.d(TAG, "System ready.");
         }
+        mBluetoothService = bluetoothService;
         mSystemReady = true;
-        reloadKeyboardLayouts();
 
         IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
         filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
@@ -237,12 +242,30 @@
                 reloadKeyboardLayouts();
             }
         }, filter, null, mHandler);
+
+        filter = new IntentFilter(BluetoothDevice.ACTION_ALIAS_CHANGED);
+        mContext.registerReceiver(new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                if (DEBUG) {
+                    Slog.d(TAG, "Bluetooth alias changed, reloading device names.");
+                }
+                reloadDeviceAliases();
+            }
+        }, filter, null, mHandler);
+
+        reloadKeyboardLayouts();
+        reloadDeviceAliases();
     }
 
     private void reloadKeyboardLayouts() {
         nativeReloadKeyboardLayouts(mPtr);
     }
 
+    private void reloadDeviceAliases() {
+        nativeReloadDeviceAliases(mPtr);
+    }
+
     public void setDisplaySize(int displayId, int width, int height,
             int externalWidth, int externalHeight) {
         if (width <= 0 || height <= 0 || externalWidth <= 0 || externalHeight <= 0) {
@@ -1121,6 +1144,15 @@
         return result;
     }
 
+    // Native callback.
+    private String getDeviceAlias(String uniqueId) {
+        if (mBluetoothService != null &&
+                BluetoothAdapter.checkBluetoothAddress(uniqueId)) {
+            return mBluetoothService.getRemoteAlias(uniqueId);
+        }
+        return null;
+    }
+
 
     /**
      * Callback interface implemented by the Window Manager.