WifiDisplayStatus.java revision 180bbc71810496e280e9993177bfeddb3ad1f558
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 known displays.
27 * <p>
28 * This object is immutable.
29 * </p>
30 *
31 * @hide
32 */
33public final class WifiDisplayStatus implements Parcelable {
34    private final boolean mEnabled;
35    private final int mScanState;
36    private final int mActiveDisplayState;
37    private final WifiDisplay mActiveDisplay;
38    private final WifiDisplay[] mKnownDisplays;
39
40    public static final int SCAN_STATE_NOT_SCANNING = 0;
41    public static final int SCAN_STATE_SCANNING = 1;
42
43    public static final int DISPLAY_STATE_NOT_CONNECTED = 0;
44    public static final int DISPLAY_STATE_CONNECTING = 1;
45    public static final int DISPLAY_STATE_CONNECTED = 2;
46
47    public static final Creator<WifiDisplayStatus> CREATOR = new Creator<WifiDisplayStatus>() {
48        public WifiDisplayStatus createFromParcel(Parcel in) {
49            boolean enabled = (in.readInt() != 0);
50            int scanState = in.readInt();
51            int activeDisplayState= in.readInt();
52
53            WifiDisplay activeDisplay = null;
54            if (in.readInt() != 0) {
55                activeDisplay = WifiDisplay.CREATOR.createFromParcel(in);
56            }
57
58            WifiDisplay[] knownDisplays = WifiDisplay.CREATOR.newArray(in.readInt());
59            for (int i = 0; i < knownDisplays.length; i++) {
60                knownDisplays[i] = WifiDisplay.CREATOR.createFromParcel(in);
61            }
62
63            return new WifiDisplayStatus(enabled, scanState, activeDisplayState,
64                    activeDisplay, knownDisplays);
65        }
66
67        public WifiDisplayStatus[] newArray(int size) {
68            return new WifiDisplayStatus[size];
69        }
70    };
71
72    public WifiDisplayStatus() {
73        this(false, SCAN_STATE_NOT_SCANNING, DISPLAY_STATE_NOT_CONNECTED,
74                null, WifiDisplay.EMPTY_ARRAY);
75    }
76
77    public WifiDisplayStatus(boolean enabled, int scanState, int activeDisplayState,
78            WifiDisplay activeDisplay, WifiDisplay[] knownDisplays) {
79        if (knownDisplays == null) {
80            throw new IllegalArgumentException("knownDisplays must not be null");
81        }
82
83        mEnabled = enabled;
84        mScanState = scanState;
85        mActiveDisplayState = activeDisplayState;
86        mActiveDisplay = activeDisplay;
87        mKnownDisplays = knownDisplays;
88    }
89
90    /**
91     * Returns true if the Wifi display feature is enabled and available for use.
92     * <p>
93     * The value of this property reflects whether Wifi and Wifi P2P functions
94     * are enabled.  Enablement is not directly controllable by the user at this
95     * time, except indirectly such as by turning off Wifi altogether.
96     * </p>
97     */
98    public boolean isEnabled() {
99        return mEnabled;
100    }
101
102    /**
103     * Returns the current state of the Wifi display scan.
104     *
105     * @return One of: {@link #SCAN_STATE_NOT_SCANNING} or {@link #SCAN_STATE_SCANNING}.
106     */
107    public int getScanState() {
108        return mScanState;
109    }
110
111    /**
112     * Get the state of the currently active display.
113     *
114     * @return One of: {@link #DISPLAY_STATE_NOT_CONNECTED}, {@link #DISPLAY_STATE_CONNECTING},
115     * or {@link #DISPLAY_STATE_CONNECTED}.
116     */
117    public int getActiveDisplayState() {
118        return mActiveDisplayState;
119    }
120
121    /**
122     * Gets the Wifi display that is currently active.  It may be connecting or
123     * connected.
124     */
125    public WifiDisplay getActiveDisplay() {
126        return mActiveDisplay;
127    }
128
129    /**
130     * Gets the list of all known Wifi displays, never null.
131     */
132    public WifiDisplay[] getKnownDisplays() {
133        return mKnownDisplays;
134    }
135
136    @Override
137    public void writeToParcel(Parcel dest, int flags) {
138        dest.writeInt(mEnabled ? 1 : 0);
139        dest.writeInt(mScanState);
140        dest.writeInt(mActiveDisplayState);
141
142        if (mActiveDisplay != null) {
143            dest.writeInt(1);
144            mActiveDisplay.writeToParcel(dest, flags);
145        } else {
146            dest.writeInt(0);
147        }
148
149        dest.writeInt(mKnownDisplays.length);
150        for (WifiDisplay display : mKnownDisplays) {
151            display.writeToParcel(dest, flags);
152        }
153    }
154
155    @Override
156    public int describeContents() {
157        return 0;
158    }
159
160    // For debugging purposes only.
161    @Override
162    public String toString() {
163        return "WifiDisplayStatus{enabled=" + mEnabled
164                + ", scanState=" + mScanState
165                + ", activeDisplayState=" + mActiveDisplayState
166                + ", activeDisplay=" + mActiveDisplay
167                + ", knownDisplays=" + Arrays.toString(mKnownDisplays)
168                + "}";
169    }
170}
171