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 22 import libcore.util.Objects; 23 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; 35 private final String mDeviceAlias; 36 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(); 43 String deviceAlias = in.readString(); 44 return new WifiDisplay(deviceAddress, deviceName, deviceAlias); 45 } 46 47 public WifiDisplay[] newArray(int size) { 48 return size == 0 ? EMPTY_ARRAY : new WifiDisplay[size]; 49 } 50 }; 51 52 public WifiDisplay(String deviceAddress, String deviceName, String deviceAlias) { 53 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; 62 mDeviceAlias = deviceAlias; 63 } 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 79 /** 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 98 @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) 106 && mDeviceName.equals(other.mDeviceName) 107 && Objects.equal(mDeviceAlias, other.mDeviceAlias); 108 } 109 110 /** 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 119 @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); 129 dest.writeString(mDeviceAlias); 130 } 131 132 @Override 133 public int describeContents() { 134 return 0; 135 } 136 137 // For debugging purposes only. 138 @Override 139 public String toString() { 140 String result = mDeviceName + " (" + mDeviceAddress + ")"; 141 if (mDeviceAlias != null) { 142 result += ", alias " + mDeviceAlias; 143 } 144 return result; 145 } 146 } 147