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.Intent;
20 import android.os.Bundle;
21 
22 import androidx.fragment.app.Fragment;
23 
24 /**
25  * An invisible retained fragment to track lock check result.
26  */
27 public class CredentialCheckResultTracker extends Fragment {
28 
29     private Listener mListener;
30     private boolean mHasResult = false;
31 
32     private boolean mResultMatched;
33     private Intent mResultData;
34     private int mResultTimeoutMs;
35     private int mResultEffectiveUserId;
36 
37     @Override
onCreate(Bundle savedInstanceState)38     public void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         setRetainInstance(true);
41     }
42 
setListener(Listener listener)43     public void setListener(Listener listener) {
44         if (mListener == listener) {
45             return;
46         }
47 
48         mListener = listener;
49         if (mListener != null && mHasResult) {
50             mListener.onCredentialChecked(mResultMatched, mResultData, mResultTimeoutMs,
51                     mResultEffectiveUserId, false /* newResult */);
52         }
53     }
54 
setResult(boolean matched, Intent intent, int timeoutMs, int effectiveUserId)55     public void setResult(boolean matched, Intent intent, int timeoutMs, int effectiveUserId) {
56         mResultMatched = matched;
57         mResultData = intent;
58         mResultTimeoutMs = timeoutMs;
59         mResultEffectiveUserId = effectiveUserId;
60 
61         mHasResult = true;
62         if (mListener != null) {
63             mListener.onCredentialChecked(mResultMatched, mResultData, mResultTimeoutMs,
64                     mResultEffectiveUserId, true /* newResult */);
65             mHasResult = false;
66         }
67     }
68 
clearResult()69     public void clearResult() {
70         mHasResult = false;
71         mResultMatched = false;
72         mResultData = null;
73         mResultTimeoutMs = 0;
74         mResultEffectiveUserId = 0;
75     }
76 
77     interface Listener {
onCredentialChecked(boolean matched, Intent intent, int timeoutMs, int effectiveUserId, boolean newResult)78         public void onCredentialChecked(boolean matched, Intent intent, int timeoutMs,
79                 int effectiveUserId, boolean newResult);
80     }
81 }
82