From 99faa2f7ef0780a8fda20a329c361eab2816e663 Mon Sep 17 00:00:00 2001 From: jens Date: Sat, 2 Jul 2022 01:38:39 +0200 Subject: [PATCH] wakelock action --- app/src/apkFlavor/AndroidManifest.xml | 1 + app/src/fdroidFlavor/AndroidManifest.xml | 1 + app/src/googlePlayFlavor/AndroidManifest.xml | 1 + .../java/com/jens/automation2/Action.java | 6 ++ .../java/com/jens/automation2/Actions.java | 72 +++++++++---- .../ActivityManageActionWakeLock.java | 99 ++++++++++++++++++ .../jens/automation2/ActivityManageRule.java | 42 ++++++++ app/src/main/res/drawable-hdpi/coffee.png | Bin 0 -> 2682 bytes .../{drawable => drawable-hdpi}/megaphone.png | Bin .../{drawable => drawable-hdpi}/script.png | Bin .../activity_manage_action_wakelock.xml | 89 ++++++++++++++++ app/src/main/res/values/strings.xml | 3 + 12 files changed, 295 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/jens/automation2/ActivityManageActionWakeLock.java create mode 100644 app/src/main/res/drawable-hdpi/coffee.png rename app/src/main/res/{drawable => drawable-hdpi}/megaphone.png (100%) rename app/src/main/res/{drawable => drawable-hdpi}/script.png (100%) create mode 100644 app/src/main/res/layout/activity_manage_action_wakelock.xml diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index 5c59e0cf..53d37d66 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -169,6 +169,7 @@ + diff --git a/app/src/fdroidFlavor/AndroidManifest.xml b/app/src/fdroidFlavor/AndroidManifest.xml index 47a480d7..0e7bb421 100644 --- a/app/src/fdroidFlavor/AndroidManifest.xml +++ b/app/src/fdroidFlavor/AndroidManifest.xml @@ -167,6 +167,7 @@ + diff --git a/app/src/googlePlayFlavor/AndroidManifest.xml b/app/src/googlePlayFlavor/AndroidManifest.xml index 8410205e..175ef7e1 100644 --- a/app/src/googlePlayFlavor/AndroidManifest.xml +++ b/app/src/googlePlayFlavor/AndroidManifest.xml @@ -154,6 +154,7 @@ + diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index ff8b1e14..32f8aa3f 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -53,6 +53,7 @@ public class Action closeNotification, sendBroadcast, runExecutable, + wakelock, sendTextMessage; public String getFullName(Context context) @@ -127,6 +128,8 @@ public class Action return context.getResources().getString(R.string.sendBroadcast); case runExecutable: return context.getResources().getString(R.string.runExecutable); + case wakelock: + return context.getResources().getString(R.string.keepDeviceAwake); default: return "Unknown"; } @@ -273,6 +276,9 @@ public class Action case runExecutable: returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.runExecutable)); break; + case wakelock: + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.keepDeviceAwake) + " (" + String.valueOf(getParameter1()) + ")"); + break; default: returnString.append(action.toString()); } diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 7c254013..57144f10 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -1142,25 +1142,7 @@ public class Actions { Miscellaneous.logEvent("i", "waitBeforeNextAction", "waitBeforeNextAction for " + String.valueOf(waitTime) + " milliseconds.", 4); - try - { - PowerManager powerManager = (PowerManager) Miscellaneous.getAnyContext().getSystemService(Context.POWER_SERVICE); - PowerManager.WakeLock fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK"); - fullWakeLock.acquire(); // turn on - try - { - Thread.sleep(60000); // turn on duration - } - catch (InterruptedException e) - { - e.printStackTrace(); - } - fullWakeLock.release(); - } - catch (Exception e) - { - } - + wakeLockStart(60000); try { Thread.sleep(waitTime); @@ -2112,4 +2094,56 @@ public class Actions return false; } + + public final static int wakeLockTimeoutDisabled = -1; + static boolean wakeLockStopRequested = false; + public static void wakeLockStart(long duration) + { + long waited = 0; + int step = 1000; + + if(duration < 0) + step = 99999; + + try + { + PowerManager powerManager = (PowerManager) Miscellaneous.getAnyContext().getSystemService(Context.POWER_SERVICE); + PowerManager.WakeLock fullWakeLock = powerManager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK"); + fullWakeLock.acquire(); // turn on + + do + { + + try + { + Thread.sleep(step); // turn on duration + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + + waited += step; + + if(false) //stop requested + { + Miscellaneous.logEvent("i", "WakeLockStart", "Stop requested.", 4); + wakeLockStopRequested = false; + break; + } + } + while(waited <= duration); + + fullWakeLock.release(); + } + catch (Exception e) + { + } + } + + public static void wakeLockStop() + { + Miscellaneous.logEvent("i", "WakeLockStart", "Requesting stop.", 4); + wakeLockStopRequested = true; + } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionWakeLock.java b/app/src/main/java/com/jens/automation2/ActivityManageActionWakeLock.java new file mode 100644 index 00000000..01a68d3f --- /dev/null +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionWakeLock.java @@ -0,0 +1,99 @@ +package com.jens.automation2; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.provider.MediaStore; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.RadioButton; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import org.apache.commons.lang3.StringUtils; + +public class ActivityManageActionWakeLock extends Activity +{ + RadioButton rbWakeLockActivate, rbWakeLockDeactivate; + CheckBox chkWakeLockTimeout; + EditText etWakeLockDuration; + Button bSaveWakelock; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manage_action_wakelock); + + rbWakeLockActivate = (RadioButton)findViewById(R.id.rbWakeLockActivate); + rbWakeLockDeactivate = (RadioButton)findViewById(R.id.rbWakeLockDeactivate); + chkWakeLockTimeout = (CheckBox)findViewById(R.id.chkWakeLockTimeout); + etWakeLockDuration = (EditText)findViewById(R.id.etWakeLockDuration); + bSaveWakelock = (Button)findViewById(R.id.bSaveWakelock); + + etWakeLockDuration.setEnabled(chkWakeLockTimeout.isChecked()); + + chkWakeLockTimeout.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() + { + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean wakeLockTimeoutSet) + { + etWakeLockDuration.setEnabled(wakeLockTimeoutSet); + + if(wakeLockTimeoutSet) + etWakeLockDuration.setText(String.valueOf(Actions.wakeLockTimeoutDisabled)); + } + }); + + if(getIntent().hasExtra(ActivityManageRule.intentNameActionParameter1)) + { + rbWakeLockActivate.setChecked(getIntent().getBooleanExtra(ActivityManageRule.intentNameActionParameter1, true)); + rbWakeLockDeactivate.setChecked(!getIntent().getBooleanExtra(ActivityManageRule.intentNameActionParameter1, false)); + + if(getIntent().hasExtra(ActivityManageRule.intentNameActionParameter2)) + { + if(Miscellaneous.isNumeric(getIntent().getStringExtra(ActivityManageRule.intentNameActionParameter2))) + { + long timeout = Long.parseLong((getIntent().getStringExtra(ActivityManageRule.intentNameActionParameter2))); + chkWakeLockTimeout.setChecked(timeout != Actions.wakeLockTimeoutDisabled); + etWakeLockDuration.setText(String.valueOf(timeout)); + } + else + { + chkWakeLockTimeout.setChecked(false); + etWakeLockDuration.setText(String.valueOf(Actions.wakeLockTimeoutDisabled)); + } + } + } + + bSaveWakelock.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + if(chkWakeLockTimeout.isChecked()) + { + if((StringUtils.isEmpty(etWakeLockDuration.getText().toString()) || Integer.parseInt(etWakeLockDuration.getText().toString()) <= 0)) + { + Toast.makeText(ActivityManageActionWakeLock.this, getResources().getString(R.string.enterAPositiveValidNonDecimalNumber), Toast.LENGTH_LONG).show(); + return; + } + + } + + Intent response = new Intent(); + response.putExtra(ActivityManageRule.intentNameActionParameter1, rbWakeLockActivate.isChecked()); + if(chkWakeLockTimeout.isChecked()) + response.putExtra(ActivityManageRule.intentNameActionParameter2, etWakeLockDuration.getText().toString()); + else + response.putExtra(ActivityManageRule.intentNameActionParameter2, String.valueOf(Actions.wakeLockTimeoutDisabled)); + setResult(RESULT_OK, response); + finish(); + } + }); + } +} diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index 0b3325b4..3ed6cead 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -129,6 +129,8 @@ public class ActivityManageRule extends Activity final static int requestCodeActionSetWifiEdit = 816; final static int requestCodeTriggerTetheringAdd = 817; final static int requestCodeTriggerTetheringEdit = 818; + final static int requestCodeActionWakeLockAdd = 819; + final static int requestCodeActionWakeLockEdit = 820; public static ActivityManageRule getInstance() { @@ -393,6 +395,12 @@ public class ActivityManageRule extends Activity activityEditSendBroadcastIntent.putExtra(intentNameActionParameter2, a.getParameter2()); startActivityForResult(activityEditSendBroadcastIntent, requestCodeActionSendBroadcastEdit); break; + case wakelock: + Intent activityEditWakeLockIntent = new Intent(ActivityManageRule.this, ActivityManageActionWakeLock.class); + activityEditWakeLockIntent.putExtra(intentNameActionParameter1, a.getParameter1()); + activityEditWakeLockIntent.putExtra(intentNameActionParameter2, a.getParameter2()); + startActivityForResult(activityEditWakeLockIntent, requestCodeActionWakeLockEdit); + break; case runExecutable: Intent activityEditRunExecutableIntent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class); activityEditRunExecutableIntent.putExtra(intentNameActionParameter1, a.getParameter1()); @@ -1551,6 +1559,17 @@ public class ActivityManageRule extends Activity this.refreshActionList(); } } + else if(requestCode == requestCodeActionWakeLockAdd) + { + if(resultCode == RESULT_OK) + { + newAction.setParentRule(ruleToEdit); + newAction.setParameter1(data.getBooleanExtra(intentNameActionParameter1, false)); + newAction.setParameter2(data.getStringExtra(intentNameActionParameter2)); + ruleToEdit.getActionSet().add(newAction); + this.refreshActionList(); + } + } else if(requestCode == requestCodeActionControlMediaAdd) { if(resultCode == RESULT_OK) @@ -1620,6 +1639,21 @@ public class ActivityManageRule extends Activity this.refreshActionList(); } } + else if(requestCode == requestCodeActionWakeLockEdit) + { + if(resultCode == RESULT_OK) + { + ruleToEdit.getActionSet().get(editIndex).setParentRule(ruleToEdit); + + if(data.hasExtra(intentNameActionParameter1)) + ruleToEdit.getActionSet().get(editIndex).setParameter1(data.getBooleanExtra(intentNameActionParameter1, true)); + + if(data.hasExtra(intentNameActionParameter2)) + ruleToEdit.getActionSet().get(editIndex).setParameter2(data.getStringExtra(intentNameActionParameter2)); + + this.refreshActionList(); + } + } else if(requestCode == requestCodeActionRunExecutableEdit) { if(resultCode == RESULT_OK) @@ -1884,6 +1918,8 @@ public class ActivityManageRule extends Activity items.add(new Item(typesLong[i].toString(), R.drawable.notification)); else if(types[i].toString().equals(Action_Enum.sendBroadcast.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.megaphone)); + else if(types[i].toString().equals(Action_Enum.wakelock.toString())) + items.add(new Item(typesLong[i].toString(), R.drawable.coffee)); else if(types[i].toString().equals(Action_Enum.runExecutable.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.script)); else if(types[i].toString().equals(Action_Enum.sendTextMessage.toString())) @@ -2058,6 +2094,12 @@ public class ActivityManageRule extends Activity Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class); startActivityForResult(intent, requestCodeActionRunExecutableAdd); } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.wakelock.toString())) + { + newAction.setAction(Action_Enum.wakelock); + Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionWakeLock.class); + startActivityForResult(intent, requestCodeActionWakeLockAdd); + } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.controlMediaPlayback.toString())) { newAction.setAction(Action_Enum.controlMediaPlayback); diff --git a/app/src/main/res/drawable-hdpi/coffee.png b/app/src/main/res/drawable-hdpi/coffee.png new file mode 100644 index 0000000000000000000000000000000000000000..4b093ca2d3a1f2ca1e5f80ed35dfa3721a6575a4 GIT binary patch literal 2682 zcmV-=3WfEFP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGeg#Z8wg#k+oC0PIf02*{fSaefwW^{L9 za%BKeVQFr3E>1;MAa*k@H7+qQF!XYv000T;Nklr)%&6~_C|)#*$dXWCBN zOlH#dgQuOuspD}PcZRkeH=Rst2-twY+zgmY0TXi-5*JB;K_DUS=3>CybO8YZN!&r8 z?elEdc3ED*3S;2IGZNBj_nhDRob#R)`jI3_iI=8mG*Ma@E0PAPJEXzdHfhb1Ej;cT z=)HkCI5yUq4x+w%#Jt#r&~Z?|=KcQ;cGhoi~m za+&-4`;EiH!?kbTym>i0J6pE6xL6npg|c>ccb^;{9wv#d^*;1YV4~4z(&px-0g0Ck z4i1`HTU$G8YikF~%F3n+3JTmgIXT|U%uKJrU~s3Wr_URp>f98+t0XaG&QD&>Fr>?` z#}Yr6k&)ra%F6QQ<>h&cii&0{D=Wv%X0yv~x7z@(W@Tk1Z*On!u_(TGQQTm-az3B0 z#OZXl*VNRE7Zw(Jki18fm`LT>j~ARpkAg3=lKdHuliV38iJpvARGkE-)+?luEnR zY8_F^;{QsU&rJNroBiNV{=7eb5@15&gn!WU+c!eihaYd+9)7&V=b~dy)#o=t&+h(w zjj*16o3LO=eS88G7p||buL}!VC(1{Ubb$f=N;E~g&1QRzn*X6+!1mdH|7kizkIs(eMQ*}`kxE24x^;D5cf8o2gk?9`-8z?z9>JDbb*S%#n5}0P5S{-+v;?Q!QO!=H}*_2?q9*U{7gaV1U|#)*yFZ zuWQoYVw|#;=gu@_ee14H`f9P_;jNX@`@ableR*@e@b(8`xv7ehizR}-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