• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.nearby;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * Represents a credential for Nearby Presence.
33  *
34  * @hide
35  */
36 @SystemApi
37 public abstract class PresenceCredential {
38     /** Private credential type. */
39     public static final int CREDENTIAL_TYPE_PRIVATE = 0;
40 
41     /** Public credential type. */
42     public static final int CREDENTIAL_TYPE_PUBLIC = 1;
43 
44     /**
45      * @hide *
46      */
47     @Retention(RetentionPolicy.SOURCE)
48     @IntDef({CREDENTIAL_TYPE_PUBLIC, CREDENTIAL_TYPE_PRIVATE})
49     public @interface CredentialType {}
50 
51     /** Unknown identity type. */
52     public static final int IDENTITY_TYPE_UNKNOWN = 0;
53 
54     /** Private identity type. */
55     public static final int IDENTITY_TYPE_PRIVATE = 1;
56     /** Provisioned identity type. */
57     public static final int IDENTITY_TYPE_PROVISIONED = 2;
58     /** Trusted identity type. */
59     public static final int IDENTITY_TYPE_TRUSTED = 3;
60 
61     /**
62      * @hide *
63      */
64     @Retention(RetentionPolicy.SOURCE)
65     @IntDef({
66         IDENTITY_TYPE_UNKNOWN,
67         IDENTITY_TYPE_PRIVATE,
68         IDENTITY_TYPE_PROVISIONED,
69         IDENTITY_TYPE_TRUSTED
70     })
71     public @interface IdentityType {}
72 
73     private final @CredentialType int mType;
74     private final @IdentityType int mIdentityType;
75     private final byte[] mSecretId;
76     private final byte[] mAuthenticityKey;
77     private final List<CredentialElement> mCredentialElements;
78 
PresenceCredential( @redentialType int type, @IdentityType int identityType, byte[] secretId, byte[] authenticityKey, List<CredentialElement> credentialElements)79     PresenceCredential(
80             @CredentialType int type,
81             @IdentityType int identityType,
82             byte[] secretId,
83             byte[] authenticityKey,
84             List<CredentialElement> credentialElements) {
85         mType = type;
86         mIdentityType = identityType;
87         mSecretId = secretId;
88         mAuthenticityKey = authenticityKey;
89         mCredentialElements = credentialElements;
90     }
91 
PresenceCredential(@redentialType int type, Parcel in)92     PresenceCredential(@CredentialType int type, Parcel in) {
93         mType = type;
94         mIdentityType = in.readInt();
95         mSecretId = new byte[in.readInt()];
96         in.readByteArray(mSecretId);
97         mAuthenticityKey = new byte[in.readInt()];
98         in.readByteArray(mAuthenticityKey);
99         mCredentialElements = new ArrayList<>();
100         in.readList(
101                 mCredentialElements,
102                 CredentialElement.class.getClassLoader(),
103                 CredentialElement.class);
104     }
105 
106     /** Returns the type of the credential. */
getType()107     public @CredentialType int getType() {
108         return mType;
109     }
110 
111     /** Returns the identity type of the credential. */
getIdentityType()112     public @IdentityType int getIdentityType() {
113         return mIdentityType;
114     }
115 
116     /** Returns the secret id of the credential. */
117     @NonNull
getSecretId()118     public byte[] getSecretId() {
119         return mSecretId;
120     }
121 
122     /** Returns the authenticity key of the credential. */
123     @NonNull
getAuthenticityKey()124     public byte[] getAuthenticityKey() {
125         return mAuthenticityKey;
126     }
127 
128     /** Returns the elements of the credential. */
129     @NonNull
getCredentialElements()130     public List<CredentialElement> getCredentialElements() {
131         return mCredentialElements;
132     }
133 
134     @Override
equals(Object obj)135     public boolean equals(Object obj) {
136         if (obj instanceof PresenceCredential) {
137             PresenceCredential that = (PresenceCredential) obj;
138             return mType == that.mType
139                     && mIdentityType == that.mIdentityType
140                     && Arrays.equals(mSecretId, that.mSecretId)
141                     && Arrays.equals(mAuthenticityKey, that.mAuthenticityKey)
142                     && mCredentialElements.equals(that.mCredentialElements);
143         }
144         return false;
145     }
146 
147     @Override
hashCode()148     public int hashCode() {
149         return Objects.hash(
150                 mType,
151                 mIdentityType,
152                 Arrays.hashCode(mSecretId),
153                 Arrays.hashCode(mAuthenticityKey),
154                 mCredentialElements.hashCode());
155     }
156 
157     /**
158      * Writes the presence credential to the parcel.
159      *
160      * @hide
161      */
writeToParcel(@onNull Parcel dest, int flags)162     public void writeToParcel(@NonNull Parcel dest, int flags) {
163         dest.writeInt(mType);
164         dest.writeInt(mIdentityType);
165         dest.writeInt(mSecretId.length);
166         dest.writeByteArray(mSecretId);
167         dest.writeInt(mAuthenticityKey.length);
168         dest.writeByteArray(mAuthenticityKey);
169         dest.writeList(mCredentialElements);
170     }
171 }
172