Make BluetoothPan implement BluetoothProfile.

This makes it consistent across all other profiles.

Change-Id: I4c0694d042d7ed1b78c92ca3be38c8d543886769
diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java
index 4ccd598..2ff3254 100644
--- a/src/com/android/settings/TetherSettings.java
+++ b/src/com/android/settings/TetherSettings.java
@@ -23,6 +23,7 @@
 import android.app.Dialog;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothPan;
+import android.bluetooth.BluetoothProfile;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -88,7 +89,12 @@
         addPreferencesFromResource(R.xml.tether_prefs);
 
         final Activity activity = getActivity();
-        mBluetoothPan = new BluetoothPan(activity);
+        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+        if (adapter != null) {
+            adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
+                    BluetoothProfile.PAN);
+        }
+
 
         mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
         mWifiApSettings = (PreferenceScreen) findPreference(WIFI_AP_SETTINGS);
@@ -119,7 +125,7 @@
         if (!bluetoothAvailable) {
             getPreferenceScreen().removePreference(mBluetoothTether);
         } else {
-            if (mBluetoothPan.isTetheringOn()) {
+            if (mBluetoothPan != null && mBluetoothPan.isTetheringOn()) {
                 mBluetoothTether.setChecked(true);
             } else {
                 mBluetoothTether.setChecked(false);
@@ -130,6 +136,16 @@
         mView = new WebView(activity);
     }
 
+    private BluetoothProfile.ServiceListener mProfileServiceListener =
+        new BluetoothProfile.ServiceListener() {
+        public void onServiceConnected(int profile, BluetoothProfile proxy) {
+            mBluetoothPan = (BluetoothPan) proxy;
+        }
+        public void onServiceDisconnected(int profile) {
+            mBluetoothPan = null;
+        }
+    };
+
     @Override
     public Dialog onCreateDialog(int id) {
         if (id == DIALOG_TETHER_HELP) {
diff --git a/src/com/android/settings/bluetooth/BluetoothEventRedirector.java b/src/com/android/settings/bluetooth/BluetoothEventRedirector.java
index fa0c2c3..3174750 100644
--- a/src/com/android/settings/bluetooth/BluetoothEventRedirector.java
+++ b/src/com/android/settings/bluetooth/BluetoothEventRedirector.java
@@ -139,14 +139,14 @@
                 mManager.getCachedDeviceManager().onProfileStateChanged(device,
                         Profile.HID, newState);
 
-            } else if (action.equals(BluetoothPan.ACTION_PAN_STATE_CHANGED)) {
+            } else if (action.equals(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED)) {
                 final int role = intent.getIntExtra(
                         BluetoothPan.EXTRA_LOCAL_ROLE, 0);
                 if (role == BluetoothPan.LOCAL_PANU_ROLE) {
                     final int newState = intent.getIntExtra(
-                            BluetoothPan.EXTRA_PAN_STATE, 0);
+                            BluetoothPan.EXTRA_STATE, 0);
                     final int oldState = intent.getIntExtra(
-                            BluetoothPan.EXTRA_PREVIOUS_PAN_STATE, 0);
+                            BluetoothPan.EXTRA_PREVIOUS_STATE, 0);
                     if (newState == BluetoothPan.STATE_DISCONNECTED &&
                             oldState == BluetoothPan.STATE_CONNECTING) {
                         Log.i(TAG, "Failed to connect BT PAN");
@@ -199,7 +199,7 @@
         filter.addAction(BluetoothDevice.ACTION_PAIRING_CANCEL);
 
         // Fine-grained state broadcasts
-        filter.addAction(BluetoothPan.ACTION_PAN_STATE_CHANGED);
+        filter.addAction(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         filter.addAction(BluetoothInputDevice.ACTION_CONNECTION_STATE_CHANGED);
         filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
         filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 889cb8a..164d2da 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -736,12 +736,22 @@
         }
     }
 
-    private static class PanProfileManager extends LocalBluetoothProfileManager {
-        private final BluetoothPan mService;
+    private static class PanProfileManager extends LocalBluetoothProfileManager
+            implements BluetoothProfile.ServiceListener {
+        private BluetoothPan mService;
 
         public PanProfileManager(LocalBluetoothManager localManager) {
             super(localManager);
-            mService = new BluetoothPan(localManager.getContext());
+            BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+            adapter.getProfileProxy(localManager.getContext(), this, BluetoothProfile.PAN);
+        }
+
+        public void onServiceConnected(int profile, BluetoothProfile proxy) {
+            mService = (BluetoothPan) proxy;
+        }
+
+        public void onServiceDisconnected(int profile) {
+            mService = null;
         }
 
         @Override
@@ -799,7 +809,7 @@
 
         @Override
         public int getConnectionStatus(BluetoothDevice device) {
-            return convertState(mService.getPanDeviceState(device));
+            return convertState(mService.getConnectionState(device));
         }
 
         @Override