1 /*
2  * Copyright (C) 2023 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.app;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.app.KeyguardManager.LockTypes;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.Objects;
26 
27 /**
28  * Provides information necessary to perform remote lock screen credentials check.
29  *
30  * @hide
31  */
32 @SystemApi
33 public final class RemoteLockscreenValidationSession implements Parcelable {
34 
35     @LockTypes
36     private int mLockType;
37 
38     private byte[] mSourcePublicKey;
39 
40     private int mRemainingAttempts;
41 
42     public static final @NonNull Parcelable.Creator<RemoteLockscreenValidationSession> CREATOR = new
43             Parcelable.Creator<RemoteLockscreenValidationSession>() {
44         @Override
45         public RemoteLockscreenValidationSession createFromParcel(Parcel source) {
46             return new RemoteLockscreenValidationSession(source);
47         }
48 
49         @Override
50         public RemoteLockscreenValidationSession[] newArray(int size) {
51             return new RemoteLockscreenValidationSession[size];
52         }
53     };
54 
55 
56     /**
57      * Builder for {@code RemoteLockscreenValidationSession}
58      */
59     public static final class Builder {
60         private RemoteLockscreenValidationSession mInstance =
61                 new RemoteLockscreenValidationSession();
62 
63         /**
64          * Sets UI type.
65          * Default value is {@code LockTypes.PASSWORD}
66          *
67          * @param lockType The UI format
68          * @return This builder.
69          */
setLockType(@ockTypes int lockType)70         public @NonNull Builder setLockType(@LockTypes int lockType) {
71             mInstance.mLockType = lockType;
72             return this;
73         }
74 
75         /**
76          * Sets public key using secure box encoding
77          * @return This builder.
78          */
setSourcePublicKey(@onNull byte[] publicKey)79         public @NonNull Builder setSourcePublicKey(@NonNull byte[] publicKey) {
80             mInstance.mSourcePublicKey = publicKey;
81             return this;
82         }
83 
84         /**
85          * Sets the number of remaining credentials check
86          * Default value is {@code 0}
87          *
88          * @return This builder.
89          */
setRemainingAttempts(int remainingAttempts)90         public @NonNull Builder setRemainingAttempts(int remainingAttempts) {
91             mInstance.mRemainingAttempts = remainingAttempts;
92             return this;
93         }
94 
95         /**
96          * Creates {@code RemoteLockscreenValidationSession}
97          *
98          * @throws NullPointerException if required fields are not set.
99          */
build()100         public @NonNull RemoteLockscreenValidationSession build() {
101             Objects.requireNonNull(mInstance.mSourcePublicKey);
102             return mInstance;
103         }
104     }
105 
106     /**
107      * Specifies lock screen credential type.
108      */
getLockType()109     public @LockTypes int getLockType() {
110         return mLockType;
111     }
112 
113     /**
114      * Public key used to send encrypted credentials.
115      */
getSourcePublicKey()116     public @NonNull byte[] getSourcePublicKey() {
117         return mSourcePublicKey;
118     }
119 
120     /**
121      * Number of remaining attempts to verify credentials.
122      *
123      * <p>After correct guess counter is reset to {@code 5}.
124      */
getRemainingAttempts()125     public int getRemainingAttempts() {
126         return mRemainingAttempts;
127     }
128 
129     @Override
writeToParcel(@onNull Parcel out, int flags)130     public void writeToParcel(@NonNull Parcel out, int flags) {
131         out.writeInt(mLockType);
132         out.writeByteArray(mSourcePublicKey);
133         out.writeInt(mRemainingAttempts);
134     }
135 
RemoteLockscreenValidationSession()136     private RemoteLockscreenValidationSession() {
137     }
138 
RemoteLockscreenValidationSession(Parcel in)139     private RemoteLockscreenValidationSession(Parcel in) {
140         mLockType = in.readInt();
141         mSourcePublicKey = in.createByteArray();
142         mRemainingAttempts = in.readInt();
143     }
144 
145     @Override
describeContents()146     public int describeContents() {
147         return 0;
148     }
149 }
150