1 /*
2  * Copyright (C) 2022 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.companion.virtual;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.Objects;
26 
27 /**
28  * Details of a particular virtual device.
29  *
30  * <p>Read-only device representation exposing the properties of an existing virtual device.
31  *
32  * <p class="note">Not to be confused with {@link VirtualDeviceManager.VirtualDevice}, which is used
33  * by the virtual device creator and allows them to manage the device.
34  */
35 public final class VirtualDevice implements Parcelable {
36 
37     private final int mId;
38     private final @Nullable String mName;
39 
40     /**
41      * Creates a new instance of {@link VirtualDevice}.
42      * Only to be used by the VirtualDeviceManagerService.
43      *
44      * @hide
45      */
VirtualDevice(int id, @Nullable String name)46     public VirtualDevice(int id, @Nullable String name) {
47         if (id <= Context.DEVICE_ID_DEFAULT) {
48             throw new IllegalArgumentException("VirtualDevice ID mist be greater than "
49                     + Context.DEVICE_ID_DEFAULT);
50         }
51         mId = id;
52         mName = name;
53     }
54 
VirtualDevice(@onNull Parcel parcel)55     private VirtualDevice(@NonNull Parcel parcel) {
56         mId = parcel.readInt();
57         mName = parcel.readString8();
58     }
59 
60     /**
61      * Returns the unique ID of the virtual device.
62      */
getDeviceId()63     public int getDeviceId() {
64         return mId;
65     }
66 
67     /**
68      * Returns the name of the virtual device (optionally) provided during its creation.
69      *
70      * @see VirtualDeviceParams.Builder#setName(String)
71      */
getName()72     public @Nullable String getName() {
73         return mName;
74     }
75 
76     @Override
describeContents()77     public int describeContents() {
78         return 0;
79     }
80 
81     @Override
writeToParcel(@onNull Parcel dest, int flags)82     public void writeToParcel(@NonNull Parcel dest, int flags) {
83         dest.writeInt(mId);
84         dest.writeString8(mName);
85     }
86 
87     @Override
equals(Object o)88     public boolean equals(Object o) {
89         if (this == o) {
90             return true;
91         }
92         if (!(o instanceof VirtualDevice)) {
93             return false;
94         }
95         VirtualDevice that = (VirtualDevice) o;
96         return mId == that.mId
97                 && Objects.equals(mName, that.mName);
98     }
99 
100     @Override
hashCode()101     public int hashCode() {
102         return Objects.hash(mId, mName);
103     }
104 
105     @Override
106     @NonNull
toString()107     public String toString() {
108         return "VirtualDevice("
109                 + " mId=" + mId
110                 + " mName=" + mName
111                 + ")";
112     }
113 
114     @NonNull
115     public static final Parcelable.Creator<VirtualDevice> CREATOR =
116             new Parcelable.Creator<VirtualDevice>() {
117                 public VirtualDevice createFromParcel(Parcel in) {
118                     return new VirtualDevice(in);
119                 }
120 
121                 public VirtualDevice[] newArray(int size) {
122                     return new VirtualDevice[size];
123                 }
124             };
125 }
126