diff options
| author | 2022-11-01 22:58:19 +0000 | |
|---|---|---|
| committer | 2022-11-01 22:58:19 +0000 | |
| commit | 73fcd46ccaaf750c9f4d3bdd10dc53db1b15a89b (patch) | |
| tree | 2f50dc432bb26992459e1c1830d83a5937fbc369 | |
| parent | 706c84fd20b6e99dffd6827a0d8259d2f656c2a7 (diff) | |
| parent | cf44275a592110ab770dd850f7ab5761c73ac9e5 (diff) | |
Merge "Force associated devices to be strongly typed and expose AssociatedDevice."
| -rw-r--r-- | core/api/current.txt | 11 | ||||
| -rw-r--r-- | core/java/android/companion/AssociatedDevice.java | 47 | ||||
| -rw-r--r-- | core/java/android/companion/AssociationInfo.java | 15 |
3 files changed, 53 insertions, 20 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index 164e14a1bc39..19b476a5c5a3 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -8956,9 +8956,18 @@ package android.appwidget { package android.companion { + public final class AssociatedDevice implements android.os.Parcelable { + method public int describeContents(); + method @Nullable public android.bluetooth.le.ScanResult getBleDevice(); + method @Nullable public android.bluetooth.BluetoothDevice getBluetoothDevice(); + method @Nullable public android.net.wifi.ScanResult getWifiDevice(); + method public void writeToParcel(@NonNull android.os.Parcel, int); + field @NonNull public static final android.os.Parcelable.Creator<android.companion.AssociatedDevice> CREATOR; + } + public final class AssociationInfo implements android.os.Parcelable { method public int describeContents(); - method @Nullable public android.os.Parcelable getAssociatedDevice(); + method @Nullable public android.companion.AssociatedDevice getAssociatedDevice(); method @Nullable public android.net.MacAddress getDeviceMacAddress(); method @Nullable public String getDeviceProfile(); method @Nullable public CharSequence getDisplayName(); diff --git a/core/java/android/companion/AssociatedDevice.java b/core/java/android/companion/AssociatedDevice.java index 3758cdb680b1..a8336615fde5 100644 --- a/core/java/android/companion/AssociatedDevice.java +++ b/core/java/android/companion/AssociatedDevice.java @@ -16,6 +16,7 @@ package android.companion; +import android.bluetooth.BluetoothDevice; import android.os.Parcel; import android.os.Parcelable; @@ -23,19 +24,14 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; /** - * Loose wrapper around device parcelable. Device can be one of three types: + * Container for device info from an association that is not self-managed. + * Device can be one of three types: * * <ul> * <li>for classic Bluetooth - {@link android.bluetooth.BluetoothDevice}</li> * <li>for Bluetooth LE - {@link android.bluetooth.le.ScanResult}</li> * <li>for WiFi - {@link android.net.wifi.ScanResult}</li> * </ul> - * - * This class serves as temporary wrapper to deliver a loosely-typed parcelable object from - * {@link com.android.companiondevicemanager.CompanionDeviceActivity} to the Companion app, - * and should only be used internally. - * - * @hide */ public final class AssociatedDevice implements Parcelable { private static final int CLASSIC_BLUETOOTH = 0; @@ -44,6 +40,7 @@ public final class AssociatedDevice implements Parcelable { @NonNull private final Parcelable mDevice; + /** @hide */ public AssociatedDevice(@NonNull Parcelable device) { mDevice = device; } @@ -54,11 +51,39 @@ public final class AssociatedDevice implements Parcelable { } /** - * Return device info. Cast to expected device type. + * Return bluetooth device info. Null if associated device is not a bluetooth device. + * @return Remote bluetooth device details containing MAC address. */ - @NonNull - public Parcelable getDevice() { - return mDevice; + @Nullable + public BluetoothDevice getBluetoothDevice() { + if (mDevice instanceof BluetoothDevice) { + return (BluetoothDevice) mDevice; + } + return null; + } + + /** + * Return bluetooth LE device info. Null if associated device is not a BLE device. + * @return BLE scan result containing details of detected BLE device. + */ + @Nullable + public android.bluetooth.le.ScanResult getBleDevice() { + if (mDevice instanceof android.bluetooth.le.ScanResult) { + return (android.bluetooth.le.ScanResult) mDevice; + } + return null; + } + + /** + * Return Wi-Fi device info. Null if associated device is not a Wi-Fi device. + * @return Wi-Fi scan result containing details of detected access point. + */ + @Nullable + public android.net.wifi.ScanResult getWifiDevice() { + if (mDevice instanceof android.net.wifi.ScanResult) { + return (android.net.wifi.ScanResult) mDevice; + } + return null; } @Override diff --git a/core/java/android/companion/AssociationInfo.java b/core/java/android/companion/AssociationInfo.java index 93964b3f4180..5fd39feceb23 100644 --- a/core/java/android/companion/AssociationInfo.java +++ b/core/java/android/companion/AssociationInfo.java @@ -164,20 +164,19 @@ public final class AssociationInfo implements Parcelable { /** * Companion device that was associated. Note that this field is not persisted across sessions. - * - * Cast to expected device type before use: + * Device can be one of the following types: * * <ul> - * <li>for classic Bluetooth - {@link android.bluetooth.BluetoothDevice}</li> - * <li>for Bluetooth LE - {@link android.bluetooth.le.ScanResult}</li> - * <li>for WiFi - {@link android.net.wifi.ScanResult}</li> + * <li>for classic Bluetooth - {@link AssociatedDevice#getBluetoothDevice()}</li> + * <li>for Bluetooth LE - {@link AssociatedDevice#getBleDevice()}</li> + * <li>for WiFi - {@link AssociatedDevice#getWifiDevice()}</li> * </ul> * * @return the companion device that was associated, or {@code null} if the device is - * self-managed. + * self-managed or this association info was retrieved from persistent storage. */ - public @Nullable Parcelable getAssociatedDevice() { - return mAssociatedDevice == null ? null : mAssociatedDevice.getDevice(); + public @Nullable AssociatedDevice getAssociatedDevice() { + return mAssociatedDevice; } /** |