Skip to content

Commit 09b4b98

Browse files
committed
Add #getFreshNotification() to NotificationPresenter
1 parent 5793f11 commit 09b4b98

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

project/app/src/main/java/com/achep/acdisplay/notifications/NotificationPresenter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.content.Context;
2323
import android.os.Handler;
2424
import android.os.Looper;
25+
import android.os.SystemClock;
2526
import android.service.notification.StatusBarNotification;
2627
import android.support.annotation.NonNull;
2728
import android.support.annotation.Nullable;
@@ -67,6 +68,8 @@ public class NotificationPresenter implements
6768
*/
6869
private static final boolean FILTER_NOISY_NOTIFICATIONS = true;
6970

71+
private static final int FRESH_NOTIFICATION_EXPIRY_TIME = 4000; // 4 sec.
72+
7073
public static final int EVENT_BATH = 0;
7174
public static final int EVENT_POSTED = 1;
7275
public static final int EVENT_CHANGED = 2;
@@ -468,6 +471,16 @@ private void rebuildLocalList() {
468471
}
469472
}
470473

474+
@Nullable
475+
public OpenNotification getFreshNotification() {
476+
for (OpenNotification n : getList()) {
477+
long delta = Math.max(n.getNotification().priority, 1) * FRESH_NOTIFICATION_EXPIRY_TIME;
478+
long past = SystemClock.elapsedRealtime() - delta;
479+
if (!n.isRead() && n.getLoadTimestamp() > past) return n;
480+
}
481+
return null;
482+
}
483+
471484
@NonNull
472485
public ArrayList<OpenNotification> getList() {
473486
return mLList.list();

project/app/src/main/java/com/achep/acdisplay/notifications/OpenNotification.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.graphics.Color;
2929
import android.graphics.drawable.Drawable;
3030
import android.os.Build;
31+
import android.os.SystemClock;
3132
import android.service.notification.StatusBarNotification;
3233
import android.support.annotation.NonNull;
3334
import android.support.annotation.Nullable;
@@ -91,6 +92,7 @@ public static OpenNotification newInstance(@NonNull Notification n) {
9192
private boolean mEmoticonsEnabled;
9293
private boolean mMine;
9394
private boolean mRead;
95+
private long mLoadedTimestamp;
9496
private int mNumber;
9597

9698
// Extracted
@@ -159,6 +161,7 @@ protected OpenNotification(@Nullable StatusBarNotification sbn, @NonNull Notific
159161
}
160162

161163
public void load(@NonNull Context context) {
164+
mLoadedTimestamp = SystemClock.elapsedRealtime();
162165
mMine = TextUtils.equals(getPackageName(), PackageUtils.getName(context));
163166
mActions = Action.makeFor(mNotification);
164167
mNumber = mNotification.number;
@@ -502,6 +505,14 @@ public String getPackageName() {
502505
return mStatusBarNotification.getPackageName();
503506
}
504507

508+
/**
509+
* Time since notification has been loaded; in {@link android.os.SystemClock#elapsedRealtime()}
510+
* format.
511+
*/
512+
public long getLoadTimestamp() {
513+
return mLoadedTimestamp;
514+
}
515+
505516
public boolean isGroupChild() {
506517
return false;
507518
}

project/app/src/main/java/com/achep/acdisplay/ui/fragments/AcDisplayFragment.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,23 @@ public boolean isNotDemo() {
10471047

10481048
//-- NOTIFICATION HANDLING ------------------------------------------------
10491049

1050+
@Nullable
1051+
private NotifyWidget find(@Nullable OpenNotification n) {
1052+
if (n == null) return null;
1053+
// Find the widget of this or previous notification,
1054+
// so we can manage it.
1055+
for (Widget item : mWidgetsMap.values()) {
1056+
if (item instanceof NotifyWidget) {
1057+
// Check if notification has the same key.
1058+
NotifyWidget nw = (NotifyWidget) item;
1059+
if (nw.hasIdenticalIds(n)) {
1060+
return nw;
1061+
}
1062+
}
1063+
}
1064+
return null;
1065+
}
1066+
10501067
@Override
10511068
public void onNotificationListChanged(@NonNull NotificationPresenter np,
10521069
OpenNotification osbn,
@@ -1060,16 +1077,7 @@ public void onNotificationListChanged(@NonNull NotificationPresenter np,
10601077

10611078
// Find the widget of this or previous notification,
10621079
// so we can manage it.
1063-
for (Widget item : mWidgetsMap.values()) {
1064-
if (item instanceof NotifyWidget) {
1065-
// Check if notification has the same key.
1066-
NotifyWidget nw = (NotifyWidget) item;
1067-
if (nw.hasIdenticalIds(osbn)) {
1068-
widgetPrev = nw;
1069-
break;
1070-
}
1071-
}
1072-
}
1080+
widgetPrev = find(osbn);
10731081
}
10741082

10751083
switch (event) { // don't update on spam-change.

0 commit comments

Comments
 (0)