1 /*
2  * Copyright (C) 2017 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.showwhenlocked;
18 
19 import android.app.Activity;
20 import android.app.KeyguardManager;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.util.Log;
24 
25 /**
26  * Sample app to test the manifest attrs {@link android.R.attr#showWhenLocked}
27  * and {@link android.R.attr#turnScreenOn}.
28  *
29  * <p>Run with adb shell am start -n com.android.showwhenlocked/.ShowWhenLockedActivity to test
30  * when the phone has a keyguard enabled and/or the screen is off.
31  *
32  * Use the extra {@link #EXTRA_SHOW_WHEN_LOCKED} and {@link #EXTRA_TURN_SCREEN_ON} to test
33  * multiple scenarios.
34  *
35  * Ex: adb shell am start -n com.android.showwhenlocked/.ShowWhenLockedActivity --ez \
36  *      showWhenLocked false \
37  *      setTurnScreenOnAtStop false
38  *
39  * Note: Behavior may change if values are set to true after the Activity is already created
40  * and only brought to the front. For example, turnScreenOn only takes effect on the next launch
41  * if set using the extra value.
42  */
43 public class ShowWhenLockedActivity extends Activity {
44     private static final String TAG = ShowWhenLockedActivity.class.getSimpleName();
45 
46     /**
47      * The value set for this extra sets {@link #setShowWhenLocked(boolean)} as soon as the app
48      * is launched. This may cause delays in when the value set takes affect.
49      */
50     private static final String EXTRA_SHOW_WHEN_LOCKED = "showWhenLocked";
51 
52     /**
53      * The value set for this extra sets {@link #setTurnScreenOn(boolean)} as soon as the app
54      * is launched. This may cause delays in when the value set takes affect.
55      */
56     private static final String EXTRA_TURN_SCREEN_ON = "turnScreenOn";
57 
58     /**
59      * The value set for this extra will call {@link #setShowWhenLocked(boolean)} at onStop so
60      * it take effect on the next launch.
61      */
62     private static final String EXTRA_SHOW_WHEN_LOCKED_STOP = "setShowWhenLockedAtStop";
63 
64     /**
65      * The value set for this extra will call {@link #setTurnScreenOn(boolean)} at onStop so
66      * it take effect on the next launch.
67      */
68     private static final String EXTRA_TURN_SCREEN_ON_STOP = "setTurnScreenOnAtStop";
69 
70     /**
71      * The value set for this extra will call
72      * {@link KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)}
73      * as soon as the app is launched.
74      */
75     private static final String EXTRA_DISMISS_KEYGUARD = "dismissKeyguard";
76 
77     private boolean showWhenLockedAtStop = true;
78     private boolean turnScreenOnAtStop = true;
79 
80     private KeyguardManager mKeyguardManager;
81 
82     @Override
onCreate(Bundle savedInstanceState)83     public void onCreate(Bundle savedInstanceState) {
84         super.onCreate(savedInstanceState);
85         Log.v(TAG, "onCreate");
86         mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
87         handleExtras(getIntent().getExtras());
88     }
89 
90     @Override
onStart()91     protected void onStart() {
92         super.onStart();
93         Log.v(TAG, "onStart");
94     }
95 
96     @Override
onNewIntent(Intent intent)97     protected void onNewIntent(Intent intent) {
98         super.onNewIntent(intent);
99         handleExtras(intent.getExtras());
100     }
101 
102     @Override
onResume()103     protected void onResume() {
104         super.onResume();
105         Log.v(TAG, "onResume");
106     }
107 
108     @Override
onPause()109     protected void onPause() {
110         super.onPause();
111         Log.v(TAG, "onPause");
112     }
113 
114     @Override
onStop()115     protected void onStop() {
116         super.onStop();
117         Log.v(TAG, "onStop");
118 
119         setShowWhenLocked(showWhenLockedAtStop);
120         setTurnScreenOn(turnScreenOnAtStop);
121     }
122 
123     @Override
onDestroy()124     protected void onDestroy() {
125         super.onDestroy();
126         Log.v(TAG, "onDestroy");
127     }
128 
handleExtras(Bundle extras)129     private void handleExtras(Bundle extras) {
130         if (extras == null) {
131             return;
132         }
133 
134         if (extras.containsKey(EXTRA_SHOW_WHEN_LOCKED)) {
135             boolean showWhenLocked = extras.getBoolean(EXTRA_SHOW_WHEN_LOCKED, true);
136             Log.v(TAG, "Setting showWhenLocked to " + showWhenLocked);
137             setShowWhenLocked(showWhenLocked);
138         }
139 
140         if (extras.containsKey(EXTRA_TURN_SCREEN_ON)) {
141             boolean turnScreenOn = extras.getBoolean(EXTRA_TURN_SCREEN_ON, true);
142             Log.v(TAG, "Setting turnScreenOn to " + turnScreenOn);
143             setTurnScreenOn(turnScreenOn);
144         }
145 
146         if (extras.containsKey(EXTRA_SHOW_WHEN_LOCKED_STOP)) {
147             showWhenLockedAtStop = extras.getBoolean(EXTRA_SHOW_WHEN_LOCKED_STOP, true);
148             Log.v(TAG, "Setting showWhenLockedAtStop to " + showWhenLockedAtStop);
149         }
150 
151         if (extras.containsKey(EXTRA_TURN_SCREEN_ON_STOP)) {
152             turnScreenOnAtStop = extras.getBoolean(EXTRA_TURN_SCREEN_ON_STOP, true);
153             Log.v(TAG, "Setting turnScreenOnAtStop to " + turnScreenOnAtStop);
154         }
155 
156         if (extras.containsKey(EXTRA_DISMISS_KEYGUARD)) {
157             if (extras.getBoolean(EXTRA_DISMISS_KEYGUARD, false)) {
158                 Log.v(TAG, "Requesting dismiss keyguard");
159                 mKeyguardManager.requestDismissKeyguard(this, null);
160             }
161         }
162     }
163 }
164 
165