770 lines
28 KiB
Java
Raw Normal View History

2021-02-16 13:42:49 +01:00
package com.jens.automation2;
import android.content.Context;
import android.os.AsyncTask;
2022-01-25 18:08:46 +01:00
import android.os.Build;
2021-02-16 13:42:49 +01:00
import android.util.Log;
2021-04-09 17:39:59 +02:00
import android.widget.Toast;
2021-02-16 13:42:49 +01:00
2021-12-09 18:03:00 +01:00
import org.apache.commons.lang3.StringUtils;
2021-02-16 13:42:49 +01:00
import java.util.ArrayList;
import java.util.Locale;
public class Action
{
2021-11-13 02:17:36 +01:00
Rule parentRule = null;
2021-03-28 20:33:44 +02:00
public static final String actionParameter2Split = "ap2split";
2022-01-27 11:34:04 +01:00
public static final String intentPairSeparator = "intPairSplit";
2021-07-04 15:53:24 +02:00
public static final String vibrateSeparator = ",";
2021-03-28 20:33:44 +02:00
2022-02-06 20:12:11 +01:00
public enum Action_Enum
{
setWifi,
setBluetooth,
setUsbTethering,
setWifiTethering,
setBluetoothTethering,
setDisplayRotation,
turnWifiOn,turnWifiOff,
turnBluetoothOn,turnBluetoothOff,
triggerUrl,
changeSoundProfile,
turnUsbTetheringOn,turnUsbTetheringOff,
turnWifiTetheringOn,turnWifiTetheringOff,
enableScreenRotation,disableScreenRotation,
startOtherActivity,
waitBeforeNextAction,
turnScreenOnOrOff,
setAirplaneMode,
setDataConnection,
speakText,
playMusic,
controlMediaPlayback,
setScreenBrightness,
playSound,
vibrate,
createNotification,
closeNotification,
2022-05-29 02:42:12 +02:00
sendBroadcast,
2022-05-29 20:14:50 +02:00
runExecutable,
2022-07-02 01:38:39 +02:00
wakelock,
2022-10-09 17:14:02 +02:00
setVariable,
2022-10-03 13:22:25 +02:00
startPhoneCall,
stopPhoneCall,
2023-03-12 23:57:54 +01:00
copyToClipboard,
2023-12-14 00:15:59 +01:00
takeScreenshot,
2023-12-19 23:52:28 +01:00
setLocationService,
2022-02-06 20:12:11 +01:00
sendTextMessage;
public String getFullName(Context context)
{
switch(this)
{
case setWifi:
return context.getResources().getString(R.string.actionSetWifi);
case setBluetooth:
return context.getResources().getString(R.string.actionSetBluetooth);
case setWifiTethering:
return context.getResources().getString(R.string.actionSetWifiTethering);
case setBluetoothTethering:
return context.getResources().getString(R.string.actionSetBluetoothTethering);
case setUsbTethering:
return context.getResources().getString(R.string.actionSetUsbTethering);
case setDisplayRotation:
return context.getResources().getString(R.string.actionSetDisplayRotation);
case turnWifiOn:
return context.getResources().getString(R.string.actionTurnWifiOn);
case turnWifiOff:
return context.getResources().getString(R.string.actionTurnWifiOff);
case turnBluetoothOn:
return context.getResources().getString(R.string.actionTurnBluetoothOn);
case turnBluetoothOff:
return context.getResources().getString(R.string.actionTurnBluetoothOff);
case triggerUrl:
return context.getResources().getString(R.string.actionTriggerUrl);
case changeSoundProfile:
return context.getResources().getString(R.string.actionChangeSoundProfile);
case turnUsbTetheringOn:
return context.getResources().getString(R.string.actionTurnUsbTetheringOn);
case turnUsbTetheringOff:
return context.getResources().getString(R.string.actionTurnUsbTetheringOff);
case turnWifiTetheringOn:
return context.getResources().getString(R.string.actionTurnWifiTetheringOn);
case turnWifiTetheringOff:
return context.getResources().getString(R.string.actionTurnWifiTetheringOff);
case enableScreenRotation:
return context.getResources().getString(R.string.actionEnableScreenRotation);
case disableScreenRotation:
return context.getResources().getString(R.string.actionDisableScreenRotation);
case startOtherActivity:
return context.getResources().getString(R.string.startOtherActivity);
case waitBeforeNextAction:
return context.getResources().getString(R.string.waitBeforeNextAction);
case turnScreenOnOrOff:
return context.getResources().getString(R.string.turnScreenOnOrOff);
case vibrate:
return context.getResources().getString(R.string.vibrate);
case setAirplaneMode:
return context.getResources().getString(R.string.airplaneMode);
case setDataConnection:
return context.getResources().getString(R.string.actionDataConnection);
case speakText:
return context.getResources().getString(R.string.actionSpeakText);
case playMusic:
return context.getResources().getString(R.string.actionPlayMusic);
case controlMediaPlayback:
return context.getResources().getString(R.string.actionMediaControl);
case playSound:
return context.getResources().getString(R.string.playSound);
case sendTextMessage:
return context.getResources().getString(R.string.sendTextMessage);
case setScreenBrightness:
return context.getResources().getString(R.string.setScreenBrightness);
case createNotification:
return context.getResources().getString(R.string.createNotification);
case closeNotification:
return context.getResources().getString(R.string.closeNotifications);
2022-05-29 02:42:12 +02:00
case sendBroadcast:
return context.getResources().getString(R.string.sendBroadcast);
2022-05-29 20:14:50 +02:00
case runExecutable:
return context.getResources().getString(R.string.runExecutable);
2022-07-02 01:38:39 +02:00
case wakelock:
return context.getResources().getString(R.string.keepDeviceAwake);
2022-10-09 17:14:02 +02:00
case setVariable:
return context.getResources().getString(R.string.setVariable);
2022-10-03 13:22:25 +02:00
case startPhoneCall:
return context.getResources().getString(R.string.startPhoneCall);
case stopPhoneCall:
return context.getResources().getString(R.string.endPhoneCall);
2023-03-12 23:57:54 +01:00
case copyToClipboard:
return context.getResources().getString(R.string.copyTextToClipboard);
2023-12-14 00:15:59 +01:00
case takeScreenshot:
return context.getResources().getString(R.string.takeScreenshot);
2023-12-19 23:52:28 +01:00
case setLocationService:
return context.getResources().getString(R.string.setLocationService);
2022-02-06 20:12:11 +01:00
default:
return "Unknown";
}
}
};
2021-02-16 13:42:49 +01:00
private Action_Enum action;
private boolean parameter1 = false;
private String parameter2 = "";
public Action_Enum getAction()
{
return action;
}
public void setAction(Action_Enum action)
{
this.action = action;
}
public boolean getParameter1()
{
return parameter1;
}
public void setParameter1(boolean parameter1)
{
this.parameter1 = parameter1;
}
public String getParameter2()
{
return parameter2;
}
public void setParameter2(String parameter)
{
this.parameter2 = parameter;
}
public String toStringShort()
{
String returnString = action.toString();
return returnString;
}
@Override
public String toString()
{
StringBuilder returnString = new StringBuilder();
2022-01-05 23:27:27 +01:00
2022-01-14 19:57:32 +01:00
try
2021-03-28 20:33:44 +02:00
{
2022-01-14 19:57:32 +01:00
switch (getAction())
{
case setWifi:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnWifiOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnWifiOff));
break;
case setBluetooth:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothOff));
break;
case setUsbTethering:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnUsbTetheringOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnUsbTetheringOff));
break;
case setWifiTethering:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnWifiTetheringOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnWifiTetheringOff));
break;
case setBluetoothTethering:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothTetheringOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnBluetoothTetheringOff));
break;
case setDisplayRotation:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionEnableScreenRotation));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionDisableScreenRotation));
break;
case setAirplaneMode:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnAirplaneModeOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTurnAirplaneModeOff));
break;
case setDataConnection:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionSetDataConnectionOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionSetDataConnectionOff));
break;
case startOtherActivity:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.startOtherActivity));
break;
case triggerUrl:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionTriggerUrl));
break;
case speakText:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionSpeakText));
break;
case playMusic:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionPlayMusic));
break;
2022-01-24 20:13:35 +01:00
case controlMediaPlayback:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionMediaControl));
break;
2022-01-14 19:57:32 +01:00
case sendTextMessage:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.sendTextMessage));
break;
case turnScreenOnOrOff:
if (getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.turnScreenOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.turnScreenOff));
break;
case playSound:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.playSound));
break;
case changeSoundProfile:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionChangeSoundProfile));
break;
case waitBeforeNextAction:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.waitBeforeNextAction));
break;
case setScreenBrightness:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.setScreenBrightness));
break;
case createNotification:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.createNotification));
break;
case closeNotification:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.closeNotifications));
break;
2022-05-29 02:42:12 +02:00
case sendBroadcast:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.sendBroadcast));
break;
2022-05-29 20:14:50 +02:00
case runExecutable:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.runExecutable));
break;
2022-07-02 01:38:39 +02:00
case wakelock:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.keepDeviceAwake) + " (" + String.valueOf(getParameter1()) + ")");
break;
2022-10-09 17:14:02 +02:00
case setVariable:
String[] variableParams = getParameter2().split(actionParameter2Split);
String addition;
if (variableParams.length >= 2)
addition = " (key: " + variableParams[0] + ", value: " + variableParams[1] + ")";
else
addition = " (delete key: " + variableParams[0] + ")";
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.setVariable) + addition);
break;
2022-10-03 13:22:25 +02:00
case startPhoneCall:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.startPhoneCall));
break;
case stopPhoneCall:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.endPhoneCall));
2022-10-02 18:59:54 +02:00
break;
2023-03-12 23:57:54 +01:00
case copyToClipboard:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.copyTextToClipboard));
2023-12-14 00:15:59 +01:00
break;
case takeScreenshot:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.takeScreenshot));
break;
2023-12-19 23:52:28 +01:00
case setLocationService:
if (this.getParameter1())
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionSetLocationServiceOn));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.actionSetLocationServiceOff));
break;
2022-01-14 19:57:32 +01:00
default:
returnString.append(action.toString());
}
if (this.getAction().equals(Action_Enum.triggerUrl))
{
String[] components = parameter2.split(";");
if (components.length >= 3)
{
returnString.append(": " + components[2]);
if (parameter1)
returnString.append(" " + Miscellaneous.getAnyContext().getResources().getString(R.string.usingAuthentication) + ".");
}
2022-01-05 23:27:27 +01:00
else
2022-01-14 19:57:32 +01:00
returnString.append(": " + components[0]);
}
else if (this.getAction().equals(Action_Enum.startOtherActivity))
{
2022-01-27 11:34:04 +01:00
returnString.append(": " + parameter2.replace(Action.intentPairSeparator, "/"));
2022-01-14 19:57:32 +01:00
}
else if (this.getAction().equals(Action_Enum.sendTextMessage))
{
String[] components = parameter2.split(Actions.smsSeparator);
if (components.length >= 2)
{
returnString.append(" " + Miscellaneous.getAnyContext().getResources().getString(R.string.toNumber) + " " + components[0]);
2021-03-29 16:36:21 +02:00
2022-01-14 19:57:32 +01:00
returnString.append(". " + Miscellaneous.getAnyContext().getResources().getString(R.string.message) + ": " + components[1]);
}
}
else if (this.getAction().equals(Action_Enum.setScreenBrightness))
2021-02-16 13:42:49 +01:00
{
2022-01-14 19:57:32 +01:00
returnString.append(" " + Miscellaneous.getAnyContext().getResources().getString(R.string.to) + " ");
if (parameter1)
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.brightnessAuto));
else
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.brightnessManual));
returnString.append(" / " + Integer.parseInt(parameter2) + "%");
2021-02-16 13:42:49 +01:00
}
2022-01-14 19:57:32 +01:00
else if (this.getAction().equals(Action_Enum.closeNotification))
2021-02-16 13:42:49 +01:00
{
2022-01-14 19:57:32 +01:00
returnString.append(" " + Miscellaneous.getAnyContext().getResources().getString(R.string.from) + " ");
String parts[] = this.getParameter2().split(Action.actionParameter2Split);
2022-01-15 01:41:00 +01:00
if (parts[0].equals(Trigger.anyAppString))
2022-01-14 19:57:32 +01:00
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.anyApp));
else
returnString.append(parts[0]);
if (!StringUtils.isBlank(parts[2]))
2022-01-15 01:41:00 +01:00
returnString.append(", " + Miscellaneous.getAnyContext().getResources().getString(R.string.ifString) + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.title) + " " + Trigger.getMatchString(parts[1]) + " " + parts[2]);
2022-01-14 19:57:32 +01:00
2022-01-15 01:41:00 +01:00
if (parts.length > 4 && !StringUtils.isBlank(parts[4]))
returnString.append(", " + Miscellaneous.getAnyContext().getResources().getString(R.string.ifString) + " " + Miscellaneous.getAnyContext().getResources().getString(R.string.text) + " " + Trigger.getMatchString(parts[3]) + " " + parts[4]);
2022-07-24 14:22:17 +02:00
2022-10-09 17:14:02 +02:00
if (parts.length >= 6)
2022-07-24 14:22:17 +02:00
{
2022-10-09 17:14:02 +02:00
if (!parts[5].equals(ActivityManageActionCloseNotification.dismissRegularString))
2022-07-24 14:22:17 +02:00
{
returnString.append(" " + String.format(Miscellaneous.getAnyContext().getResources().getString(R.string.withButton), parts[5]));
}
}
2022-06-01 22:36:30 +02:00
}
2022-10-09 17:14:02 +02:00
else if (this.getAction().equals(Action_Enum.setWifi))
2022-06-01 22:36:30 +02:00
{
2022-10-09 17:14:02 +02:00
if (!StringUtils.isEmpty(this.parameter2))
2022-06-01 22:36:30 +02:00
{
boolean useRoot = Boolean.parseBoolean(this.parameter2);
2022-10-09 17:14:02 +02:00
if (useRoot)
2022-06-01 22:36:30 +02:00
returnString.append(" " + Miscellaneous.getAnyContext().getResources().getString(R.string.usingRoot));
}
2021-02-16 13:42:49 +01:00
}
2022-10-09 17:14:02 +02:00
else if (this.getAction().equals(Action_Enum.controlMediaPlayback))
2022-01-24 20:13:35 +01:00
{
2022-01-25 18:08:46 +01:00
returnString.append(": ");
2022-01-24 20:13:35 +01:00
switch (this.getParameter2())
{
case "0":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.playPause));
break;
case "1":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.play));
break;
case "2":
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.pause));
break;
case "3":
2022-01-26 22:53:02 +01:00
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.stop));
2022-01-24 20:13:35 +01:00
break;
case "4":
2022-01-26 22:53:02 +01:00
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.previous));
break;
case "5":
2022-01-24 20:13:35 +01:00
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.next));
break;
2022-01-25 18:08:46 +01:00
default:
returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.unknown));
2022-01-24 20:13:35 +01:00
}
}
2022-10-09 17:14:02 +02:00
else if (this.getAction().equals(Action_Enum.sendBroadcast))
2022-06-07 17:18:35 +02:00
{
returnString.append(": " + parameter2.replace(Action.actionParameter2Split, "; ").replace(Action.intentPairSeparator, "/"));
}
2023-03-15 23:27:27 +01:00
else if(this.getAction().equals(Action_Enum.setVariable) || this.getAction().equals(Action_Enum.copyToClipboard))
2022-10-09 17:14:02 +02:00
; // it's completed further above already
2022-01-14 19:57:32 +01:00
else if (parameter2 != null && parameter2.length() > 0)
returnString.append(": " + parameter2.replace(Action.actionParameter2Split, "; "));
2021-02-16 13:42:49 +01:00
}
2022-01-14 19:57:32 +01:00
catch (Exception e)
2021-02-16 13:42:49 +01:00
{
2022-01-14 19:57:32 +01:00
returnString.append(": " + Miscellaneous.getAnyContext().getResources().getString(R.string.error));
2021-02-16 13:42:49 +01:00
}
return returnString.toString();
}
2021-11-13 02:17:36 +01:00
public Rule getParentRule()
{
return parentRule;
}
public void setParentRule(Rule parentRule)
{
this.parentRule = parentRule;
}
2021-02-16 13:42:49 +01:00
public static CharSequence[] getActionTypesAsArray()
{
ArrayList<String> actionTypesList = new ArrayList<String>();
for(Action_Enum action : Action_Enum.values())
{
if( // exclusion for deprecated types
!action.toString().equals("turnWifiOn")
&&
!action.toString().equals("turnWifiOff")
&&
!action.toString().equals("turnBluetoothOn")
&&
!action.toString().equals("turnBluetoothOff")
&&
!action.toString().equals("turnUsbTetheringOn")
&&
!action.toString().equals("turnUsbTetheringOff")
&&
!action.toString().equals("turnWifiTetheringOn")
&&
!action.toString().equals("turnWifiTetheringOff")
&&
!action.toString().equals("enableScreenRotation")
&&
!action.toString().equals("disableScreenRotation")
) // exclusion for deprecated types
actionTypesList.add(action.toString());
}
return (String[])actionTypesList.toArray(new String[actionTypesList.size()]);
}
public static CharSequence[] getActionTypesFullNameStringAsArray(Context context)
{
ArrayList<String> actionTypesList = new ArrayList<String>();
for(Action_Enum action : Action_Enum.values())
{
if( // exclusion for deprecated types
!action.toString().equals("turnWifiOn")
&&
!action.toString().equals("turnWifiOff")
&&
!action.toString().equals("turnBluetoothOn")
&&
!action.toString().equals("turnBluetoothOff")
&&
!action.toString().equals("turnUsbTetheringOn")
&&
!action.toString().equals("turnUsbTetheringOff")
&&
!action.toString().equals("turnWifiTetheringOn")
&&
!action.toString().equals("turnWifiTetheringOff")
&&
!action.toString().equals("enableScreenRotation")
&&
!action.toString().equals("disableScreenRotation")
) // exclusion for deprecated types
actionTypesList.add(action.getFullName(context));
}
return (String[])actionTypesList.toArray(new String[actionTypesList.size()]);
}
2022-05-29 14:15:47 +02:00
2021-02-16 13:42:49 +01:00
public void run(Context context, boolean toggleActionIfPossible)
2021-04-09 17:39:59 +02:00
{
try
2021-02-16 13:42:49 +01:00
{
2021-04-09 17:39:59 +02:00
switch(this.getAction())
{
case changeSoundProfile:
/*
* Old version. Those checks should not be necessary anymore. Also they didn't work
* because profiles were created with names like silent, vibrate and normal.
*/
2021-06-23 22:01:40 +02:00
2021-02-16 13:42:49 +01:00
Profile p = Profile.getByName(this.getParameter2());
2021-04-09 17:39:59 +02:00
if (p != null)
2021-02-16 13:42:49 +01:00
p.activate(context);
2021-06-23 22:01:40 +02:00
2021-04-09 17:39:59 +02:00
break;
case triggerUrl:
triggerUrl(context);
break;
case setBluetooth:
Actions.setBluetooth(context, getParameter1(), toggleActionIfPossible);
break;
case setUsbTethering:
Actions.setUsbTethering(context, getParameter1(), toggleActionIfPossible);
break;
case setWifi:
2022-07-15 22:37:55 +02:00
Actions.WifiStuff.setWifi(context, getParameter1(), getParameter2(), toggleActionIfPossible);
2021-04-09 17:39:59 +02:00
break;
case setWifiTethering:
Actions.setWifiTethering(context, getParameter1(), toggleActionIfPossible);
break;
2021-11-13 14:48:56 +01:00
case setBluetoothTethering:
Actions.BluetoothTetheringClass.setBluetoothTethering(context, getParameter1(), toggleActionIfPossible);
break;
2021-04-09 17:39:59 +02:00
case setDisplayRotation:
Actions.setDisplayRotation(context, getParameter1(), toggleActionIfPossible);
break;
case startOtherActivity:
2021-05-08 15:14:31 +02:00
Actions.startOtherActivity(getParameter1(), getParameter2());
2021-04-09 17:39:59 +02:00
break;
case waitBeforeNextAction:
Actions.waitBeforeNextAction(Long.parseLong(this.getParameter2()));
break;
2021-12-08 19:57:27 +01:00
case turnScreenOnOrOff:
if(getParameter1())
{
2021-12-09 18:03:00 +01:00
if(StringUtils.isNumeric(this.getParameter2()))
Actions.wakeupDevice(Long.parseLong(this.getParameter2()));
else
Actions.wakeupDevice((long)1000);
2021-12-08 19:57:27 +01:00
// wakeupDevice() will create a separate thread. That'll take some time, we wait 100ms.
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
else
{
Actions.turnOffScreen();
}
break;
2021-04-09 17:39:59 +02:00
case setAirplaneMode:
Actions.setAirplaneMode(this.getParameter1(), toggleActionIfPossible);
break;
case setDataConnection:
Actions.MobileDataStuff.setDataConnection(this.getParameter1(), toggleActionIfPossible);
break;
case speakText:
Actions.speakText(this.getParameter2());
break;
case playMusic:
Actions.playMusic(this.getParameter1(), toggleActionIfPossible);
break;
2022-01-25 18:08:46 +01:00
case controlMediaPlayback:
Actions.controlMediaPlayback(context, Integer.parseInt(getParameter2()));
break;
2021-04-09 17:39:59 +02:00
case sendTextMessage:
Actions.sendTextMessage(context, this.getParameter2().split(Actions.smsSeparator));
break;
case setScreenBrightness:
Actions.setScreenBrightness(getParameter1(), Integer.parseInt(getParameter2()));
break;
2021-07-04 15:53:24 +02:00
case vibrate:
Actions.vibrate(getParameter1(), getParameter2());
break;
2021-04-09 17:39:59 +02:00
case playSound:
Actions.playSound(getParameter1(), getParameter2());
break;
2022-01-09 22:42:47 +01:00
case createNotification:
Actions.createNotification(this);
break;
2022-01-10 23:00:36 +01:00
case closeNotification:
2022-01-25 18:08:46 +01:00
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
Actions.closeNotification(this);
else
Miscellaneous.logEvent("w", "Close notification", "Close notification was requested, but OS version is too low: " + String.valueOf(Build.VERSION.SDK_INT), 2);
2022-01-10 23:00:36 +01:00
break;
2022-05-29 02:42:12 +02:00
case sendBroadcast:
Actions.sendBroadcast(context, this.getParameter2());
break;
2022-05-29 20:14:50 +02:00
case runExecutable:
2022-06-01 22:36:30 +02:00
String[] execParts = this.getParameter2().split(Action.actionParameter2Split);
if(execParts.length == 1)
Actions.runExecutable(context, this.getParameter1(), execParts[0], null);
else if(execParts.length == 2)
Actions.runExecutable(context, this.getParameter1(), execParts[0], execParts[1]);
2022-05-29 20:14:50 +02:00
break;
2022-07-02 12:46:34 +02:00
case wakelock:
if(this.getParameter1())
Actions.wakeLockStart(context, Long.parseLong(this.getParameter2()));
else
Actions.wakeLockStop();
break;
2022-10-09 17:14:02 +02:00
case setVariable:
Actions.setVariable(this.getParameter2());
break;
2022-10-03 13:22:25 +02:00
case startPhoneCall:
Actions.startPhoneCall(context, this.getParameter2());
break;
case stopPhoneCall:
Actions.endPhoneCall(context);
2022-10-02 18:59:54 +02:00
break;
2023-03-15 23:27:27 +01:00
case copyToClipboard:
Actions.copyToClipboard(context, Miscellaneous.replaceVariablesInText(this.getParameter2(), context));
break;
2023-12-16 13:52:18 +01:00
case takeScreenshot:
Actions.takeScreenshot();
break;
2023-12-19 23:52:28 +01:00
case setLocationService:
Actions.setLocationService(this.getParameter1(), AutomationService.getInstance());
break;
2021-04-09 17:39:59 +02:00
default:
Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3);
break;
}
}
catch(Exception e)
{
Miscellaneous.logEvent("e", "ActionExecution", Log.getStackTraceString(e), 1);
Toast.makeText(context, context.getResources().getString(R.string.errorRunningRule), Toast.LENGTH_LONG).show();
2021-02-16 13:42:49 +01:00
}
}
private void triggerUrl(Context context)
{
String username = null;
String password = null;
2023-12-11 22:50:07 +01:00
String method = ActivityManageActionTriggerUrl.methodGet;
2021-02-16 13:42:49 +01:00
String url;
String[] components = getParameter2().split(";");
if(components.length >= 3)
{
username = components[0];
password = components[1];
url = components[2];
2023-12-11 22:50:07 +01:00
if(components.length >= 4)
method = components[3];
2021-02-16 13:42:49 +01:00
}
2023-05-10 23:24:47 +02:00
else // compatibility for very old versions which haven't upgraded, yet.
2021-02-16 13:42:49 +01:00
url = components[0];
try
{
url = Miscellaneous.replaceVariablesInText(url, context);
Actions myAction = new Actions();
Miscellaneous.logEvent("i", "HTTP", "Attempting download of " + url, 4); //getResources().getString("attemptingDownloadOf");
if(this.getParameter1()) // use authentication
2023-12-12 23:40:12 +01:00
new DownloadTask().execute(url, username, password, method);
2021-02-16 13:42:49 +01:00
else
new DownloadTask().execute(url, null, null);
}
catch(Exception e)
{
2021-05-19 21:06:15 +02:00
Miscellaneous.logEvent("e", "triggerUrl", context.getResources().getString(R.string.logErrorTriggeringUrl) + ": " + e.getMessage() + ", detailed: " + Log.getStackTraceString(e), 2);
2021-02-16 13:42:49 +01:00
}
}
public class DownloadTask extends AsyncTask<String, Void, String>
{
@Override
public String doInBackground(String... parameters)
{
Thread.setDefaultUncaughtExceptionHandler(Miscellaneous.uncaughtExceptionHandler);
int attempts=1;
String urlString=parameters[0];
String urlUsername = null;
String urlPassword = null;
2023-12-11 22:50:07 +01:00
String method = ActivityManageActionTriggerUrl.methodGet;
2021-02-16 13:42:49 +01:00
if(parameters.length >= 3)
{
2023-12-11 22:50:07 +01:00
urlUsername = parameters[1];
urlPassword = parameters[2];
if(parameters.length >= 4)
method = parameters[3];
2021-02-16 13:42:49 +01:00
}
String response = "httpError";
2023-12-11 22:50:07 +01:00
if(Settings.httpAttempts < 1)
2021-02-16 13:42:49 +01:00
Miscellaneous.logEvent("w", "HTTP Request", Miscellaneous.getAnyContext().getResources().getString(R.string.cantDownloadTooFewRequestsInSettings), 3);
while(attempts <= Settings.httpAttempts && response.equals("httpError"))
{
Miscellaneous.logEvent("i", "HTTP Request", "Attempt " + String.valueOf(attempts++) + " of " + String.valueOf(Settings.httpAttempts), 3);
2021-06-23 22:01:40 +02:00
// Either thorough checking or no encryption
if(!Settings.httpAcceptAllCertificates || !urlString.toLowerCase(Locale.getDefault()).contains("https"))
2023-12-11 22:50:07 +01:00
response = Miscellaneous.downloadURL(urlString, urlUsername, urlPassword, method);
2021-06-23 22:01:40 +02:00
else
2023-12-11 22:50:07 +01:00
response = Miscellaneous.downloadURLwithoutCertificateChecking(urlString, urlUsername, urlPassword, method);
2021-06-23 22:01:40 +02:00
try
{
Thread.sleep(Settings.httpAttemptGap * 1000);
}
catch (InterruptedException e1)
{
Miscellaneous.logEvent("w", "HTTP RESULT", "Failed to pause between HTTP requests.", 5);
}
2021-02-16 13:42:49 +01:00
}
2021-06-23 22:01:40 +02:00
Miscellaneous.logEvent("i", "HTTPS RESULT", response, 5);
2021-02-16 13:42:49 +01:00
return response;
}
@Override
public void onPostExecute(String result)
{
//Do something with result
//Toast.makeText(context, text, duration) result;
Miscellaneous.logEvent("i", "HTTP RESULT", result, 3);
2022-05-29 02:42:12 +02:00
Actions myAction = new Actions();
2021-02-16 13:42:49 +01:00
myAction.useDownloadedWebpage(result);
}
}
}