diff options
author | Satish kumar sugasi <ssugas@codeaurora.org> | 2013-09-03 21:26:26 -0700 |
---|---|---|
committer | Satish kumar sugasi <ssugas@codeaurora.org> | 2013-09-03 21:26:26 -0700 |
commit | bb89c2de4a8db0bcdcc8dbd3a3731e895ba8f364 (patch) | |
tree | 14fff7d41bec7d8d4fee92af08282d18a6b39454 /src/com/dsi/ant/server/AntService.java | |
parent | b50c3ac021b0198d4c63339ead91de657d3e28e5 (diff) | |
parent | 9a3be4106f3b5e906ea9fddf9799c5c260ffec45 (diff) | |
download | android_external_ant-wireless_ant_service-bb89c2de4a8db0bcdcc8dbd3a3731e895ba8f364.tar.gz android_external_ant-wireless_ant_service-bb89c2de4a8db0bcdcc8dbd3a3731e895ba8f364.tar.bz2 android_external_ant-wireless_ant_service-bb89c2de4a8db0bcdcc8dbd3a3731e895ba8f364.zip |
Merge branch 'caf' into tip
Diffstat (limited to 'src/com/dsi/ant/server/AntService.java')
-rw-r--r-- | src/com/dsi/ant/server/AntService.java | 221 |
1 files changed, 183 insertions, 38 deletions
diff --git a/src/com/dsi/ant/server/AntService.java b/src/com/dsi/ant/server/AntService.java index 1619c0b..1ed828f 100644 --- a/src/com/dsi/ant/server/AntService.java +++ b/src/com/dsi/ant/server/AntService.java @@ -18,12 +18,18 @@ package com.dsi.ant.server; +import android.bluetooth.BluetoothAdapter; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.app.Service; +import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; +import android.provider.Settings; import android.util.Log; +import android.os.SystemProperties; import com.dsi.ant.core.*; @@ -40,15 +46,94 @@ public class AntService extends Service public static final String ANT_SERVICE = "AntService"; + /** + * Allows the application to directly configure the ANT radio through the + * proxy service. Malicious applications may prevent other ANT applications + * from connecting to ANT devices + */ + public static final String ANT_ADMIN_PERMISSION = "com.dsi.ant.permission.ANT_ADMIN"; + + /** + * Request that ANT be enabled + */ + public static final String ACTION_REQUEST_ENABLE = "com.dsi.ant.server.action.REQUEST_ENABLE"; + + /** + * Request that ANT be disabled + */ + public static final String ACTION_REQUEST_DISABLE = "com.dsi.ant.server.action.REQUEST_DISABLE"; + private JAntJava mJAnt = null; private boolean mInitialized = false; + /** + * Flag for if Bluetooth needs to be enabled for ANT to enable + */ + private boolean mRequiresBluetoothOn = false; + + /** + * Flag which specifies if we are waiting for an ANT enable intent + */ + private boolean mEnablePending = false; + private Object mChangeAntPowerState_LOCK = new Object(); private static Object sAntHalServiceDestroy_LOCK = new Object(); IAntHalCallback mCallback; + /** + * Receives Bluetooth State Changed intent and sends {@link ACTION_REQUEST_ENABLE} + * and {@link ACTION_REQUEST_DISABLE} accordingly + */ + private final StateChangedReceiver mStateChangedReceiver = new StateChangedReceiver(); + + /** + * Receives {@link ACTION_REQUEST_ENABLE} and {@link ACTION_REQUEST_DISABLE} + * intents to enable and disable ANT. + */ + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (mRequiresBluetoothOn) { + String action = intent.getAction(); + if (ACTION_REQUEST_ENABLE.equals(action)) { + if (mEnablePending) { + asyncSetAntPowerState(true); + mEnablePending = false; + } + } else if (ACTION_REQUEST_DISABLE.equals(action)) { + if (mEnablePending) { + mEnablePending = false; + } else { + asyncSetAntPowerState(false); + } + } + } + } + }; + + /** + * Checks if Bluetooth needs to be turned on for ANT to enable + */ + private boolean requiresBluetoothOn() { + return isQcomPlatform(); + } + + /** + * Checks if the current platform is QCom + */ + private boolean isQcomPlatform() + { + if ((SystemProperties.get("ro.board.platform").equals("msm8974")) + || (SystemProperties.get("ro.board.platform").equals("msm8610")) + || (SystemProperties.get("ro.board.platform").equals("msm8226"))) { + + return true; + } + return false; + } + public static boolean startService(Context context) { return ( null != context.startService(new Intent(IAntHal.class.getName())) ); @@ -99,7 +184,45 @@ public class AntService extends Service { case AntHalDefine.ANT_HAL_STATE_ENABLED: { - result = asyncSetAntPowerState(true); + result = AntHalDefine.ANT_HAL_RESULT_FAIL_NOT_ENABLED; + + boolean waitForBluetoothToEnable = false; + + if (mRequiresBluetoothOn) { + + // Try to turn on BT if it is not enabled. + BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); + + if (bluetoothAdapter != null) { + + // run with permissions of ANTHALService + long callingPid = Binder.clearCallingIdentity(); + + if (!bluetoothAdapter.isEnabled()) { + + waitForBluetoothToEnable = true; + mEnablePending = true; + + boolean isEnabling = bluetoothAdapter.enable(); + + // if enabling adapter has begun, return + // success. + if (isEnabling) { + result = AntHalDefine.ANT_HAL_RESULT_SUCCESS; + // StateChangedReceiver will receive + // enabled status and then enable ANT + } else { + mEnablePending = false; + } + } + + Binder.restoreCallingIdentity(callingPid); + } + } + + if (!waitForBluetoothToEnable) { + result = asyncSetAntPowerState(true); + } break; } case AntHalDefine.ANT_HAL_STATE_DISABLED: @@ -143,44 +266,48 @@ public class AntService extends Service */ private int asyncSetAntPowerState(final boolean state) { - int result = AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; - - synchronized(mChangeAntPowerState_LOCK) { - // Check we are not already in/transitioning to the state we want - int currentState = doGetAntState(); - - if(state) { - if((AntHalDefine.ANT_HAL_STATE_ENABLED == currentState) - || (AntHalDefine.ANT_HAL_STATE_ENABLING == currentState)) { - if(DEBUG) Log.d(TAG, "Enable request ignored as already enabled/enabling"); - - return AntHalDefine.ANT_HAL_RESULT_SUCCESS; - } else if(AntHalDefine.ANT_HAL_STATE_DISABLING == currentState) { - Log.w(TAG, "Enable request ignored as already disabling"); - - return AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; - } - } else { - if((AntHalDefine.ANT_HAL_STATE_DISABLED == currentState) - || (AntHalDefine.ANT_HAL_STATE_DISABLING == currentState)) { - if(DEBUG)Log.d(TAG, "Disable request ignored as already disabled/disabling"); - - return AntHalDefine.ANT_HAL_RESULT_SUCCESS; - } else if(AntHalDefine.ANT_HAL_STATE_ENABLING == currentState) { - Log.w(TAG, "Disable request ignored as already enabling"); - - return AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; - } - } - - if (state) { - result = enableBackground(); - } else { - result = disableBackground(); - } + int result = AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; + + synchronized (mChangeAntPowerState_LOCK) { + // Check we are not already in/transitioning to the state we want + int currentState = doGetAntState(); + + if (state) { + if ((AntHalDefine.ANT_HAL_STATE_ENABLED == currentState) + || (AntHalDefine.ANT_HAL_STATE_ENABLING == currentState)) { + if (DEBUG) { + Log.d(TAG, "Enable request ignored as already enabled/enabling"); + } + + return AntHalDefine.ANT_HAL_RESULT_SUCCESS; + } else if (AntHalDefine.ANT_HAL_STATE_DISABLING == currentState) { + Log.w(TAG, "Enable request ignored as already disabling"); + + return AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; + } + } else { + if ((AntHalDefine.ANT_HAL_STATE_DISABLED == currentState) + || (AntHalDefine.ANT_HAL_STATE_DISABLING == currentState)) { + if (DEBUG) { + Log.d(TAG, "Disable request ignored as already disabled/disabling"); + } + + return AntHalDefine.ANT_HAL_RESULT_SUCCESS; + } else if (AntHalDefine.ANT_HAL_STATE_ENABLING == currentState) { + Log.w(TAG, "Disable request ignored as already enabling"); + + return AntHalDefine.ANT_HAL_RESULT_FAIL_UNKNOWN; + } + } + + if (state) { + result = enableBackground(); + } else { + result = disableBackground(); + } } - - return result; + + return result; } /** @@ -430,6 +557,7 @@ public class AntService extends Service } // create a single new JAnt HCI Interface instance mJAnt = new JAntJava(); + mRequiresBluetoothOn = requiresBluetoothOn(); JAntStatus createResult = mJAnt.create(mJAntCallback); if (createResult == JAntStatus.SUCCESS) @@ -444,6 +572,17 @@ public class AntService extends Service if (DEBUG) Log.e(TAG, "JAntJava create failed: " + createResult); } + + IntentFilter filter = new IntentFilter(); + 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); + registerReceiver(mStateChangedReceiver, stateChangedFilter); + } } @Override @@ -471,6 +610,12 @@ public class AntService extends Service { super.onDestroy(); } + + if (mRequiresBluetoothOn) { + unregisterReceiver(mStateChangedReceiver); + } + + unregisterReceiver(mReceiver); } @Override |