Dominik Laskowski | db84596 | 2019-01-27 21:20:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.view; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | |
| 24 | /** Display identifier that is stable across reboots. |
| 25 | * |
| 26 | * @hide |
| 27 | */ |
| 28 | public abstract class DisplayAddress implements Parcelable { |
| 29 | /** |
| 30 | * Creates an address for a physical display given its stable ID. |
| 31 | * |
| 32 | * A physical display ID is stable if the display can be identified using EDID information. |
| 33 | * |
| 34 | * @param physicalDisplayId A physical display ID. |
| 35 | * @return The {@link Physical} address, or {@code null} if the ID is not stable. |
| 36 | * @see SurfaceControl#getPhysicalDisplayIds |
| 37 | */ |
| 38 | @Nullable |
| 39 | public static Physical fromPhysicalDisplayId(long physicalDisplayId) { |
| 40 | final Physical address = new Physical(physicalDisplayId); |
| 41 | return address.getModel() == 0 ? null : address; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates an address for a network display given its MAC address. |
| 46 | * |
| 47 | * @param macAddress A MAC address in colon notation. |
| 48 | * @return The {@link Network} address. |
| 49 | */ |
| 50 | @NonNull |
| 51 | public static Network fromMacAddress(String macAddress) { |
| 52 | return new Network(macAddress); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Address for a physically connected display. |
| 57 | * |
| 58 | * A {@link Physical} address is represented by a 64-bit identifier combining the port and model |
| 59 | * of a display. The port, located in the least significant byte, uniquely identifies a physical |
| 60 | * connector on the device for display output like eDP or HDMI. The model, located in the upper |
| 61 | * bits, uniquely identifies a display model across manufacturers by encoding EDID information. |
| 62 | */ |
| 63 | public static final class Physical extends DisplayAddress { |
| 64 | private static final int PHYSICAL_DISPLAY_ID_MODEL_SHIFT = 8; |
| 65 | private static final int PORT_MASK = 0xFF; |
| 66 | |
| 67 | private final long mPhysicalDisplayId; |
| 68 | |
| 69 | /** |
| 70 | * Physical port to which the display is connected. |
| 71 | */ |
| 72 | public byte getPort() { |
| 73 | return (byte) mPhysicalDisplayId; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Model identifier unique across manufacturers. |
| 78 | */ |
| 79 | public long getModel() { |
| 80 | return mPhysicalDisplayId >>> PHYSICAL_DISPLAY_ID_MODEL_SHIFT; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean equals(Object other) { |
| 85 | return other instanceof Physical |
| 86 | && mPhysicalDisplayId == ((Physical) other).mPhysicalDisplayId; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public String toString() { |
| 91 | return new StringBuilder("{") |
| 92 | .append("port=").append(getPort() & PORT_MASK) |
| 93 | .append(", model=0x").append(Long.toHexString(getModel())) |
| 94 | .append("}") |
| 95 | .toString(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int hashCode() { |
| 100 | return Long.hashCode(mPhysicalDisplayId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void writeToParcel(Parcel out, int flags) { |
| 105 | out.writeLong(mPhysicalDisplayId); |
| 106 | } |
| 107 | |
| 108 | private Physical(long physicalDisplayId) { |
| 109 | mPhysicalDisplayId = physicalDisplayId; |
| 110 | } |
| 111 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 112 | public static final @android.annotation.NonNull Parcelable.Creator<Physical> CREATOR = |
Dominik Laskowski | db84596 | 2019-01-27 21:20:00 -0800 | [diff] [blame] | 113 | new Parcelable.Creator<Physical>() { |
| 114 | @Override |
| 115 | public Physical createFromParcel(Parcel in) { |
| 116 | return new Physical(in.readLong()); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public Physical[] newArray(int size) { |
| 121 | return new Physical[size]; |
| 122 | } |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Address for a network-connected display. |
| 128 | */ |
| 129 | public static final class Network extends DisplayAddress { |
| 130 | private final String mMacAddress; |
| 131 | |
| 132 | @Override |
| 133 | public boolean equals(Object other) { |
| 134 | return other instanceof Network && mMacAddress.equals(((Network) other).mMacAddress); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String toString() { |
| 139 | return mMacAddress; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public int hashCode() { |
| 144 | return mMacAddress.hashCode(); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void writeToParcel(Parcel out, int flags) { |
| 149 | out.writeString(mMacAddress); |
| 150 | } |
| 151 | |
| 152 | private Network(String macAddress) { |
| 153 | mMacAddress = macAddress; |
| 154 | } |
| 155 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 156 | public static final @android.annotation.NonNull Parcelable.Creator<Network> CREATOR = |
Dominik Laskowski | db84596 | 2019-01-27 21:20:00 -0800 | [diff] [blame] | 157 | new Parcelable.Creator<Network>() { |
| 158 | @Override |
| 159 | public Network createFromParcel(Parcel in) { |
| 160 | return new Network(in.readString()); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Network[] newArray(int size) { |
| 165 | return new Network[size]; |
| 166 | } |
| 167 | }; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public int describeContents() { |
| 172 | return 0; |
| 173 | } |
| 174 | } |