Home | History | Annotate | Download | only in stubs
      1 /*
      2  * Copyright (C) 2018 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 android.app.stubs;
     17 
     18 import android.content.ComponentName;
     19 import android.service.notification.NotificationListenerService;
     20 import android.service.notification.StatusBarNotification;
     21 import android.util.Log;
     22 
     23 import java.util.ArrayList;
     24 
     25 public class MockNotificationListener extends NotificationListenerService {
     26     public static final String TAG = "TestNotificationListener";
     27     public static final String PKG = "android.app.stubs";
     28 
     29     private ArrayList<String> mTestPackages = new ArrayList<>();
     30 
     31     public ArrayList<StatusBarNotification> mPosted = new ArrayList<>();
     32     public ArrayList<StatusBarNotification> mRemoved = new ArrayList<>();
     33     public RankingMap mRankingMap;
     34 
     35     private static MockNotificationListener sNotificationListenerInstance = null;
     36     boolean isConnected;
     37 
     38     public static String getId() {
     39         return String.format("%s/%s", MockNotificationListener.class.getPackage().getName(),
     40                 MockNotificationListener.class.getName());
     41     }
     42 
     43     public static ComponentName getComponentName() {
     44         return new ComponentName(MockNotificationListener.class.getPackage().getName(),
     45                 MockNotificationListener.class.getName());
     46     }
     47 
     48     @Override
     49     public void onCreate() {
     50         super.onCreate();
     51         mTestPackages.add(PKG);
     52     }
     53 
     54     @Override
     55     public void onListenerConnected() {
     56         super.onListenerConnected();
     57         sNotificationListenerInstance = this;
     58         isConnected = true;
     59     }
     60 
     61     @Override
     62     public void onListenerDisconnected() {
     63         isConnected = false;
     64     }
     65 
     66     public static MockNotificationListener getInstance() {
     67         return sNotificationListenerInstance;
     68     }
     69 
     70     public void resetData() {
     71         mPosted.clear();
     72         mRemoved.clear();
     73     }
     74 
     75     @Override
     76     public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
     77         if (!mTestPackages.contains(sbn.getPackageName())) { return; }
     78         mPosted.add(sbn);
     79     }
     80 
     81     @Override
     82     public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
     83         if (!mTestPackages.contains(sbn.getPackageName())) { return; }
     84         mRemoved.add(sbn);
     85     }
     86 
     87     @Override
     88     public void onNotificationRankingUpdate(RankingMap rankingMap) {
     89         mRankingMap = rankingMap;
     90     }
     91 }
     92