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 
17 package android.hardware.display;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import libcore.util.Objects;
23 
24 /**
25  * Describes the properties of a Wifi display.
26  * <p>
27  * This object is immutable.
28  * </p>
29  *
30  * @hide
31  */
32 public final class WifiDisplay implements Parcelable {
33     private final String mDeviceAddress;
34     private final String mDeviceName;
35     private final String mDeviceAlias;
36     private final boolean mIsAvailable;
37     private final boolean mCanConnect;
38     private final boolean mIsRemembered;
39 
40     public static final WifiDisplay[] EMPTY_ARRAY = new WifiDisplay[0];
41 
42     public static final Creator<WifiDisplay> CREATOR = new Creator<WifiDisplay>() {
43         public WifiDisplay createFromParcel(Parcel in) {
44             String deviceAddress = in.readString();
45             String deviceName = in.readString();
46             String deviceAlias = in.readString();
47             boolean isAvailable = (in.readInt() != 0);
48             boolean canConnect = (in.readInt() != 0);
49             boolean isRemembered = (in.readInt() != 0);
50             return new WifiDisplay(deviceAddress, deviceName, deviceAlias,
51                     isAvailable, canConnect, isRemembered);
52         }
53 
54         public WifiDisplay[] newArray(int size) {
55             return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size];
56         }
57     };
58 
WifiDisplay(String deviceAddress, String deviceName, String deviceAlias, boolean available, boolean canConnect, boolean remembered)59     public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias,
60             boolean available, boolean canConnect, boolean remembered) {
61         if (deviceAddress == null) {
62             throw new IllegalArgumentException("deviceAddress must not be null");
63         }
64         if (deviceName == null) {
65             throw new IllegalArgumentException("deviceName must not be null");
66         }
67 
68         mDeviceAddress = deviceAddress;
69         mDeviceName = deviceName;
70         mDeviceAlias = deviceAlias;
71         mIsAvailable = available;
72         mCanConnect = canConnect;
73         mIsRemembered = remembered;
74     }
75 
76     /**
77      * Gets the MAC address of the Wifi display device.
78      */
getDeviceAddress()79     public String getDeviceAddress() {
80         return mDeviceAddress;
81     }
82 
83     /**
84      * Gets the name of the Wifi display device.
85      */
getDeviceName()86     public String getDeviceName() {
87         return mDeviceName;
88     }
89 
90     /**
91      * Gets the user-specified alias of the Wifi display device, or null if none.
92      * <p>
93      * The alias should be used in the UI whenever available.  It is the value
94      * provided by the user when renaming the device.
95      * </p>
96      */
getDeviceAlias()97     public String getDeviceAlias() {
98         return mDeviceAlias;
99     }
100 
101     /**
102      * Returns true if device is available, false otherwise.
103      */
isAvailable()104     public boolean isAvailable() {
105         return mIsAvailable;
106     }
107 
108     /**
109      * Returns true if device can be connected to (not in use), false otherwise.
110      */
canConnect()111     public boolean canConnect() {
112         return mCanConnect;
113     }
114 
115     /**
116      * Returns true if device has been remembered, false otherwise.
117      */
isRemembered()118     public boolean isRemembered() {
119         return mIsRemembered;
120     }
121 
122     /**
123      * Gets the name to show in the UI.
124      * Uses the device alias if available, otherwise uses the device name.
125      */
getFriendlyDisplayName()126     public String getFriendlyDisplayName() {
127         return mDeviceAlias != null ? mDeviceAlias : mDeviceName;
128     }
129 
130     @Override
equals(Object o)131     public boolean equals(Object o) {
132         return o instanceof WifiDisplay && equals((WifiDisplay)o);
133     }
134 
135     /**
136      * Returns true if the two displays have the same identity (address, name and alias).
137      * This method does not compare the current status of the displays.
138      */
equals(WifiDisplay other)139     public boolean equals(WifiDisplay other) {
140         return other != null
141                 && mDeviceAddress.equals(other.mDeviceAddress)
142                 && mDeviceName.equals(other.mDeviceName)
143                 && Objects.equal(mDeviceAlias, other.mDeviceAlias);
144     }
145 
146     /**
147      * Returns true if the other display is not null and has the same address as this one.
148      * Can be used to perform identity comparisons on displays ignoring properties
149      * that might change during a connection such as the name or alias.
150      */
hasSameAddress(WifiDisplay other)151     public boolean hasSameAddress(WifiDisplay other) {
152         return other != null && mDeviceAddress.equals(other.mDeviceAddress);
153     }
154 
155     @Override
hashCode()156     public int hashCode() {
157         // The address on its own should be sufficiently unique for hashing purposes.
158         return mDeviceAddress.hashCode();
159     }
160 
161     @Override
writeToParcel(Parcel dest, int flags)162     public void writeToParcel(Parcel dest, int flags) {
163         dest.writeString(mDeviceAddress);
164         dest.writeString(mDeviceName);
165         dest.writeString(mDeviceAlias);
166         dest.writeInt(mIsAvailable ? 1 : 0);
167         dest.writeInt(mCanConnect ? 1 : 0);
168         dest.writeInt(mIsRemembered ? 1 : 0);
169     }
170 
171     @Override
describeContents()172     public int describeContents() {
173         return 0;
174     }
175 
176     // For debugging purposes only.
177     @Override
toString()178     public String toString() {
179         String result = mDeviceName + " (" + mDeviceAddress + ")";
180         if (mDeviceAlias != null) {
181             result += ", alias " + mDeviceAlias;
182         }
183         result += ", isAvailable " + mIsAvailable + ", canConnect " + mCanConnect
184                 + ", isRemembered " + mIsRemembered;
185         return result;
186     }
187 }
188