Home | History | Annotate | Download | only in stubs
      1 /*
      2  * Copyright (C) 2019 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 
     17 package android.app.stubs;
     18 
     19 import static android.app.Notification.CATEGORY_CALL;
     20 
     21 import android.app.Notification;
     22 import android.app.PendingIntent;
     23 import android.app.Person;
     24 import android.app.Service;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.graphics.drawable.Icon;
     28 import android.os.IBinder;
     29 
     30 /**
     31  * Used by NotificationManagerTest for testing policy around bubbles.
     32  */
     33 public class BubblesTestService extends Service {
     34 
     35     // Should be same as wht NotificationManagerTest is using
     36     private static final String NOTIFICATION_CHANNEL_ID = "NotificationManagerTest";
     37 
     38     // Must configure foreground service notification in different ways for different tests
     39     public static final String EXTRA_TEST_CASE =
     40             "android.app.stubs.BubbleTestService.EXTRA_TEST_CASE";
     41     public static final int TEST_SUCCESS = 0;
     42     public static final int TEST_NO_PERSON = 1;
     43     public static final int TEST_NO_CATEGORY = 2;
     44     public static final int TEST_NO_BUBBLE_METADATA = 3;
     45 
     46     public static final int BUBBLE_NOTIF_ID = 1;
     47 
     48     @Override
     49     public int onStartCommand(Intent intent, int flags, int startId) {
     50         final int testCase = intent.getIntExtra(EXTRA_TEST_CASE, TEST_SUCCESS);
     51         Notification n = getNotificationForTest(testCase, getApplicationContext());
     52         startForeground(BUBBLE_NOTIF_ID, n);
     53         return START_STICKY;
     54     }
     55 
     56     @Override
     57     public IBinder onBind(Intent intent) {
     58         return null;
     59     }
     60 
     61     private Notification getNotificationForTest(final int testCase, final Context context) {
     62         final Intent intent = new Intent(context, BubblesTestActivity.class);
     63         final PendingIntent pendingIntent =
     64                 PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
     65         Notification.Builder nb = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
     66                 .setContentTitle("foofoo")
     67                 .setContentIntent(pendingIntent)
     68                 .setSmallIcon(android.R.drawable.sym_def_app_icon);
     69         Person person = new Person.Builder()
     70                 .setName("bubblebot")
     71                 .build();
     72         Notification.BubbleMetadata data = new Notification.BubbleMetadata.Builder()
     73                 .setIcon(Icon.createWithResource(context, R.drawable.black))
     74                 .setIntent(pendingIntent)
     75                 .build();
     76         if (testCase != TEST_NO_PERSON) {
     77             nb.addPerson(person);
     78         }
     79         if (testCase != TEST_NO_CATEGORY) {
     80             nb.setCategory(CATEGORY_CALL);
     81         }
     82         if (testCase != TEST_NO_BUBBLE_METADATA) {
     83             nb.setBubbleMetadata(data);
     84         }
     85         return nb.build();
     86     }
     87 }
     88