From 7167f0c03d2381cc67d120b52429970767f8e450 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 29 May 2022 20:14:50 +0200 Subject: [PATCH 01/10] run executable --- app/src/apkFlavor/AndroidManifest.xml | 1 + app/src/fdroidFlavor/AndroidManifest.xml | 1 + app/src/googlePlayFlavor/AndroidManifest.xml | 1 + .../java/com/jens/automation2/Action.java | 9 ++ .../java/com/jens/automation2/Actions.java | 5 + .../ActivityManageActionRunExecutable.java | 114 ++++++++++++++++++ .../ActivityManageActionSpeakText.java | 4 +- .../ActivityManageActionTriggerUrl.java | 2 +- .../jens/automation2/ActivityManageRule.java | 42 +++++++ app/src/main/res/drawable/script.png | Bin 0 -> 679 bytes .../activity_manage_action_run_executable.xml | 112 +++++++++++++++++ .../activity_manage_action_speak_text.xml | 13 +- .../activity_manage_action_trigger_url.xml | 2 +- app/src/main/res/values/strings.xml | 7 ++ 14 files changed, 306 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/jens/automation2/ActivityManageActionRunExecutable.java create mode 100644 app/src/main/res/drawable/script.png create mode 100644 app/src/main/res/layout/activity_manage_action_run_executable.xml diff --git a/app/src/apkFlavor/AndroidManifest.xml b/app/src/apkFlavor/AndroidManifest.xml index 6bca09c5..e4464193 100644 --- a/app/src/apkFlavor/AndroidManifest.xml +++ b/app/src/apkFlavor/AndroidManifest.xml @@ -166,6 +166,7 @@ + diff --git a/app/src/fdroidFlavor/AndroidManifest.xml b/app/src/fdroidFlavor/AndroidManifest.xml index 77a006c8..8236850a 100644 --- a/app/src/fdroidFlavor/AndroidManifest.xml +++ b/app/src/fdroidFlavor/AndroidManifest.xml @@ -164,6 +164,7 @@ + diff --git a/app/src/googlePlayFlavor/AndroidManifest.xml b/app/src/googlePlayFlavor/AndroidManifest.xml index 5d60ad4a..aa4c5766 100644 --- a/app/src/googlePlayFlavor/AndroidManifest.xml +++ b/app/src/googlePlayFlavor/AndroidManifest.xml @@ -151,6 +151,7 @@ + diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index bb597f6d..c75796f9 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -52,6 +52,7 @@ public class Action createNotification, closeNotification, sendBroadcast, + runExecutable, sendTextMessage; public String getFullName(Context context) @@ -124,6 +125,8 @@ public class Action return context.getResources().getString(R.string.closeNotifications); case sendBroadcast: return context.getResources().getString(R.string.sendBroadcast); + case runExecutable: + return context.getResources().getString(R.string.runExecutable); default: return "Unknown"; } @@ -267,6 +270,9 @@ public class Action case sendBroadcast: returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.sendBroadcast)); break; + case runExecutable: + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.runExecutable)); + break; default: returnString.append(action.toString()); } @@ -545,6 +551,9 @@ public class Action case sendBroadcast: Actions.sendBroadcast(context, this.getParameter2()); break; + case runExecutable: + Actions.runExecutable(context, this.getParameter1(), this.getParameter2()); + break; default: Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3); break; diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index d3c0d5f7..d113fd71 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -1907,4 +1907,9 @@ public class Actions return android.provider.Settings.Global.getInt(context.getContentResolver(), android.provider.Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } } + + public static boolean runExecutable(Context context, boolean runAsRoot, String pathAndParams) + { + return false; + } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionRunExecutable.java b/app/src/main/java/com/jens/automation2/ActivityManageActionRunExecutable.java new file mode 100644 index 00000000..3486691d --- /dev/null +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionRunExecutable.java @@ -0,0 +1,114 @@ +package com.jens.automation2; + +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.EditText; +import android.widget.Toast; + +import androidx.annotation.Nullable; + +import org.apache.commons.lang3.StringUtils; + +import java.io.File; + +public class ActivityManageActionRunExecutable extends Activity +{ + final static int PICKFILE_RESULT_CODE = 4711; + + CheckBox chkRunExecAsRoot; + EditText etRunExecutablePath, etRunExecutableParameters; + Button bChooseExecutable, bSaveActionRunExec; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_manage_action_run_executable); + + chkRunExecAsRoot = (CheckBox)findViewById(R.id.chkRunExecAsRoot); + etRunExecutablePath = (EditText) findViewById(R.id.etRunExecutablePath); + etRunExecutableParameters = (EditText) findViewById(R.id.etRunExecutableParameters); + bChooseExecutable = (Button) findViewById(R.id.bChooseExecutable); + bSaveActionRunExec = (Button) findViewById(R.id.bSaveActionRunExec); + + bChooseExecutable.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + //Need to check for storage permissions + Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT); + chooseFile.setType("*/*"); + chooseFile = Intent.createChooser(chooseFile, getResources().getString(R.string.selectSoundFile)); + startActivityForResult(chooseFile, PICKFILE_RESULT_CODE); + } + }); + + bSaveActionRunExec.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + saveExecSettings(); + } + }); + } + + void saveExecSettings() + { + if(etRunExecutablePath.getText().toString() == null || etRunExecutablePath.getText().toString().length() == 0) + { + Toast.makeText(ActivityManageActionRunExecutable.this, getResources().getString(R.string.selectValidExecutable), Toast.LENGTH_LONG).show(); + return; + } + else + { + File executableFile = new File(etRunExecutablePath.getText().toString()); + if(!executableFile.exists()) + { + Toast.makeText(ActivityManageActionRunExecutable.this, getResources().getString(R.string.fileDoesNotExist), Toast.LENGTH_LONG).show(); + return; + } + else + { + if(!chkRunExecAsRoot.isChecked() && !executableFile.canExecute()) + { + Toast.makeText(ActivityManageActionRunExecutable.this, getResources().getString(R.string.fileNotExecutable), Toast.LENGTH_LONG).show(); + return; + } + } + } + + Intent returnData = new Intent(); + returnData.putExtra("actionParameter1", chkRunExecAsRoot.isChecked()); + + if(etRunExecutableParameters.getText() != null && !StringUtils.isEmpty(etRunExecutableParameters.getText().toString())) + returnData.putExtra("actionParameter2", etRunExecutablePath.getText().toString() + Action.actionParameter2Split + etRunExecutableParameters.getText().toString()); + else + returnData.putExtra("actionParameter2", etRunExecutablePath.getText().toString()); + + setResult(RESULT_OK, returnData); + finish(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) + { + super.onActivityResult(requestCode, resultCode, data); + + if(resultCode == RESULT_OK) + { + if(requestCode == PICKFILE_RESULT_CODE) + { + Uri fileUri = data.getData(); + String filePath = CompensateCrappyAndroidPaths.getPath(ActivityManageActionRunExecutable.this, fileUri); + etRunExecutablePath.setText(filePath); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionSpeakText.java b/app/src/main/java/com/jens/automation2/ActivityManageActionSpeakText.java index cb23f2e9..f9e8bd45 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionSpeakText.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionSpeakText.java @@ -15,8 +15,6 @@ public class ActivityManageActionSpeakText extends Activity private Button bSaveSpeakText; private EditText etSpeakText; -// private String existingUrl = ""; - public static boolean edit = false; public static Action resultingAction = null; @@ -27,7 +25,7 @@ public class ActivityManageActionSpeakText extends Activity this.setContentView(R.layout.activity_manage_action_speak_text); etSpeakText = (EditText)findViewById(R.id.etTextToSpeak); - bSaveSpeakText = (Button)findViewById(R.id.bSaveTriggerUrl); + bSaveSpeakText = (Button)findViewById(R.id.bSaveSpeakText); bSaveSpeakText.setOnClickListener(new OnClickListener() { @Override diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionTriggerUrl.java b/app/src/main/java/com/jens/automation2/ActivityManageActionTriggerUrl.java index 8a3d2bb6..9a41e210 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionTriggerUrl.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionTriggerUrl.java @@ -47,7 +47,7 @@ public class ActivityManageActionTriggerUrl extends Activity chkTriggerUrlUseAuthentication = (CheckBox)findViewById(R.id.chkTriggerUrlUseAuthentication); lvTriggerUrlPostParameters = (ListView)findViewById(R.id.lvTriggerUrlPostParameters); tlTriggerUrlAuthentication = (TableLayout)findViewById(R.id.tlTriggerUrlAuthentication); - bSaveTriggerUrl = (Button)findViewById(R.id.bSaveTriggerUrl); + bSaveTriggerUrl = (Button)findViewById(R.id.bSaveSpeakText); bSaveTriggerUrl.setOnClickListener(new OnClickListener() { @Override diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index a64e6dc2..e8f55704 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -123,6 +123,8 @@ public class ActivityManageRule extends Activity final static int requestCodeTriggerBroadcastReceivedEdit = 810; final static int requestCodeActionSendBroadcastAdd = 811; final static int requestCodeActionSendBroadcastEdit = 812; + final static int requestCodeActionRunExecutableAdd = 813; + final static int requestCodeActionRunExecutableEdit = 814; public static ActivityManageRule getInstance() { @@ -382,6 +384,12 @@ public class ActivityManageRule extends Activity activityEditSendBroadcastIntent.putExtra(intentNameActionParameter2, a.getParameter2()); startActivityForResult(activityEditSendBroadcastIntent, requestCodeActionSendBroadcastEdit); break; + case runExecutable: + Intent activityEditRunExecutableIntent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class); + activityEditRunExecutableIntent.putExtra(intentNameActionParameter1, a.getParameter1()); + activityEditRunExecutableIntent.putExtra(intentNameActionParameter2, a.getParameter2()); + startActivityForResult(activityEditRunExecutableIntent, requestCodeActionRunExecutableEdit); + break; case controlMediaPlayback: Intent activityEditControlMediaIntent = new Intent(ActivityManageRule.this, ActivityManageActionControlMedia.class); activityEditControlMediaIntent.putExtra(ActivityManageRule.intentNameActionParameter2, a.getParameter2()); @@ -1489,6 +1497,17 @@ public class ActivityManageRule extends Activity this.refreshActionList(); } } + else if(requestCode == requestCodeActionRunExecutableAdd) + { + 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) @@ -1547,6 +1566,21 @@ public class ActivityManageRule extends Activity this.refreshActionList(); } } + else if(requestCode == requestCodeActionRunExecutableEdit) + { + if(resultCode == RESULT_OK) + { + ruleToEdit.getActionSet().get(editIndex).setParentRule(ruleToEdit); + + if(data.hasExtra(intentNameActionParameter1) && data.hasExtra(intentNameActionParameter2)) + { + ruleToEdit.getActionSet().get(editIndex).setParameter1(data.getBooleanExtra(intentNameActionParameter1, false)); + ruleToEdit.getActionSet().get(editIndex).setParameter2(data.getStringExtra(intentNameActionParameter2)); + } + + this.refreshActionList(); + } + } else if(requestCode == requestCodeActionControlMediaEdit) { if(resultCode == RESULT_OK) @@ -1759,6 +1793,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.satellite)); + 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())) { // if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) @@ -1927,6 +1963,12 @@ public class ActivityManageRule extends Activity Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionSendBroadcast.class); startActivityForResult(intent, requestCodeActionSendBroadcastAdd); } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.runExecutable.toString())) + { + newAction.setAction(Action_Enum.runExecutable); + Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionRunExecutable.class); + startActivityForResult(intent, requestCodeActionRunExecutableAdd); + } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.controlMediaPlayback.toString())) { newAction.setAction(Action_Enum.controlMediaPlayback); diff --git a/app/src/main/res/drawable/script.png b/app/src/main/res/drawable/script.png new file mode 100644 index 0000000000000000000000000000000000000000..ebedb555cd530a935c757cc073f68fbef405b828 GIT binary patch literal 679 zcmeAS@N?(olHy`uVBq!ia0vp^Za}QT!3HFqFZ8SfQjEnx?oJHr&dIz4a#+$GeH|GX zHuiJ>Nn{1`ISV`@iy0VLoj{n;&a;*SC@59p8c`CQpH@Y2 zXU3vO%u}>E+!j6i&HP*VxBAwwjd5--o90HoXEQb}Sg=xW)+^_7(=*%*eu)Yh=QXj(K57f&);HgNyX^Yvn>kKO6L*~MbXy#tH~rxA&o3We z6KhuLJFfiWZ=J`|pn{z-3qrK+K3yQvW6!a9nZ)|*-dmz{g_@VEPCq@dA#1Bwv0Q(9 zQLgf_r$q-J7BpOb$@2X3Otao(mg5qKKi1eSuese(Xf^jj-u8vJ-ySNlQsvwm$KQ5X zkyE1A&E$H9iN^h~@Rx00BxEa+*qRyV_MA;~4o|m{JDg*t%-DUj>6z7BzNfdn&ZHPM zUd>|t{deAxHP^FmMh!YjRGVekzo-aYmi&POpUl9G`yv*&qIjTQFEd zM5AxNbV*@$_rcc_Po})!}_$K{4Pj#;ia8jE + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +