diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubService.java b/services/core/java/com/android/server/location/contexthub/ContextHubService.java index 61e6d148c0fbf..de8e06aeac9f0 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -20,6 +20,7 @@ import android.annotation.IntDef; import android.annotation.Nullable; import android.app.ActivityManager; import android.app.PendingIntent; +import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -154,6 +155,10 @@ public class ContextHubService extends IContextHubService.Stub { private boolean mIsWifiScanningEnabled = false; private boolean mIsWifiMainEnabled = false; + // True if BT is available for the Context Hub + private boolean mIsBtScanningEnabled = false; + private boolean mIsBtMainEnabled = false; + // A hashmap used to record if a contexthub is waiting for daily query private Set mMetricQueryPendingContextHubIds = Collections.newSetFromMap(new ConcurrentHashMap()); @@ -333,6 +338,25 @@ public class ContextHubService extends IContextHubService.Stub { } + if (mContextHubWrapper.supportsBtSettingNotifications()) { + sendBtSettingUpdate(true /* forceUpdate */); + + BroadcastReceiver btReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction()) + || BluetoothAdapter.ACTION_BLE_STATE_CHANGED.equals( + intent.getAction())) { + sendBtSettingUpdate(false /* forceUpdate */); + } + } + }; + IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); + filter.addAction(BluetoothAdapter.ACTION_BLE_STATE_CHANGED); + mContext.registerReceiver(btReceiver, filter); + } + scheduleDailyMetricSnapshot(); } @@ -735,6 +759,7 @@ public class ContextHubService extends IContextHubService.Stub { sendWifiSettingUpdate(true /* forceUpdate */); sendAirplaneModeSettingUpdate(); sendMicrophoneDisableSettingUpdateForCurrentUser(); + sendBtSettingUpdate(true /* forceUpdate */); mTransactionManager.onHubReset(); queryNanoAppsInternal(contextHubId); @@ -1132,6 +1157,39 @@ public class ContextHubService extends IContextHubService.Stub { } } + /** + * Obtains the latest BT availability setting value and notifies the Context Hub. + * + * @param forceUpdate True to force send update to the Context Hub, otherwise only send the + * update when the BT availability changes. + */ + private void sendBtSettingUpdate(boolean forceUpdate) { + final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); + // Adapter may be null if BT is not supported. + if (adapter != null) { + boolean btEnabled = adapter.isEnabled(); + boolean btScanEnabled = adapter.isBleScanAlwaysAvailable(); + if (forceUpdate || mIsBtScanningEnabled != btScanEnabled) { + mIsBtScanningEnabled = btScanEnabled; + mContextHubWrapper.onBtScanningSettingChanged(btScanEnabled); + } + if (forceUpdate || mIsBtMainEnabled != btEnabled) { + mIsBtMainEnabled = btEnabled; + mContextHubWrapper.onBtMainSettingChanged(btEnabled); + } + } else { + Log.d(TAG, "BT adapter not available. Defaulting to disabled"); + if (mIsBtMainEnabled) { + mIsBtMainEnabled = false; + mContextHubWrapper.onBtMainSettingChanged(mIsBtMainEnabled); + } + if (mIsBtScanningEnabled) { + mIsBtScanningEnabled = false; + mContextHubWrapper.onBtScanningSettingChanged(mIsBtScanningEnabled); + } + } + } + /** * Obtains the latest airplane mode setting value and notifies the Context Hub. */ diff --git a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java index 9b0b7829005df..21ea1f600b713 100644 --- a/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java +++ b/services/core/java/com/android/server/location/contexthub/IContextHubWrapper.java @@ -242,6 +242,22 @@ public abstract class IContextHubWrapper { */ public abstract void onMicrophoneSettingChanged(boolean enabled); + /** + * @return True if this version of the Contexthub HAL supports BT availability setting + * notifications. + */ + public abstract boolean supportsBtSettingNotifications(); + + /** + * Notifies the Contexthub implementation of a BT main setting change. + */ + public abstract void onBtMainSettingChanged(boolean enabled); + + /** + * Notifies the Contexthub implementation of a BT scanning setting change. + */ + public abstract void onBtScanningSettingChanged(boolean enabled); + /** * Invoked whenever a host client connects with the framework. * @@ -409,6 +425,10 @@ public abstract class IContextHubWrapper { return true; } + public boolean supportsBtSettingNotifications() { + return true; + } + public void onLocationSettingChanged(boolean enabled) { onSettingChanged(android.hardware.contexthub.Setting.LOCATION, enabled); } @@ -432,6 +452,14 @@ public abstract class IContextHubWrapper { onSettingChanged(android.hardware.contexthub.Setting.WIFI_SCANNING, enabled); } + public void onBtMainSettingChanged(boolean enabled) { + onSettingChanged(android.hardware.contexthub.Setting.BT_MAIN, enabled); + } + + public void onBtScanningSettingChanged(boolean enabled) { + onSettingChanged(android.hardware.contexthub.Setting.BT_SCANNING, enabled); + } + @Override public void onHostEndpointConnected(HostEndpointInfo info) { try { @@ -662,8 +690,14 @@ public abstract class IContextHubWrapper { mHub.registerCallback(contextHubId, mHidlCallbackMap.get(contextHubId)); } + public boolean supportsBtSettingNotifications() { + return false; + } + public void onWifiMainSettingChanged(boolean enabled) {} public void onWifiScanningSettingChanged(boolean enabled) {} + public void onBtMainSettingChanged(boolean enabled) {} + public void onBtScanningSettingChanged(boolean enabled) {} } private static class ContextHubWrapperV1_0 extends ContextHubWrapperHidl {