• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.example.android.vdmdemo.host;
17 
18 import android.app.ActivityOptions;
19 import android.app.KeyguardManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentSender;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.Display;
27 import android.view.View;
28 import android.widget.TextView;
29 
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.AppCompatActivity;
32 
33 import dagger.hilt.android.AndroidEntryPoint;
34 
35 /** A dialog prompting the user to unlock the default display for a fallback activity launch. */
36 @AndroidEntryPoint(AppCompatActivity.class)
37 public class UnlockKeyguardDialog extends Hilt_UnlockKeyguardDialog
38         implements KeyguardManager.KeyguardLockedStateListener {
39     private static final String TAG = "UnlockKeyguardDialog";
40 
41     private IntentSender mTarget;
42     private KeyguardManager mKeyguardManager;
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         final Intent intent = getIntent();
49         mTarget = intent.getParcelableExtra(Intent.EXTRA_INTENT,
50                 android.content.IntentSender.class);
51         if (mTarget == null) {
52             Log.wtf(TAG, "Invalid intent: " + intent);
53             finish();
54         }
55 
56         mKeyguardManager = getSystemService(KeyguardManager.class);
57         if (mKeyguardManager == null) {
58             Log.wtf(TAG, "KeyguardManager not available");
59             finish();
60         }
61 
62         setContentView(R.layout.unlock_keyguard_dialog);
63         ((TextView) requireViewById(R.id.message_view))
64                 .setText(getString(R.string.unlock_dialog_message, Build.MODEL));
65 
66         if (!mKeyguardManager.isKeyguardLocked()) {
67             onKeyguardLockedStateChanged(false);
68         } else {
69             mKeyguardManager.addKeyguardLockedStateListener(getMainExecutor(), this);
70         }
71 
72         setFinishOnTouchOutside(true);
73     }
74 
75     @Override
onNewIntent(@onNull Intent intent)76     public void onNewIntent(@NonNull Intent intent) {
77         super.onNewIntent(intent);
78         setIntent(intent);
79     }
80 
81     @Override
onDestroy()82     protected void onDestroy() {
83         mKeyguardManager.removeKeyguardLockedStateListener(this);
84         super.onDestroy();
85     }
86 
87     @Override
onKeyguardLockedStateChanged(boolean isKeyguardLocked)88     public void onKeyguardLockedStateChanged(boolean isKeyguardLocked) {
89         if (isKeyguardLocked) return;
90 
91         Bundle activityOptions =
92                 ActivityOptions.makeBasic()
93                         .setPendingIntentBackgroundActivityStartMode(
94                                 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
95                         .setLaunchDisplayId(Display.DEFAULT_DISPLAY)
96                         .toBundle();
97         try {
98             startIntentSender(mTarget, /* fillInIntent= */ null, /* flagsMask= */ 0,
99                     /* flagsValues= */ 0, /* extraFlags= */ 0, activityOptions);
100         } catch (IntentSender.SendIntentException e) {
101             Log.e(TAG, "Error while starting intent sender", e);
102         }
103         finish();
104     }
105 
106     /** Called when the dialog has been canceled by the user. */
onCanceled(View view)107     public void onCanceled(View view) {
108         finish();
109     }
110 
111     /**
112      * Creates an intent that launches UnlockKeyguardDialog when an activity launch is blocked.
113      */
createIntent(Context context, IntentSender target)114     public static Intent createIntent(Context context, IntentSender target) {
115         return new Intent()
116                 .setClass(context, UnlockKeyguardDialog.class)
117                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
118                 .putExtra(Intent.EXTRA_INTENT, target);
119     }
120 }
121