1 /*
2  * Copyright (C) 2021 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.tv;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SuppressLint;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * A response of {@link BroadcastInfoRequest} for information retrieved from broadcast signal.
30  */
31 @SuppressLint("ParcelNotFinal")
32 public abstract class BroadcastInfoResponse implements Parcelable {
33     /** @hide */
34     @Retention(RetentionPolicy.SOURCE)
35     @IntDef({RESPONSE_RESULT_ERROR, RESPONSE_RESULT_OK, RESPONSE_RESULT_CANCEL})
36     public @interface ResponseResult {}
37 
38     /**
39      * Response result: error. This means the request can not be set up successfully.
40      */
41     public static final int RESPONSE_RESULT_ERROR = 1;
42     /**
43      * Response result: OK. This means the request is set up successfully and the related responses
44      * are normal responses.
45      */
46     public static final int RESPONSE_RESULT_OK = 2;
47     /**
48      * Response result: cancel. This means the request has been cancelled.
49      */
50     public static final int RESPONSE_RESULT_CANCEL = 3;
51 
52     public static final @NonNull Parcelable.Creator<BroadcastInfoResponse> CREATOR =
53             new Parcelable.Creator<BroadcastInfoResponse>() {
54                 @Override
55                 public BroadcastInfoResponse createFromParcel(Parcel source) {
56                     @TvInputManager.BroadcastInfoType int type = source.readInt();
57                     switch (type) {
58                         case TvInputManager.BROADCAST_INFO_TYPE_TS:
59                             return TsResponse.createFromParcelBody(source);
60                         case TvInputManager.BROADCAST_INFO_TYPE_TABLE:
61                             return TableResponse.createFromParcelBody(source);
62                         case TvInputManager.BROADCAST_INFO_TYPE_SECTION:
63                             return SectionResponse.createFromParcelBody(source);
64                         case TvInputManager.BROADCAST_INFO_TYPE_PES:
65                             return PesResponse.createFromParcelBody(source);
66                         case TvInputManager.BROADCAST_INFO_STREAM_EVENT:
67                             return StreamEventResponse.createFromParcelBody(source);
68                         case TvInputManager.BROADCAST_INFO_TYPE_DSMCC:
69                             return DsmccResponse.createFromParcelBody(source);
70                         case TvInputManager.BROADCAST_INFO_TYPE_COMMAND:
71                             return CommandResponse.createFromParcelBody(source);
72                         case TvInputManager.BROADCAST_INFO_TYPE_TIMELINE:
73                             return TimelineResponse.createFromParcelBody(source);
74                         default:
75                             throw new IllegalStateException(
76                                     "Unexpected broadcast info response type (value "
77                                             + type + ") in parcel.");
78                     }
79                 }
80 
81                 @Override
82                 public BroadcastInfoResponse[] newArray(int size) {
83                     return new BroadcastInfoResponse[size];
84                 }
85             };
86 
87     private final @TvInputManager.BroadcastInfoType int mType;
88     private final int mRequestId;
89     private final int mSequence;
90     private final @ResponseResult int mResponseResult;
91 
BroadcastInfoResponse(@vInputManager.BroadcastInfoType int type, int requestId, int sequence, @ResponseResult int responseResult)92     BroadcastInfoResponse(@TvInputManager.BroadcastInfoType int type, int requestId,
93             int sequence, @ResponseResult int responseResult) {
94         mType = type;
95         mRequestId = requestId;
96         mSequence = sequence;
97         mResponseResult = responseResult;
98     }
99 
BroadcastInfoResponse(@vInputManager.BroadcastInfoType int type, Parcel source)100     BroadcastInfoResponse(@TvInputManager.BroadcastInfoType int type, Parcel source) {
101         mType = type;
102         mRequestId = source.readInt();
103         mSequence = source.readInt();
104         mResponseResult = source.readInt();
105     }
106 
107     /**
108      * Gets the broadcast info type.
109      *
110      * <p>The type indicates what broadcast information is requested, such as broadcast table,
111      * PES (packetized Elementary Stream), TS (transport stream), etc. The type of the
112      * request and the related responses should be the same.
113      */
114     @TvInputManager.BroadcastInfoType
getType()115     public int getType() {
116         return mType;
117     }
118 
119     /**
120      * Gets the ID of the request.
121      *
122      * <p>The ID is used to associate the response with the request.
123      *
124      * @see android.media.tv.BroadcastInfoRequest#getRequestId()
125      */
getRequestId()126     public int getRequestId() {
127         return mRequestId;
128     }
129 
130     /**
131      * Gets the sequence number which indicates the order of related responses.
132      */
getSequence()133     public int getSequence() {
134         return mSequence;
135     }
136 
137     /**
138      * Gets the result for the response.
139      *
140      * @see #RESPONSE_RESULT_OK
141      * @see #RESPONSE_RESULT_ERROR
142      * @see #RESPONSE_RESULT_CANCEL
143      */
144     @ResponseResult
getResponseResult()145     public int getResponseResult() {
146         return mResponseResult;
147     }
148 
149     @Override
describeContents()150     public int describeContents() {
151         return 0;
152     }
153 
154     @Override
writeToParcel(@onNull Parcel dest, int flags)155     public void writeToParcel(@NonNull Parcel dest, int flags) {
156         dest.writeInt(mType);
157         dest.writeInt(mRequestId);
158         dest.writeInt(mSequence);
159         dest.writeInt(mResponseResult);
160     }
161 }
162