broadcast trigger

This commit is contained in:
jens 2022-05-23 20:28:56 +02:00
parent 51caae0794
commit 343cbba8f8
4 changed files with 122 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import android.widget.Toast;
import com.google.android.gms.location.DetectedActivity;
import com.jens.automation2.receivers.ActivityDetectionReceiver;
import com.jens.automation2.receivers.BroadcastListener;
import java.util.ArrayList;
import java.util.Calendar;
@ -348,6 +349,10 @@ public class Rule implements Comparable<Rule>
if(oneTrigger.getTimeFrame().repetition > 0)
return true;
}
else if(oneTrigger.getTriggerType().equals(Trigger.Trigger_Enum.broadcastReceived))
{
return oneTrigger.getTriggerParameter() == BroadcastListener.getInstance().hasBroadcastOccurredSince(oneTrigger.getTriggerParameter2(), getLastExecution());
}
}
return false;

View File

@ -253,7 +253,7 @@ public class Trigger
it contains the specific event of this trigger.
*/
return triggerParameter == BroadcastListener.getInstance().hasBroadcastOccurredSince(triggerParameter2, getParentRule().getLastExecution());
return triggerParameter == BroadcastListener.getInstance().hasBroadcastOccurred(triggerParameter2);
}
boolean checkNotification()

View File

@ -3,6 +3,7 @@ package com.jens.automation2.receivers;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import com.jens.automation2.ActivityPermissions;
@ -49,6 +50,13 @@ public class BroadcastListener extends android.content.BroadcastReceiver impleme
{
broadcastsCollection.add(new EventOccurrence(Calendar.getInstance(), intent.getAction()));
for(String key : intent.getExtras().keySet())
{
Miscellaneous.logEvent("i", "Broadcast extra", "Broadcast " + intent.getAction() + " has extra " + key + " and type " + intent.getExtras().get(key).getClass().getName(), 4);
// Object ob = intent.getExtras().get(key);
// Miscellaneous.logEvent("i", "Broadcast extra", "Broadcast " + intent.getAction() + " has extra " + key + " and type " + intent.getExtras().get(key).getClass().getName(), 4);
}
ArrayList<Rule> ruleCandidates = Rule.findRuleCandidates(Trigger.Trigger_Enum.broadcastReceived);
for(int i=0; i<ruleCandidates.size(); i++)
{
@ -62,11 +70,22 @@ public class BroadcastListener extends android.content.BroadcastReceiver impleme
return broadcastsCollection;
}
public boolean hasBroadcastOccurred(String event)
{
for(EventOccurrence eo : broadcastsCollection)
{
if(eo.event.equalsIgnoreCase(event))
return true;
}
return false;
}
public boolean hasBroadcastOccurredSince(String event, Calendar timeLimit)
{
for(EventOccurrence eo : broadcastsCollection)
{
if(eo.time.getTimeInMillis() > timeLimit.getTimeInMillis() && eo.event.equalsIgnoreCase(event))
if(eo.event.equalsIgnoreCase(event) && (timeLimit == null || eo.time.getTimeInMillis() > timeLimit.getTimeInMillis()))
return true;
}

View File

@ -1,11 +1,14 @@
package com.jens.automation2.receivers;
import android.Manifest;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;
@ -49,7 +52,7 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
screenStateIntentFilter = new IntentFilter();
screenStateIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
screenStateIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateIntentFilter.addAction(Intent.ACTION_USER_PRESENT);
screenStateIntentFilter.addAction(Intent.ACTION_USER_PRESENT); // also fired when device is unlocked
// Intent.ACTION_USER_UNLOCKED
}
@ -104,6 +107,21 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
ScreenStateReceiver.screenState = 0;
// if(LockScreenHelper.isScreenUnlocked(context))
// ;
// PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
// KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
// if (pm.isInteractive() && pm.isScreenOn() && keyguardManager.isKeyguardLocked() && keyguardManager.isDeviceLocked())
// {
// //do your stuff
// }
KeyguardManager kgMgr = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean unlocked = kgMgr.inKeyguardRestrictedInputMode();
Automat
}
else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
{
@ -159,4 +177,81 @@ public class ScreenStateReceiver extends BroadcastReceiver implements Automation
{
return new Trigger_Enum[] { Trigger_Enum.screenState };
}
public static class LockScreenHelper
{
private static final String TAG = LockScreenHelper.class.getCanonicalName();
/**
* 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.
*
* @param context Context
* @return boolean
*/
public static boolean isScreenUnlocked(Context context)
{
if (!isInteractive(context))
{
Log.i(TAG, "device is NOT interactive");
return false;
}
else
{
Log.i(TAG, "device is interactive");
}
if (!isDeviceProvisioned(context))
{
Log.i(TAG, "device is not provisioned");
return true;
}
Object keyguardService = context.getSystemService(Context.KEYGUARD_SERVICE);
return !((KeyguardManager) keyguardService).inKeyguardRestrictedInputMode();
}
/**
* @return Whether the screen of the device is interactive (screen may or may not be locked at the time).
*/
@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
{
return manager.isScreenOn();
}
}
/**
* @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)
{
return true;
}
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;
}
}
}