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