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