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.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 import java.util.List;
28 
29 /**
30  * Defines the audio HAL version.
31  *
32  * @hide
33  */
34 @TestApi
35 public final class AudioHalVersionInfo implements Parcelable, Comparable<AudioHalVersionInfo> {
36     /**
37      * Indicate the audio HAL is implemented with HIDL (HAL interface definition language).
38      *
39      * @see <a href="https://source.android.com/docs/core/architecture/hidl/">HIDL</a>
40      *     <p>The value of AUDIO_HAL_TYPE_HIDL should match the value of {@link
41      *     android.media.AudioHalVersion.Type#HIDL}.
42      */
43     public static final int AUDIO_HAL_TYPE_HIDL = 0;
44 
45     /**
46      * Indicate the audio HAL is implemented with AIDL (Android Interface Definition Language).
47      *
48      * @see <a href="https://source.android.com/docs/core/architecture/aidl/">AIDL</a>
49      *     <p>The value of AUDIO_HAL_TYPE_AIDL should match the value of {@link
50      *     android.media.AudioHalVersion.Type#AIDL}.
51      */
52     public static final int AUDIO_HAL_TYPE_AIDL = 1;
53 
54     /** @hide */
55     @IntDef(
56             flag = false,
57             prefix = "AUDIO_HAL_TYPE_",
58             value = {AUDIO_HAL_TYPE_HIDL, AUDIO_HAL_TYPE_AIDL})
59     @Retention(RetentionPolicy.SOURCE)
60     public @interface AudioHalType {}
61 
62     /** AudioHalVersionInfo object of all valid Audio HAL versions. */
63     public static final @NonNull AudioHalVersionInfo AIDL_1_0 =
64             new AudioHalVersionInfo(AUDIO_HAL_TYPE_AIDL, 1 /* major */, 0 /* minor */);
65 
66     public static final @NonNull AudioHalVersionInfo HIDL_7_1 =
67             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 7 /* major */, 1 /* minor */);
68     public static final @NonNull AudioHalVersionInfo HIDL_7_0 =
69             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 7 /* major */, 0 /* minor */);
70     public static final @NonNull AudioHalVersionInfo HIDL_6_0 =
71             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 6 /* major */, 0 /* minor */);
72     public static final @NonNull AudioHalVersionInfo HIDL_5_0 =
73             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 5 /* major */, 0 /* minor */);
74     public static final @NonNull AudioHalVersionInfo HIDL_4_0 =
75             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 4 /* major */, 0 /* minor */);
76     public static final @NonNull AudioHalVersionInfo HIDL_2_0 =
77             new AudioHalVersionInfo(AUDIO_HAL_TYPE_HIDL, 2 /* major */, 0 /* minor */);
78 
79     /**
80      * List of all valid Audio HAL versions. This list need to be in sync with sAudioHALVersions
81      * defined in frameworks/av/media/libaudiohal/FactoryHal.cpp.
82      *
83      * Note: update {@link android.media.audio.cts.AudioHalVersionInfoTest} CTS accordingly if
84      * there is a change to supported versions.
85      */
86     public static final @NonNull List<AudioHalVersionInfo> VERSIONS =
87             List.of(AIDL_1_0, HIDL_7_1, HIDL_7_0, HIDL_6_0, HIDL_5_0);
88 
89     private static final String TAG = "AudioHalVersionInfo";
90     private AudioHalVersion mHalVersion = new AudioHalVersion();
91 
getHalType()92     public @AudioHalType int getHalType() {
93         return mHalVersion.type;
94     }
95 
getMajorVersion()96     public int getMajorVersion() {
97         return mHalVersion.major;
98     }
99 
getMinorVersion()100     public int getMinorVersion() {
101         return mHalVersion.minor;
102     }
103 
104     /** String representative of AudioHalVersion.Type */
typeToString(@udioHalType int type)105     private static @NonNull String typeToString(@AudioHalType int type) {
106         if (type == AudioHalVersion.Type.HIDL) {
107             return "HIDL";
108         } else if (type == AudioHalVersion.Type.AIDL) {
109             return "AIDL";
110         } else {
111             return "INVALID";
112         }
113     }
114 
115     /** String representative of type, major and minor */
toString(@udioHalType int type, int major, int minor)116     private static @NonNull String toString(@AudioHalType int type, int major, int minor) {
117         return typeToString(type) + ":" + Integer.toString(major) + "." + Integer.toString(minor);
118     }
119 
AudioHalVersionInfo(@udioHalType int type, int major, int minor)120     private AudioHalVersionInfo(@AudioHalType int type, int major, int minor) {
121         mHalVersion.type = type;
122         mHalVersion.major = major;
123         mHalVersion.minor = minor;
124     }
125 
AudioHalVersionInfo(Parcel in)126     private AudioHalVersionInfo(Parcel in) {
127         mHalVersion = in.readTypedObject(AudioHalVersion.CREATOR);
128     }
129 
130     /** String representative of this (AudioHalVersionInfo) object */
131     @Override
toString()132     public String toString() {
133         return toString(mHalVersion.type, mHalVersion.major, mHalVersion.minor);
134     }
135 
136     /**
137      * Compare two HAL versions by comparing their index in VERSIONS.
138      *
139      * <p>Normally all AudioHalVersionInfo object to compare should exist in the VERSIONS list. If
140      * both candidates exist in the VERSIONS list, smaller index means newer. Any candidate not
141      * exist in the VERSIONS list will be considered to be oldest version.
142      *
143      * @return 0 if the HAL version is the same as the other HAL version. Positive if the HAL
144      *     version is newer than the other HAL version. Negative if the HAL version is older than
145      *     the other version.
146      */
147     @Override
compareTo(@onNull AudioHalVersionInfo other)148     public int compareTo(@NonNull AudioHalVersionInfo other) {
149         int indexOther = VERSIONS.indexOf(other);
150         int indexThis = VERSIONS.indexOf(this);
151         if (indexThis < 0 || indexOther < 0) {
152             return indexThis - indexOther;
153         }
154         return indexOther - indexThis;
155     }
156 
157     @Override
describeContents()158     public int describeContents() {
159         return 0;
160     }
161 
162     @Override
writeToParcel(@onNull android.os.Parcel out, int flag)163     public void writeToParcel(@NonNull android.os.Parcel out, int flag) {
164         out.writeTypedObject(mHalVersion, flag);
165     }
166 
167     public static final @NonNull Parcelable.Creator<AudioHalVersionInfo> CREATOR =
168             new Parcelable.Creator<AudioHalVersionInfo>() {
169                 @Override
170                 public AudioHalVersionInfo createFromParcel(@NonNull Parcel in) {
171                     return new AudioHalVersionInfo(in);
172                 }
173 
174                 @Override
175                 public AudioHalVersionInfo[] newArray(int size) {
176                     return new AudioHalVersionInfo[size];
177                 }
178             };
179 }
180