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