From 6ce1f4109fc2b78b1ca91a721ae1021a3af6c5c7 Mon Sep 17 00:00:00 2001 From: Eugene Susla Date: Tue, 12 Jan 2021 11:39:43 -0800 Subject: [DO NOT MERGE] Scan and notify apps when their companion devices are nearby Test: manual Bug: 168052577 Change-Id: Ib2187fb76e604878b1d4dd9c0cd6cea610b2a04d (cherry picked from commit 017c2c41456e3938145cf33facea339f9918b20c) --- core/java/android/bluetooth/BluetoothAdapter.java | 55 +++++++++++++++++++++++ core/java/android/companion/Association.java | 30 ++++++------- 2 files changed, 70 insertions(+), 15 deletions(-) (limited to 'core/java/android') diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 7eda50e5c9cb..ea7e5ea7c802 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -3197,6 +3197,61 @@ public final class BluetoothAdapter { void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord); } + /** + * Register a callback to receive events whenever the bluetooth stack goes down and back up, + * e.g. in the event the bluetooth is turned off/on via settings. + * + * If the bluetooth stack is currently up, there will not be an initial callback call. + * You can use the return value as an indication of this being the case. + * + * Callbacks will be delivered on a binder thread. + * + * @return whether bluetooth is already up currently + * + * @hide + */ + public boolean registerServiceLifecycleCallback(ServiceLifecycleCallback callback) { + return getBluetoothService(callback.mRemote) != null; + } + + /** + * Unregister a callback registered via {@link #registerServiceLifecycleCallback} + * + * @hide + */ + public void unregisterServiceLifecycleCallback(ServiceLifecycleCallback callback) { + removeServiceStateCallback(callback.mRemote); + } + + /** + * A callback for {@link #registerServiceLifecycleCallback} + * + * @hide + */ + public abstract static class ServiceLifecycleCallback { + + /** Called when the bluetooth stack is up */ + public abstract void onBluetoothServiceUp(); + + /** Called when the bluetooth stack is down */ + public abstract void onBluetoothServiceDown(); + + IBluetoothManagerCallback mRemote = new IBluetoothManagerCallback.Stub() { + @Override + public void onBluetoothServiceUp(IBluetooth bluetoothService) { + ServiceLifecycleCallback.this.onBluetoothServiceUp(); + } + + @Override + public void onBluetoothServiceDown() { + ServiceLifecycleCallback.this.onBluetoothServiceDown(); + } + + @Override + public void onBrEdrDown() {} + }; + } + /** * Starts a scan for Bluetooth LE devices. * diff --git a/core/java/android/companion/Association.java b/core/java/android/companion/Association.java index 17bf11b3d4ef..960a08755cb8 100644 --- a/core/java/android/companion/Association.java +++ b/core/java/android/companion/Association.java @@ -38,7 +38,7 @@ public final class Association implements Parcelable { private final @NonNull String mDeviceMacAddress; private final @NonNull String mPackageName; private final @Nullable String mDeviceProfile; - private final boolean mKeepProfilePrivilegesWhenDeviceAway; + private final boolean mNotifyOnDeviceNearby; /** @hide */ public int getUserId() { @@ -47,7 +47,7 @@ public final class Association implements Parcelable { - // Code below generated by codegen v1.0.21. + // Code below generated by codegen v1.0.22. // // DO NOT MODIFY! // CHECKSTYLE:OFF Generated code @@ -71,7 +71,7 @@ public final class Association implements Parcelable { @NonNull String deviceMacAddress, @NonNull String packageName, @Nullable String deviceProfile, - boolean keepProfilePrivilegesWhenDeviceAway) { + boolean notifyOnDeviceNearby) { this.mUserId = userId; com.android.internal.util.AnnotationValidations.validate( UserIdInt.class, null, mUserId); @@ -82,7 +82,7 @@ public final class Association implements Parcelable { com.android.internal.util.AnnotationValidations.validate( NonNull.class, null, mPackageName); this.mDeviceProfile = deviceProfile; - this.mKeepProfilePrivilegesWhenDeviceAway = keepProfilePrivilegesWhenDeviceAway; + this.mNotifyOnDeviceNearby = notifyOnDeviceNearby; // onConstructed(); // You can define this method to get a callback } @@ -103,8 +103,8 @@ public final class Association implements Parcelable { } @DataClass.Generated.Member - public boolean isKeepProfilePrivilegesWhenDeviceAway() { - return mKeepProfilePrivilegesWhenDeviceAway; + public boolean isNotifyOnDeviceNearby() { + return mNotifyOnDeviceNearby; } @Override @@ -118,7 +118,7 @@ public final class Association implements Parcelable { "deviceMacAddress = " + mDeviceMacAddress + ", " + "packageName = " + mPackageName + ", " + "deviceProfile = " + mDeviceProfile + ", " + - "keepProfilePrivilegesWhenDeviceAway = " + mKeepProfilePrivilegesWhenDeviceAway + + "notifyOnDeviceNearby = " + mNotifyOnDeviceNearby + " }"; } @@ -139,7 +139,7 @@ public final class Association implements Parcelable { && Objects.equals(mDeviceMacAddress, that.mDeviceMacAddress) && Objects.equals(mPackageName, that.mPackageName) && Objects.equals(mDeviceProfile, that.mDeviceProfile) - && mKeepProfilePrivilegesWhenDeviceAway == that.mKeepProfilePrivilegesWhenDeviceAway; + && mNotifyOnDeviceNearby == that.mNotifyOnDeviceNearby; } @Override @@ -153,7 +153,7 @@ public final class Association implements Parcelable { _hash = 31 * _hash + Objects.hashCode(mDeviceMacAddress); _hash = 31 * _hash + Objects.hashCode(mPackageName); _hash = 31 * _hash + Objects.hashCode(mDeviceProfile); - _hash = 31 * _hash + Boolean.hashCode(mKeepProfilePrivilegesWhenDeviceAway); + _hash = 31 * _hash + Boolean.hashCode(mNotifyOnDeviceNearby); return _hash; } @@ -164,7 +164,7 @@ public final class Association implements Parcelable { // void parcelFieldName(Parcel dest, int flags) { ... } byte flg = 0; - if (mKeepProfilePrivilegesWhenDeviceAway) flg |= 0x10; + if (mNotifyOnDeviceNearby) flg |= 0x10; if (mDeviceProfile != null) flg |= 0x8; dest.writeByte(flg); dest.writeInt(mUserId); @@ -185,7 +185,7 @@ public final class Association implements Parcelable { // static FieldType unparcelFieldName(Parcel in) { ... } byte flg = in.readByte(); - boolean keepProfilePrivilegesWhenDeviceAway = (flg & 0x10) != 0; + boolean notifyOnDeviceNearby = (flg & 0x10) != 0; int userId = in.readInt(); String deviceMacAddress = in.readString(); String packageName = in.readString(); @@ -201,7 +201,7 @@ public final class Association implements Parcelable { com.android.internal.util.AnnotationValidations.validate( NonNull.class, null, mPackageName); this.mDeviceProfile = deviceProfile; - this.mKeepProfilePrivilegesWhenDeviceAway = keepProfilePrivilegesWhenDeviceAway; + this.mNotifyOnDeviceNearby = notifyOnDeviceNearby; // onConstructed(); // You can define this method to get a callback } @@ -221,10 +221,10 @@ public final class Association implements Parcelable { }; @DataClass.Generated( - time = 1606940835778L, - codegenVersion = "1.0.21", + time = 1610482674799L, + codegenVersion = "1.0.22", sourceFile = "frameworks/base/core/java/android/companion/Association.java", - inputSignatures = "private final @android.annotation.UserIdInt int mUserId\nprivate final @android.annotation.NonNull java.lang.String mDeviceMacAddress\nprivate final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.Nullable java.lang.String mDeviceProfile\nprivate final boolean mKeepProfilePrivilegesWhenDeviceAway\npublic int getUserId()\nclass Association extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true, genHiddenConstructor=true)") + inputSignatures = "private final @android.annotation.UserIdInt int mUserId\nprivate final @android.annotation.NonNull java.lang.String mDeviceMacAddress\nprivate final @android.annotation.NonNull java.lang.String mPackageName\nprivate final @android.annotation.Nullable java.lang.String mDeviceProfile\nprivate final boolean mNotifyOnDeviceNearby\npublic int getUserId()\nclass Association extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genToString=true, genHiddenConstructor=true)") @Deprecated private void __metadata() {} -- cgit v1.2.3