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