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.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * A response for Stream Event from broadcast signal.
26  */
27 public final class StreamEventResponse extends BroadcastInfoResponse implements Parcelable {
28     private static final @TvInputManager.BroadcastInfoType int RESPONSE_TYPE =
29             TvInputManager.BROADCAST_INFO_STREAM_EVENT;
30 
31     public static final @NonNull Parcelable.Creator<StreamEventResponse> CREATOR =
32             new Parcelable.Creator<StreamEventResponse>() {
33                 @Override
34                 public StreamEventResponse createFromParcel(Parcel source) {
35                     source.readInt();
36                     return createFromParcelBody(source);
37                 }
38 
39                 @Override
40                 public StreamEventResponse[] newArray(int size) {
41                     return new StreamEventResponse[size];
42                 }
43             };
44 
45     private final int mEventId;
46     private final long mNptMillis;
47     private final byte[] mData;
48 
createFromParcelBody(Parcel in)49     static StreamEventResponse createFromParcelBody(Parcel in) {
50         return new StreamEventResponse(in);
51     }
52 
StreamEventResponse(int requestId, int sequence, @ResponseResult int responseResult, int eventId, long nptMillis, @Nullable byte[] data)53     public StreamEventResponse(int requestId, int sequence, @ResponseResult int responseResult,
54             int eventId, long nptMillis, @Nullable byte[] data) {
55         super(RESPONSE_TYPE, requestId, sequence, responseResult);
56         mEventId = eventId;
57         mNptMillis = nptMillis;
58         mData = data;
59     }
60 
StreamEventResponse(@onNull Parcel source)61     private StreamEventResponse(@NonNull Parcel source) {
62         super(RESPONSE_TYPE, source);
63         mEventId = source.readInt();
64         mNptMillis = source.readLong();
65         int dataLength = source.readInt();
66         if (dataLength > 0) {
67             mData = new byte[dataLength];
68             source.readByteArray(mData);
69         } else {
70             mData = null;
71         }
72     }
73 
74     /**
75      * Returns the event ID.
76      */
getEventId()77     public int getEventId() {
78         return mEventId;
79     }
80 
81     /**
82      * Returns the NPT(Normal Play Time) value when the event occurred or will occur.
83      * <p>The time unit of NPT is millisecond.
84      */
getNptMillis()85     public long getNptMillis() {
86         return mNptMillis;
87     }
88 
89     /**
90      * Returns the application specific data.
91      */
92     @Nullable
getData()93     public byte[] getData() {
94         return mData;
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(mEventId);
106         dest.writeLong(mNptMillis);
107         if (mData != null && mData.length > 0) {
108             dest.writeInt(mData.length);
109             dest.writeByteArray(mData);
110         } else {
111             dest.writeInt(0);
112         }
113     }
114 }
115