1/*
2 * Copyright (C) 2012 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.hardware.display;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Describes the current global state of Wifi display connectivity, including the
28 * currently connected display and all available or remembered displays.
29 * <p>
30 * This object is immutable.
31 * </p>
32 *
33 * @hide
34 */
35public final class WifiDisplayStatus implements Parcelable {
36    private final int mFeatureState;
37    private final int mScanState;
38    private final int mActiveDisplayState;
39    private final WifiDisplay mActiveDisplay;
40    private final WifiDisplay[] mDisplays;
41
42    /** Session info needed for Miracast Certification */
43    private final WifiDisplaySessionInfo mSessionInfo;
44
45    /** Feature state: Wifi display is not available on this device. */
46    public static final int FEATURE_STATE_UNAVAILABLE = 0;
47    /** Feature state: Wifi display is disabled, probably because Wifi is disabled. */
48    public static final int FEATURE_STATE_DISABLED = 1;
49    /** Feature state: Wifi display is turned off in settings. */
50    public static final int FEATURE_STATE_OFF = 2;
51    /** Feature state: Wifi display is turned on in settings. */
52    public static final int FEATURE_STATE_ON = 3;
53
54    /** Scan state: Not currently scanning. */
55    public static final int SCAN_STATE_NOT_SCANNING = 0;
56    /** Scan state: Currently scanning. */
57    public static final int SCAN_STATE_SCANNING = 1;
58
59    /** Display state: Not connected. */
60    public static final int DISPLAY_STATE_NOT_CONNECTED = 0;
61    /** Display state: Connecting to active display. */
62    public static final int DISPLAY_STATE_CONNECTING = 1;
63    /** Display state: Connected to active display. */
64    public static final int DISPLAY_STATE_CONNECTED = 2;
65
66    public static final Creator<WifiDisplayStatus> CREATOR = new Creator<WifiDisplayStatus>() {
67        public WifiDisplayStatus createFromParcel(Parcel in) {
68            int featureState = in.readInt();
69            int scanState = in.readInt();
70            int activeDisplayState= in.readInt();
71
72            WifiDisplay activeDisplay = null;
73            if (in.readInt() != 0) {
74                activeDisplay = WifiDisplay.CREATOR.createFromParcel(in);
75            }
76
77            WifiDisplay[] displays = WifiDisplay.CREATOR.newArray(in.readInt());
78            for (int i = 0; i < displays.length; i++) {
79                displays[i] = WifiDisplay.CREATOR.createFromParcel(in);
80            }
81
82            WifiDisplaySessionInfo sessionInfo =
83                    WifiDisplaySessionInfo.CREATOR.createFromParcel(in);
84
85            return new WifiDisplayStatus(featureState, scanState, activeDisplayState,
86                    activeDisplay, displays, sessionInfo);
87        }
88
89        public WifiDisplayStatus[] newArray(int size) {
90            return new WifiDisplayStatus[size];
91        }
92    };
93
94    public WifiDisplayStatus() {
95        this(FEATURE_STATE_UNAVAILABLE, SCAN_STATE_NOT_SCANNING, DISPLAY_STATE_NOT_CONNECTED,
96                null, WifiDisplay.EMPTY_ARRAY, null);
97    }
98
99    public WifiDisplayStatus(int featureState, int scanState, int activeDisplayState,
100            WifiDisplay activeDisplay, WifiDisplay[] displays, WifiDisplaySessionInfo sessionInfo) {
101        if (displays == null) {
102            throw new IllegalArgumentException("displays must not be null");
103        }
104
105        mFeatureState = featureState;
106        mScanState = scanState;
107        mActiveDisplayState = activeDisplayState;
108        mActiveDisplay = activeDisplay;
109        mDisplays = displays;
110
111        mSessionInfo = (sessionInfo != null) ? sessionInfo : new WifiDisplaySessionInfo();
112    }
113
114    /**
115     * Returns the state of the Wifi display feature on this device.
116     * <p>
117     * The value of this property reflects whether the device supports the Wifi display,
118     * whether it has been enabled by the user and whether the prerequisites for
119     * connecting to displays have been met.
120     * </p>
121     */
122    public int getFeatureState() {
123        return mFeatureState;
124    }
125
126    /**
127     * Returns the current state of the Wifi display scan.
128     *
129     * @return One of: {@link #SCAN_STATE_NOT_SCANNING} or {@link #SCAN_STATE_SCANNING}.
130     */
131    public int getScanState() {
132        return mScanState;
133    }
134
135    /**
136     * Get the state of the currently active display.
137     *
138     * @return One of: {@link #DISPLAY_STATE_NOT_CONNECTED}, {@link #DISPLAY_STATE_CONNECTING},
139     * or {@link #DISPLAY_STATE_CONNECTED}.
140     */
141    public int getActiveDisplayState() {
142        return mActiveDisplayState;
143    }
144
145    /**
146     * Gets the Wifi display that is currently active.  It may be connecting or
147     * connected.
148     */
149    public WifiDisplay getActiveDisplay() {
150        return mActiveDisplay;
151    }
152
153    /**
154     * Gets the list of Wifi displays, returns a combined list of all available
155     * Wifi displays as reported by the most recent scan, and all remembered
156     * Wifi displays (not necessarily available at the time).
157     */
158    public WifiDisplay[] getDisplays() {
159        return mDisplays;
160    }
161
162    /**
163     * Gets the Wifi display session info (required for certification only)
164     */
165    public WifiDisplaySessionInfo getSessionInfo() {
166        return mSessionInfo;
167    }
168
169    @Override
170    public void writeToParcel(Parcel dest, int flags) {
171        dest.writeInt(mFeatureState);
172        dest.writeInt(mScanState);
173        dest.writeInt(mActiveDisplayState);
174
175        if (mActiveDisplay != null) {
176            dest.writeInt(1);
177            mActiveDisplay.writeToParcel(dest, flags);
178        } else {
179            dest.writeInt(0);
180        }
181
182        dest.writeInt(mDisplays.length);
183        for (WifiDisplay display : mDisplays) {
184            display.writeToParcel(dest, flags);
185        }
186
187        mSessionInfo.writeToParcel(dest, flags);
188    }
189
190    @Override
191    public int describeContents() {
192        return 0;
193    }
194
195    // For debugging purposes only.
196    @Override
197    public String toString() {
198        return "WifiDisplayStatus{featureState=" + mFeatureState
199                + ", scanState=" + mScanState
200                + ", activeDisplayState=" + mActiveDisplayState
201                + ", activeDisplay=" + mActiveDisplay
202                + ", displays=" + Arrays.toString(mDisplays)
203                + ", sessionInfo=" + mSessionInfo
204                + "}";
205    }
206}
207