From f279a49ba40be8a26d1aa8901db1fbb84e562224 Mon Sep 17 00:00:00 2001 From: "Rui Zhao (renyuneyun)" Date: Sat, 10 Feb 2018 22:33:23 +0000 Subject: [PATCH 1/2] Introduce formatting representation to user-input strings Introduce date & time to formatting representation --- app/src/main/java/ryey/easer/Utils.java | 19 ++++++++++++ .../plugins/operation/alarm/AlarmLoader.java | 3 +- .../operation/broadcast/BroadcastLoader.java | 30 +++++++++++++++++-- .../operation/command/CommandLoader.java | 3 +- .../SendNotificationLoader.java | 5 ++-- .../plugins/operation/send_sms/SmsLoader.java | 7 ++++- 6 files changed, 59 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/ryey/easer/Utils.java b/app/src/main/java/ryey/easer/Utils.java index 2fe51c39..3ec971a2 100644 --- a/app/src/main/java/ryey/easer/Utils.java +++ b/app/src/main/java/ryey/easer/Utils.java @@ -9,8 +9,11 @@ import android.widget.Toast; import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collection; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -112,4 +115,20 @@ public static int checkedIndexFirst(CompoundButton[] buttons) { } throw new IllegalStateException("At least one button should be checked"); } + + private static final String F_DATE = "%DATE%"; + private static final String F_TIME = "%TIME%"; + + private static final SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-DD", Locale.US); + private static final SimpleDateFormat sdf_time = new SimpleDateFormat("HH-mm-ss", Locale.US); + + public static String format(String format_string) { + if (format_string == null) + return null; + Date now = Calendar.getInstance().getTime(); + String res = format_string; + res = res.replaceAll(F_DATE, sdf_date.format(now)); + res = res.replaceAll(F_TIME, sdf_time.format(now)); + return res; + } } diff --git a/app/src/main/java/ryey/easer/plugins/operation/alarm/AlarmLoader.java b/app/src/main/java/ryey/easer/plugins/operation/alarm/AlarmLoader.java index 14be180b..8c4afc55 100644 --- a/app/src/main/java/ryey/easer/plugins/operation/alarm/AlarmLoader.java +++ b/app/src/main/java/ryey/easer/plugins/operation/alarm/AlarmLoader.java @@ -27,6 +27,7 @@ import java.util.Calendar; +import ryey.easer.Utils; import ryey.easer.commons.plugindef.ValidData; import ryey.easer.commons.plugindef.operationplugin.OperationLoader; @@ -54,7 +55,7 @@ public boolean load(@ValidData @NonNull AlarmOperationData data) { } intent.putExtra(AlarmClock.EXTRA_HOUR, hour); intent.putExtra(AlarmClock.EXTRA_MINUTES, minute); - intent.putExtra(AlarmClock.EXTRA_MESSAGE, data.message); + intent.putExtra(AlarmClock.EXTRA_MESSAGE, Utils.format(data.message)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { intent.putExtra(AlarmClock.EXTRA_VIBRATE, true); } diff --git a/app/src/main/java/ryey/easer/plugins/operation/broadcast/BroadcastLoader.java b/app/src/main/java/ryey/easer/plugins/operation/broadcast/BroadcastLoader.java index cc7c1e33..f706a15f 100644 --- a/app/src/main/java/ryey/easer/plugins/operation/broadcast/BroadcastLoader.java +++ b/app/src/main/java/ryey/easer/plugins/operation/broadcast/BroadcastLoader.java @@ -21,11 +21,14 @@ import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.support.annotation.NonNull; import com.orhanobut.logger.Logger; +import java.util.ArrayList; + import ryey.easer.Utils; import ryey.easer.commons.plugindef.ValidData; import ryey.easer.commons.plugindef.operationplugin.OperationLoader; @@ -37,7 +40,7 @@ public BroadcastLoader(Context context) { @Override public boolean load(@ValidData @NonNull BroadcastOperationData data) { - IntentData iData = data.data; + IntentData iData = preprocess(data.data); Intent intent = new Intent(); intent.setAction(iData.action); if (iData.category != null) @@ -70,9 +73,30 @@ public boolean load(@ValidData @NonNull BroadcastOperationData data) { } intent.putExtras(extras); } - Logger.d(intent); context.sendBroadcast(intent); - Logger.d("broadcast has been sent [%s]", iData); return true; } + + private static IntentData preprocess(IntentData data) { + IntentData res = new IntentData(); + res.action = Utils.format(data.action); + if (data.category != null) { + res.category = new ArrayList<>(data.category.size()); + for (String c : data.category) { + res.category.add(Utils.format(c)); + } + } + res.type = Utils.format(data.type); + res.data = Uri.parse(Utils.format(data.data.getPath())); + if (data.extras != null) { + res.extras = new ArrayList<>(data.extras.size()); + for (IntentData.ExtraItem extra : data.extras) { + IntentData.ExtraItem p_extra = new IntentData.ExtraItem(); + p_extra.key = Utils.format(extra.key); + p_extra.value = Utils.format(extra.value); + res.extras.add(p_extra); + } + } + return res; + } } diff --git a/app/src/main/java/ryey/easer/plugins/operation/command/CommandLoader.java b/app/src/main/java/ryey/easer/plugins/operation/command/CommandLoader.java index e036c89b..08050fd5 100644 --- a/app/src/main/java/ryey/easer/plugins/operation/command/CommandLoader.java +++ b/app/src/main/java/ryey/easer/plugins/operation/command/CommandLoader.java @@ -26,6 +26,7 @@ import java.io.IOException; +import ryey.easer.Utils; import ryey.easer.commons.plugindef.ValidData; import ryey.easer.commons.plugindef.operationplugin.OperationLoader; import ryey.easer.plugins.reusable.PluginHelper; @@ -38,7 +39,7 @@ public CommandLoader(Context context) { @Override public boolean load(@ValidData @NonNull CommandOperationData data) { boolean success = true; - String text = data.get(); + String text = Utils.format(data.get()); String []commands = text.split("\n"); Process process; try { diff --git a/app/src/main/java/ryey/easer/plugins/operation/send_notification/SendNotificationLoader.java b/app/src/main/java/ryey/easer/plugins/operation/send_notification/SendNotificationLoader.java index 9ef75f62..83168be4 100644 --- a/app/src/main/java/ryey/easer/plugins/operation/send_notification/SendNotificationLoader.java +++ b/app/src/main/java/ryey/easer/plugins/operation/send_notification/SendNotificationLoader.java @@ -28,6 +28,7 @@ import java.util.concurrent.ThreadLocalRandom; import ryey.easer.R; +import ryey.easer.Utils; import ryey.easer.commons.plugindef.ValidData; import ryey.easer.commons.plugindef.operationplugin.OperationLoader; @@ -50,8 +51,8 @@ public boolean load(@ValidData @NonNull SendNotificationOperationData data) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.mipmap.ic_launcher); - builder.setContentTitle(data.title); - builder.setContentText(data.content); + builder.setContentTitle(Utils.format(data.title)); + builder.setContentText(Utils.format(data.content)); notificationManager.notify(NOTIFICATION_ID, builder.build()); NOTIFICATION_ID++; diff --git a/app/src/main/java/ryey/easer/plugins/operation/send_sms/SmsLoader.java b/app/src/main/java/ryey/easer/plugins/operation/send_sms/SmsLoader.java index c660ba80..4929029b 100644 --- a/app/src/main/java/ryey/easer/plugins/operation/send_sms/SmsLoader.java +++ b/app/src/main/java/ryey/easer/plugins/operation/send_sms/SmsLoader.java @@ -23,6 +23,7 @@ import android.support.annotation.NonNull; import android.telephony.SmsManager; +import ryey.easer.Utils; import ryey.easer.commons.plugindef.ValidData; import ryey.easer.commons.plugindef.operationplugin.OperationLoader; @@ -33,8 +34,12 @@ public SmsLoader(Context context) { @Override public boolean load(@ValidData @NonNull SmsOperationData data) { + String destination = data.destination; + String content = Utils.format(data.content); SmsManager smsManager = SmsManager.getDefault(); - smsManager.sendTextMessage(data.destination, null, data.content, null, null); + if (smsManager == null) + return false; + smsManager.sendTextMessage(destination, null, content, null, null); return true; } } From 11933b25a1472c0e8a60511825802bc309e621a3 Mon Sep 17 00:00:00 2001 From: "Rui Zhao (renyuneyun)" Date: Sun, 11 Feb 2018 12:02:54 +0000 Subject: [PATCH 2/2] Add "passive mode" and settings entry --- app/src/main/java/ryey/easer/core/EHService.java | 6 ++++-- app/src/main/java/ryey/easer/core/Lotus.java | 4 +++- app/src/main/java/ryey/easer/core/SettingsHelper.java | 5 +++++ app/src/main/res/values-zh/strings.xml | 3 +++ app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 4 ++++ app/src/main/res/xml/preferences.xml | 6 ++++++ 7 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ryey/easer/core/EHService.java b/app/src/main/java/ryey/easer/core/EHService.java index 403798fa..289f3b72 100644 --- a/app/src/main/java/ryey/easer/core/EHService.java +++ b/app/src/main/java/ryey/easer/core/EHService.java @@ -128,8 +128,10 @@ public void onCreate() { filter.addAction(ProfileLoaderIntentService.ACTION_PROFILE_LOADED); registerReceiver(mReceiver, filter); reloadTriggers(); - for (Lotus lotus : mLotusArray) { - lotus.check(); + if (SettingsHelper.passiveMode(this)) { + for (Lotus lotus : mLotusArray) { + lotus.check(); + } } Logger.i("EHService created"); } diff --git a/app/src/main/java/ryey/easer/core/Lotus.java b/app/src/main/java/ryey/easer/core/Lotus.java index bf97ec43..a3f756d2 100644 --- a/app/src/main/java/ryey/easer/core/Lotus.java +++ b/app/src/main/java/ryey/easer/core/Lotus.java @@ -252,7 +252,9 @@ private void triggerAndPromote() { Lotus subLotus = new Lotus(context, sub, executorService); subs.add(subLotus); subLotus.listen(); - subLotus.check(); + if (SettingsHelper.passiveMode(context)) { + subLotus.check(); + } } } } diff --git a/app/src/main/java/ryey/easer/core/SettingsHelper.java b/app/src/main/java/ryey/easer/core/SettingsHelper.java index f9d7d816..901b6942 100644 --- a/app/src/main/java/ryey/easer/core/SettingsHelper.java +++ b/app/src/main/java/ryey/easer/core/SettingsHelper.java @@ -13,4 +13,9 @@ public static int coolDownInterval(Context context) { return Integer.parseInt(interval_pref); } + public static boolean passiveMode(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(context.getString(R.string.key_pref_passive_mode), true); + } + } diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 237f782f..fbbeea37 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -85,5 +85,8 @@ 反轉情境 固定滿足態 不固定滿足態 + 被動模式 + 當激活事件時,檢查當前狀態 + 僅監聽事件;事件激活時不檢查 diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 569b19c3..3bf08a91 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -8,6 +8,7 @@ preference_version preference_use_root preference_cool_down_time + preference_passive_mode preference_plugins preference_enabled_operation_plugins preference_enabled_event_plugins diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b8ac1135..455f727f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -102,6 +102,10 @@ Reminder: Easer is still in development. It may contain bugs and the UI definite Interval between two triggers of the same event The number must not be lower than 0 and not too large to fit in a integer + Passive mode + Listen to Event; no checking when activated + Check the satisfaction state when (each) Event is activated + Enabled plugins Enable or disable certain plugins diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 6dab9138..64438fcb 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -35,6 +35,12 @@ android:singleLine="true" android:summary="@string/pref_cooldown_summary" android:title="@string/pref_cooldown_title" /> +