1/*
2 * Copyright (C) 2013 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
17package android.media;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.ArrayList;
23
24/**
25 * Information available from MediaRouterService about the state perceived by
26 * a particular client and the routes that are available to it.
27 *
28 * Clients must not modify the contents of this object.
29 * @hide
30 */
31public final class MediaRouterClientState implements Parcelable {
32    /**
33     * A list of all known routes.
34     */
35    public final ArrayList<RouteInfo> routes;
36
37    /**
38     * The id of the current globally selected route, or null if none.
39     * Globally selected routes override any other route selections that applications
40     * may have made.  Used for remote displays.
41     */
42    public String globallySelectedRouteId;
43
44    public MediaRouterClientState() {
45        routes = new ArrayList<RouteInfo>();
46    }
47
48    MediaRouterClientState(Parcel src) {
49        routes = src.createTypedArrayList(RouteInfo.CREATOR);
50        globallySelectedRouteId = src.readString();
51    }
52
53    public RouteInfo getRoute(String id) {
54        final int count = routes.size();
55        for (int i = 0; i < count; i++) {
56            final RouteInfo route = routes.get(i);
57            if (route.id.equals(id)) {
58                return route;
59            }
60        }
61        return null;
62    }
63
64    @Override
65    public int describeContents() {
66        return 0;
67    }
68
69    @Override
70    public void writeToParcel(Parcel dest, int flags) {
71        dest.writeTypedList(routes);
72        dest.writeString(globallySelectedRouteId);
73    }
74
75    @Override
76    public String toString() {
77        return "MediaRouterClientState{ globallySelectedRouteId="
78                + globallySelectedRouteId + ", routes=" + routes.toString() + " }";
79    }
80
81    public static final Parcelable.Creator<MediaRouterClientState> CREATOR =
82            new Parcelable.Creator<MediaRouterClientState>() {
83        @Override
84        public MediaRouterClientState createFromParcel(Parcel in) {
85            return new MediaRouterClientState(in);
86        }
87
88        @Override
89        public MediaRouterClientState[] newArray(int size) {
90            return new MediaRouterClientState[size];
91        }
92    };
93
94    public static final class RouteInfo implements Parcelable {
95        public String id;
96        public String name;
97        public String description;
98        public int supportedTypes;
99        public boolean enabled;
100        public int statusCode;
101        public int playbackType;
102        public int playbackStream;
103        public int volume;
104        public int volumeMax;
105        public int volumeHandling;
106        public int presentationDisplayId;
107        public @MediaRouter.RouteInfo.DeviceType int deviceType;
108
109        public RouteInfo(String id) {
110            this.id = id;
111            enabled = true;
112            statusCode = MediaRouter.RouteInfo.STATUS_NONE;
113            playbackType = MediaRouter.RouteInfo.PLAYBACK_TYPE_REMOTE;
114            playbackStream = -1;
115            volumeHandling = MediaRouter.RouteInfo.PLAYBACK_VOLUME_FIXED;
116            presentationDisplayId = -1;
117            deviceType = MediaRouter.RouteInfo.DEVICE_TYPE_UNKNOWN;
118        }
119
120        public RouteInfo(RouteInfo other) {
121            id = other.id;
122            name = other.name;
123            description = other.description;
124            supportedTypes = other.supportedTypes;
125            enabled = other.enabled;
126            statusCode = other.statusCode;
127            playbackType = other.playbackType;
128            playbackStream = other.playbackStream;
129            volume = other.volume;
130            volumeMax = other.volumeMax;
131            volumeHandling = other.volumeHandling;
132            presentationDisplayId = other.presentationDisplayId;
133            deviceType = other.deviceType;
134        }
135
136        RouteInfo(Parcel in) {
137            id = in.readString();
138            name = in.readString();
139            description = in.readString();
140            supportedTypes = in.readInt();
141            enabled = in.readInt() != 0;
142            statusCode = in.readInt();
143            playbackType = in.readInt();
144            playbackStream = in.readInt();
145            volume = in.readInt();
146            volumeMax = in.readInt();
147            volumeHandling = in.readInt();
148            presentationDisplayId = in.readInt();
149            deviceType = in.readInt();
150        }
151
152        @Override
153        public int describeContents() {
154            return 0;
155        }
156
157        @Override
158        public void writeToParcel(Parcel dest, int flags) {
159            dest.writeString(id);
160            dest.writeString(name);
161            dest.writeString(description);
162            dest.writeInt(supportedTypes);
163            dest.writeInt(enabled ? 1 : 0);
164            dest.writeInt(statusCode);
165            dest.writeInt(playbackType);
166            dest.writeInt(playbackStream);
167            dest.writeInt(volume);
168            dest.writeInt(volumeMax);
169            dest.writeInt(volumeHandling);
170            dest.writeInt(presentationDisplayId);
171            dest.writeInt(deviceType);
172        }
173
174        @Override
175        public String toString() {
176            return "RouteInfo{ id=" + id
177                    + ", name=" + name
178                    + ", description=" + description
179                    + ", supportedTypes=0x" + Integer.toHexString(supportedTypes)
180                    + ", enabled=" + enabled
181                    + ", statusCode=" + statusCode
182                    + ", playbackType=" + playbackType
183                    + ", playbackStream=" + playbackStream
184                    + ", volume=" + volume
185                    + ", volumeMax=" + volumeMax
186                    + ", volumeHandling=" + volumeHandling
187                    + ", presentationDisplayId=" + presentationDisplayId
188                    + ", deviceType=" + deviceType
189                    + " }";
190        }
191
192        @SuppressWarnings("hiding")
193        public static final Parcelable.Creator<RouteInfo> CREATOR =
194                new Parcelable.Creator<RouteInfo>() {
195            @Override
196            public RouteInfo createFromParcel(Parcel in) {
197                return new RouteInfo(in);
198            }
199
200            @Override
201            public RouteInfo[] newArray(int size) {
202                return new RouteInfo[size];
203            }
204        };
205    }
206}
207