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.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.SuppressLint;
23 import android.media.tv.flags.Flags;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * A request for the information retrieved from broadcast signal.
32  */
33 @SuppressLint("ParcelNotFinal")
34 public abstract class BroadcastInfoRequest implements Parcelable {
35     /** @hide */
36     @Retention(RetentionPolicy.SOURCE)
37     @IntDef({REQUEST_OPTION_REPEAT, REQUEST_OPTION_AUTO_UPDATE,
38             REQUEST_OPTION_ONEWAY, REQUEST_OPTION_ONESHOT})
39     public @interface RequestOption {}
40 
41     /**
42      * Request option: repeat.
43      * <p>With this option, a response is sent when related broadcast information is detected,
44      * even if the same information has been sent previously.
45      */
46     public static final int REQUEST_OPTION_REPEAT = 0;
47     /**
48      * Request option: auto update.
49      * <p>With this option, a response is sent only when broadcast information is detected for the
50      * first time, new values are detected.
51      */
52     public static final int REQUEST_OPTION_AUTO_UPDATE = 1;
53     /**
54      * Request option: one-way
55      * <p> With this option, no response is expected after sending the request.
56      */
57     @FlaggedApi(Flags.FLAG_TIAF_V_APIS)
58     public static final int REQUEST_OPTION_ONEWAY = 2;
59     /**
60      * Request option: one-shot
61      * <p> With this option, only one response will be given per request.
62      */
63     @FlaggedApi(Flags.FLAG_TIAF_V_APIS)
64     public static final int REQUEST_OPTION_ONESHOT = 3;
65 
66     public static final @NonNull Parcelable.Creator<BroadcastInfoRequest> CREATOR =
67             new Parcelable.Creator<BroadcastInfoRequest>() {
68                 @Override
69                 public BroadcastInfoRequest createFromParcel(Parcel source) {
70                     @TvInputManager.BroadcastInfoType int type = source.readInt();
71                     switch (type) {
72                         case TvInputManager.BROADCAST_INFO_TYPE_TS:
73                             return TsRequest.createFromParcelBody(source);
74                         case TvInputManager.BROADCAST_INFO_TYPE_TABLE:
75                             return TableRequest.createFromParcelBody(source);
76                         case TvInputManager.BROADCAST_INFO_TYPE_SECTION:
77                             return SectionRequest.createFromParcelBody(source);
78                         case TvInputManager.BROADCAST_INFO_TYPE_PES:
79                             return PesRequest.createFromParcelBody(source);
80                         case TvInputManager.BROADCAST_INFO_STREAM_EVENT:
81                             return StreamEventRequest.createFromParcelBody(source);
82                         case TvInputManager.BROADCAST_INFO_TYPE_DSMCC:
83                             return DsmccRequest.createFromParcelBody(source);
84                         case TvInputManager.BROADCAST_INFO_TYPE_COMMAND:
85                             return CommandRequest.createFromParcelBody(source);
86                         case TvInputManager.BROADCAST_INFO_TYPE_TIMELINE:
87                             return TimelineRequest.createFromParcelBody(source);
88                         case TvInputManager.BROADCAST_INFO_TYPE_SIGNALING_DATA:
89                             return SignalingDataRequest.createFromParcelBody(source);
90                         default:
91                             throw new IllegalStateException(
92                                     "Unexpected broadcast info request type (value "
93                                             + type + ") in parcel.");
94                     }
95                 }
96 
97                 @Override
98                 public BroadcastInfoRequest[] newArray(int size) {
99                     return new BroadcastInfoRequest[size];
100                 }
101             };
102 
103     private final @TvInputManager.BroadcastInfoType int mType;
104     private final int mRequestId;
105     private final @RequestOption int mOption;
106 
BroadcastInfoRequest(@vInputManager.BroadcastInfoType int type, int requestId, @RequestOption int option)107     BroadcastInfoRequest(@TvInputManager.BroadcastInfoType int type,
108             int requestId, @RequestOption int option) {
109         mType = type;
110         mRequestId = requestId;
111         mOption = option;
112     }
113 
BroadcastInfoRequest(@vInputManager.BroadcastInfoType int type, Parcel source)114     BroadcastInfoRequest(@TvInputManager.BroadcastInfoType int type, Parcel source) {
115         mType = type;
116         mRequestId = source.readInt();
117         mOption = source.readInt();
118     }
119 
120     /**
121      * Gets the broadcast info type.
122      *
123      * <p>The type indicates what broadcast information is requested, such as broadcast table,
124      * PES (packetized Elementary Stream), TS (transport stream), etc. The type of the
125      * request and the related responses should be the same.
126      */
127     @TvInputManager.BroadcastInfoType
getType()128     public int getType() {
129         return mType;
130     }
131 
132     /**
133      * Gets the ID of the request.
134      *
135      * <p>The ID is used to associate the response with the request.
136      *
137      * @see android.media.tv.BroadcastInfoResponse#getRequestId()
138      */
getRequestId()139     public int getRequestId() {
140         return mRequestId;
141     }
142 
143     /**
144      * Gets the request option of the request.
145      *
146      * @see #REQUEST_OPTION_REPEAT
147      * @see #REQUEST_OPTION_AUTO_UPDATE
148      */
149     @RequestOption
getOption()150     public int getOption() {
151         return mOption;
152     }
153 
154     @Override
describeContents()155     public int describeContents() {
156         return 0;
157     }
158 
159     @Override
writeToParcel(@onNull Parcel dest, int flags)160     public void writeToParcel(@NonNull Parcel dest, int flags) {
161         dest.writeInt(mType);
162         dest.writeInt(mRequestId);
163         dest.writeInt(mOption);
164     }
165 }
166