1 /* 2 * Copyright (C) 2018 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 com.android.car.settings.security; 18 19 import android.os.Bundle; 20 21 import androidx.fragment.app.Fragment; 22 23 import com.android.car.settings.common.Logger; 24 import com.android.internal.widget.LockPatternChecker; 25 import com.android.internal.widget.LockPatternUtils; 26 import com.android.internal.widget.LockPatternView; 27 28 import java.util.List; 29 30 /** 31 * An invisible retained worker fragment to track the AsyncTask that checks the entered lock 32 * credential (pattern/pin/password). 33 */ 34 public class CheckLockWorker extends Fragment implements LockPatternChecker.OnCheckCallback { 35 36 private static final Logger LOG = new Logger(CheckLockWorker.class); 37 38 private boolean mHasPendingResult; 39 private boolean mLockMatched; 40 private boolean mCheckInProgress; 41 private Listener mListener; 42 private LockPatternUtils mLockPatternUtils; 43 44 @Override onCreate(Bundle savedInstanceState)45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 setRetainInstance(true); 48 mLockPatternUtils = new LockPatternUtils(getContext()); 49 } 50 51 @Override onChecked(boolean matched, int throttleTimeoutMs)52 public void onChecked(boolean matched, int throttleTimeoutMs) { 53 mCheckInProgress = false; 54 55 if (mListener == null) { 56 mHasPendingResult = true; 57 mLockMatched = matched; 58 } else { 59 mListener.onCheckCompleted(matched); 60 } 61 } 62 63 /** 64 * Sets the listener for callback when lock check is completed. 65 */ setListener(Listener listener)66 public void setListener(Listener listener) { 67 mListener = listener; 68 if (mListener != null && mHasPendingResult) { 69 mHasPendingResult = false; 70 mListener.onCheckCompleted(mLockMatched); 71 } 72 } 73 74 /** Returns whether a lock check is in progress. */ isCheckInProgress()75 public final boolean isCheckInProgress() { 76 return mCheckInProgress; 77 } 78 79 /** 80 * Checks lock pattern asynchronously. To receive callback when check is completed, 81 * implement {@link Listener} and call {@link #setListener(Listener)}. 82 */ checkPattern(int userId, List<LockPatternView.Cell> pattern)83 public final void checkPattern(int userId, List<LockPatternView.Cell> pattern) { 84 if (mCheckInProgress) { 85 LOG.w("Check pattern request issued while one is still running"); 86 return; 87 } 88 89 mCheckInProgress = true; 90 LockPatternChecker.checkPattern(mLockPatternUtils, pattern, userId, this); 91 } 92 93 /** 94 * Checks lock PIN/password asynchronously. To receive callback when check is completed, 95 * implement {@link Listener} and call {@link #setListener(Listener)}. 96 */ checkPinPassword(int userId, byte[] password)97 public final void checkPinPassword(int userId, byte[] password) { 98 if (mCheckInProgress) { 99 LOG.w("Check pin/password request issued while one is still running"); 100 return; 101 } 102 mCheckInProgress = true; 103 LockPatternChecker.checkPassword(mLockPatternUtils, password, userId, this); 104 } 105 106 /** 107 * Callback when lock check is completed. 108 */ 109 interface Listener { 110 /** 111 * @param matched Whether the entered password matches the stored record. 112 */ onCheckCompleted(boolean matched)113 void onCheckCompleted(boolean matched); 114 } 115 } 116 117