Home | History | Annotate | Download | only in display
      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     @Override
    111     public int hashCode() {
    112         // The address on its own should be sufficiently unique for hashing purposes.
    113         return mDeviceAddress.hashCode();
    114     }
    115 
    116     @Override
    117     public void writeToParcel(Parcel dest, int flags) {
    118         dest.writeString(mDeviceAddress);
    119         dest.writeString(mDeviceName);
    120         dest.writeString(mDeviceAlias);
    121     }
    122 
    123     @Override
    124     public int describeContents() {
    125         return 0;
    126     }
    127 
    128     // For debugging purposes only.
    129     @Override
    130     public String toString() {
    131         String result = mDeviceName + " (" + mDeviceAddress + ")";
    132         if (mDeviceAlias != null) {
    133             result += ", alias " + mDeviceAlias;
    134         }
    135         return result;
    136     }
    137 }
    138