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 | |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 23 | import java.util.Objects; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 24 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 36 | private final String mDeviceAlias; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 37 | private final boolean mIsAvailable; |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 38 | private final boolean mCanConnect; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 39 | private final boolean mIsRemembered; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 40 | |
| 41 | public static final WifiDisplay[] EMPTY_ARRAY = new WifiDisplay[0]; |
| 42 | |
| 43 | public static final Creator<WifiDisplay> CREATOR = new Creator<WifiDisplay>() { |
| 44 | public WifiDisplay createFromParcel(Parcel in) { |
| 45 | String deviceAddress = in.readString(); |
| 46 | String deviceName = in.readString(); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 47 | String deviceAlias = in.readString(); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 48 | boolean isAvailable = (in.readInt() != 0); |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 49 | boolean canConnect = (in.readInt() != 0); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 50 | boolean isRemembered = (in.readInt() != 0); |
| 51 | return new WifiDisplay(deviceAddress, deviceName, deviceAlias, |
| 52 | isAvailable, canConnect, isRemembered); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public WifiDisplay[] newArray(int size) { |
| 56 | return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size]; |
| 57 | } |
| 58 | }; |
| 59 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 60 | public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias, |
| 61 | boolean available, boolean canConnect, boolean remembered) { |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 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; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 71 | mDeviceAlias = deviceAlias; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 72 | mIsAvailable = available; |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 73 | mCanConnect = canConnect; |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 74 | mIsRemembered = remembered; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Gets the MAC address of the Wifi display device. |
| 79 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 80 | @UnsupportedAppUsage |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 81 | public String getDeviceAddress() { |
| 82 | return mDeviceAddress; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the name of the Wifi display device. |
| 87 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 88 | @UnsupportedAppUsage |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 89 | public String getDeviceName() { |
| 90 | return mDeviceName; |
| 91 | } |
| 92 | |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 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 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 100 | @UnsupportedAppUsage |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 101 | public String getDeviceAlias() { |
| 102 | return mDeviceAlias; |
| 103 | } |
| 104 | |
| 105 | /** |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 106 | * Returns true if device is available, false otherwise. |
| 107 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 108 | @UnsupportedAppUsage |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 109 | public boolean isAvailable() { |
| 110 | return mIsAvailable; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * 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] | 115 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 116 | @UnsupportedAppUsage |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 117 | public boolean canConnect() { |
| 118 | return mCanConnect; |
| 119 | } |
| 120 | |
| 121 | /** |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 122 | * Returns true if device has been remembered, false otherwise. |
| 123 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 124 | @UnsupportedAppUsage |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 125 | public boolean isRemembered() { |
| 126 | return mIsRemembered; |
| 127 | } |
| 128 | |
| 129 | /** |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 130 | * Gets the name to show in the UI. |
| 131 | * Uses the device alias if available, otherwise uses the device name. |
| 132 | */ |
| 133 | public String getFriendlyDisplayName() { |
| 134 | return mDeviceAlias != null ? mDeviceAlias : mDeviceName; |
| 135 | } |
| 136 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 137 | @Override |
| 138 | public boolean equals(Object o) { |
| 139 | return o instanceof WifiDisplay && equals((WifiDisplay)o); |
| 140 | } |
| 141 | |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 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 | */ |
Mathew Inwood | bcbe440 | 2018-08-08 15:42:59 +0100 | [diff] [blame] | 146 | @UnsupportedAppUsage |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 147 | public boolean equals(WifiDisplay other) { |
| 148 | return other != null |
| 149 | && mDeviceAddress.equals(other.mDeviceAddress) |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 150 | && mDeviceName.equals(other.mDeviceName) |
Narayan Kamath | 607223f | 2018-02-19 14:09:02 +0000 | [diff] [blame] | 151 | && Objects.equals(mDeviceAlias, other.mDeviceAlias); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jeff Brown | 74da109 | 2012-11-07 16:02:13 -0800 | [diff] [blame] | 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 | */ |
| 159 | public boolean hasSameAddress(WifiDisplay other) { |
| 160 | return other != null && mDeviceAddress.equals(other.mDeviceAddress); |
| 161 | } |
| 162 | |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 163 | @Override |
| 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 |
| 170 | public void writeToParcel(Parcel dest, int flags) { |
| 171 | dest.writeString(mDeviceAddress); |
| 172 | dest.writeString(mDeviceName); |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 173 | dest.writeString(mDeviceAlias); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 174 | dest.writeInt(mIsAvailable ? 1 : 0); |
Chong Zhang | 21f6039 | 2013-06-04 18:01:46 -0700 | [diff] [blame] | 175 | dest.writeInt(mCanConnect ? 1 : 0); |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 176 | dest.writeInt(mIsRemembered ? 1 : 0); |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public int describeContents() { |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | // For debugging purposes only. |
| 185 | @Override |
| 186 | public String toString() { |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 187 | String result = mDeviceName + " (" + mDeviceAddress + ")"; |
| 188 | if (mDeviceAlias != null) { |
| 189 | result += ", alias " + mDeviceAlias; |
| 190 | } |
Chong Zhang | ab87a63 | 2013-06-11 10:25:49 -0700 | [diff] [blame] | 191 | result += ", isAvailable " + mIsAvailable + ", canConnect " + mCanConnect |
| 192 | + ", isRemembered " + mIsRemembered; |
Jeff Brown | 89d5546 | 2012-09-19 11:33:42 -0700 | [diff] [blame] | 193 | return result; |
Jeff Brown | e08ae38 | 2012-09-07 20:36:36 -0700 | [diff] [blame] | 194 | } |
| 195 | } |