1 /* 2 * Copyright (C) 2015 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.settings.password; 18 19 import android.app.Fragment; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.AsyncTask; 23 import android.os.Bundle; 24 import android.os.UserManager; 25 26 import com.android.internal.widget.LockPatternUtils; 27 28 /** 29 * An invisible retained worker fragment to track the AsyncWork that saves (and optionally 30 * verifies if a challenge is given) the chosen lock credential (pattern/pin/password). 31 */ 32 abstract class SaveChosenLockWorkerBase extends Fragment { 33 34 private Listener mListener; 35 private boolean mFinished; 36 private Intent mResultData; 37 38 protected LockPatternUtils mUtils; 39 protected boolean mHasChallenge; 40 protected long mChallenge; 41 protected boolean mWasSecureBefore; 42 protected int mUserId; 43 44 private boolean mBlocking; 45 46 @Override onCreate(Bundle savedInstanceState)47 public void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 setRetainInstance(true); 50 } 51 setListener(Listener listener)52 public void setListener(Listener listener) { 53 if (mListener == listener) { 54 return; 55 } 56 57 mListener = listener; 58 if (mFinished && mListener != null) { 59 mListener.onChosenLockSaveFinished(mWasSecureBefore, mResultData); 60 } 61 } 62 prepare(LockPatternUtils utils, boolean credentialRequired, boolean hasChallenge, long challenge, int userId)63 protected void prepare(LockPatternUtils utils, boolean credentialRequired, 64 boolean hasChallenge, long challenge, int userId) { 65 mUtils = utils; 66 mUserId = userId; 67 68 mHasChallenge = hasChallenge; 69 mChallenge = challenge; 70 // This will be a no-op for non managed profiles. 71 mWasSecureBefore = mUtils.isSecure(mUserId); 72 73 Context context = getContext(); 74 // If context is null, we're being invoked to change the setCredentialRequiredToDecrypt, 75 // and we made sure that this is the primary user already. 76 if (context == null || UserManager.get(context).getUserInfo(mUserId).isPrimary()) { 77 mUtils.setCredentialRequiredToDecrypt(credentialRequired); 78 } 79 80 mFinished = false; 81 mResultData = null; 82 } 83 start()84 protected void start() { 85 if (mBlocking) { 86 finish(saveAndVerifyInBackground()); 87 } else { 88 new Task().execute(); 89 } 90 } 91 92 /** 93 * Executes the save and verify work in background. 94 * @return Intent with challenge token or null. 95 */ saveAndVerifyInBackground()96 protected abstract Intent saveAndVerifyInBackground(); 97 finish(Intent resultData)98 protected void finish(Intent resultData) { 99 mFinished = true; 100 mResultData = resultData; 101 if (mListener != null) { 102 mListener.onChosenLockSaveFinished(mWasSecureBefore, mResultData); 103 } 104 } 105 setBlocking(boolean blocking)106 public void setBlocking(boolean blocking) { 107 mBlocking = blocking; 108 } 109 110 private class Task extends AsyncTask<Void, Void, Intent> { 111 @Override doInBackground(Void... params)112 protected Intent doInBackground(Void... params){ 113 return saveAndVerifyInBackground(); 114 } 115 116 @Override onPostExecute(Intent resultData)117 protected void onPostExecute(Intent resultData) { 118 finish(resultData); 119 } 120 } 121 122 interface Listener { onChosenLockSaveFinished(boolean wasSecureBefore, Intent resultData)123 public void onChosenLockSaveFinished(boolean wasSecureBefore, Intent resultData); 124 } 125 } 126