1 /*
2  * Copyright (C) 2014 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 package android.hardware.hdmi;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import androidx.annotation.IntRange;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * Encapsulates HDMI port information. Contains the capability of the ports such as
32  * HDMI-CEC, MHL, ARC(Audio Return Channel), eARC and physical address assigned to each port.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class HdmiPortInfo implements Parcelable {
38     /** HDMI port type: Input */
39     public static final int PORT_INPUT = 0;
40 
41     /** HDMI port type: Output */
42     public static final int PORT_OUTPUT = 1;
43 
44     /**
45      * @hide
46      *
47      * @see HdmiPortInfo#getType()
48      */
49     @IntDef(prefix = { "PORT_" }, value = {
50             PORT_INPUT,
51             PORT_OUTPUT
52     })
53     @Retention(RetentionPolicy.SOURCE)
54     public @interface PortType {}
55 
56     private final int mId;
57     private final int mType;
58     private final int mAddress;
59     private final boolean mCecSupported;
60     private final boolean mArcSupported;
61     private final boolean mEarcSupported;
62     private final boolean mMhlSupported;
63 
64     /**
65      * Constructor.
66      *
67      * @param id identifier assigned to each port. 1 for HDMI OUT port 1
68      * @param type HDMI port input/output type
69      * @param address physical address of the port
70      * @param cec {@code true} if HDMI-CEC is supported on the port
71      * @param mhl {@code true} if MHL is supported on the port
72      * @param arc {@code true} if audio return channel is supported on the port
73      *
74      * @deprecated use {@link Builder()} instead
75      */
76     @Deprecated
HdmiPortInfo(int id, @PortType int type, @IntRange(from = 0) int address, boolean cec, boolean mhl, boolean arc)77     public HdmiPortInfo(int id, @PortType int type,
78             @IntRange(from = 0) int address, boolean cec, boolean mhl, boolean arc) {
79         mId = id;
80         mType = type;
81         mAddress = address;
82         mCecSupported = cec;
83         mArcSupported = arc;
84         mEarcSupported = false;
85         mMhlSupported = mhl;
86     }
87 
88     /**
89      * Converts an instance to a builder
90      *
91      * @hide
92      */
toBuilder()93     public Builder toBuilder() {
94         return new Builder(this);
95     }
96 
HdmiPortInfo(Builder builder)97     private HdmiPortInfo(Builder builder) {
98         this.mId = builder.mId;
99         this.mType = builder.mType;
100         this.mAddress = builder.mAddress;
101         this.mCecSupported = builder.mCecSupported;
102         this.mArcSupported = builder.mArcSupported;
103         this.mEarcSupported = builder.mEarcSupported;
104         this.mMhlSupported = builder.mMhlSupported;
105     }
106 
107     /**
108      * Returns the port id.
109      */
getId()110     public int getId() {
111         return mId;
112     }
113 
114     /**
115      * Returns the port type.
116      */
117     @PortType
getType()118     public int getType() {
119         return mType;
120     }
121 
122     /**
123      * Returns the port address.
124      */
125     @IntRange(from = 0)
getAddress()126     public int getAddress() {
127         return mAddress;
128     }
129 
130     /**
131      * Returns {@code true} if the port supports HDMI-CEC signaling.
132      */
isCecSupported()133     public boolean isCecSupported() {
134         return mCecSupported;
135     }
136 
137     /**
138      * Returns {@code true} if the port supports MHL signaling.
139      */
isMhlSupported()140     public boolean isMhlSupported() {
141         return mMhlSupported;
142     }
143 
144     /**
145      * Returns {@code true} if the port supports audio return channel.
146      */
isArcSupported()147     public boolean isArcSupported() {
148         return mArcSupported;
149     }
150 
151     /**
152      * Returns {@code true} if the port supports eARC.
153      */
isEarcSupported()154     public boolean isEarcSupported() {
155         return mEarcSupported;
156     }
157 
158     /**
159      * Describes the kinds of special objects contained in this Parcelable's
160      * marshalled representation.
161      */
162     @Override
describeContents()163     public int describeContents() {
164         return 0;
165     }
166 
167 
168     /**
169      * A helper class to deserialize {@link HdmiPortInfo} for a parcel.
170      */
171     public static final @android.annotation.NonNull Parcelable.Creator<HdmiPortInfo> CREATOR =
172             new Parcelable.Creator<HdmiPortInfo>() {
173                 @Override
174                 public HdmiPortInfo createFromParcel(Parcel source) {
175                     int id = source.readInt();
176                     int type = source.readInt();
177                     int address = source.readInt();
178                     boolean cec = (source.readInt() == 1);
179                     boolean arc = (source.readInt() == 1);
180                     boolean mhl = (source.readInt() == 1);
181                     boolean earc = (source.readInt() == 1);
182                     return new Builder(id, type, address)
183                             .setCecSupported(cec)
184                             .setArcSupported(arc)
185                             .setEarcSupported(earc)
186                             .setMhlSupported(mhl)
187                             .build();
188                 }
189 
190                 @Override
191                 public HdmiPortInfo[] newArray(int size) {
192                     return new HdmiPortInfo[size];
193                 }
194             };
195 
196     /**
197      * Serializes this object into a {@link Parcel}.
198      *
199      * @param dest The Parcel in which the object should be written.
200      * @param flags Additional flags about how the object should be written.
201      *        May be 0 or {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}.
202      * @hide
203      */
204     @SystemApi
205     @Override
writeToParcel(Parcel dest, int flags)206     public void writeToParcel(Parcel dest, int flags) {
207         dest.writeInt(mId);
208         dest.writeInt(mType);
209         dest.writeInt(mAddress);
210         dest.writeInt(mCecSupported ? 1 : 0);
211         dest.writeInt(mArcSupported ? 1 : 0);
212         dest.writeInt(mMhlSupported ? 1 : 0);
213         dest.writeInt(mEarcSupported ? 1 : 0);
214     }
215 
216     @NonNull
217     @Override
toString()218     public String toString() {
219         StringBuilder s = new StringBuilder();
220         s.append("port_id: ").append(mId).append(", ");
221         s.append("type: ").append((mType == PORT_INPUT) ? "HDMI_IN" : "HDMI_OUT").append(", ");
222         s.append("address: ").append(String.format("0x%04x", mAddress)).append(", ");
223         s.append("cec: ").append(mCecSupported).append(", ");
224         s.append("arc: ").append(mArcSupported).append(", ");
225         s.append("mhl: ").append(mMhlSupported).append(", ");
226         s.append("earc: ").append(mEarcSupported);
227         return s.toString();
228     }
229 
230     @Override
equals(@ullable Object o)231     public boolean equals(@Nullable Object o) {
232         if (!(o instanceof HdmiPortInfo)) {
233             return false;
234         }
235         final HdmiPortInfo other = (HdmiPortInfo) o;
236         return mId == other.mId && mType == other.mType && mAddress == other.mAddress
237                 && mCecSupported == other.mCecSupported && mArcSupported == other.mArcSupported
238                 && mMhlSupported == other.mMhlSupported && mEarcSupported == other.mEarcSupported;
239     }
240 
241     @Override
hashCode()242     public int hashCode() {
243         return java.util.Objects.hash(
244                 mId, mType, mAddress, mCecSupported, mArcSupported, mMhlSupported, mEarcSupported);
245     }
246 
247     /**
248      * Builder for {@link HdmiPortInfo} instances.
249      */
250     public static final class Builder {
251         // Required parameters
252         private int mId;
253         private int mType;
254         private int mAddress;
255 
256         // Optional parameters that are set to false by default.
257         private boolean mCecSupported;
258         private boolean mArcSupported;
259         private boolean mEarcSupported;
260         private boolean mMhlSupported;
261 
262         /**
263          * Constructor
264          *
265          * @param id      identifier assigned to each port. 1 for HDMI OUT port 1
266          * @param type    HDMI port input/output type
267          * @param address physical address of the port
268          * @throws IllegalArgumentException if the parameters are invalid
269          */
Builder(int id, @PortType int type, @IntRange(from = 0) int address)270         public Builder(int id, @PortType int type, @IntRange(from = 0) int address) {
271             if (type != PORT_INPUT && type != PORT_OUTPUT) {
272                 throw new IllegalArgumentException(
273                         "type should be " + PORT_INPUT + " or " + PORT_OUTPUT + ".");
274             }
275             if (address < 0) {
276                 throw new IllegalArgumentException("address should be positive.");
277             }
278             mId = id;
279             mType = type;
280             mAddress = address;
281         }
282 
Builder(@onNull HdmiPortInfo hdmiPortInfo)283         private Builder(@NonNull HdmiPortInfo hdmiPortInfo) {
284             mId = hdmiPortInfo.mId;
285             mType = hdmiPortInfo.mType;
286             mAddress = hdmiPortInfo.mAddress;
287             mCecSupported = hdmiPortInfo.mCecSupported;
288             mArcSupported = hdmiPortInfo.mArcSupported;
289             mEarcSupported = hdmiPortInfo.mEarcSupported;
290             mMhlSupported = hdmiPortInfo.mMhlSupported;
291         }
292 
293         /**
294          * Create a new {@link HdmiPortInfo} object.
295          */
296         @NonNull
build()297         public HdmiPortInfo build() {
298             return new HdmiPortInfo(this);
299         }
300 
301         /**
302          * Sets the value for whether the port supports HDMI-CEC signaling.
303          */
304         @NonNull
setCecSupported(boolean supported)305         public Builder setCecSupported(boolean supported) {
306             mCecSupported = supported;
307             return this;
308         }
309 
310         /**
311          * Sets the value for whether the port supports audio return channel.
312          */
313         @NonNull
setArcSupported(boolean supported)314         public Builder setArcSupported(boolean supported) {
315             mArcSupported = supported;
316             return this;
317         }
318 
319         /**
320          * Sets the value for whether the port supports eARC.
321          */
322         @NonNull
setEarcSupported(boolean supported)323         public Builder setEarcSupported(boolean supported) {
324             mEarcSupported = supported;
325             return this;
326         }
327 
328         /**
329          * Sets the value for whether the port supports MHL signaling.
330          */
331         @NonNull
setMhlSupported(boolean supported)332         public Builder setMhlSupported(boolean supported) {
333             mMhlSupported = supported;
334             return this;
335         }
336     }
337 }
338