diff --git a/app/src/main/java/com/jens/automation2/Action.java b/app/src/main/java/com/jens/automation2/Action.java index 5a59e4a4..bb31d05d 100644 --- a/app/src/main/java/com/jens/automation2/Action.java +++ b/app/src/main/java/com/jens/automation2/Action.java @@ -240,6 +240,9 @@ public class Action case setScreenBrightness: returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.setScreenBrightness)); break; + case createNotification: + returnString.append(Miscellaneous.getAnyContext().getResources().getString(R.string.createNotification)); + break; default: returnString.append(action.toString()); } @@ -284,7 +287,7 @@ public class Action } else if (parameter2 != null && parameter2.length() > 0) - returnString.append(": " + parameter2); + returnString.append(": " + parameter2.replace(Action.actionParameter2Split, "; ")); return returnString.toString(); } @@ -454,6 +457,9 @@ public class Action case playSound: Actions.playSound(getParameter1(), getParameter2()); break; + case createNotification: + Actions.createNotification(this); + 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 4df26e7b..eb7c78cf 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -54,6 +54,7 @@ import java.lang.reflect.Method; import java.net.InetAddress; import java.net.NetworkInterface; import java.security.KeyStore; +import java.util.Calendar; import java.util.Collections; import java.util.List; import java.util.Set; @@ -79,7 +80,27 @@ public class Actions public static final String wireguard_tunnel_down = "com.wireguard.android.action.SET_TUNNEL_DOWN"; public static final String wireguard_tunnel_refresh = "com.wireguard.android.action.REFRESH_TUNNEL_STATES"; - public static class WifiStuff + public static void createNotification(Action action) + { + String[] elements = action.getParameter2().split(Action.actionParameter2Split); + + Miscellaneous.logEvent("w", "createNotification", "Creating notification with title " + elements[0] + " and text " + elements[1], 3); + + int notificationId = Math.round(Calendar.getInstance().getTimeInMillis()/1000); + + try + { + String title = Miscellaneous.replaceVariablesInText(elements[0], Miscellaneous.getAnyContext()); + String text = Miscellaneous.replaceVariablesInText(elements[1], Miscellaneous.getAnyContext()); + Miscellaneous.createDismissableNotification(title, text, notificationId, null); + } + catch (Exception e) + { + Miscellaneous.logEvent("w", "createNotification", "Error occurred while replacing vars: " + Log.getStackTraceString(e), 3); + } + } + + public static class WifiStuff { public static Boolean setWifi(Context context, Boolean desiredState, boolean toggleActionIfPossible) { diff --git a/app/src/main/java/com/jens/automation2/ActivityManageRule.java b/app/src/main/java/com/jens/automation2/ActivityManageRule.java index 4efaf175..04a5b0c1 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageRule.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageRule.java @@ -1559,6 +1559,8 @@ public class ActivityManageRule extends Activity 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.createNotification.toString())) + items.add(new Item(typesLong[i].toString(), R.drawable.notification)); else if(types[i].toString().equals(Action_Enum.sendTextMessage.toString())) { // if(ActivityPermissions.isPermissionDeclaratedInManifest(ActivityManageSpecificRule.this, "android.permission.SEND_SMS") && !Miscellaneous.isGooglePlayInstalled(ActivityManageSpecificRule.this)) diff --git a/app/src/main/java/com/jens/automation2/AutomationService.java b/app/src/main/java/com/jens/automation2/AutomationService.java index a14a91d0..abf36e2f 100644 --- a/app/src/main/java/com/jens/automation2/AutomationService.java +++ b/app/src/main/java/com/jens/automation2/AutomationService.java @@ -365,9 +365,9 @@ public class AutomationService extends Service implements OnInitListener Miscellaneous.logEvent("w", "Features disabled", "Features disabled because of rule " + rule, 5); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) - Miscellaneous.createDismissableNotificationWithDelay(1010, getResources().getString(R.string.featuresDisabled), ActivityPermissions.notificationIdPermissions, pi); + Miscellaneous.createDismissableNotificationWithDelay(1010, null, getResources().getString(R.string.featuresDisabled), ActivityPermissions.notificationIdPermissions, pi); else - Miscellaneous.createDismissableNotification(getResources().getString(R.string.featuresDisabled), ActivityPermissions.notificationIdPermissions, pi); + Miscellaneous.createDismissableNotification(null, getResources().getString(R.string.featuresDisabled), ActivityPermissions.notificationIdPermissions, pi); } } } @@ -389,9 +389,9 @@ public class AutomationService extends Service implements OnInitListener Miscellaneous.logEvent("w", "Features disabled", "Background location disabled because Google to blame.", 5); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) - Miscellaneous.createDismissableNotificationWithDelay(3300, getResources().getString(R.string.featuresDisabled), notificationIdRestrictions, pi); + Miscellaneous.createDismissableNotificationWithDelay(3300, null, getResources().getString(R.string.featuresDisabled), notificationIdRestrictions, pi); else - Miscellaneous.createDismissableNotification(getResources().getString(R.string.featuresDisabled), notificationIdRestrictions, pi); + Miscellaneous.createDismissableNotification(null, getResources().getString(R.string.featuresDisabled), notificationIdRestrictions, pi); } } } @@ -411,9 +411,9 @@ public class AutomationService extends Service implements OnInitListener Miscellaneous.logEvent("w", "Features disabled", "Background location disabled because Google to blame.", 5); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) - Miscellaneous.createDismissableNotificationWithDelay(2200, getResources().getString(R.string.featuresDisabled), notificationIdLocationRestriction, pi); + Miscellaneous.createDismissableNotificationWithDelay(2200, null, getResources().getString(R.string.featuresDisabled), notificationIdLocationRestriction, pi); else - Miscellaneous.createDismissableNotification(getResources().getString(R.string.featuresDisabled), notificationIdLocationRestriction, pi); + Miscellaneous.createDismissableNotification(null, getResources().getString(R.string.featuresDisabled), notificationIdLocationRestriction, pi); } } diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index 25da177f..66b37eba 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -877,7 +877,7 @@ public class Miscellaneous extends Service @SuppressLint("NewApi") @SuppressWarnings("deprecation") - public static void createDismissableNotificationWithDelay(long delay, String textToDisplay, int notificationId, PendingIntent pendingIntent) + public static void createDismissableNotificationWithDelay(long delay, String title, String textToDisplay, int notificationId, PendingIntent pendingIntent) { /* Now what's this about? @@ -903,7 +903,7 @@ public class Miscellaneous extends Service catch(Exception e) {} - createDismissableNotification(textToDisplay, notificationId, pendingIntent); + createDismissableNotification(title, textToDisplay, notificationId, pendingIntent); return null; } @@ -924,17 +924,23 @@ public class Miscellaneous extends Service @SuppressLint("NewApi") @SuppressWarnings("deprecation") - public static void createDismissableNotification(String textToDisplay, int notificationId, PendingIntent pendingIntent) + public static void createDismissableNotification(String title, String textToDisplay, int notificationId, PendingIntent pendingIntent) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createDismissableNotificationSdk26(textToDisplay, notificationId, pendingIntent); + createDismissableNotificationSdk26(title, textToDisplay, notificationId, pendingIntent); return; } NotificationManager mNotificationManager = (NotificationManager) Miscellaneous.getAnyContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder dismissableNotificationBuilder = createDismissableNotificationBuilder(pendingIntent); + + if(title == null) + dismissableNotificationBuilder.setContentTitle(AutomationService.getInstance().getResources().getString(R.string.app_name)); + else + dismissableNotificationBuilder.setContentTitle(title); + dismissableNotificationBuilder.setContentText(textToDisplay); dismissableNotificationBuilder.setContentIntent(pendingIntent); dismissableNotificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(textToDisplay)); @@ -957,7 +963,7 @@ public class Miscellaneous extends Service mNotificationManager.notify(0, dismissableNotification);*/ } - static void createDismissableNotificationSdk26(String textToDisplay, int notificationId, PendingIntent pendingIntent) + static void createDismissableNotificationSdk26(String title, String textToDisplay, int notificationId, PendingIntent pendingIntent) { NotificationManager mNotificationManager = (NotificationManager) AutomationService.getInstance().getSystemService(Context.NOTIFICATION_SERVICE); @@ -983,7 +989,11 @@ public class Miscellaneous extends Service builder.setWhen(System.currentTimeMillis()); builder.setContentIntent(pendingIntent); - builder.setContentTitle(AutomationService.getInstance().getResources().getString(R.string.app_name)); + if(title == null) + builder.setContentTitle(AutomationService.getInstance().getResources().getString(R.string.app_name)); + else + builder.setContentTitle(title); + builder.setOnlyAlertOnce(true); if(Settings.showIconWhenServiceIsRunning) diff --git a/app/src/main/res/layout/activity_manage_action_create_notification.xml b/app/src/main/res/layout/activity_manage_action_create_notification.xml index 5166c600..be71b41b 100644 --- a/app/src/main/res/layout/activity_manage_action_create_notification.xml +++ b/app/src/main/res/layout/activity_manage_action_create_notification.xml @@ -39,13 +39,6 @@ android:ems="10" /> - - @@ -62,7 +55,41 @@ android:layout_height="wrap_content" android:ems="10" /> - > + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f527067..874f3b04 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -758,4 +758,6 @@ Create notification Enter a title. Enter a text. + Icon + Sound \ No newline at end of file