delayed lock screen

This commit is contained in:
jens 2022-05-26 15:50:39 +02:00
parent 29a93e0e43
commit ad18313284

View File

@ -3,12 +3,14 @@ package com.jens.automation2.receivers;
import android.Manifest; import android.Manifest;
import android.app.KeyguardManager; import android.app.KeyguardManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@ -21,6 +23,9 @@ import com.jens.automation2.Rule;
import com.jens.automation2.Trigger.Trigger_Enum; import com.jens.automation2.Trigger.Trigger_Enum;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class ScreenStateReceiver extends BroadcastReceiver implements AutomationListenerInterface public class ScreenStateReceiver extends BroadcastReceiver implements AutomationListenerInterface
{ {
@ -36,7 +41,7 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
public static BroadcastReceiver getScreenStateReceiverInstance() public static BroadcastReceiver getScreenStateReceiverInstance()
{ {
if(screenStateReceiverInstance == null) if (screenStateReceiverInstance == null)
screenStateReceiverInstance = new ScreenStateReceiver(); screenStateReceiverInstance = new ScreenStateReceiver();
return screenStateReceiverInstance; return screenStateReceiverInstance;
@ -44,14 +49,14 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
public static void startScreenStateReceiver(final AutomationService automationServiceRef) public static void startScreenStateReceiver(final AutomationService automationServiceRef)
{ {
if(!screenStateReceiverActive) if (!screenStateReceiverActive)
{ {
ScreenStateReceiver.automationServiceRef = automationServiceRef; ScreenStateReceiver.automationServiceRef = automationServiceRef;
if(screenStateReceiverInstance == null) if (screenStateReceiverInstance == null)
screenStateReceiverInstance = new ScreenStateReceiver(); screenStateReceiverInstance = new ScreenStateReceiver();
if(screenStateIntentFilter == null) if (screenStateIntentFilter == null)
{ {
screenStateIntentFilter = new IntentFilter(); screenStateIntentFilter = new IntentFilter();
screenStateIntentFilter.addAction(Intent.ACTION_SCREEN_OFF); screenStateIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
@ -66,11 +71,12 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
screenStateReceiverActive = true; screenStateReceiverActive = true;
} }
} }
public static void stopScreenStateReceiver() public static void stopScreenStateReceiver()
{ {
if(screenStateReceiverActive) if (screenStateReceiverActive)
{ {
if(screenStateReceiverInstance != null) if (screenStateReceiverInstance != null)
{ {
automationServiceRef.unregisterReceiver(screenStateReceiverInstance); automationServiceRef.unregisterReceiver(screenStateReceiverInstance);
screenStateReceiverInstance = null; screenStateReceiverInstance = null;
@ -110,45 +116,45 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
try try
{ {
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{ {
ScreenStateReceiver.screenState = 0; ScreenStateReceiver.screenState = 0;
// Method 1
Miscellaneous.logEvent("i", "ScreenStateReceiver", "Method 1: " + String.valueOf(LockScreenHelper.isScreenUnlocked(context)), 4);
if(!LockScreenHelper.isScreenUnlocked(context))
{
Intent lockedBroadcastIntent = new Intent();
lockedBroadcastIntent.setAction(broadcastScreenLocked);
context.sendBroadcast(lockedBroadcastIntent);
}
// Method 2 // Method 2
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); // PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
Miscellaneous.logEvent("i", "ScreenStateReceiver", "Method 2: " + String.valueOf(pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked()), 4); // Miscellaneous.logEvent("i", "ScreenStateReceiver", "Method 2: " + String.valueOf(pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked()), 4);
if (pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked()) // if (pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked())
{ // {
//
// }
} // Miscellaneous.logEvent("i", "ScreenStateReceiver", "pm.isInteractive(): " + String.valueOf(pm.isInteractive()), 4);
// Miscellaneous.logEvent("i", "ScreenStateReceiver", "pm.isScreenOn(): " + String.valueOf(pm.isScreenOn()), 4);
Miscellaneous.logEvent("i", "ScreenStateReceiver", "keyguardManager.isKeyguardLocked(): " + String.valueOf(keyguardManager.isKeyguardLocked()), 4);
Miscellaneous.logEvent("i", "ScreenStateReceiver", "keyguardManager.isDeviceLocked(): " + String.valueOf(keyguardManager.isDeviceLocked()), 4);
// Method 3 boolean locked = keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked();
KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if (locked)
boolean unlocked = kgMgr.inKeyguardRestrictedInputMode();
Miscellaneous.logEvent("i", "ScreenStateReceiver", "Method 3: " + String.valueOf(kgMgr.inKeyguardRestrictedInputMode()), 4);
if(!unlocked)
{ {
sendLockBroadcast(context);
}
else
{
// Lock may be activated delayed, not at power button press
ScreenLockMonitor mon = new ScreenLockMonitor();
mon.startMonitor();
} }
} }
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{ {
ScreenStateReceiver.screenState = 1; ScreenStateReceiver.screenState = 1;
} }
else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)) else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT))
{ {
ScreenStateReceiver.screenState = 2; ScreenStateReceiver.screenState = 2;
} }
else if(intent.getAction().equals(broadcastScreenLocked)) else if (intent.getAction().equals(broadcastScreenLocked))
{ {
ScreenStateReceiver.screenState = 3; ScreenStateReceiver.screenState = 3;
} }
@ -157,15 +163,15 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
Miscellaneous.logEvent("e", "ScreenStateReceiver", "Unknown state received: " + intent.getAction(), 3); Miscellaneous.logEvent("e", "ScreenStateReceiver", "Unknown state received: " + intent.getAction(), 3);
} }
} }
catch(Exception e) catch (Exception e)
{ {
Miscellaneous.logEvent("e", "ScreenStateReceiver", "Error receiving screen state: " + e.getMessage(), 3); Miscellaneous.logEvent("e", "ScreenStateReceiver", "Error receiving screen state: " + e.getMessage(), 3);
} }
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.screenState); ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger_Enum.screenState);
for(int i=0; i<ruleCandidates.size(); i++) for (int i = 0; i < ruleCandidates.size(); i++)
{ {
if(ruleCandidates.get(i).getsGreenLight(context)) if (ruleCandidates.get(i).getsGreenLight(context))
ruleCandidates.get(i).activate(automationServiceRef, false); ruleCandidates.get(i).activate(automationServiceRef, false);
} }
} }
@ -175,6 +181,7 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
{ {
ScreenStateReceiver.startScreenStateReceiver(automationService); ScreenStateReceiver.startScreenStateReceiver(automationService);
} }
@Override @Override
public void stopListener(AutomationService automationService) public void stopListener(AutomationService automationService)
{ {
@ -196,83 +203,65 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
@Override @Override
public Trigger_Enum[] getMonitoredTrigger() public Trigger_Enum[] getMonitoredTrigger()
{ {
return new Trigger_Enum[] { Trigger_Enum.screenState }; return new Trigger_Enum[]{Trigger_Enum.screenState};
} }
public static class LockScreenHelper class ScreenLockMonitor
{ {
private static final String TAG = LockScreenHelper.class.getCanonicalName(); long runs = 0;
final long maxRuns = 20;
final long interval = 1000;
/** Timer timer = new Timer();
* Determine if the screen is on and the device is unlocked;
* i.e. the user will see what is going on in the main activity. TimerTask task = new TimerTask()
*
* @param context Context
* @return boolean
*/
public static boolean isScreenUnlocked(Context context)
{ {
if (!isInteractive(context)) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
@Override
public void run()
{ {
Log.i(TAG, "device is NOT interactive"); KeyguardManager keyguardManager = (KeyguardManager) Miscellaneous.getAnyContext().getSystemService(Context.KEYGUARD_SERVICE);
return false;
boolean locked = keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked();
if (locked)
{
sendLockBroadcast(Miscellaneous.getAnyContext());
timer.purge();
timer.cancel();
} }
else else
{ {
Log.i(TAG, "device is interactive"); if (runs++ > maxRuns)
}
if (!isDeviceProvisioned(context))
{ {
Log.i(TAG, "device is not provisioned"); Miscellaneous.logEvent("w", "ScreenStateReceiver->ScreenLockMonitor", "Lock never came.", 4);
return true; timer.purge();
timer.cancel();
} }
}
}
};
Object keyguardService = context.getSystemService(Context.KEYGUARD_SERVICE); public void startMonitor()
return !((KeyguardManager) keyguardService).inKeyguardRestrictedInputMode(); {
} ContentResolver mResolver = Miscellaneous.getAnyContext().getContentResolver();
long lockscreen_timeout = 0;
/** if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1)
* @return Whether the screen of the device is interactive (screen may or may not be locked at the time). lockscreen_timeout = Settings.System.getInt(mResolver, "lock_screen_lock_after_timeout", 0);
*/
@SuppressWarnings("deprecation")
public static boolean isInteractive(Context context)
{
PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
{
return manager.isInteractive();
}
else else
{ lockscreen_timeout = Settings.Secure.getInt(mResolver, "lock_screen_lock_after_timeout", 0);
return manager.isScreenOn();
if(lockscreen_timeout > 0)
timer.schedule(task, lockscreen_timeout);
else
timer.scheduleAtFixedRate(task, 0, interval);
} }
} }
/** static void sendLockBroadcast(Context context)
* @return Whether the device has been provisioned (0 = false, 1 = true).
* On a multiuser device with a separate system user, the screen may be locked as soon as this
* is set to true and further activities cannot be launched on the system user unless they are
* marked to show over keyguard.
*/
private static boolean isDeviceProvisioned(Context context)
{ {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) Intent lockedBroadcastIntent = new Intent();
{ lockedBroadcastIntent.setAction(broadcastScreenLocked);
return true; context.sendBroadcast(lockedBroadcastIntent);
}
if (context == null)
{
return true;
}
if (context.getContentResolver() == null)
{
return true;
}
return android.provider.Settings.Global.getInt(context.getContentResolver(), android.provider.Settings.Global.DEVICE_PROVISIONED, 0) != 0;
}
} }
} }