Historical notification access API.

Similar to getActiveNotifications(),
getHistoricalNotifications() returns a list of all
notifications that have been posted, in
reverse-chronological order. It currently includes duplicate
entries for notifications that have been updated (so it
really is tracking every notification that has been posted
to the system).

Change-Id: Icce8d6f96bbe76710c989fd0068ff971c6498605
diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl
index bb10f62..1f4c81d 100644
--- a/core/java/android/app/INotificationManager.aidl
+++ b/core/java/android/app/INotificationManager.aidl
@@ -38,5 +38,6 @@
     boolean areNotificationsEnabledForPackage(String pkg, int uid);
 
     StatusBarNotification[] getActiveNotifications(String callingPkg);
+    StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count);
 }
 
diff --git a/services/java/com/android/server/NotificationManagerService.java b/services/java/com/android/server/NotificationManagerService.java
index 1a2c3de..13bf39f 100644
--- a/services/java/com/android/server/NotificationManagerService.java
+++ b/services/java/com/android/server/NotificationManagerService.java
@@ -235,6 +235,31 @@
                 }
             };
         }
+
+        public StatusBarNotification[] getArray(int count) {
+            if (count == 0) count = Archive.BUFFER_SIZE;
+            final StatusBarNotification[] a
+                    = new StatusBarNotification[Math.min(count, mBuffer.size())];
+            Iterator<StatusBarNotification> iter = descendingIterator();
+            int i=0;
+            while (iter.hasNext() && i < count) {
+                a[i++] = iter.next();
+            }
+            return a;
+        }
+
+        public StatusBarNotification[] getArray(int count, String pkg, int userId) {
+            if (count == 0) count = Archive.BUFFER_SIZE;
+            final StatusBarNotification[] a
+                    = new StatusBarNotification[Math.min(count, mBuffer.size())];
+            Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
+            int i=0;
+            while (iter.hasNext() && i < count) {
+                a[i++] = iter.next();
+            }
+            return a;
+        }
+
     }
 
     Archive mArchive = new Archive();
@@ -347,10 +372,9 @@
 
     public StatusBarNotification[] getActiveNotifications(String callingPkg) {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
-                "NotificationManagerService");
+                "NotificationManagerService.getActiveNotifications");
 
         StatusBarNotification[] tmp = null;
-        int userId = UserHandle.getCallingUserId();
         int uid = Binder.getCallingUid();
 
         if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
@@ -366,6 +390,22 @@
         return tmp;
     }
 
+    public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
+                "NotificationManagerService.getHistoricalNotifications");
+
+        StatusBarNotification[] tmp = null;
+        int uid = Binder.getCallingUid();
+
+        if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
+                == AppOpsManager.MODE_ALLOWED) {
+            synchronized (mArchive) {
+                tmp = mArchive.getArray(count);
+            }
+        }
+        return tmp;
+    }
+
     public static final class NotificationRecord
     {
         final StatusBarNotification sbn;