New API to request a list of current notifications.

The ACCESS_NOTIFICATIONS permission is signature|system only.

Change-Id: I41338230aee9611117cbdac251c1b6b6c3cebf00
diff --git a/core/java/android/app/AppOpsManager.java b/core/java/android/app/AppOpsManager.java
index 241a9ae..34708ab 100644
--- a/core/java/android/app/AppOpsManager.java
+++ b/core/java/android/app/AppOpsManager.java
@@ -40,6 +40,10 @@
     public static final int MODE_IGNORED = 1;
     public static final int MODE_ERRORED = 2;
 
+    // when adding one of these:
+    //  - increment _NUM_OP
+    //  - add rows to sOpToSwitch, sOpNames, sOpPerms
+    //  - add descriptive strings to Settings/res/values/arrays.xml
     public static final int OP_NONE = -1;
     public static final int OP_COARSE_LOCATION = 0;
     public static final int OP_FINE_LOCATION = 1;
@@ -66,8 +70,9 @@
     public static final int OP_WRITE_ICC_SMS = 22;
     public static final int OP_WRITE_SETTINGS = 23;
     public static final int OP_SYSTEM_ALERT_WINDOW = 24;
+    public static final int OP_ACCESS_NOTIFICATIONS = 25;
     /** @hide */
-    public static final int _NUM_OP = 25;
+    public static final int _NUM_OP = 26;
 
     /**
      * This maps each operation to the operation that serves as the
@@ -103,6 +108,7 @@
             OP_WRITE_SMS,
             OP_WRITE_SETTINGS,
             OP_SYSTEM_ALERT_WINDOW,
+            OP_ACCESS_NOTIFICATIONS,
     };
 
     /**
@@ -135,6 +141,7 @@
             "WRITE_ICC_SMS",
             "WRITE_SETTINGS",
             "SYSTEM_ALERT_WINDOW",
+            "ACCESS_NOTIFICATIONS",
     };
 
     /**
@@ -167,6 +174,7 @@
             android.Manifest.permission.WRITE_SMS,
             android.Manifest.permission.WRITE_SETTINGS,
             android.Manifest.permission.SYSTEM_ALERT_WINDOW,
+            android.Manifest.permission.ACCESS_NOTIFICATIONS,
     };
 
     public static int opToSwitch(int op) {
diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl
index d400eba..bb10f62 100644
--- a/core/java/android/app/INotificationManager.aidl
+++ b/core/java/android/app/INotificationManager.aidl
@@ -21,6 +21,8 @@
 import android.app.Notification;
 import android.content.Intent;
 
+import com.android.internal.statusbar.StatusBarNotification;
+
 /** {@hide} */
 interface INotificationManager
 {
@@ -34,5 +36,7 @@
 
     void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled);
     boolean areNotificationsEnabledForPackage(String pkg, int uid);
+
+    StatusBarNotification[] getActiveNotifications(String callingPkg);
 }
 
diff --git a/core/java/com/android/internal/statusbar/StatusBarNotification.java b/core/java/com/android/internal/statusbar/StatusBarNotification.java
index a91aa3c..23e87fc 100644
--- a/core/java/com/android/internal/statusbar/StatusBarNotification.java
+++ b/core/java/com/android/internal/statusbar/StatusBarNotification.java
@@ -21,23 +21,13 @@
 import android.os.Parcelable;
 import android.os.UserHandle;
 
-/*
-boolean clearable = !n.ongoingEvent && ((notification.flags & Notification.FLAG_NO_CLEAR) == 0);
-
-
-// TODO: make this restriction do something smarter like never fill
-// more than two screens.  "Why would anyone need more than 80 characters." :-/
-final int maxTickerLen = 80;
-if (truncatedTicker != null && truncatedTicker.length() > maxTickerLen) {
-    truncatedTicker = truncatedTicker.subSequence(0, maxTickerLen);
-}
-*/
-
 /**
- * Class encapsulating a Notification. Sent by the NotificationManagerService to the IStatusBar (in System UI).
+ * Class encapsulating a Notification. Sent by the NotificationManagerService to clients including
+ * the IStatusBar (in System UI).
  */
 public class StatusBarNotification implements Parcelable {
     public final String pkg;
+    public final String basePkg;
     public final int id;
     public final String tag;
     public final int uid;
@@ -47,6 +37,7 @@
     public final Notification notification;
     public final int score;
     public final UserHandle user;
+    public final long postTime;
 
     /** This is temporarily needed for the JB MR1 PDK. */
     @Deprecated
@@ -57,10 +48,23 @@
 
     public StatusBarNotification(String pkg, int id, String tag, int uid, int initialPid, int score,
             Notification notification, UserHandle user) {
+        this(pkg, null, id, tag, uid, initialPid, score, notification, user);
+    }
+
+    public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid,
+            int initialPid, int score, Notification notification, UserHandle user) {
+        this(pkg, basePkg, id, tag, uid, initialPid, score, notification, user,
+                System.currentTimeMillis());
+    }
+
+    public StatusBarNotification(String pkg, String basePkg, int id, String tag, int uid,
+            int initialPid, int score, Notification notification, UserHandle user,
+            long postTime) {
         if (pkg == null) throw new NullPointerException();
         if (notification == null) throw new NullPointerException();
 
         this.pkg = pkg;
+        this.basePkg = pkg;
         this.id = id;
         this.tag = tag;
         this.uid = uid;
@@ -69,10 +73,13 @@
         this.notification = notification;
         this.user = user;
         this.notification.setUser(user);
+
+        this.postTime = postTime;
     }
 
     public StatusBarNotification(Parcel in) {
         this.pkg = in.readString();
+        this.basePkg = in.readString();
         this.id = in.readInt();
         if (in.readInt() != 0) {
             this.tag = in.readString();
@@ -84,11 +91,13 @@
         this.score = in.readInt();
         this.notification = new Notification(in);
         this.user = UserHandle.readFromParcel(in);
-        this.notification.setUser(user);
+        this.notification.setUser(this.user);
+        this.postTime = in.readLong();
     }
 
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(this.pkg);
+        out.writeString(this.basePkg);
         out.writeInt(this.id);
         if (this.tag != null) {
             out.writeInt(1);
@@ -101,6 +110,8 @@
         out.writeInt(this.score);
         this.notification.writeToParcel(out, flags);
         user.writeToParcel(out, flags);
+
+        out.writeLong(this.postTime);
     }
 
     public int describeContents() {
@@ -123,14 +134,17 @@
 
     @Override
     public StatusBarNotification clone() {
-        return new StatusBarNotification(this.pkg, this.id, this.tag, this.uid, this.initialPid,
-                this.score, this.notification.clone(), this.user);
+        return new StatusBarNotification(this.pkg, this.basePkg,
+                this.id, this.tag, this.uid, this.initialPid,
+                this.score, this.notification.clone(), this.user, this.postTime);
     }
 
     @Override
     public String toString() {
-        return "StatusBarNotification(pkg=" + pkg + " id=" + id + " tag=" + tag + " score=" + score
-                + " notn=" + notification + " user=" + user + ")";
+        return String.format(
+                "StatusBarNotification(pkg=%s user=%s id=%d tag=%s score=%d: %s)",
+                this.pkg, this.user, this.id, this.tag,
+                this.score, this.notification);
     }
 
     public boolean isOngoing() {
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 5783bf6..5d0614c 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -2175,6 +2175,14 @@
         android:description="@string/permdesc_updateLock"
         android:protectionLevel="signatureOrSystem" />
 
+    <!-- Allows an application to read the current set of notifications, including
+         any metadata and intents attached.
+         @hide -->
+    <permission android:name="android.permission.ACCESS_NOTIFICATIONS"
+        android:label="@string/permlab_accessNotifications"
+        android:description="@string/permdesc_accessNotifications"
+        android:protectionLevel="signature|system" />
+
     <!-- The system process is explicitly the only one allowed to launch the
          confirmation UI for full backup/restore -->
     <uses-permission android:name="android.permission.CONFIRM_FULL_BACKUP"/>
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index 22f4e2e..00c6f6d 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -1806,6 +1806,11 @@
     <!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
     <string name="permdesc_modifyNetworkAccounting">Allows the app to modify how network usage is accounted against apps. Not for use by normal apps.</string>
 
+    <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
+    <string name="permlab_accessNotifications">access notifications</string>
+    <!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
+    <string name="permdesc_accessNotifications">Allows the app to retrieve, examine, and clear notifications, including those posted by other apps.</string>
+
     <!-- Policy administration -->
 
     <!-- Title of policy access to limiting the user's password choices -->