blob: 2fd52b8f4e7a6094a20c33574cf45e886b5b0b18 [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;
Jeff Browne08ae382012-09-07 20:36:36 -070036
37 public static final WifiDisplay[] EMPTY_ARRAY = new WifiDisplay[0];
38
39 public static final Creator<WifiDisplay> CREATOR = new Creator<WifiDisplay>() {
40 public WifiDisplay createFromParcel(Parcel in) {
41 String deviceAddress = in.readString();
42 String deviceName = in.readString();
Jeff Brown89d55462012-09-19 11:33:42 -070043 String deviceAlias = in.readString();
44 return new WifiDisplay(deviceAddress, deviceName, deviceAlias);
Jeff Browne08ae382012-09-07 20:36:36 -070045 }
46
47 public WifiDisplay[] newArray(int size) {
48 return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size];
49 }
50 };
51
Jeff Brown89d55462012-09-19 11:33:42 -070052 public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias) {
Jeff Browne08ae382012-09-07 20:36:36 -070053 if (deviceAddress == null) {
54 throw new IllegalArgumentException("deviceAddress must not be null");
55 }
56 if (deviceName == null) {
57 throw new IllegalArgumentException("deviceName must not be null");
58 }
59
60 mDeviceAddress = deviceAddress;
61 mDeviceName = deviceName;
Jeff Brown89d55462012-09-19 11:33:42 -070062 mDeviceAlias = deviceAlias;
Jeff Browne08ae382012-09-07 20:36:36 -070063 }
64
65 /**
66 * Gets the MAC address of the Wifi display device.
67 */
68 public String getDeviceAddress() {
69 return mDeviceAddress;
70 }
71
72 /**
73 * Gets the name of the Wifi display device.
74 */
75 public String getDeviceName() {
76 return mDeviceName;
77 }
78
Jeff Brown89d55462012-09-19 11:33:42 -070079 /**
80 * Gets the user-specified alias of the Wifi display device, or null if none.
81 * <p>
82 * The alias should be used in the UI whenever available. It is the value
83 * provided by the user when renaming the device.
84 * </p>
85 */
86 public String getDeviceAlias() {
87 return mDeviceAlias;
88 }
89
90 /**
91 * Gets the name to show in the UI.
92 * Uses the device alias if available, otherwise uses the device name.
93 */
94 public String getFriendlyDisplayName() {
95 return mDeviceAlias != null ? mDeviceAlias : mDeviceName;
96 }
97
Jeff Browne08ae382012-09-07 20:36:36 -070098 @Override
99 public boolean equals(Object o) {
100 return o instanceof WifiDisplay && equals((WifiDisplay)o);
101 }
102
103 public boolean equals(WifiDisplay other) {
104 return other != null
105 && mDeviceAddress.equals(other.mDeviceAddress)
Jeff Brown89d55462012-09-19 11:33:42 -0700106 && mDeviceName.equals(other.mDeviceName)
107 && Objects.equal(mDeviceAlias, other.mDeviceAlias);
Jeff Browne08ae382012-09-07 20:36:36 -0700108 }
109
Jeff Brown74da1092012-11-07 16:02:13 -0800110 /**
111 * Returns true if the other display is not null and has the same address as this one.
112 * Can be used to perform identity comparisons on displays ignoring properties
113 * that might change during a connection such as the name or alias.
114 */
115 public boolean hasSameAddress(WifiDisplay other) {
116 return other != null && mDeviceAddress.equals(other.mDeviceAddress);
117 }
118
Jeff Browne08ae382012-09-07 20:36:36 -0700119 @Override
120 public int hashCode() {
121 // The address on its own should be sufficiently unique for hashing purposes.
122 return mDeviceAddress.hashCode();
123 }
124
125 @Override
126 public void writeToParcel(Parcel dest, int flags) {
127 dest.writeString(mDeviceAddress);
128 dest.writeString(mDeviceName);
Jeff Brown89d55462012-09-19 11:33:42 -0700129 dest.writeString(mDeviceAlias);
Jeff Browne08ae382012-09-07 20:36:36 -0700130 }
131
132 @Override
133 public int describeContents() {
134 return 0;
135 }
136
137 // For debugging purposes only.
138 @Override
139 public String toString() {
Jeff Brown89d55462012-09-19 11:33:42 -0700140 String result = mDeviceName + " (" + mDeviceAddress + ")";
141 if (mDeviceAlias != null) {
142 result += ", alias " + mDeviceAlias;
143 }
144 return result;
Jeff Browne08ae382012-09-07 20:36:36 -0700145 }
146}