blob: af5a84e61116563761c52b3057eb0dfdafd561ac [file] [log] [blame]
Jeff Browne08ae382012-09-07 20:36:36 -07001/*
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
Jeff Brown89d55462012-09-19 11:33:42 -070022import libcore.util.Objects;
23
Jeff Browne08ae382012-09-07 20:36:36 -070024/**
25 * Describes the properties of a Wifi display.
26 * <p>
27 * This object is immutable.
28 * </p>
29 *
30 * @hide
31 */
32public final class WifiDisplay implements Parcelable {
33 private final String mDeviceAddress;
34 private final String mDeviceName;
Jeff Brown89d55462012-09-19 11:33:42 -070035 private final String mDeviceAlias;
Chong Zhangab87a632013-06-11 10:25:49 -070036 private final boolean mIsAvailable;
Chong Zhang21f60392013-06-04 18:01:46 -070037 private final boolean mCanConnect;
Chong Zhangab87a632013-06-11 10:25:49 -070038 private final boolean mIsRemembered;
Jeff Browne08ae382012-09-07 20:36:36 -070039
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();
Jeff Brown89d55462012-09-19 11:33:42 -070046 String deviceAlias = in.readString();
Chong Zhangab87a632013-06-11 10:25:49 -070047 boolean isAvailable = (in.readInt() != 0);
Chong Zhang21f60392013-06-04 18:01:46 -070048 boolean canConnect = (in.readInt() != 0);
Chong Zhangab87a632013-06-11 10:25:49 -070049 boolean isRemembered = (in.readInt() != 0);
50 return new WifiDisplay(deviceAddress, deviceName, deviceAlias,
51 isAvailable, canConnect, isRemembered);
Jeff Browne08ae382012-09-07 20:36:36 -070052 }
53
54 public WifiDisplay[] newArray(int size) {
55 return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size];
56 }
57 };
58
Chong Zhangab87a632013-06-11 10:25:49 -070059 public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias,
60 boolean available, boolean canConnect, boolean remembered) {
Jeff Browne08ae382012-09-07 20:36:36 -070061 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;
Jeff Brown89d55462012-09-19 11:33:42 -070070 mDeviceAlias = deviceAlias;
Chong Zhangab87a632013-06-11 10:25:49 -070071 mIsAvailable = available;
Chong Zhang21f60392013-06-04 18:01:46 -070072 mCanConnect = canConnect;
Chong Zhangab87a632013-06-11 10:25:49 -070073 mIsRemembered = remembered;
Jeff Browne08ae382012-09-07 20:36:36 -070074 }
75
76 /**
77 * Gets the MAC address of the Wifi display device.
78 */
79 public String getDeviceAddress() {
80 return mDeviceAddress;
81 }
82
83 /**
84 * Gets the name of the Wifi display device.
85 */
86 public String getDeviceName() {
87 return mDeviceName;
88 }
89
Jeff Brown89d55462012-09-19 11:33:42 -070090 /**
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 */
97 public String getDeviceAlias() {
98 return mDeviceAlias;
99 }
100
101 /**
Chong Zhangab87a632013-06-11 10:25:49 -0700102 * Returns true if device is available, false otherwise.
103 */
104 public boolean isAvailable() {
105 return mIsAvailable;
106 }
107
108 /**
109 * Returns true if device can be connected to (not in use), false otherwise.
Chong Zhang21f60392013-06-04 18:01:46 -0700110 */
111 public boolean canConnect() {
112 return mCanConnect;
113 }
114
115 /**
Chong Zhangab87a632013-06-11 10:25:49 -0700116 * Returns true if device has been remembered, false otherwise.
117 */
118 public boolean isRemembered() {
119 return mIsRemembered;
120 }
121
122 /**
Jeff Brown89d55462012-09-19 11:33:42 -0700123 * Gets the name to show in the UI.
124 * Uses the device alias if available, otherwise uses the device name.
125 */
126 public String getFriendlyDisplayName() {
127 return mDeviceAlias != null ? mDeviceAlias : mDeviceName;
128 }
129
Jeff Browne08ae382012-09-07 20:36:36 -0700130 @Override
131 public boolean equals(Object o) {
132 return o instanceof WifiDisplay && equals((WifiDisplay)o);
133 }
134
Chong Zhangab87a632013-06-11 10:25:49 -0700135 /**
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 */
Jeff Browne08ae382012-09-07 20:36:36 -0700139 public boolean equals(WifiDisplay other) {
140 return other != null
141 && mDeviceAddress.equals(other.mDeviceAddress)
Jeff Brown89d55462012-09-19 11:33:42 -0700142 && mDeviceName.equals(other.mDeviceName)
Chong Zhangab87a632013-06-11 10:25:49 -0700143 && Objects.equal(mDeviceAlias, other.mDeviceAlias);
Jeff Browne08ae382012-09-07 20:36:36 -0700144 }
145
Jeff Brown74da1092012-11-07 16:02:13 -0800146 /**
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 */
151 public boolean hasSameAddress(WifiDisplay other) {
152 return other != null && mDeviceAddress.equals(other.mDeviceAddress);
153 }
154
Jeff Browne08ae382012-09-07 20:36:36 -0700155 @Override
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
162 public void writeToParcel(Parcel dest, int flags) {
163 dest.writeString(mDeviceAddress);
164 dest.writeString(mDeviceName);
Jeff Brown89d55462012-09-19 11:33:42 -0700165 dest.writeString(mDeviceAlias);
Chong Zhangab87a632013-06-11 10:25:49 -0700166 dest.writeInt(mIsAvailable ? 1 : 0);
Chong Zhang21f60392013-06-04 18:01:46 -0700167 dest.writeInt(mCanConnect ? 1 : 0);
Chong Zhangab87a632013-06-11 10:25:49 -0700168 dest.writeInt(mIsRemembered ? 1 : 0);
Jeff Browne08ae382012-09-07 20:36:36 -0700169 }
170
171 @Override
172 public int describeContents() {
173 return 0;
174 }
175
176 // For debugging purposes only.
177 @Override
178 public String toString() {
Jeff Brown89d55462012-09-19 11:33:42 -0700179 String result = mDeviceName + " (" + mDeviceAddress + ")";
180 if (mDeviceAlias != null) {
181 result += ", alias " + mDeviceAlias;
182 }
Chong Zhangab87a632013-06-11 10:25:49 -0700183 result += ", isAvailable " + mIsAvailable + ", canConnect " + mCanConnect
184 + ", isRemembered " + mIsRemembered;
Jeff Brown89d55462012-09-19 11:33:42 -0700185 return result;
Jeff Browne08ae382012-09-07 20:36:36 -0700186 }
187}