From ff82ea455806f6a7eea8f02050662ecb87253d08 Mon Sep 17 00:00:00 2001 From: Khurshid Date: Thu, 18 Jul 2013 15:40:23 -0600 Subject: ANTDROID-1987: Enable Bluetooth when ANT is enabled if required by chip. --- Android.mk | 13 ++++ AndroidManifest.xml | 11 +++ project.properties | 2 +- src/com/dsi/ant/server/AntService.java | 93 +++++++++++++++++++++++- src/com/dsi/ant/server/StateChangedReceiver.java | 43 +++++++++++ 5 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 src/com/dsi/ant/server/StateChangedReceiver.java diff --git a/Android.mk b/Android.mk index 2169f06..92d7751 100644 --- a/Android.mk +++ b/Android.mk @@ -20,6 +20,9 @@ ifneq ($(BOARD_ANT_WIRELESS_DEVICE),) LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) +BT_ON_SRC_FILES := \ + src/com/dsi/ant/server/StateChangedReceiver.java + # # ANT java system service # @@ -29,6 +32,16 @@ LOCAL_SRC_FILES := \ src/com/dsi/ant/server/IAntHal.aidl \ src/com/dsi/ant/server/IAntHalCallback.aidl +# +# If target board is not one that requires Bluetooth to be enabled for ANT to enable, +# filter out files that are only needed for Bluetooth to enable when ANT is enabled. +# + +ifeq ($(filter msm8610 msm8226 msm8974, $(TARGET_BOARD_PLATFORM)),) +LOCAL_SRC_FILES := \ + $(filter-out $(BT_ON_SRC_FILES), $(LOCAL_SRC_FILES)) +endif + LOCAL_REQUIRED_MODULES := libantradio LOCAL_PROGUARD_FLAG_FILES := proguard.flags LOCAL_CERTIFICATE := platform diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9336b82..7284179 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -50,6 +50,15 @@ limitations under the License. + + + + + + @@ -71,5 +80,7 @@ limitations under the License. android:description="@string/permdesc_antradio" android:label="@string/permlab_antradio"/> + + diff --git a/project.properties b/project.properties index 5a70945..c4f09d2 100644 --- a/project.properties +++ b/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-7 +target=android-17 diff --git a/src/com/dsi/ant/server/AntService.java b/src/com/dsi/ant/server/AntService.java index 1619c0b..388a311 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,57 @@ public class AntService extends Service public static final String ANT_SERVICE = "AntService"; + public static final String ANT_ADMIN_PERMISSION = "com.dsi.ant.permission.ANT_ADMIN"; + + public static final String ACTION_REQUEST_ENABLE = "com.dsi.ant.server.action.REQUEST_ENABLE"; + + public static final String ACTION_REQUEST_DISABLE = "com.dsi.ant.server.action.REQUEST_DISABLE"; + private JAntJava mJAnt = null; private boolean mInitialized = false; + private boolean mRequiresBluetoothOn = false; + + private boolean mEnablePending = false; + private Object mChangeAntPowerState_LOCK = new Object(); private static Object sAntHalServiceDestroy_LOCK = new Object(); IAntHalCallback mCallback; + 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; + } + + 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); + } + } + } + } + }; + public static boolean startService(Context context) { return ( null != context.startService(new Intent(IAntHal.class.getName())) ); @@ -99,7 +147,42 @@ 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) { + long callingPid = Binder.clearCallingIdentity(); + + if (!bluetoothAdapter.isEnabled()) { + + waitForBluetoothToEnable = true; + + // check permissions of ANTHALService + boolean isEnabling = bluetoothAdapter.enable(); + + // if enabling adapter has begun, return + // success. + if (isEnabling) { + mEnablePending = true; + result = AntHalDefine.ANT_HAL_RESULT_SUCCESS; + // StateChangedReceiver will receive + // enabled status and then enable ANT + } + } + + Binder.restoreCallingIdentity(callingPid); + } + } + + if (!waitForBluetoothToEnable) { + result = asyncSetAntPowerState(true); + } break; } case AntHalDefine.ANT_HAL_STATE_DISABLED: @@ -430,6 +513,7 @@ public class AntService extends Service } // create a single new JAnt HCI Interface instance mJAnt = new JAntJava(); + mRequiresBluetoothOn = isQcomPlatform(); JAntStatus createResult = mJAnt.create(mJAntCallback); if (createResult == JAntStatus.SUCCESS) @@ -444,6 +528,11 @@ 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); } @Override @@ -471,6 +560,8 @@ public class AntService extends Service { super.onDestroy(); } + + unregisterReceiver(mReceiver); } @Override diff --git a/src/com/dsi/ant/server/StateChangedReceiver.java b/src/com/dsi/ant/server/StateChangedReceiver.java new file mode 100644 index 0000000..4479003 --- /dev/null +++ b/src/com/dsi/ant/server/StateChangedReceiver.java @@ -0,0 +1,43 @@ +/* + Copyright 2013 Dynastream Innovations + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.dsi.ant.server; + +import android.bluetooth.BluetoothAdapter; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class StateChangedReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + // Bluetooth State Changed + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) { + + int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0); + + if (BluetoothAdapter.STATE_OFF == bluetoothState) { + Intent antIntent = new Intent(AntService.ACTION_REQUEST_DISABLE); + context.sendBroadcast(antIntent, AntService.ANT_ADMIN_PERMISSION); + } else if (BluetoothAdapter.STATE_ON == bluetoothState) { + Intent antIntent = new Intent(AntService.ACTION_REQUEST_ENABLE); + context.sendBroadcast(antIntent, AntService.ANT_ADMIN_PERMISSION); + } + } // else if <> state change + + } +} -- cgit v1.2.3 From cff3d231d19de744a9de779988865f8410bc7f8a Mon Sep 17 00:00:00 2001 From: Khurshid Date: Thu, 18 Jul 2013 18:28:15 -0600 Subject: Android_System_ANTHALService_3-0-1 --- Android.mk | 2 +- AndroidManifest.xml | 4 +- ReleaseNotes.txt | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 ReleaseNotes.txt diff --git a/Android.mk b/Android.mk index 92d7751..f119ec0 100644 --- a/Android.mk +++ b/Android.mk @@ -33,7 +33,7 @@ LOCAL_SRC_FILES := \ src/com/dsi/ant/server/IAntHalCallback.aidl # -# If target board is not one that requires Bluetooth to be enabled for ANT to enable, +# If target board is not one that requires Bluetooth to be enabled for ANT to enable, # filter out files that are only needed for Bluetooth to enable when ANT is enabled. # diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 7284179..7e0a17e 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -16,8 +16,8 @@ limitations under the License. --> Date: Fri, 19 Jul 2013 16:16:41 -0600 Subject: Android_System_ANTHALService_3-1-0 --- Android.mk | 13 --- AndroidManifest.xml | 13 +-- ReleaseNotes.txt | 10 ++- src/com/dsi/ant/server/AntService.java | 156 ++++++++++++++++++++++----------- 4 files changed, 115 insertions(+), 77 deletions(-) diff --git a/Android.mk b/Android.mk index f119ec0..2169f06 100644 --- a/Android.mk +++ b/Android.mk @@ -20,9 +20,6 @@ ifneq ($(BOARD_ANT_WIRELESS_DEVICE),) LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -BT_ON_SRC_FILES := \ - src/com/dsi/ant/server/StateChangedReceiver.java - # # ANT java system service # @@ -32,16 +29,6 @@ LOCAL_SRC_FILES := \ src/com/dsi/ant/server/IAntHal.aidl \ src/com/dsi/ant/server/IAntHalCallback.aidl -# -# If target board is not one that requires Bluetooth to be enabled for ANT to enable, -# filter out files that are only needed for Bluetooth to enable when ANT is enabled. -# - -ifeq ($(filter msm8610 msm8226 msm8974, $(TARGET_BOARD_PLATFORM)),) -LOCAL_SRC_FILES := \ - $(filter-out $(BT_ON_SRC_FILES), $(LOCAL_SRC_FILES)) -endif - LOCAL_REQUIRED_MODULES := libantradio LOCAL_PROGUARD_FLAG_FILES := proguard.flags LOCAL_CERTIFICATE := platform diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 7e0a17e..28b95be 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -16,8 +16,8 @@ limitations under the License. --> - - - - - - diff --git a/ReleaseNotes.txt b/ReleaseNotes.txt index d889dc7..82df96b 100644 --- a/ReleaseNotes.txt +++ b/ReleaseNotes.txt @@ -1,6 +1,6 @@ AntHalService - Release Notes -v3.0.1 -2013-07-18 +v3.1.0 +2013-07-19 Copyright 2011 Dynastream Innovations @@ -27,6 +27,12 @@ messages, enable, disable and get the enebaled state. ============================================================= 2. REVISION HISTORY +v3.1.0 : 2013-07-19 : Android_System_ANTHALService_3-1-0 + +Task + [ANTDROID-2035] - Remove tabs from AntService + [ANTDROID-1987] - Enable Bluetooth when ANT is enabled, if required by chip + v3.0.0 : 2013-01-17 : Android_System_ANTHALService_3-0-0 Bug diff --git a/src/com/dsi/ant/server/AntService.java b/src/com/dsi/ant/server/AntService.java index 388a311..1ed828f 100644 --- a/src/com/dsi/ant/server/AntService.java +++ b/src/com/dsi/ant/server/AntService.java @@ -46,18 +46,35 @@ 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(); @@ -65,17 +82,16 @@ public class AntService extends Service IAntHalCallback mCallback; - 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; - } + /** + * 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) { @@ -97,6 +113,27 @@ public class AntService extends Service } }; + /** + * 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())) ); @@ -157,22 +194,25 @@ public class AntService extends Service BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null) { + + // run with permissions of ANTHALService long callingPid = Binder.clearCallingIdentity(); if (!bluetoothAdapter.isEnabled()) { waitForBluetoothToEnable = true; - - // check permissions of ANTHALService + mEnablePending = true; + boolean isEnabling = bluetoothAdapter.enable(); // if enabling adapter has begun, return // success. if (isEnabling) { - mEnablePending = true; result = AntHalDefine.ANT_HAL_RESULT_SUCCESS; // StateChangedReceiver will receive // enabled status and then enable ANT + } else { + mEnablePending = false; } } @@ -226,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; } /** @@ -513,7 +557,7 @@ public class AntService extends Service } // create a single new JAnt HCI Interface instance mJAnt = new JAntJava(); - mRequiresBluetoothOn = isQcomPlatform(); + mRequiresBluetoothOn = requiresBluetoothOn(); JAntStatus createResult = mJAnt.create(mJAntCallback); if (createResult == JAntStatus.SUCCESS) @@ -533,6 +577,12 @@ 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); + registerReceiver(mStateChangedReceiver, stateChangedFilter); + } } @Override @@ -561,6 +611,10 @@ public class AntService extends Service super.onDestroy(); } + if (mRequiresBluetoothOn) { + unregisterReceiver(mStateChangedReceiver); + } + unregisterReceiver(mReceiver); } -- cgit v1.2.3