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