diff options
author | chaviw <chaviw@google.com> | 2017-06-13 12:05:44 -0700 |
---|---|---|
committer | chaviw <chaviw@google.com> | 2017-06-29 13:33:43 -0700 |
commit | 59b9885bc8b781b6312adedd67c2c7c270f9213c (patch) | |
tree | ef828c70dfe767b2be66ca43cee920e8fd38f2eb /tests/ShowWhenLockedApp | |
parent | e3bb95dddb3849d3a54538f507bfe83f6b74cb62 (diff) |
Added Activity flags to show on lock screen.
Added two new flags to the Activity to turn the screen on and
show on the lock screen. These can be used instead of the Window flags
LayouParams.FLAG_TURN_SCREEN_ON and LayoutParams.FLAG_SHOW_WHEN_LOCKED
to prevent the double onStart/onResume lifecycle events.
The flags can be set as an attr for the Activity in the AndroidManifest
using android:showWhenLocked="true" and android:turnScreenOn="true".
They can also be set through methods in the Activity class using
setShowWhenLocked(true) and setTurnScreen(true).
Fixes: 36850100
Test: Created sample application, tests/ShowWhenLockedApp, that set the
flags in the manifest and code. Tested multiple scenarios to
launch the Activity with the flags set and unset.
Test: cts-tradefed run commandAndExit cts-dev --module CtsServicesHostTestCases -t android.server.cts.KeyguardTransitionTests
Test: cts-tradefed run commandAndExit cts-dev --module CtsServicesHostTestCases -t android.server.cts.ActivityManagerActivityVisibilityTests
Test: cts-tradefed run commandAndExit cts-dev --module CtsServicesHostTestCases -t android.server.cts.KeyguardTests
Change-Id: I44f0e313df4531d49c7ac56108b6bf80e41fefc1
Diffstat (limited to 'tests/ShowWhenLockedApp')
-rw-r--r-- | tests/ShowWhenLockedApp/Android.mk | 10 | ||||
-rw-r--r-- | tests/ShowWhenLockedApp/AndroidManifest.xml | 31 | ||||
-rw-r--r-- | tests/ShowWhenLockedApp/src/com/android/showwhenlocked/ShowWhenLockedActivity.java | 164 |
3 files changed, 205 insertions, 0 deletions
diff --git a/tests/ShowWhenLockedApp/Android.mk b/tests/ShowWhenLockedApp/Android.mk new file mode 100644 index 000000000000..006416791dd9 --- /dev/null +++ b/tests/ShowWhenLockedApp/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := ShowWhenLocked + +LOCAL_MODULE_TAGS := tests + +include $(BUILD_PACKAGE)
\ No newline at end of file diff --git a/tests/ShowWhenLockedApp/AndroidManifest.xml b/tests/ShowWhenLockedApp/AndroidManifest.xml new file mode 100644 index 000000000000..a872e061526f --- /dev/null +++ b/tests/ShowWhenLockedApp/AndroidManifest.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.showwhenlocked"> + <application android:label="ShowWhenLocked"> + <activity android:name=".ShowWhenLockedActivity" + android:showWhenLocked="true" + android:turnScreenOn="true" + android:launchMode="singleTask"> + <intent-filter> + <action android:name="android.intent.action.MAIN"/> + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + </activity> + </application> +</manifest> diff --git a/tests/ShowWhenLockedApp/src/com/android/showwhenlocked/ShowWhenLockedActivity.java b/tests/ShowWhenLockedApp/src/com/android/showwhenlocked/ShowWhenLockedActivity.java new file mode 100644 index 000000000000..f71483182676 --- /dev/null +++ b/tests/ShowWhenLockedApp/src/com/android/showwhenlocked/ShowWhenLockedActivity.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.showwhenlocked; + +import android.app.Activity; +import android.app.KeyguardManager; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +/** + * Sample app to test the manifest attrs {@link android.R.attr#showWhenLocked} + * and {@link android.R.attr#turnScreenOn}. + * + * <p>Run with adb shell am start -n com.android.showwhenlocked/.ShowWhenLockedActivity to test + * when the phone has a keyguard enabled and/or the screen is off. + * + * Use the extra {@link #EXTRA_SHOW_WHEN_LOCKED} and {@link #EXTRA_TURN_SCREEN_ON} to test + * multiple scenarios. + * + * Ex: adb shell am start -n com.android.showwhenlocked/.ShowWhenLockedActivity --ez \ + * showWhenLocked false \ + * setTurnScreenOnAtStop false + * + * Note: Behavior may change if values are set to true after the Activity is already created + * and only brought to the front. For example, turnScreenOn only takes effect on the next launch + * if set using the extra value. + */ +public class ShowWhenLockedActivity extends Activity { + private static final String TAG = ShowWhenLockedActivity.class.getSimpleName(); + + /** + * The value set for this extra sets {@link #setShowWhenLocked(boolean)} as soon as the app + * is launched. This may cause delays in when the value set takes affect. + */ + private static final String EXTRA_SHOW_WHEN_LOCKED = "showWhenLocked"; + + /** + * The value set for this extra sets {@link #setTurnScreenOn(boolean)} as soon as the app + * is launched. This may cause delays in when the value set takes affect. + */ + private static final String EXTRA_TURN_SCREEN_ON = "turnScreenOn"; + + /** + * The value set for this extra will call {@link #setShowWhenLocked(boolean)} at onStop so + * it take effect on the next launch. + */ + private static final String EXTRA_SHOW_WHEN_LOCKED_STOP = "setShowWhenLockedAtStop"; + + /** + * The value set for this extra will call {@link #setTurnScreenOn(boolean)} at onStop so + * it take effect on the next launch. + */ + private static final String EXTRA_TURN_SCREEN_ON_STOP = "setTurnScreenOnAtStop"; + + /** + * The value set for this extra will call + * {@link KeyguardManager#requestDismissKeyguard(Activity, KeyguardManager.KeyguardDismissCallback)} + * as soon as the app is launched. + */ + private static final String EXTRA_DISMISS_KEYGUARD = "dismissKeyguard"; + + private boolean showWhenLockedAtStop = true; + private boolean turnScreenOnAtStop = true; + + private KeyguardManager mKeyguardManager; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Log.v(TAG, "onCreate"); + mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); + handleExtras(getIntent().getExtras()); + } + + @Override + protected void onStart() { + super.onStart(); + Log.v(TAG, "onStart"); + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + handleExtras(intent.getExtras()); + } + + @Override + protected void onResume() { + super.onResume(); + Log.v(TAG, "onResume"); + } + + @Override + protected void onPause() { + super.onPause(); + Log.v(TAG, "onPause"); + } + + @Override + protected void onStop() { + super.onStop(); + Log.v(TAG, "onStop"); + + setShowWhenLocked(showWhenLockedAtStop); + setTurnScreenOn(turnScreenOnAtStop); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + Log.v(TAG, "onDestroy"); + } + + private void handleExtras(Bundle extras) { + if (extras == null) { + return; + } + + if (extras.containsKey(EXTRA_SHOW_WHEN_LOCKED)) { + boolean showWhenLocked = extras.getBoolean(EXTRA_SHOW_WHEN_LOCKED, true); + Log.v(TAG, "Setting showWhenLocked to " + showWhenLocked); + setShowWhenLocked(showWhenLocked); + } + + if (extras.containsKey(EXTRA_TURN_SCREEN_ON)) { + boolean turnScreenOn = extras.getBoolean(EXTRA_TURN_SCREEN_ON, true); + Log.v(TAG, "Setting turnScreenOn to " + turnScreenOn); + setTurnScreenOn(turnScreenOn); + } + + if (extras.containsKey(EXTRA_SHOW_WHEN_LOCKED_STOP)) { + showWhenLockedAtStop = extras.getBoolean(EXTRA_SHOW_WHEN_LOCKED_STOP, true); + Log.v(TAG, "Setting showWhenLockedAtStop to " + showWhenLockedAtStop); + } + + if (extras.containsKey(EXTRA_TURN_SCREEN_ON_STOP)) { + turnScreenOnAtStop = extras.getBoolean(EXTRA_TURN_SCREEN_ON_STOP, true); + Log.v(TAG, "Setting turnScreenOnAtStop to " + turnScreenOnAtStop); + } + + if (extras.containsKey(EXTRA_DISMISS_KEYGUARD)) { + if (extras.getBoolean(EXTRA_DISMISS_KEYGUARD, false)) { + Log.v(TAG, "Requesting dismiss keyguard"); + mKeyguardManager.requestDismissKeyguard(this, null); + } + } + } +} + |