Make BluetoothPan inherit from BluetoothProfile.

Change-Id: Ibd3e24e391be93ebe2cf975bd075efb68e10c1ff
diff --git a/core/java/android/server/BluetoothEventLoop.java b/core/java/android/server/BluetoothEventLoop.java
index c0b3cc8..578580a 100644
--- a/core/java/android/server/BluetoothEventLoop.java
+++ b/core/java/android/server/BluetoothEventLoop.java
@@ -785,7 +785,7 @@
 
             boolean connected = false;
             BluetoothDevice device = mAdapter.getRemoteDevice(address);
-            int state = mBluetoothService.getPanDeviceState(device);
+            int state = mBluetoothService.getPanDeviceConnectionState(device);
             if (state == BluetoothPan.STATE_CONNECTING) {
                 if (result == BluetoothPan.PAN_CONNECT_FAILED_ALREADY_CONNECTED) {
                     connected = true;
diff --git a/core/java/android/server/BluetoothPanProfileHandler.java b/core/java/android/server/BluetoothPanProfileHandler.java
index fb96439..3f24811 100644
--- a/core/java/android/server/BluetoothPanProfileHandler.java
+++ b/core/java/android/server/BluetoothPanProfileHandler.java
@@ -58,6 +58,9 @@
     private Context mContext;
     private BluetoothService mBluetoothService;
 
+    static final String NAP_ROLE = "nap";
+    static final String NAP_BRIDGE = "pan1";
+
     private BluetoothPanProfileHandler(Context context, BluetoothService service) {
         mContext = context;
         mPanDevices = new HashMap<BluetoothDevice, BluetoothPanDevice>();
@@ -114,7 +117,7 @@
         }
     }
 
-    synchronized int getPanDeviceState(BluetoothDevice device) {
+    synchronized int getPanDeviceConnectionState(BluetoothDevice device) {
         BluetoothPanDevice panDevice = mPanDevices.get(device);
         if (panDevice == null) {
             return BluetoothPan.STATE_DISCONNECTED;
@@ -125,13 +128,13 @@
     synchronized boolean connectPanDevice(BluetoothDevice device) {
         String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
         if (DBG) Log.d(TAG, "connect PAN(" + objectPath + ")");
-        if (getPanDeviceState(device) != BluetoothPan.STATE_DISCONNECTED) {
+        if (getPanDeviceConnectionState(device) != BluetoothPan.STATE_DISCONNECTED) {
             errorLog(device + " already connected to PAN");
         }
 
         int connectedCount = 0;
         for (BluetoothDevice panDevice: mPanDevices.keySet()) {
-            if (getPanDeviceState(panDevice) == BluetoothPan.STATE_CONNECTED) {
+            if (getPanDeviceConnectionState(panDevice) == BluetoothPan.STATE_CONNECTED) {
                 connectedCount ++;
             }
         }
@@ -187,18 +190,33 @@
         List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
 
         for (BluetoothDevice device: mPanDevices.keySet()) {
-            if (getPanDeviceState(device) == BluetoothPan.STATE_CONNECTED) {
+            if (getPanDeviceConnectionState(device) == BluetoothPan.STATE_CONNECTED) {
                 devices.add(device);
             }
         }
         return devices;
     }
 
+    synchronized List<BluetoothDevice> getPanDevicesMatchingConnectionStates(int[] states) {
+        List<BluetoothDevice> devices = new ArrayList<BluetoothDevice>();
+
+        for (BluetoothDevice device: mPanDevices.keySet()) {
+            int panDeviceState = getPanDeviceConnectionState(device);
+            for (int state : states) {
+                if (state == panDeviceState) {
+                    devices.add(device);
+                    break;
+                }
+            }
+        }
+        return devices;
+    }
+
     synchronized boolean disconnectPanDevice(BluetoothDevice device) {
         String objectPath = mBluetoothService.getObjectPathFromAddress(device.getAddress());
         debugLog("disconnect PAN(" + objectPath + ")");
 
-        int state = getPanDeviceState(device);
+        int state = getPanDeviceConnectionState(device);
         if (state != BluetoothPan.STATE_CONNECTED) {
             debugLog(device + " already disconnected from PAN");
             return false;
@@ -274,14 +292,10 @@
             panDevice.mLocalRole = role;
         }
 
-        if (state == BluetoothPan.STATE_DISCONNECTED) {
-            mPanDevices.remove(device);
-        }
-
-        Intent intent = new Intent(BluetoothPan.ACTION_PAN_STATE_CHANGED);
+        Intent intent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
-        intent.putExtra(BluetoothPan.EXTRA_PREVIOUS_PAN_STATE, prevState);
-        intent.putExtra(BluetoothPan.EXTRA_PAN_STATE, state);
+        intent.putExtra(BluetoothPan.EXTRA_PREVIOUS_STATE, prevState);
+        intent.putExtra(BluetoothPan.EXTRA_STATE, state);
         intent.putExtra(BluetoothPan.EXTRA_LOCAL_ROLE, role);
         mContext.sendBroadcast(intent, BluetoothService.BLUETOOTH_PERM);
 
diff --git a/core/java/android/server/BluetoothService.java b/core/java/android/server/BluetoothService.java
index 70aaf0a..ebe3ef2 100644
--- a/core/java/android/server/BluetoothService.java
+++ b/core/java/android/server/BluetoothService.java
@@ -85,6 +85,7 @@
     private BluetoothEventLoop mEventLoop;
     private BluetoothHeadset mBluetoothHeadset;
     private BluetoothInputDevice mInputDevice;
+    private BluetoothPan mPan;
     private boolean mIsAirplaneSensitive;
     private boolean mIsAirplaneToggleable;
     private int mBluetoothState;
@@ -357,7 +358,8 @@
         setBluetoothState(BluetoothAdapter.STATE_TURNING_OFF);
 
         if (mAdapterSdpHandles != null) removeReservedServiceRecordsNative(mAdapterSdpHandles);
-        setBluetoothTetheringNative(false, BluetoothPan.NAP_ROLE, BluetoothPan.NAP_BRIDGE);
+        setBluetoothTetheringNative(false, BluetoothPanProfileHandler.NAP_ROLE,
+                BluetoothPanProfileHandler.NAP_BRIDGE);
 
         // Allow 3 seconds for profiles to gracefully disconnect
         // TODO: Introduce a callback mechanism so that each profile can notify
@@ -606,7 +608,8 @@
         addReservedSdpRecords(uuids);
 
         // Enable profiles maintained by Bluez userspace.
-        setBluetoothTetheringNative(true, BluetoothPan.NAP_ROLE, BluetoothPan.NAP_BRIDGE);
+        setBluetoothTetheringNative(true, BluetoothPanProfileHandler.NAP_ROLE,
+                BluetoothPanProfileHandler.NAP_BRIDGE);
 
         // Add SDP records for profiles maintained by Bluez userspace
         uuids.add(BluetoothUuid.AudioSource);
@@ -2082,6 +2085,8 @@
                                  mBluetoothProfileServiceListener, BluetoothProfile.HEADSET);
         mAdapter.getProfileProxy(mContext,
                 mBluetoothProfileServiceListener, BluetoothProfile.INPUT_DEVICE);
+        mAdapter.getProfileProxy(mContext,
+                mBluetoothProfileServiceListener, BluetoothProfile.PAN);
 
         pw.println("\n--Known devices--");
         for (String address : mDeviceProperties.keySet()) {
@@ -2125,6 +2130,7 @@
 
         dumpHeadsetProfile(pw);
         dumpInputDeviceProfile(pw);
+        dumpPanProfile(pw);
 
         pw.println("\n--Application Service Records--");
         for (Integer handle : mServiceRecordToPid.keySet()) {
@@ -2138,10 +2144,10 @@
         if (mBluetoothHeadset != null) {
             List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();
             if (deviceList.size() == 0) {
-                pw.println("\n--No headsets connected--");
+                pw.println("No headsets connected");
             } else {
                 BluetoothDevice device = deviceList.get(0);
-                pw.println("\ngetConnectedDevices[0] = " + device);
+                pw.println("getConnectedDevices[0] = " + device);
 
                 switch (mBluetoothHeadset.getConnectionState(device)) {
                     case BluetoothHeadset.STATE_CONNECTING:
@@ -2164,7 +2170,7 @@
             deviceList.clear();
             deviceList = mBluetoothHeadset.getDevicesMatchingConnectionStates(new int[] {
                      BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED});
-            pw.println("\n--Connected and Disconnected Headsets");
+            pw.println("--Connected and Disconnected Headsets");
             for (BluetoothDevice device: deviceList) {
                 pw.println(device);
                 if (mBluetoothHeadset.isAudioConnected(device)) {
@@ -2180,9 +2186,9 @@
         if (mInputDevice != null) {
             List<BluetoothDevice> deviceList = mInputDevice.getConnectedDevices();
             if (deviceList.size() == 0) {
-                pw.println("\nNo input devices connected--");
+                pw.println("No input devices connected");
             } else {
-                pw.println("\nNumber of connected devices:" + deviceList.size());
+                pw.println("Number of connected devices:" + deviceList.size());
                 BluetoothDevice device = deviceList.get(0);
                 pw.println("getConnectedDevices[0] = " + device);
                 pw.println("Priority of Connected device = " + mInputDevice.getPriority(device));
@@ -2210,6 +2216,41 @@
         mAdapter.closeProfileProxy(BluetoothProfile.INPUT_DEVICE, mBluetoothHeadset);
     }
 
+    private void dumpPanProfile(PrintWriter pw) {
+        pw.println("\n--Bluetooth Service- Pan Profile");
+        if (mPan != null) {
+            List<BluetoothDevice> deviceList = mPan.getConnectedDevices();
+            if (deviceList.size() == 0) {
+                pw.println("No Pan devices connected");
+            } else {
+                pw.println("Number of connected devices:" + deviceList.size());
+                BluetoothDevice device = deviceList.get(0);
+                pw.println("getConnectedDevices[0] = " + device);
+                pw.println("Priority of Connected device = " + mPan.getPriority(device));
+
+                switch (mPan.getConnectionState(device)) {
+                    case BluetoothInputDevice.STATE_CONNECTING:
+                        pw.println("getConnectionState() = STATE_CONNECTING");
+                        break;
+                    case BluetoothInputDevice.STATE_CONNECTED:
+                        pw.println("getConnectionState() = STATE_CONNECTED");
+                        break;
+                    case BluetoothInputDevice.STATE_DISCONNECTING:
+                        pw.println("getConnectionState() = STATE_DISCONNECTING");
+                        break;
+                }
+            }
+            deviceList.clear();
+            deviceList = mPan.getDevicesMatchingConnectionStates(new int[] {
+                     BluetoothProfile.STATE_CONNECTED, BluetoothProfile.STATE_DISCONNECTED});
+            pw.println("--Connected and Disconnected Pan devices");
+            for (BluetoothDevice device: deviceList) {
+                pw.println(device);
+            }
+        }
+        mAdapter.closeProfileProxy(BluetoothProfile.PAN, mBluetoothHeadset);
+    }
+
     private BluetoothProfile.ServiceListener mBluetoothProfileServiceListener =
         new BluetoothProfile.ServiceListener() {
         public void onServiceConnected(int profile, BluetoothProfile proxy) {
@@ -2217,6 +2258,8 @@
                 mBluetoothHeadset = (BluetoothHeadset) proxy;
             } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                 mInputDevice = (BluetoothInputDevice) proxy;
+            } else if (profile == BluetoothProfile.PAN) {
+                mPan = (BluetoothPan) proxy;
             }
         }
         public void onServiceDisconnected(int profile) {
@@ -2224,6 +2267,8 @@
                 mBluetoothHeadset = null;
             } else if (profile == BluetoothProfile.INPUT_DEVICE) {
                 mInputDevice = null;
+            } else if (profile == BluetoothProfile.PAN) {
+                mPan = null;
             }
         }
     };
@@ -2302,9 +2347,9 @@
         mBluetoothPanProfileHandler.setBluetoothTethering(value);
     }
 
-    public synchronized int getPanDeviceState(BluetoothDevice device) {
+    public synchronized int getPanDeviceConnectionState(BluetoothDevice device) {
         mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
-        return mBluetoothPanProfileHandler.getPanDeviceState(device);
+        return mBluetoothPanProfileHandler.getPanDeviceConnectionState(device);
     }
 
     public synchronized boolean connectPanDevice(BluetoothDevice device) {
@@ -2318,6 +2363,12 @@
         return mBluetoothPanProfileHandler.getConnectedPanDevices();
     }
 
+    public synchronized List<BluetoothDevice> getPanDevicesMatchingConnectionStates(
+            int[] states) {
+        mContext.enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
+        return mBluetoothPanProfileHandler.getPanDevicesMatchingConnectionStates(states);
+    }
+
     public synchronized boolean disconnectPanDevice(BluetoothDevice device) {
         mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM,
             "Need BLUETOOTH_ADMIN permission");