wakelock action

This commit is contained in:
jens 2022-07-02 01:38:39 +02:00
parent f3fac2f4e8
commit 99faa2f7ef
12 changed files with 295 additions and 19 deletions

View File

@ -169,6 +169,7 @@
<activity android:name=".ActivityManageActionRunExecutable" /> <activity android:name=".ActivityManageActionRunExecutable" />
<activity android:name=".ActivityManageActionWifi" /> <activity android:name=".ActivityManageActionWifi" />
<activity android:name=".ActivityManageTriggerTethering" /> <activity android:name=".ActivityManageTriggerTethering" />
<activity android:name=".ActivityManageActionWakeLock" />
<activity <activity
android:name=".ActivityMainTabLayout" android:name=".ActivityMainTabLayout"
android:launchMode="singleTask"> android:launchMode="singleTask">

View File

@ -167,6 +167,7 @@
<activity android:name=".ActivityManageActionRunExecutable" /> <activity android:name=".ActivityManageActionRunExecutable" />
<activity android:name=".ActivityManageActionWifi" /> <activity android:name=".ActivityManageActionWifi" />
<activity android:name=".ActivityManageTriggerTethering" /> <activity android:name=".ActivityManageTriggerTethering" />
<activity android:name=".ActivityManageActionWakeLock" />
<activity <activity
android:name=".ActivityMainTabLayout" android:name=".ActivityMainTabLayout"
android:launchMode="singleTask"> android:launchMode="singleTask">

View File

@ -154,6 +154,7 @@
<activity android:name=".ActivityManageActionRunExecutable" /> <activity android:name=".ActivityManageActionRunExecutable" />
<activity android:name=".ActivityManageActionWifi" /> <activity android:name=".ActivityManageActionWifi" />
<activity android:name=".ActivityManageTriggerTethering" /> <activity android:name=".ActivityManageTriggerTethering" />
<activity android:name=".ActivityManageActionWakeLock" />
<activity <activity
android:name=".ActivityMainTabLayout" android:name=".ActivityMainTabLayout"
android:launchMode="singleTask"> android:launchMode="singleTask">

View File

@ -53,6 +53,7 @@ public class Action
closeNotification, closeNotification,
sendBroadcast, sendBroadcast,
runExecutable, runExecutable,
wakelock,
sendTextMessage; sendTextMessage;
public String getFullName(Context context) public String getFullName(Context context)
@ -127,6 +128,8 @@ public class Action
return context.getResources().getString(R.string.sendBroadcast); return context.getResources().getString(R.string.sendBroadcast);
case runExecutable: case runExecutable:
return context.getResources().getString(R.string.runExecutable); return context.getResources().getString(R.string.runExecutable);
case wakelock:
return context.getResources().getString(R.string.keepDeviceAwake);
default: default:
return "Unknown"; return "Unknown";
} }
@ -273,6 +276,9 @@ public class Action
case runExecutable: case runExecutable:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.runExecutable)); returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.runExecutable));
break; break;
case wakelock:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.keepDeviceAwake) + " (" + String.valueOf(getParameter1()) + ")");
break;
default: default:
returnString.append(action.toString()); returnString.append(action.toString());
} }

View File

@ -1142,25 +1142,7 @@ public class Actions
{ {
Miscellaneous.logEvent("i", "waitBeforeNextAction", "waitBeforeNextAction for " + String.valueOf(waitTime) + " milliseconds.", 4); Miscellaneous.logEvent("i", "waitBeforeNextAction", "waitBeforeNextAction for " + String.valueOf(waitTime) + " milliseconds.", 4);
try wakeLockStart(60000);
{
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)
{
}
try try
{ {
Thread.sleep(waitTime); Thread.sleep(waitTime);
@ -2112,4 +2094,56 @@ public class Actions
return false; 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;
}
} }

View File

@ -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();
}
});
}
}

View File

@ -129,6 +129,8 @@ public class ActivityManageRule extends Activity
final static int requestCodeActionSetWifiEdit = 816; final static int requestCodeActionSetWifiEdit = 816;
final static int requestCodeTriggerTetheringAdd = 817; final static int requestCodeTriggerTetheringAdd = 817;
final static int requestCodeTriggerTetheringEdit = 818; final static int requestCodeTriggerTetheringEdit = 818;
final static int requestCodeActionWakeLockAdd = 819;
final static int requestCodeActionWakeLockEdit = 820;
public static ActivityManageRule getInstance() public static ActivityManageRule getInstance()
{ {
@ -393,6 +395,12 @@ public class ActivityManageRule extends Activity
activityEditSendBroadcastIntent.putExtra(intentNameActionParameter2, a.getParameter2()); activityEditSendBroadcastIntent.putExtra(intentNameActionParameter2, a.getParameter2());
startActivityForResult(activityEditSendBroadcastIntent, requestCodeActionSendBroadcastEdit); startActivityForResult(activityEditSendBroadcastIntent, requestCodeActionSendBroadcastEdit);
break; 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: case runExecutable:
Intent activityEditRunExecutableIntent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class); Intent activityEditRunExecutableIntent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class);
activityEditRunExecutableIntent.putExtra(intentNameActionParameter1, a.getParameter1()); activityEditRunExecutableIntent.putExtra(intentNameActionParameter1, a.getParameter1());
@ -1551,6 +1559,17 @@ public class ActivityManageRule extends Activity
this.refreshActionList(); 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) else if(requestCode == requestCodeActionControlMediaAdd)
{ {
if(resultCode == RESULT_OK) if(resultCode == RESULT_OK)
@ -1620,6 +1639,21 @@ public class ActivityManageRule extends Activity
this.refreshActionList(); 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) else if(requestCode == requestCodeActionRunExecutableEdit)
{ {
if(resultCode == RESULT_OK) if(resultCode == RESULT_OK)
@ -1884,6 +1918,8 @@ public class ActivityManageRule extends Activity
items.add(new Item(typesLong[i].toString(), R.drawable.notification)); items.add(new Item(typesLong[i].toString(), R.drawable.notification));
else if(types[i].toString().equals(Action_Enum.sendBroadcast.toString())) else if(types[i].toString().equals(Action_Enum.sendBroadcast.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.megaphone)); 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())) else if(types[i].toString().equals(Action_Enum.runExecutable.toString()))
items.add(new Item(typesLong[i].toString(), R.drawable.script)); items.add(new Item(typesLong[i].toString(), R.drawable.script));
else if(types[i].toString().equals(Action_Enum.sendTextMessage.toString())) 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); Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class);
startActivityForResult(intent, requestCodeActionRunExecutableAdd); 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())) else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.controlMediaPlayback.toString()))
{ {
newAction.setAction(Action_Enum.controlMediaPlayback); newAction.setAction(Action_Enum.controlMediaPlayback);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 679 B

After

Width:  |  Height:  |  Size: 679 B

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="@dimen/default_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="2"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginBottom="@dimen/default_margin"
android:text="@string/keepDeviceAwake" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/direction" />
<RadioGroup>
<RadioButton
android:id="@+id/rbWakeLockActivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/activate" />
<RadioButton
android:id="@+id/rbWakeLockDeactivate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/deactivate" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView
android:text="@string/wakeLockTimeout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/chkWakeLockTimeout"
android:text=""
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/duration" />
<EditText
android:id="@+id/etWakeLockDuration"
android:inputType="number"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</TableRow>
</TableLayout>
<Button
android:id="@+id/bSaveWakelock"
android:layout_marginTop="@dimen/default_margin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save" />
</LinearLayout>

View File

@ -836,4 +836,7 @@
<string name="tetheringState">Tethering state</string> <string name="tetheringState">Tethering state</string>
<string name="regularExpressionsIfEquals">If \"equals\" is selected, you may enter a regular expression.</string> <string name="regularExpressionsIfEquals">If \"equals\" is selected, you may enter a regular expression.</string>
<string name="enter_a_number">Enter a number.</string> <string name="enter_a_number">Enter a number.</string>
<string name="duration">Duration [ms]</string>
<string name="keepDeviceAwake">Keep device awake</string>
<string name="wakeLockTimeout">Specify timeout</string>
</resources> </resources>