diff options
author | Linux Build Service Account <lnxbuild@localhost> | 2014-08-27 01:57:50 -0700 |
---|---|---|
committer | Gerrit - the friendly Code Review server <code-review@localhost> | 2014-08-27 01:57:50 -0700 |
commit | d1cd68eca1bbcfe523b365edc9808fe79cc20c90 (patch) | |
tree | 6946720660c4772a5592eabf46bc672ce1138ad3 /src | |
parent | 5c9ec1753dfcb83b98c26354d92d05f4be4fd808 (diff) | |
parent | 83cab5026087debc28bbe8b187655626c55e9103 (diff) | |
download | android_external_ant-wireless_ant_service-d1cd68eca1bbcfe523b365edc9808fe79cc20c90.tar.gz android_external_ant-wireless_ant_service-d1cd68eca1bbcfe523b365edc9808fe79cc20c90.tar.bz2 android_external_ant-wireless_ant_service-d1cd68eca1bbcfe523b365edc9808fe79cc20c90.zip |
Merge "Merge remote-tracking branch 'remotes/quic/caf/github/master' into tip"HEADreplicant-6.0-0004-transitionreplicant-6.0-0004-rc6replicant-6.0-0004-rc5-transitionreplicant-6.0-0004-rc5replicant-6.0-0004-rc4replicant-6.0-0004-rc3replicant-6.0-0004-rc2replicant-6.0-0004-rc1replicant-6.0-0004replicant-6.0-0003replicant-6.0-0002replicant-6.0-0001staging/cm-12.1staging/cm-12.0-cafstable/cm-13.0-ZNH5Ystable/cm-13.0-ZNH2KBstable/cm-13.0-ZNH2Kstable/cm-13.0-ZNH0Estable/cm-12.1-YOG7Dstable/cm-12.1-YOG4Pstable/cm-12.1-YOG3Cstable/cm-12.0-YNG4Nstable/cm-12.0-YNG3Cstable/cm-12.0-YNG1TAstable/cm-12.0-YNG1Tstable/cm-12.0-YNG1Icm-13.0cm-12.1cm-12.0
Diffstat (limited to 'src')
-rw-r--r-- | src/com/dsi/ant/server/AntService.java | 74 |
1 files changed, 47 insertions, 27 deletions
diff --git a/src/com/dsi/ant/server/AntService.java b/src/com/dsi/ant/server/AntService.java index 92e4e67..4e6f891 100644 --- a/src/com/dsi/ant/server/AntService.java +++ b/src/com/dsi/ant/server/AntService.java @@ -80,7 +80,11 @@ public class AntService extends Service private Object mChangeAntPowerState_LOCK = new Object(); private static Object sAntHalServiceDestroy_LOCK = new Object(); - IAntHalCallback mCallback; + /** Callback object for sending events to the upper layers */ + private volatile IAntHalCallback mCallback; + /** Used for synchronizing changes to {@link #mCallback} + * The synchronization is needed because of how {@link #doUnregisterAntHalCallback(IAntHalCallback)} works */ + private final Object mCallback_LOCK = new Object(); /** * Receives Bluetooth State Changed intent and sends {@link ACTION_REQUEST_ENABLE} @@ -126,7 +130,7 @@ public class AntService extends Service } /** - * Calls back the registered callback with the change to the new state + * Calls back the registered callback with the change to the new state * @param state the {@link AntHalDefine} state */ private void setState(int state) @@ -134,13 +138,16 @@ public class AntService extends Service synchronized(mChangeAntPowerState_LOCK) { if(DEBUG) Log.i(TAG, "Setting ANT State = "+ state +" / "+ AntHalDefine.getAntHalStateString(state)); - if (mCallback != null) + // Use caching instead of synchronization so that we do not have to hold a lock during a callback. + // It is safe to not hold the lock because we are not doing any write accesses. + IAntHalCallback callback = mCallback; + if (callback != null) { try { - if(DEBUG) Log.d(TAG, "Calling status changed callback "+ mCallback.toString()); + if(DEBUG) Log.d(TAG, "Calling status changed callback "+ callback.toString()); - mCallback.antHalStateChanged(state); + callback.antHalStateChanged(state); } catch (RemoteException e) { @@ -188,7 +195,7 @@ public class AntService extends Service waitForBluetoothToEnable = true; mEnablePending = true; - + boolean isEnabling = bluetoothAdapter.enable(); // if enabling adapter has begun, return @@ -222,7 +229,7 @@ public class AntService extends Service break; } } - + return result; } } @@ -236,7 +243,7 @@ public class AntService extends Service if(DEBUG) Log.v(TAG, "doGetAntState start"); int retState = mJAnt.getRadioEnabledStatus(); // ANT state is native state - + if(DEBUG) Log.i(TAG, "Get ANT State = "+ retState +" / "+ AntHalDefine.getAntHalStateString(retState)); return retState; @@ -244,11 +251,9 @@ public class AntService extends Service /** * Perform a power change if required. - * Tries to use changeAntWirelessState() in {@link BluetoothService}. If it does not exist then - * it defaults to a native enable() or disable() call * @param state true for enable, false for disable - * @return {@link AntHalDefine#ANT_HAL_RESULT_SUCCESS} when the request is successfully sent, - * false otherwise + * @return {@link AntHalDefine#ANT_HAL_RESULT_SUCCESS} when the request has + * been posted, false otherwise */ private int asyncSetAntPowerState(final boolean state) { @@ -435,7 +440,10 @@ public class AntService extends Service { if(DEBUG) Log.i(TAG, "Registering callback: "+ callback.toString()); - mCallback = callback; + synchronized(mCallback_LOCK) + { + mCallback = callback; + } return AntHalDefine.ANT_HAL_RESULT_SUCCESS; } @@ -446,10 +454,13 @@ public class AntService extends Service int result = AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; - if(mCallback.asBinder() == callback.asBinder()) + synchronized (mCallback_LOCK) { - mCallback = null; - result = AntHalDefine.ANT_HAL_RESULT_SUCCESS; + if(mCallback.asBinder() == callback.asBinder()) + { + mCallback = null; + result = AntHalDefine.ANT_HAL_RESULT_SUCCESS; + } } return result; @@ -535,7 +546,7 @@ public class AntService extends Service if (DEBUG) Log.d(TAG, "onCreate() entered"); super.onCreate(); - + if(null != mJAnt) { // This somehow happens when quickly starting/stopping an application. @@ -548,9 +559,9 @@ public class AntService extends Service if (createResult == JAntStatus.SUCCESS) { - mInitialized = true; + mInitialized = true; - if (DEBUG) Log.d(TAG, "JAntJava create success"); + if (DEBUG) Log.d(TAG, "JAntJava create success"); } else { @@ -563,7 +574,7 @@ public class AntService extends Service filter.addAction(ACTION_REQUEST_ENABLE); filter.addAction(ACTION_REQUEST_DISABLE); registerReceiver(mReceiver, filter); - + if (mRequiresBluetoothOn) { IntentFilter stateChangedFilter = new IntentFilter(); stateChangedFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); @@ -578,19 +589,22 @@ public class AntService extends Service try { - synchronized(sAntHalServiceDestroy_LOCK) + synchronized(sAntHalServiceDestroy_LOCK) { if(null != mJAnt) { int result = disableBlocking(); if (DEBUG) Log.d(TAG, "onDestroy: disable result is: " + AntHalDefine.getAntHalResultString(result)); - + mJAnt.destroy(); mJAnt = null; } } - mCallback = null; + synchronized (mCallback_LOCK) + { + mCallback = null; + } } finally { @@ -633,7 +647,10 @@ public class AntService extends Service { if (DEBUG) Log.d(TAG, "onUnbind() entered"); - mCallback = null; + synchronized (mCallback_LOCK) + { + mCallback = null; + } return super.onUnbind(intent); } @@ -657,11 +674,14 @@ public class AntService extends Service { public synchronized void ANTRxMessage( byte[] message) { - if(null != mCallback) + // Use caching instead of synchronization so that we do not have to hold a lock during a callback. + // It is safe to not hold the lock because we are not doing any write accesses. + IAntHalCallback callback = mCallback; + if(null != callback) { try { - mCallback.antHalRxMessage(message); + callback.antHalRxMessage(message); } catch (RemoteException e) { @@ -678,7 +698,7 @@ public class AntService extends Service public synchronized void ANTStateChange(int NewState) { if (DEBUG) Log.i(TAG, "ANTStateChange callback to " + NewState); - + setState(NewState); } }; |