Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Show only one time notification #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ proguard/
*.iml
*.ipr
*.iws
.idea/
.idea/

#export folder
export/

#key file
key.key
3 changes: 2 additions & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

# Project target.
target=android-17
android.library.reference.1=../locale-api
android.library.reference.1=../display/locale-api
android.library=false
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

<string name="app_name" tools:ignore="MissingTranslation">Pebble Notifier</string>
<string name="notification_title_hint" tools:ignore="MissingTranslation">My pebble notification title</string>
<string name="notification_body_hint" tools:ignore="MissingTranslation">Notification body goes here! You can use any tasker variables such as %NTITLE for the notification title</string>
Expand Down Expand Up @@ -84,5 +83,6 @@
<string name="dialog_title_remove_rename">Do you want to remove the rename for </string>
<string name="error_cant_be_blank">This can\'t be blank</string>
<string name="ignore_case_insensitive">Case\nInsensitive</string>
<string name="pref_dont_throw_many_time">Don\'t repeat notification</string>

</resources>
3 changes: 3 additions & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
android:key="pref_no_ongoing_notif"
android:summary="@string/pref_no_ongoing_notif_summ"
android:title="@string/pref_no_ongoing_notif" />
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_dont_throw_many_time" android:title="@string/pref_dont_throw_many_time"/>
<com.dattasmoon.pebble.plugin.ConvertPreference
android:defaultValue="[]"
android:key="pref_converts"
Expand Down
1 change: 1 addition & 0 deletions src/com/dattasmoon/pebble/plugin/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class Constants {
public static final String PREFERENCE_CONVERTS = "pref_converts";
public static final String PREFERENCE_IGNORE = "pref_ignore";
public static final String PREFERENCE_PKG_RENAMES = "pref_pkg_renames";
public static final String PREFERENCE_DONT_THROW_MANY_TIME = "pref_dont_throw_many_time";

// Intents
public static final String INTENT_SEND_PEBBLE_NOTIFICATION = "com.getpebble.action.SEND_NOTIFICATION";
Expand Down
172 changes: 103 additions & 69 deletions src/com/dattasmoon/pebble/plugin/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -55,24 +60,26 @@ public queueItem(String title, String body) {
}
}

private Mode mode = Mode.EXCLUDE;
private boolean notifications_only = false;
private boolean no_ongoing_notifs = false;
private boolean notification_extras = false;
private boolean quiet_hours = false;
private boolean notifScreenOn = true;
private JSONArray converts = new JSONArray();
private JSONArray ignores = new JSONArray();
private JSONArray pkg_renames = new JSONArray();
private long min_notification_wait = 0 * 1000;
private long notification_last_sent = 0;
private Date quiet_hours_before = null;
private Date quiet_hours_after = null;
private String[] packages = null;
private Handler mHandler;
private File watchFile;
private Long lastChange;
Queue<queueItem> queue;
private Mode mode = Mode.EXCLUDE;
private boolean notifications_only = false;
private boolean no_ongoing_notifs = false;
private boolean notification_extras = false;
private boolean quiet_hours = false;
private boolean notifScreenOn = true;
private boolean dontThrowManyTime = false;
private JSONArray converts = new JSONArray();
private JSONArray ignores = new JSONArray();
private JSONArray pkg_renames = new JSONArray();
private long min_notification_wait = 0 * 1000;
private long notification_last_sent = 0;
private Date quiet_hours_before = null;
private Date quiet_hours_after = null;
private String[] packages = null;
private Handler mHandler;
private File watchFile;
private Long lastChange;
private HashMap<String, String> previousNotification = null;
Queue<queueItem> queue;

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Expand All @@ -92,25 +99,26 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
return;
}

//handle quiet hours
if(quiet_hours){
// handle quiet hours
if (quiet_hours) {

Calendar c = Calendar.getInstance();
Date now = new Date(0, 0, 0, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG, "Checking quiet hours. Now: " + now.toString() + " vs " +
quiet_hours_before.toString() + " and " +quiet_hours_after.toString());
Log.i(Constants.LOG_TAG,
"Checking quiet hours. Now: " + now.toString() + " vs " + quiet_hours_before.toString()
+ " and " + quiet_hours_after.toString());
}

if(quiet_hours_before.after(quiet_hours_after)){
if(now.after(quiet_hours_after) && now.before(quiet_hours_before)){
if (quiet_hours_before.after(quiet_hours_after)) {
if (now.after(quiet_hours_after) && now.before(quiet_hours_before)) {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG, "Time is during quiet time. Returning.");
}
return;
}

} else if(now.before(quiet_hours_before) || now.after(quiet_hours_after)){
} else if (now.before(quiet_hours_before) || now.after(quiet_hours_after)) {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG, "Time is before or after the quiet hours time. Returning.");
}
Expand All @@ -133,11 +141,11 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
}
}
}
if (no_ongoing_notifs){
if (no_ongoing_notifs) {
Parcelable parcelable = event.getParcelableData();
if (parcelable instanceof Notification) {
Notification notif = (Notification) parcelable;
if ((notif.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT){
if ((notif.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG,
"Event is a notification, notification flag contains ongoing, and no ongoing notification is true. Returning.");
Expand All @@ -146,13 +154,11 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
}
} else {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG,
"Event is not a notification.");
Log.i(Constants.LOG_TAG, "Event is not a notification.");
}
}
}


// Handle the do not disturb screen on settings
PowerManager powMan = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (Constants.IS_LOGGABLE) {
Expand All @@ -177,7 +183,7 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
PackageManager pm = getPackageManager();

String eventPackageName;
if (event.getPackageName() != null){
if (event.getPackageName() != null) {
eventPackageName = event.getPackageName().toString();
} else {
eventPackageName = "";
Expand Down Expand Up @@ -230,13 +236,13 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
String title = "";
try {
boolean renamed = false;
for(int i = 0; i < pkg_renames.length(); i++){
if(pkg_renames.getJSONObject(i).getString("pkg").equalsIgnoreCase(eventPackageName)){
for (int i = 0; i < pkg_renames.length(); i++) {
if (pkg_renames.getJSONObject(i).getString("pkg").equalsIgnoreCase(eventPackageName)) {
renamed = true;
title = pkg_renames.getJSONObject(i).getString("to");
}
}
if(!renamed){
if (!renamed) {
title = pm.getApplicationLabel(pm.getApplicationInfo(eventPackageName, 0)).toString();
}
} catch (NameNotFoundException e) {
Expand Down Expand Up @@ -266,48 +272,67 @@ public void onAccessibilityEvent(AccessibilityEvent event) {
}

// Check ignore lists
for(int i = 0; i < ignores.length(); i++){
try{
for (int i = 0; i < ignores.length(); i++) {
try {
JSONObject ignore = ignores.getJSONObject(i);
String app = ignore.getString("app");
boolean exclude = ignore.optBoolean("exclude", true);
boolean case_insensitive = ignore.optBoolean("insensitive", true);
if((!app.equals("-1")) && (!eventPackageName.equalsIgnoreCase(app))){
//this rule doesn't apply to all apps and this isn't the app we're looking for.
if ((!app.equals("-1")) && (!eventPackageName.equalsIgnoreCase(app))) {
// this rule doesn't apply to all apps and this isn't the
// app we're looking for.
continue;
}
String regex = "";
if(case_insensitive){
if (case_insensitive) {
regex += "(?i)";
}
if(!ignore.getBoolean("raw")){
if (!ignore.getBoolean("raw")) {
regex += Pattern.quote(ignore.getString("match"));
} else {
regex += ignore.getString("match");
}
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(notificationText);
if(m.find()){
if(exclude){
if (m.find()) {
if (exclude) {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText + "' matches: '" + regex +"' and exclude is on. Returning");
Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText + "' matches: '"
+ regex + "' and exclude is on. Returning");
}
return;
}
} else {
if(!exclude){
if(Constants.IS_LOGGABLE){
Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText + "' does not match: '" + regex +"' and include is on. Returning");
if (!exclude) {
if (Constants.IS_LOGGABLE) {
Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText
+ "' does not match: '" + regex + "' and include is on. Returning");
}
return;
}

}
} catch (JSONException e){
} catch (JSONException e) {
continue;
}
}

// validate don't throw same notification
try {
if (dontThrowManyTime) {
if (previousNotification == null) {
previousNotification = new HashMap<String, String>();
}
if (previousNotification.containsKey(eventPackageName)
&& previousNotification.get(eventPackageName).equalsIgnoreCase(notificationText)) {
Log.i(Constants.LOG_TAG, "Notification many time =" + notificationText);
return;
}
}
previousNotification.put(eventPackageName, notificationText);
} catch (Exception e) {
e.printStackTrace();
}

// Send the alert to Pebble

Expand All @@ -328,18 +353,19 @@ private void sendToPebble(String title, String notificationText) {
}
return;
}
for(int i = 0; i < converts.length(); i++){
for (int i = 0; i < converts.length(); i++) {
String from;
String to;
try{
try {
JSONObject convert = converts.getJSONObject(i);
from = "(?i)" + Pattern.quote(convert.getString("from"));
to = convert.getString("to");
} catch (JSONException e){
} catch (JSONException e) {
continue;
}
//not sure if the title should be replaced as well or not. I'm guessing not
//title = title.replaceAll(from, to);
// not sure if the title should be replaced as well or not. I'm
// guessing not
// title = title.replaceAll(from, to);
notificationText = notificationText.replaceAll(from, to);
}

Expand Down Expand Up @@ -405,16 +431,22 @@ private void loadPrefs() {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

SharedPreferences sharedPreferences = getSharedPreferences(Constants.LOG_TAG, MODE_MULTI_PROCESS | MODE_PRIVATE);
//if old preferences exist, convert them.
if(sharedPreferences.contains(Constants.LOG_TAG + ".mode")){
// if old preferences exist, convert them.
if (sharedPreferences.contains(Constants.LOG_TAG + ".mode")) {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(Constants.PREFERENCE_MODE, sharedPreferences.getInt(Constants.LOG_TAG + ".mode", Constants.Mode.OFF.ordinal()));
editor.putString(Constants.PREFERENCE_PACKAGE_LIST, sharedPreferences.getString(Constants.LOG_TAG + ".packageList", ""));
editor.putBoolean(Constants.PREFERENCE_NOTIFICATIONS_ONLY, sharedPreferences.getBoolean(Constants.LOG_TAG + ".notificationsOnly", true));
editor.putBoolean(Constants.PREFERENCE_NOTIFICATION_EXTRA, sharedPreferences.getBoolean(Constants.LOG_TAG + ".fetchNotificationExtras", false));
editor.putInt(Constants.PREFERENCE_MODE,
sharedPreferences.getInt(Constants.LOG_TAG + ".mode", Constants.Mode.OFF.ordinal()));
editor.putString(Constants.PREFERENCE_PACKAGE_LIST,
sharedPreferences.getString(Constants.LOG_TAG + ".packageList", ""));
editor.putBoolean(Constants.PREFERENCE_NOTIFICATIONS_ONLY,
sharedPreferences.getBoolean(Constants.LOG_TAG + ".notificationsOnly", true));
editor.putBoolean(Constants.PREFERENCE_NOTIFICATION_EXTRA,
sharedPreferences.getBoolean(Constants.LOG_TAG + ".fetchNotificationExtras", false));
editor.putBoolean(Constants.PREFERENCE_DONT_THROW_MANY_TIME,
sharedPreferences.getBoolean(Constants.LOG_TAG + ".dontThrowManyTime", false));
editor.commit();

//clear out all old preferences
// clear out all old preferences
editor = sharedPreferences.edit();
editor.clear();
editor.commit();
Expand All @@ -438,25 +470,27 @@ private void loadPrefs() {
notification_extras = sharedPref.getBoolean(Constants.PREFERENCE_NOTIFICATION_EXTRA, false);
notifScreenOn = sharedPref.getBoolean(Constants.PREFERENCE_NOTIF_SCREEN_ON, true);
quiet_hours = sharedPref.getBoolean(Constants.PREFERENCE_QUIET_HOURS, false);
try{
dontThrowManyTime = sharedPref.getBoolean(Constants.PREFERENCE_DONT_THROW_MANY_TIME, false);
try {
converts = new JSONArray(sharedPref.getString(Constants.PREFERENCE_CONVERTS, "[]"));
} catch (JSONException e){
} catch (JSONException e) {
converts = new JSONArray();
}
try{
try {
ignores = new JSONArray(sharedPref.getString(Constants.PREFERENCE_IGNORE, "[]"));
} catch (JSONException e){
} catch (JSONException e) {
ignores = new JSONArray();
}
try{
try {
pkg_renames = new JSONArray(sharedPref.getString(Constants.PREFERENCE_PKG_RENAMES, "[]"));
} catch (JSONException e){
} catch (JSONException e) {
pkg_renames = new JSONArray();
}
//we only need to pull this if quiet hours are enabled. Save the cycles for the cpu! (haha)
if(quiet_hours){
// we only need to pull this if quiet hours are enabled. Save the cycles
// for the cpu! (haha)
if (quiet_hours) {
String[] pieces = sharedPref.getString(Constants.PREFERENCE_QUIET_HOURS_BEFORE, "00:00").split(":");
quiet_hours_before= new Date(0, 0, 0, Integer.parseInt(pieces[0]), Integer.parseInt(pieces[1]));
quiet_hours_before = new Date(0, 0, 0, Integer.parseInt(pieces[0]), Integer.parseInt(pieces[1]));
pieces = sharedPref.getString(Constants.PREFERENCE_QUIET_HOURS_AFTER, "23:59").split(":");
quiet_hours_after = new Date(0, 0, 0, Integer.parseInt(pieces[0]), Integer.parseInt(pieces[1]));
}
Expand Down
Loading