Use the live AdapterService in ProfileService.

Previously, if AdapterService was null while ProfileService is
created, the ProfileService would never work because it would
store a stale reference to the AdapterService.

This change ensures that ProfileService accesses the current
AdapterService at any given time.

Bug: 29519304

Change-Id: I25efbf4a58932f519ac9f0b89c4112a442c994eb
diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java
index 6aaea6e..30c35f9 100644
--- a/src/com/android/bluetooth/btservice/AdapterService.java
+++ b/src/com/android/bluetooth/btservice/AdapterService.java
@@ -330,7 +330,9 @@
 
     public void addProfile(ProfileService profile) {
         synchronized (mProfiles) {
-            mProfiles.add(profile);
+            if (!mProfiles.contains(profile)) {
+                mProfiles.add(profile);
+            }
         }
     }
 
diff --git a/src/com/android/bluetooth/btservice/ProfileService.java b/src/com/android/bluetooth/btservice/ProfileService.java
index 0202947..2621fc5 100644
--- a/src/com/android/bluetooth/btservice/ProfileService.java
+++ b/src/com/android/bluetooth/btservice/ProfileService.java
@@ -55,8 +55,6 @@
     protected boolean mStartError=false;
     private boolean mCleaningUp = false;
 
-    private AdapterService mAdapterService;
-
     protected String getName() {
         return getClass().getSimpleName();
     }
@@ -109,16 +107,17 @@
         super.onCreate();
         mAdapter = BluetoothAdapter.getDefaultAdapter();
         mBinder = initBinder();
-        mAdapterService = AdapterService.getAdapterService();
-        if (mAdapterService != null) {
-            mAdapterService.addProfile(this);
-        } else {
-            Log.w(TAG, "onCreate, null mAdapterService");
-        }
     }
 
     public int onStartCommand(Intent intent, int flags, int startId) {
         if (DBG) log("onStartCommand()");
+        AdapterService adapterService = AdapterService.getAdapterService();
+        if (adapterService != null) {
+            adapterService.addProfile(this);
+        } else {
+            Log.w(TAG, "Could not add this profile because AdapterService is null.");
+        }
+
         if (mStartError || mAdapter == null) {
             Log.w(mName, "Stopping profile service: device does not have BT");
             doStop(intent);
@@ -178,7 +177,8 @@
     @Override
     public void onDestroy() {
         if (DBG) log("Destroying service.");
-        if (mAdapterService != null) mAdapterService.removeProfile(this);
+        AdapterService adapterService = AdapterService.getAdapterService();
+        if (adapterService != null) adapterService.removeProfile(this);
 
         if (mCleaningUp) {
             if (DBG) log("Cleanup already started... Skipping cleanup()...");
@@ -222,15 +222,17 @@
 
     protected void notifyProfileServiceStateChanged(int state) {
         //Notify adapter service
-        if (mAdapterService != null) {
-            mAdapterService.onProfileServiceStateChanged(getClass().getName(), state);
+        AdapterService adapterService = AdapterService.getAdapterService();
+        if (adapterService != null) {
+            adapterService.onProfileServiceStateChanged(getClass().getName(), state);
         }
     }
 
     public void notifyProfileConnectionStateChanged(BluetoothDevice device,
             int profileId, int newState, int prevState) {
-        if (mAdapterService != null) {
-            mAdapterService.onProfileConnectionStateChanged(device, profileId, newState, prevState);
+        AdapterService adapterService = AdapterService.getAdapterService();
+        if (adapterService != null) {
+            adapterService.onProfileConnectionStateChanged(device, profileId, newState, prevState);
         }
     }