Skip to content

Commit

Permalink
Bugfix/22/do not reply for older notifications (#47)
Browse files Browse the repository at this point in the history
Avoid replying to notifications older than 1 min

This is a hacky way to prevent replying to old notifications that are posted before app is enabled

#22
  • Loading branch information
spuday90 committed Feb 9, 2021
1 parent 80cbd8b commit 38ddc52
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions app/src/main/java/com/parishod/watomatic/NotificationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class NotificationService extends NotificationListenerService {
CustomRepliesData customRepliesData;
private WhatsappAutoReplyLogsDB whatsappAutoReplyLogsDB;
private final int DELAY_BETWEEN_REPLY_IN_MILLISEC = 30 * 1000;
private final int DELAY_BETWEEN_NOTIFICATION_RECEIVED_IN_MILLISEC = 60 * 1000;

/*
These are the package names of the apps. for which we want to
Expand All @@ -44,13 +45,11 @@ public void onNotificationPosted(StatusBarNotification sbn) {
}

private boolean canReply(StatusBarNotification sbn){
if(PreferencesManager.getPreferencesInstance(this).isServiceEnabled() &&
return isServiceEnabled() &&
isSupportedPackage(sbn) &&
isNewNotification(sbn) &&
isGroupMessageAndReplyAllowed(sbn) &&
canSendReplyNow(sbn.getNotification().extras.getString("android.title"))) {
return true;
}
return false;
canSendReplyNow(sbn);
}

@Override
Expand Down Expand Up @@ -147,7 +146,8 @@ private boolean isSupportedPackage(StatusBarNotification sbn) {
}
}

private boolean canSendReplyNow(String userId){
private boolean canSendReplyNow(StatusBarNotification sbn){
String userId = sbn.getNotification().extras.getString("android.title");
whatsappAutoReplyLogsDB = WhatsappAutoReplyLogsDB.getInstance(getApplicationContext());
long timeDelay = PreferencesManager.getPreferencesInstance(this).getAutoReplyDelay();
return (System.currentTimeMillis() - whatsappAutoReplyLogsDB.logsDao().getLastReplyTimeStamp(userId) >= max(timeDelay, DELAY_BETWEEN_REPLY_IN_MILLISEC));
Expand All @@ -166,4 +166,19 @@ private boolean isGroupMessageAndReplyAllowed(StatusBarNotification sbn){
return PreferencesManager.getPreferencesInstance(this).isGroupReplyEnabled();
}
}

private boolean isServiceEnabled(){
return PreferencesManager.getPreferencesInstance(this).isServiceEnabled();
}

/*
This method is used to avoid replying to unreplied notifications
which are posted again when next message is received
*/
private boolean isNewNotification(StatusBarNotification sbn){
//For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
//by default unless explicitly set by the apps hence checking not 0
return sbn.getNotification().when == 0 ||
(System.currentTimeMillis() - sbn.getNotification().when) < DELAY_BETWEEN_NOTIFICATION_RECEIVED_IN_MILLISEC;
}
}

0 comments on commit 38ddc52

Please sign in to comment.