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