1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.TestApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import java.util.List; 26 27 /** 28 * Defines the audio HAL version. 29 * 30 * @hide 31 */ 32 @TestApi 33 public final class AudioHalVersionInfo implements Parcelable, Comparable<AudioHalVersionInfo> { 34 /** 35 * Indicate the audio HAL is implemented with HIDL (HAL interface definition language). 36 * 37 * @see <a href="https://source.android.com/docs/core/architecture/hidl/">HIDL</a> 38 * <p>The value of AUDIO_HAL_TYPE_HIDL should match the value of {@link 39 * android.media.AudioHalVersion.Type#HIDL}. 40 */ 41 public static final int AUDIO_HAL_TYPE_HIDL = 0; 42 43 /** 44 * Indicate the audio HAL is implemented with AIDL (Android Interface Definition Language). 45 * 46 * @see <a href="https://source.android.com/docs/core/architecture/aidl/">AIDL</a> 47 * <p>The value of AUDIO_HAL_TYPE_AIDL should match the value of {@link 48 * android.media.AudioHalVersion.Type#AIDL}. 49 */ 50 public static final int AUDIO_HAL_TYPE_AIDL = 1; 51 52 /** @hide */ 53 @IntDef( 54 flag = false, 55 prefix = "AUDIO_HAL_TYPE_", 56 value = {AUDIO_HAL_TYPE_HIDL, AUDIO_HAL_TYPE_AIDL}) 57 public @interface AudioHalType {} 58 59 /** AudioHalVersionInfo object of all valid Audio HAL versions. */ 60 public static final @NonNull AudioHalVersionInfo AIDL_1_0 = 61 new AudioHalVersionInfo(AUDIO_HAL_TYPE_AIDL, 1 /* major */, 0 /* minor */); 62 63 public static final @NonNull AudioHalVersionInfo HIDL_7_1 = 64 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 7 /* major */, 1 /* minor */); 65 public static final @NonNull AudioHalVersionInfo HIDL_7_0 = 66 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 7 /* major */, 0 /* minor */); 67 public static final @NonNull AudioHalVersionInfo HIDL_6_0 = 68 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 6 /* major */, 0 /* minor */); 69 public static final @NonNull AudioHalVersionInfo HIDL_5_0 = 70 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 5 /* major */, 0 /* minor */); 71 public static final @NonNull AudioHalVersionInfo HIDL_4_0 = 72 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 4 /* major */, 0 /* minor */); 73 public static final @NonNull AudioHalVersionInfo HIDL_2_0 = 74 new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 2 /* major */, 0 /* minor */); 75 76 /** 77 * List of all valid Audio HAL versions. This list need to be in sync with sAudioHALVersions 78 * defined in frameworks/av/media/libaudiohal/FactoryHalHidl.cpp. 79 */ 80 // TODO: add AIDL_1_0 with sAudioHALVersions. 81 public static final @NonNull List<AudioHalVersionInfo> VERSIONS = 82 List.of(HIDL_7_1, HIDL_7_0, HIDL_6_0, HIDL_5_0, HIDL_4_0); 83 84 private static final String TAG = "AudioHalVersionInfo"; 85 private AudioHalVersion mHalVersion = new AudioHalVersion(); 86 getHalType()87 public @AudioHalType int getHalType() { 88 return mHalVersion.type; 89 } 90 getMajorVersion()91 public int getMajorVersion() { 92 return mHalVersion.major; 93 } 94 getMinorVersion()95 public int getMinorVersion() { 96 return mHalVersion.minor; 97 } 98 99 /** String representative of AudioHalVersion.Type */ typeToString(@udioHalType int type)100 private static @NonNull String typeToString(@AudioHalType int type) { 101 if (type == AudioHalVersion.Type.HIDL) { 102 return "HIDL"; 103 } else if (type == AudioHalVersion.Type.AIDL) { 104 return "AIDL"; 105 } else { 106 return "INVALID"; 107 } 108 } 109 110 /** String representative of type, major and minor */ toString(@udioHalType int type, int major, int minor)111 private static @NonNull String toString(@AudioHalType int type, int major, int minor) { 112 return typeToString(type) + ":" + Integer.toString(major) + "." + Integer.toString(minor); 113 } 114 AudioHalVersionInfo(@udioHalType int type, int major, int minor)115 private AudioHalVersionInfo(@AudioHalType int type, int major, int minor) { 116 mHalVersion.type = type; 117 mHalVersion.major = major; 118 mHalVersion.minor = minor; 119 } 120 AudioHalVersionInfo(Parcel in)121 private AudioHalVersionInfo(Parcel in) { 122 mHalVersion = in.readTypedObject(AudioHalVersion.CREATOR); 123 } 124 125 /** String representative of this (AudioHalVersionInfo) object */ 126 @Override toString()127 public String toString() { 128 return toString(mHalVersion.type, mHalVersion.major, mHalVersion.minor); 129 } 130 131 /** 132 * Compare two HAL versions by comparing their index in VERSIONS. 133 * 134 * <p>Normally all AudioHalVersionInfo object to compare should exist in the VERSIONS list. If 135 * both candidates exist in the VERSIONS list, smaller index means newer. Any candidate not 136 * exist in the VERSIONS list will be considered to be oldest version. 137 * 138 * @return 0 if the HAL version is the same as the other HAL version. Positive if the HAL 139 * version is newer than the other HAL version. Negative if the HAL version is older than 140 * the other version. 141 */ 142 @Override compareTo(@onNull AudioHalVersionInfo other)143 public int compareTo(@NonNull AudioHalVersionInfo other) { 144 int indexOther = VERSIONS.indexOf(other); 145 int indexThis = VERSIONS.indexOf(this); 146 if (indexThis < 0 || indexOther < 0) { 147 return indexThis - indexOther; 148 } 149 return indexOther - indexThis; 150 } 151 152 @Override describeContents()153 public int describeContents() { 154 return 0; 155 } 156 157 @Override writeToParcel(@onNull android.os.Parcel out, int flag)158 public void writeToParcel(@NonNull android.os.Parcel out, int flag) { 159 out.writeTypedObject(mHalVersion, flag); 160 } 161 162 public static final @NonNull Parcelable.Creator<AudioHalVersionInfo> CREATOR = 163 new Parcelable.Creator<AudioHalVersionInfo>() { 164 @Override 165 public AudioHalVersionInfo createFromParcel(@NonNull Parcel in) { 166 return new AudioHalVersionInfo(in); 167 } 168 169 @Override 170 public AudioHalVersionInfo[] newArray(int size) { 171 return new AudioHalVersionInfo[size]; 172 } 173 }; 174 } 175