Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cf44275a authored by Raphael Kim's avatar Raphael Kim
Browse files

Force associated devices to be strongly typed and expose AssociatedDevice.

Bug: 254279707
Test: atest CtsCompanionDeviceManagerUiAutomationTestCases
      atest CtsCompanionDeviceManagerCoreTestCases
Change-Id: I1a505132ff52b14b6838e6404ecafae483b4b623
parent 81defaa0
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -8948,9 +8948,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();
+36 −11
Original line number Diff line number Diff line
@@ -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
+7 −8
Original line number Diff line number Diff line
@@ -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;
    }

    /**