Fix first notification of each app is not shown
When app tries to post its first notification,
value of now is equal to that of mLastEventTime.
And hence getRate return a very large number.
Bug: 28902358
Change-Id: If5b5b3c46e2bb80a9b40988ba7f7d777e40cc8e7
diff --git a/services/core/java/com/android/server/notification/NotificationUsageStats.java b/services/core/java/com/android/server/notification/NotificationUsageStats.java
index 00d7a7b..7b573da 100644
--- a/services/core/java/com/android/server/notification/NotificationUsageStats.java
+++ b/services/core/java/com/android/server/notification/NotificationUsageStats.java
@@ -384,7 +384,7 @@
noisyImportance = new ImportanceHistogram(context, "note_imp_noisy_");
quietImportance = new ImportanceHistogram(context, "note_imp_quiet_");
finalImportance = new ImportanceHistogram(context, "note_importance_");
- enqueueRate = new RateEstimator(mCreated);
+ enqueueRate = new RateEstimator();
}
public AggregatedStats getPrevious() {
diff --git a/services/core/java/com/android/server/notification/RateEstimator.java b/services/core/java/com/android/server/notification/RateEstimator.java
index 4dc30a4..c17db4a 100644
--- a/services/core/java/com/android/server/notification/RateEstimator.java
+++ b/services/core/java/com/android/server/notification/RateEstimator.java
@@ -25,30 +25,43 @@
public class RateEstimator {
private static final double RATE_ALPHA = 0.8;
private static final double MINIMUM_DT = 0.0005;
- private long mLastEventTime;
- private float mInterarrivalTime;
+ private Long mLastEventTime;
+ private Float mInterarrivalTime;
- public RateEstimator(long now) {
- mLastEventTime = now;
- }
+ public RateEstimator() {}
- /** Update the estimate to account for an event that jsut happened. */
+ /** Update the estimate to account for an event that just happened. */
public float update(long now) {
- mInterarrivalTime = (float) getInterarrivalEstimate(now);
+ float rate;
+ if (mLastEventTime == null) {
+ // No last event time, rate is zero.
+ rate = 0f;
+ } else {
+ // Calculate the new inter-arrival time based on last event time.
+ mInterarrivalTime = (float) getInterarrivalEstimate(now);
+ rate = (float) (1.0 / mInterarrivalTime);
+ }
mLastEventTime = now;
- return (float) (1.0 / mInterarrivalTime);
+ return rate;
}
/** @return the estimated rate if there were a new event right now. */
public float getRate(long now) {
+ if (mLastEventTime == null) {
+ return 0f;
+ }
return (float) (1.0 / getInterarrivalEstimate(now));
}
/** @return the average inter-arrival time if there were a new event right now. */
private double getInterarrivalEstimate(long now) {
- // a*iat_old + (1-a)*(t_now-t_last)
double dt = ((double) (now - mLastEventTime)) / 1000.0;
dt = Math.max(dt, MINIMUM_DT);
+ if (mInterarrivalTime == null) {
+ // No last inter-arrival time, return the new value directly.
+ return dt;
+ }
+ // a*iat_old + (1-a)*(t_now-t_last)
return (RATE_ALPHA * mInterarrivalTime + (1.0 - RATE_ALPHA) * dt);
}
}