Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 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 | |
Maurice Lam | 2eb170c | 2017-04-28 16:18:47 -0700 | [diff] [blame] | 17 | package com.android.settings.password; |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 18 | |
Adrian Roos | 77181e9 | 2016-02-25 15:43:24 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.os.AsyncTask; |
| 22 | import android.os.Bundle; |
Robin Lee | 5bb1e4f | 2016-01-27 17:21:00 +0000 | [diff] [blame] | 23 | import android.os.UserManager; |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 24 | import android.util.Pair; |
| 25 | import android.widget.Toast; |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 26 | |
tmfang | 99cc23d | 2018-06-26 19:01:57 +0800 | [diff] [blame] | 27 | import androidx.fragment.app.Fragment; |
| 28 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 29 | import com.android.internal.widget.LockPatternUtils; |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 30 | import com.android.settings.R; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 31 | |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 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; |
Clara Bayarri | b8a22e4 | 2015-12-18 16:40:18 +0000 | [diff] [blame] | 46 | protected int mUserId; |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 47 | |
Adrian Roos | 62775bf | 2016-01-28 13:23:53 -0800 | [diff] [blame] | 48 | private boolean mBlocking; |
| 49 | |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 50 | @Override |
| 51 | public void onCreate(Bundle savedInstanceState) { |
| 52 | super.onCreate(savedInstanceState); |
| 53 | setRetainInstance(true); |
| 54 | } |
| 55 | |
| 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 | |
| 67 | protected void prepare(LockPatternUtils utils, boolean credentialRequired, |
Clara Bayarri | b8a22e4 | 2015-12-18 16:40:18 +0000 | [diff] [blame] | 68 | boolean hasChallenge, long challenge, int userId) { |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 69 | mUtils = utils; |
Clara Bayarri | b8a22e4 | 2015-12-18 16:40:18 +0000 | [diff] [blame] | 70 | mUserId = userId; |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 71 | |
| 72 | mHasChallenge = hasChallenge; |
| 73 | mChallenge = challenge; |
Clara Bayarri | b8a22e4 | 2015-12-18 16:40:18 +0000 | [diff] [blame] | 74 | // This will be a no-op for non managed profiles. |
Clara Bayarri | b8a22e4 | 2015-12-18 16:40:18 +0000 | [diff] [blame] | 75 | mWasSecureBefore = mUtils.isSecure(mUserId); |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 76 | |
Adrian Roos | 77181e9 | 2016-02-25 15:43:24 -0800 | [diff] [blame] | 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()) { |
Robin Lee | 5bb1e4f | 2016-01-27 17:21:00 +0000 | [diff] [blame] | 81 | mUtils.setCredentialRequiredToDecrypt(credentialRequired); |
| 82 | } |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 83 | |
| 84 | mFinished = false; |
| 85 | mResultData = null; |
| 86 | } |
| 87 | |
| 88 | protected void start() { |
Adrian Roos | 62775bf | 2016-01-28 13:23:53 -0800 | [diff] [blame] | 89 | if (mBlocking) { |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 90 | finish(saveAndVerifyInBackground().second); |
Adrian Roos | 62775bf | 2016-01-28 13:23:53 -0800 | [diff] [blame] | 91 | } else { |
| 92 | new Task().execute(); |
| 93 | } |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Executes the save and verify work in background. |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 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. |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 100 | */ |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 101 | protected abstract Pair<Boolean, Intent> saveAndVerifyInBackground(); |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 102 | |
| 103 | protected void finish(Intent resultData) { |
| 104 | mFinished = true; |
| 105 | mResultData = resultData; |
| 106 | if (mListener != null) { |
| 107 | mListener.onChosenLockSaveFinished(mWasSecureBefore, mResultData); |
| 108 | } |
| 109 | } |
| 110 | |
Adrian Roos | 62775bf | 2016-01-28 13:23:53 -0800 | [diff] [blame] | 111 | public void setBlocking(boolean blocking) { |
| 112 | mBlocking = blocking; |
| 113 | } |
| 114 | |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 115 | private class Task extends AsyncTask<Void, Void, Pair<Boolean, Intent>> { |
| 116 | |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 117 | @Override |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 118 | protected Pair<Boolean, Intent> doInBackground(Void... params){ |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 119 | return saveAndVerifyInBackground(); |
| 120 | } |
| 121 | |
| 122 | @Override |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 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); |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | interface Listener { |
Irina Dumitrescu | 4b96dc3 | 2019-02-28 15:35:36 +0000 | [diff] [blame] | 133 | void onChosenLockSaveFinished(boolean wasSecureBefore, Intent resultData); |
Xiyuan Xia | 3e7e3d6 | 2015-08-25 15:04:57 -0700 | [diff] [blame] | 134 | } |
| 135 | } |