• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.WorkerThread;
20 import android.os.AsyncTask;
21 import android.os.Bundle;
22 import android.support.annotation.VisibleForTesting;
23 import android.support.v4.app.Fragment;
24 
25 import com.android.car.settings.common.Logger;
26 import com.android.internal.widget.LockPatternUtils;
27 
28 /**
29  * An invisible retained worker fragment to track the AsyncWork that saves
30  * the chosen lock credential (pattern/pin/password).
31  */
32 abstract class SaveLockWorkerBase extends Fragment {
33     /**
34      * Callback when lock save has finished
35      */
36     interface Listener {
onChosenLockSaveFinished(boolean isSaveSuccessful)37         void onChosenLockSaveFinished(boolean isSaveSuccessful);
38     }
39 
40     private static final Logger LOG = new Logger(SaveLockWorkerBase.class);
41 
42     private Listener mListener;
43     private boolean mFinished;
44     private boolean mIsSaveSuccessful;
45     private LockPatternUtils mUtils;
46     private int mUserId;
47 
getUtils()48     final LockPatternUtils getUtils() {
49         return mUtils;
50     }
51 
getUserId()52     final int getUserId() {
53         return mUserId;
54     }
55 
isFinished()56     final boolean isFinished() {
57         return mFinished;
58     }
59 
60     @Override
onCreate(Bundle savedInstanceState)61     public void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         setRetainInstance(true);
64     }
65 
init(int userId)66     final void init(int userId) {
67         mUtils = new LockPatternUtils(getContext());
68         mUserId = userId;
69     }
70 
71     /**
72      * Start executing the async task.
73      */
start()74     final void start() {
75         mFinished = false;
76         new Task().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
77     }
78 
79     /**
80      * Set the listener to get callback when finished saving the chosen lock.
81      */
setListener(Listener listener)82     public void setListener(Listener listener) {
83         if (mListener != listener) {
84             mListener = listener;
85             if (mFinished && mListener != null) {
86                 mListener.onChosenLockSaveFinished(mIsSaveSuccessful);
87             }
88         }
89     }
90 
91     @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
saveAndVerifyInBackground(Void... params)92     boolean saveAndVerifyInBackground(Void... params) {
93         boolean isSaveSuccessful = true;
94 
95         try {
96             saveLock();
97         } catch (Exception e) {
98             LOG.e("Save lock exception", e);
99             isSaveSuccessful = false;
100         }
101 
102         return isSaveSuccessful;
103     }
104 
finish(boolean isSaveSuccessful)105     private void finish(boolean isSaveSuccessful) {
106         mFinished = true;
107         mIsSaveSuccessful = isSaveSuccessful;
108         if (mListener != null) {
109             mListener.onChosenLockSaveFinished(isSaveSuccessful);
110         }
111     }
112 
113     /**
114      * Executes the save and verify work in background.
115      */
116     @WorkerThread
saveLock()117     abstract void saveLock();
118 
119     // Save chosen lock task.
120     private class Task extends AsyncTask<Void, Void, Boolean> {
121         @Override
doInBackground(Void... params)122         protected Boolean doInBackground(Void... params) {
123             return saveAndVerifyInBackground();
124         }
125 
126         @Override
onPostExecute(Boolean isSaveSuccessful)127         protected void onPostExecute(Boolean isSaveSuccessful) {
128             finish(isSaveSuccessful);
129         }
130     }
131 }
132