blob: 2798b3d7f472f7988f0296c8f12cc72d7c450822 [file] [log] [blame]
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -07001/*
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 Lam2eb170c2017-04-28 16:18:47 -070017package com.android.settings.password;
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070018
Adrian Roos77181e92016-02-25 15:43:24 -080019import android.content.Context;
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070020import android.content.Intent;
21import android.os.AsyncTask;
22import android.os.Bundle;
Robin Lee5bb1e4f2016-01-27 17:21:00 +000023import android.os.UserManager;
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +000024import android.util.Pair;
25import android.widget.Toast;
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070026
tmfang99cc23d2018-06-26 19:01:57 +080027import androidx.fragment.app.Fragment;
28
Fan Zhang23f8d592018-08-28 15:11:40 -070029import com.android.internal.widget.LockPatternUtils;
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +000030import com.android.settings.R;
Fan Zhang23f8d592018-08-28 15:11:40 -070031
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070032/**
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 */
36abstract 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 Bayarrib8a22e42015-12-18 16:40:18 +000046 protected int mUserId;
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070047
Adrian Roos62775bf2016-01-28 13:23:53 -080048 private boolean mBlocking;
49
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070050 @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 Bayarrib8a22e42015-12-18 16:40:18 +000068 boolean hasChallenge, long challenge, int userId) {
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070069 mUtils = utils;
Clara Bayarrib8a22e42015-12-18 16:40:18 +000070 mUserId = userId;
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070071
72 mHasChallenge = hasChallenge;
73 mChallenge = challenge;
Clara Bayarrib8a22e42015-12-18 16:40:18 +000074 // This will be a no-op for non managed profiles.
Clara Bayarrib8a22e42015-12-18 16:40:18 +000075 mWasSecureBefore = mUtils.isSecure(mUserId);
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070076
Adrian Roos77181e92016-02-25 15:43:24 -080077 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 Lee5bb1e4f2016-01-27 17:21:00 +000081 mUtils.setCredentialRequiredToDecrypt(credentialRequired);
82 }
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070083
84 mFinished = false;
85 mResultData = null;
86 }
87
88 protected void start() {
Adrian Roos62775bf2016-01-28 13:23:53 -080089 if (mBlocking) {
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +000090 finish(saveAndVerifyInBackground().second);
Adrian Roos62775bf2016-01-28 13:23:53 -080091 } else {
92 new Task().execute();
93 }
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -070094 }
95
96 /**
97 * Executes the save and verify work in background.
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +000098 * @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 Xia3e7e3d62015-08-25 15:04:57 -0700100 */
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +0000101 protected abstract Pair<Boolean, Intent> saveAndVerifyInBackground();
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -0700102
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 Roos62775bf2016-01-28 13:23:53 -0800111 public void setBlocking(boolean blocking) {
112 mBlocking = blocking;
113 }
114
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +0000115 private class Task extends AsyncTask<Void, Void, Pair<Boolean, Intent>> {
116
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -0700117 @Override
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +0000118 protected Pair<Boolean, Intent> doInBackground(Void... params){
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -0700119 return saveAndVerifyInBackground();
120 }
121
122 @Override
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +0000123 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 Xia3e7e3d62015-08-25 15:04:57 -0700129 }
130 }
131
132 interface Listener {
Irina Dumitrescu4b96dc32019-02-28 15:35:36 +0000133 void onChosenLockSaveFinished(boolean wasSecureBefore, Intent resultData);
Xiyuan Xia3e7e3d62015-08-25 15:04:57 -0700134 }
135}