Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 22 | import libcore.util.Objects; |
| 23 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 35 | private final String mDeviceAlias; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 36 | private final boolean mIsAvailable; |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 37 | private final boolean mCanConnect; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 38 | private final boolean mIsRemembered; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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(); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 46 | String deviceAlias = in.readString(); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 47 | boolean isAvailable = (in.readInt() != 0); |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 48 | boolean canConnect = (in.readInt() != 0); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 49 | boolean isRemembered = (in.readInt() != 0); |
| 50 | return new WifiDisplay(deviceAddress, deviceName, deviceAlias, |
| 51 | isAvailable, canConnect, isRemembered); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | public WifiDisplay[] newArray(int size) { |
| 55 | return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size]; |
| 56 | } |
| 57 | }; |
| 58 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 59 | public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias, |
| 60 | boolean available, boolean canConnect, boolean remembered) { |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 70 | mDeviceAlias = deviceAlias; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 71 | mIsAvailable = available; |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 72 | mCanConnect = canConnect; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 73 | mIsRemembered = remembered; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 74 | } |
| 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 Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 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 | */ |
| 97 | public String getDeviceAlias() { |
| 98 | return mDeviceAlias; |
| 99 | } |
| 100 | |
| 101 | /** |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 102 | * 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 Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 110 | */ |
| 111 | public boolean canConnect() { |
| 112 | return mCanConnect; |
| 113 | } |
| 114 | |
| 115 | /** |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 116 | * Returns true if device has been remembered, false otherwise. |
| 117 | */ |
| 118 | public boolean isRemembered() { |
| 119 | return mIsRemembered; |
| 120 | } |
| 121 | |
| 122 | /** |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 123 | * 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 Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 130 | @Override |
| 131 | public boolean equals(Object o) { |
| 132 | return o instanceof WifiDisplay && equals((WifiDisplay)o); |
| 133 | } |
| 134 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 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 | */ |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 139 | public boolean equals(WifiDisplay other) { |
| 140 | return other != null |
| 141 | && mDeviceAddress.equals(other.mDeviceAddress) |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 142 | && mDeviceName.equals(other.mDeviceName) |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 143 | && Objects.equal(mDeviceAlias, other.mDeviceAlias); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 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 | */ |
| 151 | public boolean hasSameAddress(WifiDisplay other) { |
| 152 | return other != null && mDeviceAddress.equals(other.mDeviceAddress); |
| 153 | } |
| 154 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 155 | @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 Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 165 | dest.writeString(mDeviceAlias); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 166 | dest.writeInt(mIsAvailable ? 1 : 0); |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 167 | dest.writeInt(mCanConnect ? 1 : 0); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 168 | dest.writeInt(mIsRemembered ? 1 : 0); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public int describeContents() { |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | // For debugging purposes only. |
| 177 | @Override |
| 178 | public String toString() { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 179 | String result = mDeviceName + " (" + mDeviceAddress + ")"; |
| 180 | if (mDeviceAlias != null) { |
| 181 | result += ", alias " + mDeviceAlias; |
| 182 | } |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 183 | result += ", isAvailable " + mIsAvailable + ", canConnect " + mCanConnect |
| 184 | + ", isRemembered " + mIsRemembered; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 185 | return result; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |