Port CtsVerifier to API 21 Notification Listener API. DO NOT MERGE.

The cancelNotificaiton method now takes a key, so we have to remember those.

This is a back port of changes from lmp-sprout-dev so it should not be mergedd.

Bug: 18054058
Change-Id: Icad265ed28a998e7276d8386e9754a63af6e7b04
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/notifications/MockListener.java b/apps/CtsVerifier/src/com/android/cts/verifier/notifications/MockListener.java
index 3d0e0d7..b4863fa 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/notifications/MockListener.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/notifications/MockListener.java
@@ -31,7 +31,9 @@
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 public class MockListener extends NotificationListenerService {
     static final String TAG = "MockListener";
@@ -67,8 +69,10 @@
 
     private ArrayList<String> mPosted = new ArrayList<String>();
     private ArrayMap<String, JSONObject> mNotifications = new ArrayMap<>();
+    private ArrayMap<String, String> mNotificationKeys = new ArrayMap<>();
     private ArrayList<String> mRemoved = new ArrayList<String>();
     private ArrayList<String> mOrder = new ArrayList<>();
+    private Set<String> mTestPackages = new HashSet<>();
     private BroadcastReceiver mReceiver;
     private int mDND = -1;
 
@@ -77,6 +81,8 @@
         super.onCreate();
         Log.d(TAG, "created");
 
+        mTestPackages.add("com.android.cts.verifier");
+
         mPosted = new ArrayList<String>();
         mRemoved = new ArrayList<String>();
 
@@ -123,10 +129,13 @@
                     setResultCode(Activity.RESULT_OK);
                 } else if (SERVICE_CLEAR_ONE.equals(action)) {
                     Log.d(TAG, "SERVICE_CLEAR_ONE");
-                    MockListener.this.cancelNotification(
-                            context.getApplicationInfo().packageName,
-                            intent.getStringExtra(EXTRA_TAG),
-                            intent.getIntExtra(EXTRA_CODE, 0));
+                    String tag = intent.getStringExtra(EXTRA_TAG);
+                    String key = mNotificationKeys.get(tag);
+                    if (key != null) {
+                        MockListener.this.cancelNotification(key);
+                    } else {
+                        Log.w(TAG, "Notification does not exist: " + tag);
+                    }
                 } else if (SERVICE_CLEAR_ALL.equals(action)) {
                     Log.d(TAG, "SERVICE_CLEAR_ALL");
                     MockListener.this.cancelAllNotifications();
@@ -205,6 +214,7 @@
 
     @Override
     public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
+        if (!mTestPackages.contains(sbn.getPackageName())) { return; }
         Log.d(TAG, "posted: " + sbn.getTag());
         mPosted.add(sbn.getTag());
         JSONObject notification = new JSONObject();
@@ -216,6 +226,7 @@
             notification.put(JSON_ICON, sbn.getNotification().icon);
             notification.put(JSON_FLAGS, sbn.getNotification().flags);
             mNotifications.put(sbn.getKey(), notification);
+            mNotificationKeys.put(sbn.getTag(), sbn.getKey());
         } catch (JSONException e) {
             Log.e(TAG, "failed to pack up notification payload", e);
         }
@@ -228,6 +239,7 @@
         mRemoved.add(sbn.getTag());
         mNotifications.remove(sbn.getKey());
         onNotificationRankingUpdate(rankingMap);
+        mNotificationKeys.remove(sbn.getTag());
     }
 
     public static void resetListenerData(Context context) {