diff options
author | Ashwini Munigala <AshwiniM@codeaurora.org> | 2016-02-01 19:59:49 +0530 |
---|---|---|
committer | Hemant Gupta <hemantg@codeaurora.org> | 2016-03-01 19:13:47 +0530 |
commit | 5b6543df8f17cdbe96c09b260925c368a928a844 (patch) | |
tree | c74259ffe5099b7a82a48e0bd0cbb7f51bb49e69 | |
parent | ba1e36fe97428e317f4e710b99f3bee429198e96 (diff) | |
download | android_packages_apps_Bluetooth-5b6543df8f17cdbe96c09b260925c368a928a844.tar.gz android_packages_apps_Bluetooth-5b6543df8f17cdbe96c09b260925c368a928a844.tar.bz2 android_packages_apps_Bluetooth-5b6543df8f17cdbe96c09b260925c368a928a844.zip |
MAP: Handle following in MAPS:
- Expose isMapStarted() API to maintain the Profile
internal state and perform start/stop based on later.
- Track registration and unregistration for BluetoothMapAppObserver
BroadcastReceiver through a instance flag to avoid
possible IntentReceiver leaks.
- Avoid MasInstance shutdown block to handle possible ANR.
- Other conditional checks to avoid possible NPE.
CRs-Fixed: 969230
Change-Id: I473f8ad8998a1bf7f2da3bcab2380ed0954ed8bc
4 files changed, 57 insertions, 17 deletions
diff --git a/src/com/android/bluetooth/map/BluetoothMapAppObserver.java b/src/com/android/bluetooth/map/BluetoothMapAppObserver.java index ee0075669..7721b7e9e 100644 --- a/src/com/android/bluetooth/map/BluetoothMapAppObserver.java +++ b/src/com/android/bluetooth/map/BluetoothMapAppObserver.java @@ -55,6 +55,7 @@ public class BluetoothMapAppObserver{ private PackageManager mPackageManager = null; BluetoothMapAccountEmailLoader mLoader; BluetoothMapService mMapService = null; + private boolean mRegisteredReceiver = false; public BluetoothMapAppObserver(final Context context, BluetoothMapService mapService) { mContext = context; @@ -283,12 +284,26 @@ public class BluetoothMapAppObserver{ } } }; - mContext.registerReceiver(mReceiver,intentFilter); + if (!mRegisteredReceiver) { + try { + mContext.registerReceiver(mReceiver,intentFilter); + mRegisteredReceiver = true; + } catch (Exception e) { + Log.e(TAG,"Unable to register MapAppObserver receiver",e); + } + } } private void removeReceiver(){ if(D)Log.d(TAG,"removeReceiver()\n"); - mContext.unregisterReceiver(mReceiver); + if (mRegisteredReceiver) { + try { + mRegisteredReceiver = false; + mContext.unregisterReceiver(mReceiver); + } catch (Exception e) { + Log.e(TAG,"Unable to unregister mapAppObserver receiver",e); + } + } } /** diff --git a/src/com/android/bluetooth/map/BluetoothMapContentObserver.java b/src/com/android/bluetooth/map/BluetoothMapContentObserver.java index f39264eb2..2a333aec9 100644 --- a/src/com/android/bluetooth/map/BluetoothMapContentObserver.java +++ b/src/com/android/bluetooth/map/BluetoothMapContentObserver.java @@ -3400,7 +3400,9 @@ public class BluetoothMapContentObserver { }; public void init() { - mSmsBroadcastReceiver.register(); + if (mSmsBroadcastReceiver != null ) { + mSmsBroadcastReceiver.register(); + } registerPhoneServiceStateListener(); mInitialized = true; } @@ -3408,7 +3410,9 @@ public class BluetoothMapContentObserver { public void deinit() { mInitialized = false; unregisterObserver(); - mSmsBroadcastReceiver.unregister(); + if (mSmsBroadcastReceiver != null ) { + mSmsBroadcastReceiver.unregister(); + } unRegisterPhoneServiceStateListener(); failPendingMessages(); removeDeletedMessages(); diff --git a/src/com/android/bluetooth/map/BluetoothMapMasInstance.java b/src/com/android/bluetooth/map/BluetoothMapMasInstance.java index 4cc371883..fc253c7a4 100644 --- a/src/com/android/bluetooth/map/BluetoothMapMasInstance.java +++ b/src/com/android/bluetooth/map/BluetoothMapMasInstance.java @@ -421,7 +421,8 @@ public class BluetoothMapMasInstance implements IObexConnectionHandler { closeConnectionSocket(); - closeServerSockets(true); + if(V) Log.v(TAG, "Block acceptThreads: FALSE"); + closeServerSockets(false); } /** diff --git a/src/com/android/bluetooth/map/BluetoothMapService.java b/src/com/android/bluetooth/map/BluetoothMapService.java index 743c1b924..5dfa7f483 100755 --- a/src/com/android/bluetooth/map/BluetoothMapService.java +++ b/src/com/android/bluetooth/map/BluetoothMapService.java @@ -150,6 +150,7 @@ public class BluetoothMapService extends ProfileService { private boolean mSdpSearchInitiated = false; SdpMnsRecord mMnsRecord = null; private MapServiceMessageHandler mSessionStatusHandler; + private boolean mStartError = true; // package and class name to which we send intent to check phone book access permission private static final String ACCESS_AUTHORITY_PACKAGE = "com.android.settings"; @@ -465,6 +466,10 @@ public class BluetoothMapService extends ProfileService { return mState; } + protected boolean isMapStarted() { + return !mStartError; + } + public static BluetoothDevice getRemoteDevice() { return mRemoteDevice; } @@ -504,7 +509,7 @@ public class BluetoothMapService extends ProfileService { public boolean disconnectMap(BluetoothDevice device) { boolean result = false; if (DEBUG) Log.d(TAG, "disconnectMap"); - if (getRemoteDevice().equals(device)) { + if (getRemoteDevice()!= null && getRemoteDevice().equals(device)) { switch (mState) { case BluetoothMap.STATE_CONNECTED: /* Disconnect all connections and restart all MAS instances */ @@ -589,9 +594,14 @@ public class BluetoothMapService extends ProfileService { Log.w(TAG, "start received for non-active user, ignoring"); return false; } + //Start MapProfile if not already done. + if (isMapStarted()) { + Log.w(TAG, "start received for already started, ignoring"); + return false; + } - if (VERBOSE) Log.v(TAG, "verbose logging is enabled"); + if (VERBOSE) Log.v(TAG, "verbose logging is enabled"); HandlerThread thread = new HandlerThread("BluetoothMapHandler"); thread.start(); Looper looper = thread.getLooper(); @@ -631,7 +641,8 @@ public class BluetoothMapService extends ProfileService { // start RFCOMM listener sendStartListenerMessage(-1); - return true; + mStartError = false; + return !mStartError; } /** @@ -797,17 +808,25 @@ public class BluetoothMapService extends ProfileService { Log.e(TAG,"Unable to unregister map receiver",e); } } - CountDownLatch latch = new CountDownLatch(1); - sendShutdownMessage(latch); - // We need to wait for shutdown to complete to avoid being garbage collected before - // shutdown completes. - if(DEBUG) Log.i(TAG, "Waiting for shutdown to complete"); - try { - latch.await(); - } catch (InterruptedException e) { - Log.e(TAG, "Interrupt received while waiting for shutdown to complete", e); + //Stop MapProfile if already started. + //TODO: Check if the profile state can be retreived from ProfileService or AdapterService. + if (!isMapStarted()) { + if (DEBUG) Log.d(TAG, "Service Not Available to STOP, ignoring"); + return true; + } else { + if (VERBOSE) Log.d(TAG, "Service Stoping()"); } if (mSessionStatusHandler != null) { + CountDownLatch latch = new CountDownLatch(1); + sendShutdownMessage(latch); + // We need to wait for shutdown to complete to avoid being garbage collected before + // shutdown completes. + if(DEBUG) Log.i(TAG, "Waiting for shutdown to complete"); + try { + latch.await(); + } catch (InterruptedException e) { + Log.e(TAG, "Interrupt received while waiting for shutdown to complete", e); + } mSessionStatusHandler.removeCallbacksAndMessages(null); Looper looper = mSessionStatusHandler.getLooper(); if (looper != null) { @@ -815,6 +834,7 @@ public class BluetoothMapService extends ProfileService { } mSessionStatusHandler = null; } + mStartError = true; setState(BluetoothMap.STATE_DISCONNECTED, BluetoothMap.RESULT_CANCELED); if (DEBUG) Log.d(TAG, "stop() out"); return true; |