From a8bfc6f7ec90d2e191e071cfea7c03848131baa2 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 4 Jul 2021 15:53:24 +0200 Subject: [PATCH] Vibrate pattern --- .../java/com/jens/automation2/Action.java | 7 + .../java/com/jens/automation2/Actions.java | 35 ++++- .../ActivityManageActionSendTextMessage.java | 10 -- .../ActivityManageActionVibrate.java | 54 ++++++-- .../jens/automation2/ActivityManageRule.java | 128 +++++------------- .../jens/automation2/ActivityPermissions.java | 4 +- app/src/main/res/drawable-hdpi/vibrate.png | Bin 0 -> 2487 bytes .../layout/activity_manage_action_vibrate.xml | 3 +- app/src/main/res/values-de/strings.xml | 4 + app/src/main/res/values/strings.xml | 5 +- 10 files changed, 128 insertions(+), 122 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/vibrate.png diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 0cb58e1..54f2ca8 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -15,6 +15,7 @@ public class Action { public static final String actionParameter2Split = "ap2split"; public static final String intentPairSeperator = "intPairSplit"; + public static final String vibrateSeparator = ","; public enum Action_Enum { setWifi, @@ -38,6 +39,7 @@ public class Action playMusic, setScreenBrightness, playSound, + vibrate, sendTextMessage; public String getFullName(Context context) @@ -84,6 +86,8 @@ public class Action return context.getResources().getString(R.string.waitBeforeNextAction); case wakeupDevice: return context.getResources().getString(R.string.wakeupDevice); + case vibrate: + return context.getResources().getString(R.string.vibrate); case setAirplaneMode: return context.getResources().getString(R.string.airplaneMode); case setDataConnection: @@ -405,6 +409,9 @@ public class Action case setScreenBrightness: Actions.setScreenBrightness(getParameter1(), Integer.parseInt(getParameter2())); break; + case vibrate: + Actions.vibrate(getParameter1(), getParameter2()); + break; case playSound: Actions.playSound(getParameter1(), getParameter2()); break; diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index 70939fd..e2873e9 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -16,6 +16,8 @@ import android.net.wifi.WifiManager; import android.os.Build; import android.os.PowerManager; import android.os.PowerManager.WakeLock; +import android.os.VibrationEffect; +import android.os.Vibrator; import android.provider.MediaStore; import android.telephony.SmsManager; import android.telephony.SubscriptionManager; @@ -544,7 +546,38 @@ public class Actions Miscellaneous.logEvent("i", "Play sound file", "Not playing sound file because phone is on some kind of mute state.", 2); } - public void useDownloadedWebpage(String result) + public static void vibrate(boolean parameter1, String parameter2) + { + String vibrateDurations[] = parameter2.split(Action.vibrateSeparator); + + int counter = 1; + for(String vibrate : vibrateDurations) + { + if(counter % 2 != 0) + { + Vibrator vibrator = (Vibrator) Miscellaneous.getAnyContext().getSystemService(Context.VIBRATOR_SERVICE); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + vibrator.vibrate(VibrationEffect.createOneShot(Long.parseLong(vibrate), VibrationEffect.DEFAULT_AMPLITUDE)); + else + vibrator.vibrate(Long.parseLong(vibrate)); + } + else + { + try + { + Thread.sleep(Long.parseLong(vibrate)); + } + catch (Exception e) + { + Miscellaneous.logEvent("e", "VibrateSleep", Log.getStackTraceString(e), 5); + } + } + + counter++; + } + } + + public void useDownloadedWebpage(String result) { // Toast.makeText(context, "Result: " + result, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java index f399e33..b7558f9 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionSendTextMessage.java @@ -84,20 +84,10 @@ public class ActivityManageActionSendTextMessage extends Activity etPhoneNumber.setText(parameters[0]); etSendTextMessage.setText(parameters[1]); } - - -// String url = getIntent().getStringExtra("urlToTrigger"); -// if(url != null) -// existingUrl = url; } private void backToRuleManager() { -// Intent returnIntent = new Intent(); -// returnIntent.putExtra("urlToTrigger", existingUrl); - -// setResult(RESULT_OK, returnIntent); - if(edit && resultingAction != null) { ActivityManageActionSendTextMessage.resultingAction.setParameter2(etPhoneNumber.getText().toString() + Actions.smsSeparator + etSendTextMessage.getText().toString()); diff --git a/app/src/main/java/com/jens/automation2/ActivityManageActionVibrate.java b/app/src/main/java/com/jens/automation2/ActivityManageActionVibrate.java index 6f40466..5eeb51c 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageActionVibrate.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageActionVibrate.java @@ -3,6 +3,7 @@ package com.jens.automation2; import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.VibrationEffect; @@ -11,9 +12,12 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; + public class ActivityManageActionVibrate extends Activity { TextView etVibratePattern; @@ -29,29 +33,53 @@ public class ActivityManageActionVibrate extends Activity bTestVibratePattern = (Button)findViewById(R.id.bTestVibratePattern); bSaveVibratePattern = (Button)findViewById(R.id.bSaveVibratePattern); + bSaveVibratePattern.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View view) + { + if(checkInput()) + { + Intent answer = new Intent(); + answer.putExtra("vibratePattern", etVibratePattern.getText().toString()); + setResult(RESULT_OK, answer); + finish(); + } + } + }); + bTestVibratePattern.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if(ActivityPermissions.havePermission(Manifest.permission.VIBRATE, ActivityManageActionVibrate.this)) + if(checkInput()) { - String vibrateDurations[] = etVibratePattern.getText().toString().split(","); - - for(String duration : vibrateDurations) + if (ActivityPermissions.havePermission(Manifest.permission.VIBRATE, ActivityManageActionVibrate.this)) { - Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - { - vibrator.vibrate(VibrationEffect.createOneShot(Long.parseLong(duration), VibrationEffect.DEFAULT_AMPLITUDE)); - } - else - { - vibrator.vibrate(Long.parseLong(duration)); - } + String pattern = etVibratePattern.getText().toString(); + Actions.vibrate(false, pattern); } } } }); + + Intent input = getIntent(); + + if(input.hasExtra("vibratePattern")) + etVibratePattern.setText(input.getStringExtra("vibratePattern")); + } + + boolean checkInput() + { + String vibratePattern = etVibratePattern.getText().toString(); + String regex = "^[0-9,]+$"; + if(StringUtils.isEmpty(vibratePattern) || !vibratePattern.matches(regex) || vibratePattern.substring(0, 1).equals(",") || vibratePattern.substring(vibratePattern.length()-1).equals(",")) + { + Toast.makeText(ActivityManageActionVibrate.this, getResources().getString(R.string.pleaseEnterValidVibrationPattern), Toast.LENGTH_SHORT).show(); + return false; + } + + return true; } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index 65b91c9..0476b4e 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -103,6 +103,9 @@ public class ActivityManageRule extends Activity final static int requestCodeTriggerPhoneCallEdit = 602; final static int requestCodeTriggerWifiAdd = 723; final static int requestCodeTriggerWifiEdit = 724; + final static int requestCodeActionSendTextMessageAdd = 5001; + final static int requestCodeActionVibrateAdd = 801; + final static int requestCodeActionVibrateEdit = 802; public static ActivityManageRule getInstance() { @@ -228,27 +231,11 @@ public class ActivityManageRule extends Activity Trigger selectedTrigger = (Trigger)triggerListView.getItemAtPosition(arg2); switch(selectedTrigger.getTriggerType()) { -// case batteryLevel: -// break; -// case charging: -// break; -// case noiseLevel: -// break; -// case pointOfInterest: -// break; -// case process_started_stopped: -// break; -// case speed: -// break; case timeFrame: ActivityManageTriggerTimeFrame.editedTimeFrameTrigger = selectedTrigger; Intent timeFrameEditor = new Intent(ActivityManageRule.this, ActivityManageTriggerTimeFrame.class); startActivityForResult(timeFrameEditor, requestCodeTriggerTimeframeEdit); break; -// case usb_host_connection: -// break; -// case wifiConnection: -// break; case bluetoothConnection: ActivityManageTriggerBluetooth.editedBluetoothTrigger = selectedTrigger; Intent bluetoothEditor = new Intent(ActivityManageRule.this, ActivityManageTriggerBluetooth.class); @@ -306,14 +293,6 @@ public class ActivityManageRule extends Activity Action a = (Action)actionListView.getItemAtPosition(arg2); switch(a.getAction()) { -// case changeSoundProfile: -// break; -// case disableScreenRotation: -// break; -// case enableScreenRotation: -// break; -// case setAirplaneMode: -// break; case startOtherActivity: Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionStartActivity.class); ActivityManageActionStartActivity.resultingAction = a; @@ -340,11 +319,15 @@ public class ActivityManageRule extends Activity break; case setScreenBrightness: Intent activityEditScreenBrightnessIntent = new Intent(ActivityManageRule.this, ActivityManageActionBrightnessSetting.class); -// ActivityEditTriggerUrl.resultingAction = a; activityEditScreenBrightnessIntent.putExtra("autoBrightness", a.getParameter1()); activityEditScreenBrightnessIntent.putExtra("brightnessValue", Integer.parseInt(a.getParameter2())); startActivityForResult(activityEditScreenBrightnessIntent, requestCodeActionScreenBrightnessEdit); break; + case vibrate: + Intent activityEditVibrateIntent = new Intent(ActivityManageRule.this, ActivityManageActionVibrate.class); + activityEditVibrateIntent.putExtra("vibratePattern", a.getParameter2()); + startActivityForResult(activityEditVibrateIntent, requestCodeActionVibrateEdit); + break; case playSound: Intent actionPlaySoundIntent = new Intent(context, ActivityManageActionPlaySound.class); actionPlaySoundIntent.putExtra("edit", true); @@ -686,13 +669,6 @@ public class ActivityManageRule extends Activity String[] choices = (String[]) choicesList.toArray(new String[choicesList.size()]); getTriggerNoiseDialog(myContext, choices).show(); } -// else if(triggerType.equals(Trigger.Event_Enum.timeFrame)) -// { -// newTrigger.setTriggerType(Trigger.Event_Enum.timeFrame); -// ActivityManageTimeFrame.editedTimeFrameTrigger = null; -// Intent timeFrameEditor = new Intent(myContext, ActivityManageTimeFrame.class); -// startActivityForResult(timeFrameEditor, 2000); -// } else if(triggerType.equals(Trigger_Enum.wifiConnection)) { newTrigger.setTriggerType(Trigger_Enum.wifiConnection); @@ -703,7 +679,6 @@ public class ActivityManageRule extends Activity progressDialog = ProgressDialog.show(myContext, null, getResources().getString(R.string.gettingListOfInstalledApplications), true, false); newTrigger.setTriggerType(Trigger_Enum.process_started_stopped); new GenerateApplicationSelectionsDialogTask().execute(ActivityManageRule.this); -// getTriggerRunningProcessDialog1(myContext).show(); } else if(triggerType.equals(Trigger_Enum.phoneCall)) { @@ -1298,6 +1273,25 @@ public class ActivityManageRule extends Activity this.refreshActionList(); } } + else if(requestCode == requestCodeActionVibrateAdd) + { + if(resultCode == RESULT_OK) + { + newAction.setParameter2(data.getStringExtra("vibratePattern")); + ruleToEdit.getActionSet().add(newAction); + this.refreshActionList(); + } + } + else if(requestCode == requestCodeActionVibrateEdit) + { + if(resultCode == RESULT_OK) + { + if(data.hasExtra("vibratePattern")) + ruleToEdit.getActionSet().get(editIndex).setParameter2(data.getStringExtra("vibratePattern")); + + this.refreshActionList(); + } + } else if(requestCode == requestCodeActionPlaySoundAdd) { if(resultCode == RESULT_OK) @@ -1375,6 +1369,8 @@ public class ActivityManageRule extends Activity items.add(new Item(typesLong[i].toString(), R.drawable.brightness)); else if(types[i].toString().equals(Action_Enum.playSound.toString())) items.add(new Item(typesLong[i].toString(), R.drawable.sound)); + else if(types[i].toString().equals(Action_Enum.vibrate.toString())) + items.add(new Item(typesLong[i].toString(), R.drawable.vibrate)); else if(types[i].toString().equals(Action_Enum.sendTextMessage.toString())) { // if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) @@ -1504,14 +1500,13 @@ public class ActivityManageRule extends Activity } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.sendTextMessage.toString())) { -// if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageRule.this, "android.permission.SEND_SMS")) { //launch other activity to enter parameters; newAction.setAction(Action_Enum.sendTextMessage); ActivityManageActionSendTextMessage.resultingAction = null; Intent editTriggerIntent = new Intent(context, ActivityManageActionSendTextMessage.class); - startActivityForResult(editTriggerIntent, 5001); + startActivityForResult(editTriggerIntent, requestCodeActionSendTextMessageAdd); } } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.playMusic.toString())) @@ -1520,6 +1515,12 @@ public class ActivityManageRule extends Activity ruleToEdit.getActionSet().add(newAction); refreshActionList(); } + else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.vibrate.toString())) + { + newAction.setAction(Action_Enum.vibrate); + Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionVibrate.class); + startActivityForResult(intent, requestCodeActionVibrateAdd); + } else if(Action.getActionTypesAsArray()[which].toString().equals(Action_Enum.setScreenBrightness.toString())) { newAction.setAction(Action_Enum.setScreenBrightness); @@ -1637,63 +1638,6 @@ public class ActivityManageRule extends Activity return alertDialog; } - /*private AlertDialog getActionStartActivityDialog1(final Context myContext) - { - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); - alertDialogBuilder.setTitle(myContext.getResources().getString(R.string.selectApplication)); - final String[] applicationArray = ActivityManageStartActivity.getApplicationNameListString(ActivityManageSpecificRule.this); - alertDialogBuilder.setItems(applicationArray, new DialogInterface.OnClickListener() - { - @Override - public void onClick(DialogInterface dialog, int which) - { - getActionStartActivityDialog2(myContext, applicationArray[which]).show(); - } - }); - AlertDialog alertDialog = alertDialogBuilder.create(); - - return alertDialog; - } - private AlertDialog getActionStartActivityDialog2(final Context myContext, String applicationName) - { - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); - alertDialogBuilder.setTitle(myContext.getResources().getString(R.string.selectPackageOfApplication)); - final String[] packageArray = ActivityManageStartActivity.getPackageListString(ActivityManageSpecificRule.this, applicationName); - alertDialogBuilder.setItems(packageArray, new DialogInterface.OnClickListener() - { - @Override - public void onClick(DialogInterface dialog, int which) - { - getActionStartActivityDialog3(ActivityManageSpecificRule.this, packageArray[which]).show(); - } - }); - AlertDialog alertDialog = alertDialogBuilder.create(); - - return alertDialog; - } - private AlertDialog getActionStartActivityDialog3(final Context myContext, final String packageName) - { - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); - alertDialogBuilder.setTitle(myContext.getResources().getString(R.string.selectActivityToBeStarted)); - final String activityArray[] = ActivityManageStartActivity.getActivityListForPackageName(packageName); - alertDialogBuilder.setItems(activityArray, new DialogInterface.OnClickListener() - { - @Override - public void onClick(DialogInterface dialog, int which) - { - ActivityInfo ai = ActivityManageStartActivity.getActivityInfoForPackageNameAndActivityName(packageName, activityArray[which]); -// Log.i("Selected", ai.packageName + " / " + ai.name); - newAction.setParameter2(ai.packageName + ";" + ai.name); - newAction.toString(); - ruleToEdit.getActionSet().add(newAction); - refreshActionList(); - } - }); - AlertDialog alertDialog = alertDialogBuilder.create(); - - return alertDialog; - }*/ - private AlertDialog getActionWaitBeforeNextActionDialog(final Context myContext) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(this); diff --git a/app/src/main/java/com/jens/automation2/ActivityPermissions.java b/app/src/main/java/com/jens/automation2/ActivityPermissions.java index e0bd1d4..e17cf1d 100644 --- a/app/src/main/java/com/jens/automation2/ActivityPermissions.java +++ b/app/src/main/java/com/jens/automation2/ActivityPermissions.java @@ -852,9 +852,7 @@ public class ActivityPermissions extends Activity { if (requestCode == requestCodeForPermissionsBackgroundLocation) { - NotificationManager mNotificationManager = (NotificationManager) ActivityPermissions.this.getSystemService(Context.NOTIFICATION_SERVICE); - - if (mNotificationManager.isNotificationPolicyAccessGranted()) + if (havePermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION, ActivityPermissions.this)) requestPermissions(cachedPermissionsToRequest, true); } } diff --git a/app/src/main/res/drawable-hdpi/vibrate.png b/app/src/main/res/drawable-hdpi/vibrate.png new file mode 100644 index 0000000000000000000000000000000000000000..6e9c4e443907ae4d2fcf5e07054bf9add8eb557b GIT binary patch literal 2487 zcmXX|dpy%?7~d(mTa03EXLAc7CX9&L#+bXvZ4S05X)5ITy!Vt~~0Dze%@illKe-6Q8Je&c53t9lc)ieNLhre|7H2@HA2mq`G z0RR>y0DydSd560t|ASaG#y1uKkO2K`zm5HK`73`>F%In$=NT0mmk=Ct3g8xe=Fd0- z7xW3pIb#D810zGj&6ojS4}78_TC3W~Cn*pVR=xPOGa z6L%wo_%veFYuGehB~Dq=L~-2ka=wqg-PmHz(eCD{^64*|>Ffi%#j2CqdLY;PbP}06 z9MYq z6Ob3xTLA4&^I?xn-lqrt3NeG=vv-MIU{~qun4Tv+k}ZL{^YF?&AKgMzx$M4#i67W# z+3gmOI}H=_A<+%H-SIg5k}yX`fs&zfvX!36knU{^4Q>k3iInMi022gXDLr%6r;Rn3k431(&XPMnBFSoo~u6JxOM9`Kpas?|{^)!ayczgN9;m@MYHvTZe=rtWL-Er=t z&Loe!X2oyl%$@}C9liOtU<^8|`vkRu8%vU$$~6-pub`qX8b`>^8M7MoJI@JQeQft? z*#j4K`*y z3kwlSF4Eg74{B3!6`I*&S%P{5vi4NgOiY|G%@i;vBQ66{oz}{69+9=|E9y*EFco4@KCtAxb^q>NWnERsVOEL8idO; zQHJExm*So_Pot3J2|4(b_V=nkBM1g344d~?im%D3BBhWVxP{weIWufX*I+{l5X1W}%2=Uav*X56y>9%b9rl6=h` z+DC~VtQw=Rt@~u35_G^YFZ8t5c}~>N953c|ck5KG4O>Yw$>}|J0&9wIX75Y?!~gnE zfuY(iAggR%tjDKx$%u?!TM%Nq=ew_(9H|E@l@`mlGmfXW&x0F7KP-rLv%#kMh(w^h zyzwtfpSB2Ih(IVjda*KW3@E6$VDu$2+y z(7L}x01Z{NlMM!@WSD5r8a71MInUBfZkRW&=oC9Hn?28=3=J~+iK6#Ei|ekhU)kqQ z%&(iljtE$QNfe^>#;_!`+Zmul7wKebJV!%@2A?YPtd|O?KF2SNZY5=97LjKrMR5Ft zo~5bfT>@csTSk6~7Ko^5ru8@tFm#B9-qFGs8zCm-Gwa(rt2Y^RX8!r9+i&BK6)f1i zjWaTsGyjQ&j7~)VcFnqQuELUui&3kqp>M<1E=mm?pDjA?8`yEj-8VD-h<~JE#BZ!f~(i^p*4fVXCQtTnfVoJm3|7PcpHQ( z*;E^#9w>Aqjo;77H%~knt<0>OatfSrlKruZy;S6cAq9io0UvnRRp1x384KnqnrJjV zDIQ7QJQMqOpzH3%cs%kV0v)oU|Lr}!yW1NxzO{o+bUs!gfY*@a&+@BgK#`G#8V{Lqi2sLd~D_?;|P#KG;LudFF<%%=w1?CvlI(7xc#IhefJKxE+p@Exxs$0_r(I zJm6={FRM(?m0`|WA)LqOumcLF#kcG0x#D|tm>jlXzazs0?xg~e+o$GXne!{_65xxJnu5?9+!S@17MYdTG zC1;pyA#5;Pp*Kb?x=j;rd~5+iG21uCY!Cc>&5xs+Apqbt;$3a2^4yAaki!ip=`f9# zCxI;`8o7sjyx(<{x@ldFiY5=){kRur%bGj}q?bo7fe(L`nyOzRmvEb6_zx$51KI`E IWE+(7AJn9aCIA2c literal 0 HcmV?d00001 diff --git a/app/src/main/res/layout/activity_manage_action_vibrate.xml b/app/src/main/res/layout/activity_manage_action_vibrate.xml index 102d0e9..a7702df 100644 --- a/app/src/main/res/layout/activity_manage_action_vibrate.xml +++ b/app/src/main/res/layout/activity_manage_action_vibrate.xml @@ -26,7 +26,8 @@ + android:layout_height="wrap_content" + android:layout_marginVertical="@dimen/default_margin"/>