Merge "AudioDeviceAddress: new class to store audio device information"
am: 4ea970a8ed

Change-Id: I9ff0a075f128c8e6aaa5c1c306264907f9be58ef
diff --git a/api/system-current.txt b/api/system-current.txt
index 23af14b..e80581a 100755
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -3702,6 +3702,19 @@
     method public android.media.AudioAttributes.Builder setInternalCapturePreset(int);
   }
 
+  public final class AudioDeviceAddress implements android.os.Parcelable {
+    ctor public AudioDeviceAddress(@NonNull android.media.AudioDeviceInfo);
+    ctor public AudioDeviceAddress(int, int, @NonNull String);
+    method public int describeContents();
+    method @NonNull public String getAddress();
+    method public int getRole();
+    method public int getType();
+    method public void writeToParcel(@NonNull android.os.Parcel, int);
+    field @NonNull public static final android.os.Parcelable.Creator<android.media.AudioDeviceAddress> CREATOR;
+    field public static final int ROLE_INPUT = 1; // 0x1
+    field public static final int ROLE_OUTPUT = 2; // 0x2
+  }
+
   public final class AudioFocusInfo implements android.os.Parcelable {
     method public int describeContents();
     method @NonNull public android.media.AudioAttributes getAttributes();
diff --git a/media/java/android/media/AudioDeviceAddress.aidl b/media/java/android/media/AudioDeviceAddress.aidl
new file mode 100644
index 0000000..6a1a7f7
--- /dev/null
+++ b/media/java/android/media/AudioDeviceAddress.aidl
@@ -0,0 +1,18 @@
+/* Copyright 2019, The Android Open Source Project
+**
+** 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 android.media;
+
+parcelable AudioDeviceAddress;
diff --git a/media/java/android/media/AudioDeviceAddress.java b/media/java/android/media/AudioDeviceAddress.java
new file mode 100644
index 0000000..415e77d
--- /dev/null
+++ b/media/java/android/media/AudioDeviceAddress.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * 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 android.media;
+
+import android.annotation.IntDef;
+import android.annotation.NonNull;
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.Objects;
+
+/**
+ * @hide
+ * Class to represent device type (speaker, headset...), address and role (input, output)
+ * of an audio device.
+ * <p>Unlike {@link AudioDeviceInfo}, the device
+ * doesn't need to be connected to be uniquely identified, it can
+ * for instance represent a specific A2DP headset even after a
+ * disconnection, whereas the corresponding <code>AudioDeviceInfo</code>
+ * would then be invalid.
+ * <p>While creating / obtaining an instance is not protected by a
+ * permission, APIs using one rely on MODIFY_AUDIO_ROUTING.
+ */
+@SystemApi
+public final class AudioDeviceAddress implements Parcelable {
+
+    /**
+     * A role identifying input devices, such as microphones.
+     */
+    public static final int ROLE_INPUT = AudioPort.ROLE_SOURCE;
+    /**
+     * A role identifying output devices, such as speakers or headphones.
+     */
+    public static final int ROLE_OUTPUT = AudioPort.ROLE_SINK;
+
+    /** @hide */
+    @IntDef(flag = false, prefix = "ROLE_", value = {
+            ROLE_INPUT, ROLE_OUTPUT }
+    )
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface Role {}
+
+    /**
+     * The audio device type, as defined in {@link AudioDeviceInfo}
+     */
+    private final @AudioDeviceInfo.AudioDeviceType int mType;
+    /**
+     * The unique address of the device. Some devices don't have addresses, only an empty string.
+     */
+    private final @NonNull String mAddress;
+
+    /**
+     * Is input or output device
+     */
+    private final @Role int mRole;
+
+    /**
+     * Constructor from a valid {@link AudioDeviceInfo}
+     * @param deviceInfo the connected audio device from which to obtain the device-identifying
+     *                   type and address.
+     */
+    public AudioDeviceAddress(@NonNull AudioDeviceInfo deviceInfo) {
+        Objects.requireNonNull(deviceInfo);
+        mRole = deviceInfo.isSink() ? ROLE_OUTPUT : ROLE_INPUT;
+        mType = deviceInfo.getType();
+        mAddress = deviceInfo.getAddress();
+    }
+
+    public AudioDeviceAddress(@Role int role, @AudioDeviceInfo.AudioDeviceType int type,
+                              @NonNull String address) {
+        Objects.requireNonNull(address);
+        if (role != ROLE_OUTPUT && role != ROLE_INPUT) {
+            throw new IllegalArgumentException("Invalid role " + role);
+        }
+        if (role == ROLE_OUTPUT && !AudioDeviceInfo.isValidAudioDeviceTypeOut(type)) {
+            throw new IllegalArgumentException("Invalid output device type " + type);
+        }
+        if (role == ROLE_INPUT && !AudioDeviceInfo.isValidAudioDeviceTypeIn(type)) {
+            throw new IllegalArgumentException("Invalid input device type " + type);
+        }
+
+        mRole = role;
+        mType = type;
+        mAddress = address;
+    }
+
+    public @Role int getRole() {
+        return mRole;
+    }
+
+    public @AudioDeviceInfo.AudioDeviceType int getType() {
+        return mType;
+    }
+
+    public @NonNull String getAddress() {
+        return mAddress;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(mRole, mType, mAddress);
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        AudioDeviceAddress that = (AudioDeviceAddress) o;
+        return ((mRole == that.mRole)
+                && (mType == that.mType)
+                && mAddress.equals(that.mAddress));
+    }
+
+    /** @hide */
+    public static String roleToString(@Role int role) {
+        return (role == ROLE_OUTPUT ? "output" : "input");
+    }
+
+    @Override
+    public String toString() {
+        return new String("AudioDeviceAddress:"
+                + " role:" + roleToString(mRole)
+                + " type:" + (mRole == ROLE_OUTPUT ? AudioSystem.getOutputDeviceName(
+                        AudioDeviceInfo.convertDeviceTypeToInternalDevice(mType))
+                        : AudioSystem.getInputDeviceName(
+                                AudioDeviceInfo.convertDeviceTypeToInternalDevice(mType)))
+                + " addr:" + mAddress);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(@NonNull Parcel dest, int flags) {
+        dest.writeInt(mRole);
+        dest.writeInt(mType);
+        dest.writeString(mAddress);
+    }
+
+    private AudioDeviceAddress(@NonNull Parcel in) {
+        mRole = in.readInt();
+        mType = in.readInt();
+        mAddress = in.readString();
+    }
+
+    public static final @NonNull Parcelable.Creator<AudioDeviceAddress> CREATOR =
+            new Parcelable.Creator<AudioDeviceAddress>() {
+        /**
+         * Rebuilds an AudioDeviceAddress previously stored with writeToParcel().
+         * @param p Parcel object to read the AudioDeviceAddress from
+         * @return a new AudioDeviceAddress created from the data in the parcel
+         */
+        public AudioDeviceAddress createFromParcel(Parcel p) {
+            return new AudioDeviceAddress(p);
+        }
+
+        public AudioDeviceAddress[] newArray(int size) {
+            return new AudioDeviceAddress[size];
+        }
+    };
+}
diff --git a/media/java/android/media/AudioDeviceInfo.java b/media/java/android/media/AudioDeviceInfo.java
index ca895fc..a39bc51 100644
--- a/media/java/android/media/AudioDeviceInfo.java
+++ b/media/java/android/media/AudioDeviceInfo.java
@@ -149,6 +149,55 @@
             TYPE_AUX_LINE,
             TYPE_IP,
             TYPE_BUS,
+            TYPE_HEARING_AID,
+            TYPE_BUILTIN_MIC,
+            TYPE_FM_TUNER,
+            TYPE_TV_TUNER }
+    )
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface AudioDeviceType {}    /** @hide */
+    @IntDef(flag = false, prefix = "TYPE", value = {
+            TYPE_BUILTIN_MIC,
+            TYPE_BLUETOOTH_SCO,
+            TYPE_BLUETOOTH_A2DP,
+            TYPE_WIRED_HEADSET,
+            TYPE_HDMI,
+            TYPE_TELEPHONY,
+            TYPE_DOCK,
+            TYPE_USB_ACCESSORY,
+            TYPE_USB_DEVICE,
+            TYPE_USB_HEADSET,
+            TYPE_FM_TUNER,
+            TYPE_TV_TUNER,
+            TYPE_LINE_ANALOG,
+            TYPE_LINE_DIGITAL,
+            TYPE_IP,
+            TYPE_BUS }
+    )
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface AudioDeviceTypeIn {}
+
+    /** @hide */
+    @IntDef(flag = false, prefix = "TYPE", value = {
+            TYPE_BUILTIN_EARPIECE,
+            TYPE_BUILTIN_SPEAKER,
+            TYPE_WIRED_HEADSET,
+            TYPE_WIRED_HEADPHONES,
+            TYPE_BLUETOOTH_SCO,
+            TYPE_BLUETOOTH_A2DP,
+            TYPE_HDMI,
+            TYPE_DOCK,
+            TYPE_USB_ACCESSORY,
+            TYPE_USB_DEVICE,
+            TYPE_USB_HEADSET,
+            TYPE_TELEPHONY,
+            TYPE_LINE_ANALOG,
+            TYPE_HDMI_ARC,
+            TYPE_LINE_DIGITAL,
+            TYPE_FM,
+            TYPE_AUX_LINE,
+            TYPE_IP,
+            TYPE_BUS,
             TYPE_HEARING_AID }
     )
     @Retention(RetentionPolicy.SOURCE)
@@ -183,6 +232,31 @@
         }
     }
 
+    /** @hide */
+    /*package*/ static boolean isValidAudioDeviceTypeIn(int type) {
+        switch (type) {
+            case TYPE_BUILTIN_MIC:
+            case TYPE_BLUETOOTH_SCO:
+            case TYPE_BLUETOOTH_A2DP:
+            case TYPE_WIRED_HEADSET:
+            case TYPE_HDMI:
+            case TYPE_TELEPHONY:
+            case TYPE_DOCK:
+            case TYPE_USB_ACCESSORY:
+            case TYPE_USB_DEVICE:
+            case TYPE_USB_HEADSET:
+            case TYPE_FM_TUNER:
+            case TYPE_TV_TUNER:
+            case TYPE_LINE_ANALOG:
+            case TYPE_LINE_DIGITAL:
+            case TYPE_IP:
+            case TYPE_BUS:
+                return true;
+            default:
+                return false;
+        }
+    }
+
     @Override
     public boolean equals(Object o) {
         if (this == o) return true;