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 00000000..4b093ca2 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/coffee.png differ diff --git a/app/src/main/res/drawable/megaphone.png b/app/src/main/res/drawable-hdpi/megaphone.png similarity index 100% rename from app/src/main/res/drawable/megaphone.png rename to app/src/main/res/drawable-hdpi/megaphone.png diff --git a/app/src/main/res/drawable/script.png b/app/src/main/res/drawable-hdpi/script.png similarity index 100% rename from app/src/main/res/drawable/script.png rename to app/src/main/res/drawable-hdpi/script.png diff --git a/app/src/main/res/layout/activity_manage_action_wakelock.xml b/app/src/main/res/layout/activity_manage_action_wakelock.xml new file mode 100644 index 00000000..c96fd074 --- /dev/null +++ b/app/src/main/res/layout/activity_manage_action_wakelock.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +