How to completely prevent Android Lock Screen - android

I made an field guide app for biology. It does tons of stuff, for example, recordings of wildlife sounds, and it is made to run the whole day with a single battery charge.
Since it is intended to be run a whole day, I cannot keep the screen on all the time. So naturally the screen goes off. Then, the lock screen kicks in, blocking the app. Then you need to enter the pin/pattern/whatever and you then miss the opportunity of a precious recording. Dozens of times per day. And in general the user is using my app in the wild, where there is much less risk of theft.
So I present the user an option in the preference section of my app to turn lock screen off while using it. There is no problem with this (see below for the code I made), except that when I switch to a secondary activity the lock screen appears. It is not truly a "lock screen", in the sense that it shows the back button that when you press it the lock screen disappears. But still, a pain when you're in a hurry. I want no lock screen. At all.
Interestingly, when I switch back to the primary activity from a secondary, no lock screen is shown...
This is the way I found to (partially) disable the lock screen (executed in each activity act):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
act.setShowWhenLocked(true);
act.setTurnScreenOn(true);
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
// act.setInheritShowWhenLocked(true); // makes no difference?
} else {
Window window = act.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
Of course, I also have in the Manifest:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

It turns out that the requestDismissKeyguard was causing the Lock Screen to be activated instead of deactivated (??!!). So I removed the line
((KeyguardManager) act.getSystemService(Context.KEYGUARD_SERVICE)).requestDismissKeyguard(act, null);
and now everything works just fine.

Related

Screen Pinning unreliable: sometimes asks for passcode on awakening, other times skips directly to pinned app

(this question was originally posted on the android enthusiasts stub in a slightly altered form)
I've been trying to lock an android tablet in kiosk mode for a survey app I'm making; for that I've made use of the Screen Pinning lollipop feature (with passcode enabled).
I used the following code
#Override
protected void onResume() {
super.onResume();
startLockTask();
}
For the most part, this worked "reliably",
- If the application is pinned and the user lets the device screen time out (or presses the power/lock button), the device will enter sleep and next time he tries to awaken the device (via the power/lock button) the pinned app will pop into view again (without the user having to enter the passcode that he obviously doesn't know).
- On the other hand if the user attempts to unpin it, he/she is presented with the lock screen and the passcode.
So far so good.
However some times (and this is puzzling me) when the user tries to awaken the device, instead of going directly to the pinned app, the device will display the lock screen and ask the user for the passcode!
I'm not sure why the inconsistency in behavior (ie on wake, it sometimes asks for passcode, while other times goes directly to the pinned app) and couldn't find any mention anywhere of such a behavior.
Any input will be greatly appreciated!
Thanks!
Apparently every time the device wakes up, the startLockTask() is being executed. In case it was already in pinned mode this would run again, and cause issues.
Eventually, I tested out my initial assumption and edited the onResume() function to perform a startLockTask() only if it is not pinned already.
This seems to have solved the problems (even though I don't understand why it this behavior). Would be glad if somebody could explain this.
I'm posting the answer here for anybody who bumps into this issue.
onResume()
#Override
protected void onResume() {
super.onResume();
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP) {
if (!isAppInLockTaskMode()) {
startLockTask();
}
}
}
isAppInLockTaskMode() was taken from enter link description here and solves the issue for different API versions.
public boolean isAppInLockTaskMode() {
ActivityManager activityManager=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) { // When SDK version is 23
int lockTaskMode=activityManager.getLockTaskModeState();
return lockTaskMode != ActivityManager.LOCK_TASK_MODE_NONE ? true : false;
}
else if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP &&
Build.VERSION.SDK_INT< Build.VERSION_CODES.M) {
//When SDK version <=21 and <23. This API is deprecated in 23.
return activityManager.isInLockTaskMode();
}
else {
return false;
}
}

Android - Turn off screen without going in StandBy Mode

I know that this question has been asked a lot of times but it has never been answered satisfactorily.
My problem is the following:
I have an activity which prevents the screen from turning off for a predefined amount of time.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
When the predefined time is over I show a dialog with a countdown to inform the user that the display will turn off in 10 seconds if he doesnt press "cancel".
I managed to turn off the display but the phone always switches into StandBy-Mode.
For switching off I used:
Window mywindow = getWindow();
WindowManager.LayoutParams lp = mywindow.getAttributes();
lp.screenBrightness = 0.0f;
mywindow.setAttributes(lp);
Is there any possibility to completely darken the display without going to StandBy-Mode (which pauses the activity).
My goal is that the user should be able to just tap the display to brighten up the screen again. So the activity has to remain in an active state.
A similar question has been asked here.
Since this question is almost a year old I am hoping that maybe somebody managed to this in the mean time.
Lots of greetings
Siggy
Seems like it isn't possible to turn off the screen AND reactivate just by touching the display.
My new approach now:
private WakeLock screenWakeLock;
PowerManager pm = PowerManager.getSystemService(Context.POWER_SERVICE);
screenWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"screenWakeLock");
screenWakeLock.acquire();
The PARTIAL_WAKE_LOCK keeps the CPU running but allows the display to shut down.
When power or home button is pressed the display turns on again and the activity becomes visible again (without having to "slide-to-unlock" or sth. else).
Don't forget to release the screenWakeLock.
In my case I did it in the onResume() of the activity:
if (screenWakeLock != null) {
if(screenWakeLock.isHeld())
screenWakeLock.release();
screenWakeLock = null;
}
Maybe this helps someone with a similar problem in the future.
Note : i wasn't able to work with WAKELOCK
I have figured a workaround which involves changing SCREEN_OFF_TIMEOUT. Use the below
code to achieve it.
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, 10);
this sets 10 milliseconds as timeout for screen_timeout SYSTEM-WIDE .
Now you might be troubled by SYSTEM-WIDE changes brought upon by this. To work around that you can get the default screen_timeout_time and save it to a variable then set back the System's SCREEN_OFF_TIMEOUT at finish() of your activity.
Before setting 10ms as our screen_timeout get SCREEN_OFF_TIMEOUT,
int defaultScreenTimeout= android.provider.Settings.
System.getInt(getContentResolver(),Settings.System.SCREEN_OFF_TIMEOUT,-1);
Now when you have finished with your changes or when your activity ends you may set the SCREEN_OFF_TIMEOUT back .
#Override
public void finish(){
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_OFF_TIMEOUT, defaultScreenTimeout);
super.finish();
}

Using FLAG_SHOW_WHEN_LOCKED with disableKeyguard() in secured Android lock screen

The Context
Recently, I have been looking for reliable ways to control a secured Android Keyguard. Mainly to display a custom lock screen. I know that Google had stated custom lock screens are not officially supported by the platform and should expect things to break, however, with the existing APIs, I believe there must be ways to do this. I have done tons of research for about a week but still having problem here and there. What I have implemented, assuming a secured Keyguard is enabled, so far are,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a window) to be displayed on screen on, putting the Keyguard behind, and all unsafe actions are prevented. Notification panel is disabled, finishing the activity will bring up the Keyguard. I implemented as following in my lock screen activity.
#Override
public void onAttachedToWindow() {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
KeyguardManager, KeyguardManager.KeyguardLock are deprecated classes, but they still work all the way to Jelly Bean. To do this, I have a Service that handles two things, holding a static KeyguardManager and the related objects, and have it hold a BroadcastReceiver to receive Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF. (all the objects are initialized properly)
For ScreenReceiver
public static synchronized void disableKeyguard() {
if ( isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.disableKeyguard();
isLocked = false;
}
}
public static synchronized void reenableKeyguard() {
if ( !isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.reenableKeyguard();
keyguardLock = null;
isLocked = true;
}
}
For BroadcastReceiver
#Override
public void onReceive( Context context, Intent intent ) {
if ( intent.getAction().equals(Intent.ACTION_SCREEN_ON) ) {
Intent start = new Intent(context, LockScreen.class);
start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(start);
} else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ) {
ScreenReceiverService.reenableKeyguard();
}
}
For LockScreenActivity, when the user had input the correct passcode,
window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ScreenReceiverService.disableKeyguard();
finish();
The Problem
Things that works
ACTION_ON and ACTION_OFF are received reliably.
LockScreenActivity is shown before the Keyguard (without telephone state handling yet)
Notification cannot be pulled down, exiting the activity in any way would display the lockscreen.
Things that does not work
After I disable Keyguard and call finish(), my app exits and homescreen or the last activity before the screen went off is shown. However, whenever I press the Home Key, the Keyguard will flash into the screen, quickly dismissing itself immediately, and the normal Home Key function/event is not handled (will not return to homescreen after flashing). This is observed when I rapidly tapped the Home Key repeatedly.
I even looked into the Android source code to find out the Home Key handling, but it is never sent to third-party applications unless the window type is WindowManager.LayoutParams.TYPE_KEYGUARD or WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG, which will throw SecurityException on 4.0+ even it worked on earlier platforms. And for the Keyguard, I have declared DISABLE_KEYGUARD permission use this shouldn't be the problem. My guess is the flag FLAG_SHOW_WHEN_LOCKED will tell the system to handle to Keyguard in some ways that would conflict with other disable calls. Since this flag is mostly used for Alarm/SMS type application, which is to show limited information to the user, then dismiss themselves and bring up the Keyguard. But in my case, having the user unlock my lock screen then unlock the system lockscreen simply defeats the purpose of my app.
So the question is why would the Keyguard flashes whenever I press Home after I disabled it? Is there any workaround/solution for this issue?
P.S. Thank you for reading such a long question. This is my first time asking a question here, if there is anything that I did wrong, please tell me (i.e. format, grammar, code convention, tags, etc.). Also I had no experience with any programming knowledge, I started with Android before I know what Java is. So I have not taken any proper course/training yet, this community is awesome and often help people like I even if they are simple questions, and of course watching Google I/O videos, reading blogs, read others' code help me a lot. So please tolerate any dumb mistakes/obvious bugs/stupid questions. I am only 16. ^_^"
I have used this with some success in both Gingerbread and ICS to open my activity (via a background service which is starting it). In the activity being started:
#Override
public void onAttachedToWindow() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I had the same problem for the click of HOME button while unlocking the device. This can be solved by reseting the password to blank (ie "") :
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;
devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
devicePolicyManager.resetPassword("",DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
and then disabling the keygaurd :
this.keyGuardLock = ((KeyguardManager)getSystemService("keyguard")).newKeyguardLock("keyguard");
keyGuardLock.disableKeyguard();
Hope this solved your problem. \m/ keep coding!
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a
window) to be displayed on screen on, putting the Keyguard behind
I tried to get this but my activity always preceded by the system lock screen. isOrderdBroadcast() says that ACTION_SCREEN_NO is an ordered broadcast.
I added flag to the activity :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
in onAttachedView(). But still the system lock is getting the preference over my Custom screen lock activity.
How did you get your activity before the system lock screen?
EDIT
On a hindsight, I think my understanding of the lock screen concept was wrong. My broadcast receiver was getting the broadcast first. But what was showing before that was the system lock screen launched when SCREEN_OFF is received. Fixed that problem as of now.
But stumped by the ambiguity of home button behavior. This won't be a problem in post ICS devices as all hard buttons are discouraged.
In your LockScreenActivity, ending the validation code by finish(); kills the LockscreenActivity and thus the whole app. Instead of that, you could just launch back your main activity (or any other) like this :
startActivity(new Intent(LockScreenActivity.this, MainActivity.class));
If AOSP is in your control then you need to set the simple flag and keyguard() is gone for good.
Here is the details to do that, get into the file
"overlay/frameworks/base/packages/SystemUI/res/values/config.xml"
and search for "config_enableKeyguardService" then set the flag to false.
NO MORE keyGuard, pheww

android - how do i dismiss the lock screen on new android version?

i need to dismiss the default lock screen on some cases . it is possible using KeyguardManager.KeyguardLock , but it's deprecated and might not be available on the new android v4 version (ICS) .
instead , here android docs i read that i should use "Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED "
so I've tried playing with them , but no matter what i try , after closing the activity that use those flags , i get back to the default lock screen , even if it's not the secure one .
what is going on , and how should i fix it?
I've also read that there are some workarounds , by replacing the launcher application , but this is a weird fix , and requires the user's intervention , even though the application is not really a launcher application , and i'm not sure how i would
such a feature might even be used for replacing the lock screen with your own customized lock screen
Check our issue 8778, which seems to be similar to what you are facing. The FLAG_DISMISS_KEYGUARD constant will ONLY work if you are using an Activity, it must not be PixelFormat.TRANSLUCENT, and it is forcing itself to be full screen, hiding system decoration like the status bar. The KeyguardManager API is deprecated, but it still works on Android 4.0 ICS and it is arguably your best bet at disabling the lock screen reliably and from any part of your code (even a background Service).
In case somebody needs Kotlin code that's up to date:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
val keyguardManager = getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
keyguardManager.requestDismissKeyguard(this, null)
setShowWhenLocked(true)
} else {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
}
And if you need a longer explanation on keyguard flags, check here.

Air for Android app is keeping screen awake when not in app

I have an air for android app that has event listeners for ACTIVATE and DEACTIVATE, inside the activate I tell the screen to stay awake and in the deactivate I tell it to go back to normal like so :
stage.addEventListener(Event.DEACTIVATE, deactivateHandler);
stage.addEventListener(Event.ACTIVATE, activateHandler);
protected function deactivateHandler(event:Event):void{
SFX.disableSound();
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.NORMAL;
}
protected function activateHandler(event:Event):void{
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
}
But the screen will stay awake at all times even when on the android home screen unless you force close the app... any ideas?
Thanks
I had this exact problem. My app was able to keep the screen on by setting systemIdleMode to SystemIdleMode.KEEP_AWAKE, and it would force the screen to stay on. However, when the app tried to set the systemIdleMode back to SystemIdleMode.NORMAL, so the screen could turn off, the screen was still staying on.
What turned out to be the problem in my case is was a missing android permission. I had already added this permission to my app XML file, so that i could use the keep-alive function:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
Turns out this is not the only permission you need. I added this permission also:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
And suddenly my app was able to let the screen be turned off again.
You could try using this in your java code:
// Gets one of the views visible on the screen and sets keepScreenOn to true.
// This means the screen will stay on as long as the specified view is visible.
this.findViewById(R.id.viewId).setKeepScreenOn(true);
Or you could put android:keepScreenOn="true" in your layout.

Categories

Resources