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
17package android.media.tv;
18
19import android.annotation.SystemApi;
20import android.media.AudioManager;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.Log;
24
25/**
26 * Simple container for information about TV input hardware.
27 * Not for third-party developers.
28 *
29 * @hide
30 */
31@SystemApi
32public final class TvInputHardwareInfo implements Parcelable {
33    static final String TAG = "TvInputHardwareInfo";
34
35    // Match hardware/libhardware/include/hardware/tv_input.h
36    public static final int TV_INPUT_TYPE_OTHER_HARDWARE = 1;
37    public static final int TV_INPUT_TYPE_TUNER          = 2;
38    public static final int TV_INPUT_TYPE_COMPOSITE      = 3;
39    public static final int TV_INPUT_TYPE_SVIDEO         = 4;
40    public static final int TV_INPUT_TYPE_SCART          = 5;
41    public static final int TV_INPUT_TYPE_COMPONENT      = 6;
42    public static final int TV_INPUT_TYPE_VGA            = 7;
43    public static final int TV_INPUT_TYPE_DVI            = 8;
44    public static final int TV_INPUT_TYPE_HDMI           = 9;
45    public static final int TV_INPUT_TYPE_DISPLAY_PORT   = 10;
46
47    public static final Parcelable.Creator<TvInputHardwareInfo> CREATOR =
48            new Parcelable.Creator<TvInputHardwareInfo>() {
49        @Override
50        public TvInputHardwareInfo createFromParcel(Parcel source) {
51            try {
52                TvInputHardwareInfo info = new TvInputHardwareInfo();
53                info.readFromParcel(source);
54                return info;
55            } catch (Exception e) {
56                Log.e(TAG, "Exception creating TvInputHardwareInfo from parcel", e);
57                return null;
58            }
59        }
60
61        @Override
62        public TvInputHardwareInfo[] newArray(int size) {
63            return new TvInputHardwareInfo[size];
64        }
65    };
66
67    private int mDeviceId;
68    private int mType;
69    private int mAudioType;
70    private String mAudioAddress;
71    private int mHdmiPortId;
72
73    private TvInputHardwareInfo() {
74    }
75
76    public int getDeviceId() {
77        return mDeviceId;
78    }
79
80    public int getType() {
81        return mType;
82    }
83
84    public int getAudioType() {
85        return mAudioType;
86    }
87
88    public String getAudioAddress() {
89        return mAudioAddress;
90    }
91
92    public int getHdmiPortId() {
93        if (mType != TV_INPUT_TYPE_HDMI) {
94            throw new IllegalStateException();
95        }
96        return mHdmiPortId;
97    }
98
99    @Override
100    public String toString() {
101        StringBuilder b = new StringBuilder(128);
102        b.append("TvInputHardwareInfo {id=").append(mDeviceId);
103        b.append(", type=").append(mType);
104        b.append(", audio_type=").append(mAudioType);
105        b.append(", audio_addr=").append(mAudioAddress);
106        if (mType == TV_INPUT_TYPE_HDMI) {
107            b.append(", hdmi_port=").append(mHdmiPortId);
108        }
109        b.append("}");
110        return b.toString();
111    }
112
113    // Parcelable
114    @Override
115    public int describeContents() {
116        return 0;
117    }
118
119    @Override
120    public void writeToParcel(Parcel dest, int flags) {
121        dest.writeInt(mDeviceId);
122        dest.writeInt(mType);
123        dest.writeInt(mAudioType);
124        dest.writeString(mAudioAddress);
125        if (mType == TV_INPUT_TYPE_HDMI) {
126            dest.writeInt(mHdmiPortId);
127        }
128    }
129
130    public void readFromParcel(Parcel source) {
131        mDeviceId = source.readInt();
132        mType = source.readInt();
133        mAudioType = source.readInt();
134        mAudioAddress = source.readString();
135        if (mType == TV_INPUT_TYPE_HDMI) {
136            mHdmiPortId = source.readInt();
137        }
138    }
139
140    public static final class Builder {
141        private Integer mDeviceId = null;
142        private Integer mType = null;
143        private int mAudioType = AudioManager.DEVICE_NONE;
144        private String mAudioAddress = "";
145        private Integer mHdmiPortId = null;
146
147        public Builder() {
148        }
149
150        public Builder deviceId(int deviceId) {
151            mDeviceId = deviceId;
152            return this;
153        }
154
155        public Builder type(int type) {
156            mType = type;
157            return this;
158        }
159
160        public Builder audioType(int audioType) {
161            mAudioType = audioType;
162            return this;
163        }
164
165        public Builder audioAddress(String audioAddress) {
166            mAudioAddress = audioAddress;
167            return this;
168        }
169
170        public Builder hdmiPortId(int hdmiPortId) {
171            mHdmiPortId = hdmiPortId;
172            return this;
173        }
174
175        public TvInputHardwareInfo build() {
176            if (mDeviceId == null || mType == null) {
177                throw new UnsupportedOperationException();
178            }
179            if ((mType == TV_INPUT_TYPE_HDMI && mHdmiPortId == null) ||
180                    (mType != TV_INPUT_TYPE_HDMI && mHdmiPortId != null)) {
181                throw new UnsupportedOperationException();
182            }
183
184            TvInputHardwareInfo info = new TvInputHardwareInfo();
185            info.mDeviceId = mDeviceId;
186            info.mType = mType;
187            info.mAudioType = mAudioType;
188            if (info.mAudioType != AudioManager.DEVICE_NONE) {
189                info.mAudioAddress = mAudioAddress;
190            }
191            if (mHdmiPortId != null) {
192                info.mHdmiPortId = mHdmiPortId;
193            }
194            return info;
195        }
196    }
197}
198