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.car.trust;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.util.Objects;
25 
26 
27 /**
28  * Contains basic info of a trusted device.
29  *
30  * @hide
31  * @deprecated Adding a trusted device is no longer a supported feature and these APIs will be
32  * removed in the next Android release.
33  */
34 @Deprecated
35 @SystemApi
36 public final class TrustedDeviceInfo implements Parcelable {
37 
38     // TODO(b/124052887)
39     public static final String DEFAULT_NAME = "Default";
40     private static final String DEVICE_INFO_DELIMITER = ",";
41     private final long mHandle;
42     private final String mAddress;
43     private final String mName;
44 
TrustedDeviceInfo(long handle, @NonNull String address, @NonNull String name)45     public TrustedDeviceInfo(long handle, @NonNull String address, @NonNull String name) {
46         mHandle = handle;
47         mAddress = address;
48         mName = name;
49     }
50 
51     /**
52      * Returns the handle of current device
53      *
54      * @return handle which is unique for every device
55      */
getHandle()56     public long getHandle() {
57         return mHandle;
58     }
59 
60     /**
61      * Get local device name of current device
62      *
63      * @return local device name
64      */
65     @NonNull
getName()66     public String getName() {
67         return mName;
68     }
69 
70     /**
71      * Get MAC address of current device
72      *
73      * @return MAC address
74      */
75     @NonNull
getAddress()76     public String getAddress() {
77         return mAddress;
78     }
79 
TrustedDeviceInfo(Parcel in)80     public TrustedDeviceInfo(Parcel in) {
81         mHandle = in.readLong();
82         mName = in.readString();
83         mAddress = in.readString();
84     }
85 
86     @Override
describeContents()87     public int describeContents() {
88         return 0;
89     }
90 
91     @Override
writeToParcel(Parcel dest, int flags)92     public void writeToParcel(Parcel dest, int flags) {
93         dest.writeLong(mHandle);
94         dest.writeString(mName);
95         dest.writeString(mAddress);
96     }
97 
98 
99     @Override
toString()100     public String toString() {
101         return String.format("TrustedDevice{ handle=%d. address=%s, name=%s }", mHandle, mAddress,
102                 mName);
103     }
104 
105     @Override
equals(Object obj)106     public boolean equals(Object obj) {
107         if (!(obj instanceof TrustedDeviceInfo)) {
108             return false;
109         }
110         // If the handles of two devices are the same, then they will be considered as equals.
111         TrustedDeviceInfo secondDevice = (TrustedDeviceInfo) obj;
112         return mHandle == secondDevice.getHandle();
113     }
114 
115     @Override
hashCode()116     public int hashCode() {
117         return Objects.hash(mHandle);
118     }
119 
120     /**
121      * Serialize the trusted device info into string
122      *
123      * @return string contains current trusted device information with certain format
124      */
serialize()125     public String serialize() {
126         return String.join(DEVICE_INFO_DELIMITER, String.valueOf(mHandle), mAddress, mName);
127     }
128 
129     /**
130      * Deserialize the string to trusted device info
131      *
132      * @param deviceInfo string which contains trusted device info, should be originally generated
133      *                   by serialize method
134      * @return TrustedDeviceInfo object constructed from the trusted device info in the string
135      */
deserialize(String deviceInfo)136     public static TrustedDeviceInfo deserialize(String deviceInfo) {
137         String[] res = deviceInfo.split(DEVICE_INFO_DELIMITER);
138         return new TrustedDeviceInfo(Long.valueOf(res[0]), res[1], res[2]);
139     }
140 
141     public static final Creator CREATOR = new Creator() {
142         public TrustedDeviceInfo createFromParcel(Parcel in) {
143             return new TrustedDeviceInfo(in);
144         }
145 
146         public TrustedDeviceInfo[] newArray(int size) {
147             return new TrustedDeviceInfo[size];
148         }
149     };
150 }
151