• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.settings.connecteddevice.virtual;
17 
18 import android.companion.AssociationInfo;
19 import android.content.Context;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import com.android.settings.R;
27 
28 import java.util.Objects;
29 
30 /** Parcelable representing a virtual device along with its association properties. */
31 class VirtualDeviceWrapper implements Parcelable {
32 
33     /** The CDM Association for this device. */
34     @NonNull
35     private final AssociationInfo mAssociationInfo;
36     /** The unique VDM identifier for the device, persisted even when the device is inactive. */
37     @NonNull
38     private final String mPersistentDeviceId;
39     /** The identifier for the device if it's active, Context.DEVICE_ID_INVALID otherwise. */
40     private int mDeviceId;
41 
VirtualDeviceWrapper(@onNull AssociationInfo associationInfo, @NonNull String persistentDeviceId, int deviceId)42     VirtualDeviceWrapper(@NonNull AssociationInfo associationInfo,
43             @NonNull String persistentDeviceId, int deviceId) {
44         mAssociationInfo = associationInfo;
45         mPersistentDeviceId = persistentDeviceId;
46         mDeviceId = deviceId;
47     }
48 
49     @NonNull
getAssociationInfo()50     AssociationInfo getAssociationInfo() {
51         return mAssociationInfo;
52     }
53 
54     @NonNull
getPersistentDeviceId()55     String getPersistentDeviceId() {
56         return mPersistentDeviceId;
57     }
58 
59     @NonNull
getDeviceName(Context context)60     CharSequence getDeviceName(Context context) {
61         return mAssociationInfo.getDisplayName() != null
62                 ? mAssociationInfo.getDisplayName()
63                 : context.getString(R.string.virtual_device_unknown);
64     }
65 
getDeviceId()66     int getDeviceId() {
67         return mDeviceId;
68     }
69 
setDeviceId(int deviceId)70     void setDeviceId(int deviceId) {
71         mDeviceId = deviceId;
72     }
73 
VirtualDeviceWrapper(Parcel in)74     private VirtualDeviceWrapper(Parcel in) {
75         mAssociationInfo = in.readTypedObject(AssociationInfo.CREATOR);
76         mPersistentDeviceId = in.readString8();
77         mDeviceId = in.readInt();
78     }
79 
80     @Override
writeToParcel(@onNull Parcel dest, int flags)81     public void writeToParcel(@NonNull Parcel dest, int flags) {
82         dest.writeTypedObject(mAssociationInfo, flags);
83         dest.writeString8(mPersistentDeviceId);
84         dest.writeInt(mDeviceId);
85     }
86 
87     @Override
equals(@ullable Object o)88     public boolean equals(@Nullable Object o) {
89         if (this == o) return true;
90         if (!(o instanceof VirtualDeviceWrapper that)) return false;
91         return Objects.equals(mAssociationInfo, that.mAssociationInfo)
92                 && Objects.equals(mPersistentDeviceId, that.mPersistentDeviceId)
93                 && mDeviceId == that.mDeviceId;
94     }
95 
96     @Override
hashCode()97     public int hashCode() {
98         return Objects.hash(mAssociationInfo, mPersistentDeviceId, mDeviceId);
99     }
100 
101     @Override
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     public static final Parcelable.Creator<VirtualDeviceWrapper> CREATOR =
107             new Parcelable.Creator<>() {
108                 @NonNull
109                 public VirtualDeviceWrapper createFromParcel(@NonNull Parcel in) {
110                     return new VirtualDeviceWrapper(in);
111                 }
112 
113                 @NonNull
114                 public VirtualDeviceWrapper[] newArray(int size) {
115                     return new VirtualDeviceWrapper[size];
116                 }
117             };
118 }
119