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.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /**
26  * A response for Section from broadcast signal.
27  */
28 public final class SectionResponse extends BroadcastInfoResponse implements Parcelable {
29     private static final @TvInputManager.BroadcastInfoType int RESPONSE_TYPE =
30             TvInputManager.BROADCAST_INFO_TYPE_SECTION;
31 
32     public static final @NonNull Parcelable.Creator<SectionResponse> CREATOR =
33             new Parcelable.Creator<SectionResponse>() {
34                 @Override
35                 public SectionResponse createFromParcel(Parcel source) {
36                     source.readInt();
37                     return createFromParcelBody(source);
38                 }
39 
40                 @Override
41                 public SectionResponse[] newArray(int size) {
42                     return new SectionResponse[size];
43                 }
44             };
45 
46     private final int mSessionId;
47     private final int mVersion;
48     private final Bundle mSessionData;
49 
createFromParcelBody(Parcel in)50     static SectionResponse createFromParcelBody(Parcel in) {
51         return new SectionResponse(in);
52     }
53 
SectionResponse(int requestId, int sequence, @ResponseResult int responseResult, int sessionId, int version, @Nullable Bundle sessionData)54     public SectionResponse(int requestId, int sequence, @ResponseResult int responseResult,
55             int sessionId, int version, @Nullable Bundle sessionData) {
56         super(RESPONSE_TYPE, requestId, sequence, responseResult);
57         mSessionId = sessionId;
58         mVersion = version;
59         mSessionData = sessionData;
60     }
61 
SectionResponse(Parcel source)62     SectionResponse(Parcel source) {
63         super(RESPONSE_TYPE, source);
64         mSessionId = source.readInt();
65         mVersion = source.readInt();
66         mSessionData = source.readBundle();
67     }
68 
69     /**
70      * Gets the Session Id of requested session.
71      */
getSessionId()72     public int getSessionId() {
73         return mSessionId;
74     }
75 
76     /**
77      * Gets the Version number of requested session. If it is null, value will be -1.
78      * <p>The consistency of version numbers between request and response depends on
79      * {@link BroadcastInfoRequest.RequestOption}. If the request has RequestOption value
80      * REQUEST_OPTION_AUTO_UPDATE, then the response may be set to the latest version which may be
81      * different from the version of the request. Otherwise, response with a different version from
82      * its request will be considered invalid.
83      */
getVersion()84     public int getVersion() {
85         return mVersion;
86     }
87 
88     /**
89      * Gets the raw data of session. The sessionData field represents payload data of the session
90      * after session header, which includes version and sessionId.
91      */
92     @NonNull
getSessionData()93     public Bundle getSessionData() {
94         return mSessionData;
95     }
96 
97     @Override
describeContents()98     public int describeContents() {
99         return 0;
100     }
101 
102     @Override
writeToParcel(@onNull Parcel dest, int flags)103     public void writeToParcel(@NonNull Parcel dest, int flags) {
104         super.writeToParcel(dest, flags);
105         dest.writeInt(mSessionId);
106         dest.writeInt(mVersion);
107         dest.writeBundle(mSessionData);
108     }
109 }
110