1 /*
2  * Copyright (C) 2016 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 android.server.wm.app;
18 
19 import static android.server.wm.app.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_CANCELLED;
20 import static android.server.wm.app.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_ERROR;
21 import static android.server.wm.app.Components.KeyguardDismissLoggerCallback.ENTRY_ON_DISMISS_SUCCEEDED;
22 import static android.server.wm.app.Components.KeyguardDismissLoggerCallback.KEYGUARD_DISMISS_LOG_TAG;
23 
24 import android.app.KeyguardManager;
25 import android.app.KeyguardManager.KeyguardDismissCallback;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.server.wm.TestJournalProvider;
29 import android.util.Log;
30 
31 public class KeyguardDismissLoggerCallback extends KeyguardDismissCallback {
32 
33     private final Context mContext;
34     private final ComponentName mOwnerName;
35 
KeyguardDismissLoggerCallback(Context context, ComponentName name)36     KeyguardDismissLoggerCallback(Context context, ComponentName name) {
37         mContext = context;
38         mOwnerName = name;
39     }
40 
41     @Override
onDismissError()42     public void onDismissError() {
43         Log.i(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_ERROR);
44         putCallbackResult(ENTRY_ON_DISMISS_ERROR);
45     }
46 
47     @Override
onDismissSucceeded()48     public void onDismissSucceeded() {
49         if (mContext.getSystemService(KeyguardManager.class).isDeviceLocked()) {
50             // Device is still locked? What a fail. Don't print "onDismissSucceeded" such that the
51             // log fails.
52             Log.i(KEYGUARD_DISMISS_LOG_TAG,
53                     "dismiss succeeded was called but device is still locked.");
54         } else {
55             Log.i(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_SUCCEEDED);
56             putCallbackResult(ENTRY_ON_DISMISS_SUCCEEDED);
57         }
58     }
59 
60     @Override
onDismissCancelled()61     public void onDismissCancelled() {
62         Log.i(KEYGUARD_DISMISS_LOG_TAG, ENTRY_ON_DISMISS_CANCELLED);
63         putCallbackResult(ENTRY_ON_DISMISS_CANCELLED);
64     }
65 
putCallbackResult(String callbackName)66     private void putCallbackResult(String callbackName) {
67         TestJournalProvider.putExtras(mContext, mOwnerName,
68                 extras -> extras.putBoolean(callbackName, true));
69     }
70 }
71