Home | History | Annotate | Download | only in notifications
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.cts.verifier.notifications;
     17 
     18 import android.app.Notification;
     19 import android.content.ComponentName;
     20 import android.service.notification.NotificationListenerService;
     21 import android.service.notification.NotificationStats;
     22 import android.service.notification.StatusBarNotification;
     23 import android.util.ArrayMap;
     24 import android.util.Log;
     25 
     26 import org.json.JSONException;
     27 import org.json.JSONObject;
     28 
     29 import java.util.ArrayList;
     30 import java.util.Collection;
     31 import java.util.HashSet;
     32 import java.util.Set;
     33 
     34 public class MockListener extends NotificationListenerService {
     35     static final String TAG = "MockListener";
     36 
     37     public static final ComponentName COMPONENT_NAME =
     38             new ComponentName("com.android.cts.verifier", MockListener.class.getName());
     39 
     40 
     41     public static final String JSON_FLAGS = "flag";
     42     public static final String JSON_ICON = "icon";
     43     public static final String JSON_ID = "id";
     44     public static final String JSON_PACKAGE = "pkg";
     45     public static final String JSON_WHEN = "when";
     46     public static final String JSON_TAG = "tag";
     47     public static final String JSON_RANK = "rank";
     48     public static final String JSON_AMBIENT = "ambient";
     49     public static final String JSON_MATCHES_ZEN_FILTER = "matches_zen_filter";
     50     public static final String JSON_REASON = "reason";
     51     public static final String JSON_STATS = "stats";
     52 
     53     ArrayList<String> mPosted = new ArrayList<String>();
     54     ArrayMap<String, JSONObject> mNotifications = new ArrayMap<>();
     55     ArrayMap<String, String> mNotificationKeys = new ArrayMap<>();
     56     ArrayList<String> mRemoved = new ArrayList<String>();
     57     ArrayMap<String, JSONObject> mRemovedReason = new ArrayMap<>();
     58     ArrayList<String> mSnoozed = new ArrayList<>();
     59     ArrayList<String> mOrder = new ArrayList<>();
     60     Set<String> mTestPackages = new HashSet<>();
     61     int mDND = -1;
     62     ArrayList<Notification> mPostedNotifications = new ArrayList<Notification>();
     63     private static MockListener sNotificationListenerInstance = null;
     64     boolean isConnected;
     65 
     66     @Override
     67     public void onCreate() {
     68         super.onCreate();
     69         Log.d(TAG, "created");
     70 
     71         mTestPackages.add("com.android.cts.verifier");
     72         mTestPackages.add("com.android.cts.robot");
     73     }
     74 
     75     protected Collection<JSONObject> getPosted() {
     76         return mNotifications.values();
     77     }
     78 
     79     protected String getKeyForTag(String tag) {
     80         return mNotificationKeys.get(tag);
     81     }
     82 
     83     @Override
     84     public void onDestroy() {
     85         super.onDestroy();
     86         Log.d(TAG, "destroyed");
     87     }
     88 
     89     @Override
     90     public void onListenerConnected() {
     91         super.onListenerConnected();
     92         mDND = getCurrentInterruptionFilter();
     93         Log.d(TAG, "initial value of CurrentInterruptionFilter is " + mDND);
     94         sNotificationListenerInstance = this;
     95         isConnected = true;
     96     }
     97 
     98     @Override
     99     public void onListenerDisconnected() {
    100         isConnected = false;
    101     }
    102 
    103     @Override
    104     public void onInterruptionFilterChanged(int interruptionFilter) {
    105         super.onInterruptionFilterChanged(interruptionFilter);
    106         mDND = interruptionFilter;
    107         Log.d(TAG, "value of CurrentInterruptionFilter changed to " + mDND);
    108     }
    109 
    110     public static MockListener getInstance() {
    111         return sNotificationListenerInstance;
    112     }
    113 
    114     public void resetData() {
    115         mPosted.clear();
    116         mNotifications.clear();
    117         mRemoved.clear();
    118         mOrder.clear();
    119         mRemovedReason.clear();
    120         mSnoozed.clear();
    121         mPostedNotifications.clear();
    122     }
    123 
    124     @Override
    125     public void onNotificationRankingUpdate(RankingMap rankingMap) {
    126         String[] orderedKeys = rankingMap.getOrderedKeys();
    127         mOrder.clear();
    128         Ranking rank = new Ranking();
    129         for( int i = 0; i < orderedKeys.length; i++) {
    130             String key = orderedKeys[i];
    131             mOrder.add(key);
    132             rankingMap.getRanking(key, rank);
    133             JSONObject note = mNotifications.get(key);
    134             if (note != null) {
    135                 try {
    136                     note.put(JSON_RANK, rank.getRank());
    137                     note.put(JSON_AMBIENT, rank.isAmbient());
    138                     note.put(JSON_MATCHES_ZEN_FILTER, rank.matchesInterruptionFilter());
    139                 } catch (JSONException e) {
    140                     Log.e(TAG, "failed to pack up notification payload", e);
    141                 }
    142             }
    143         }
    144     }
    145 
    146     @Override
    147     public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
    148         if (!mTestPackages.contains(sbn.getPackageName())) { return; }
    149         Log.d(TAG, "posted: " + sbn.getTag());
    150         mPosted.add(sbn.getTag());
    151         mPostedNotifications.add(sbn.getNotification());
    152         JSONObject notification = new JSONObject();
    153         try {
    154             notification.put(JSON_TAG, sbn.getTag());
    155             notification.put(JSON_ID, sbn.getId());
    156             notification.put(JSON_PACKAGE, sbn.getPackageName());
    157             notification.put(JSON_WHEN, sbn.getNotification().when);
    158             notification.put(JSON_ICON, sbn.getNotification().icon);
    159             notification.put(JSON_FLAGS, sbn.getNotification().flags);
    160             mNotifications.put(sbn.getKey(), notification);
    161             mNotificationKeys.put(sbn.getTag(), sbn.getKey());
    162         } catch (JSONException e) {
    163             Log.e(TAG, "failed to pack up notification payload", e);
    164         }
    165         onNotificationRankingUpdate(rankingMap);
    166     }
    167 
    168     @Override
    169     public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
    170         Log.d(TAG, "removed: " + sbn.getTag());
    171         mRemoved.add(sbn.getTag());
    172         mNotifications.remove(sbn.getKey());
    173         mNotificationKeys.remove(sbn.getTag());
    174         onNotificationRankingUpdate(rankingMap);
    175     }
    176 
    177     @Override
    178     public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,
    179             NotificationStats stats, int reason) {
    180         Log.d(TAG, "removed: " + sbn.getTag() + " for reason " + reason);
    181         mRemoved.add(sbn.getTag());
    182         JSONObject removed = new JSONObject();
    183         try {
    184             removed.put(JSON_TAG, sbn.getTag());
    185             removed.put(JSON_REASON, reason);
    186             removed.put(JSON_STATS, stats != null);
    187         } catch (JSONException e) {
    188             Log.e(TAG, "failed to pack up notification payload", e);
    189         }
    190         mNotifications.remove(sbn.getKey());
    191         mNotificationKeys.remove(sbn.getTag());
    192         mRemovedReason.put(sbn.getTag(), removed);
    193         onNotificationRankingUpdate(rankingMap);
    194     }
    195 }
    196