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 android.app.Notification;
20 import android.app.PendingIntent;
21 import android.app.Person;
22 import android.app.Service;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.graphics.drawable.Icon;
26 import android.os.IBinder;
27 import android.os.SystemClock;
28 
29 /**
30  * Used by NotificationManagerTest for testing policy around bubbles.
31  */
32 public class BubblesTestService extends Service {
33 
34     // Should be same as wht NotificationManagerTest is using
35     private static final String NOTIFICATION_CHANNEL_ID = "NotificationManagerTest";
36     private static final String BUBBLE_SHORTCUT_ID_DYNAMIC = "bubbleShortcutIdDynamic";
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_CALL = 0;
42     public static final int TEST_MESSAGING = 1;
43 
44     public static final int BUBBLE_NOTIF_ID = 1;
45 
46     @Override
onStartCommand(Intent intent, int flags, int startId)47     public int onStartCommand(Intent intent, int flags, int startId) {
48         final int testCase = intent.getIntExtra(EXTRA_TEST_CASE, TEST_CALL);
49         Notification n = getNotificationForTest(testCase, getApplicationContext());
50         startForeground(BUBBLE_NOTIF_ID, n);
51         return START_STICKY;
52     }
53 
54     @Override
onBind(Intent intent)55     public IBinder onBind(Intent intent) {
56         return null;
57     }
58 
getNotificationForTest(int testCase, Context context)59     private Notification getNotificationForTest(int testCase, Context context) {
60         final Intent intent = new Intent(context, SendBubbleActivity.class);
61         final PendingIntent pendingIntent =
62                 PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_MUTABLE_UNAUDITED);
63         Person person = new Person.Builder()
64                 .setName("bubblebot")
65                 .build();
66         Notification.Builder nb = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID)
67                 .setContentTitle("foofoo")
68                 .setContentIntent(pendingIntent)
69                 .setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
70                 .setSmallIcon(android.R.drawable.sym_def_app_icon)
71                 .setStyle(new Notification.MessagingStyle(person)
72                         .setConversationTitle("Bubble Chat")
73                         .addMessage("Hello?",
74                                 SystemClock.currentThreadTimeMillis() - 300000, person)
75                         .addMessage("Is it me you're looking for?",
76                                 SystemClock.currentThreadTimeMillis(), person));
77         Notification.BubbleMetadata data = new Notification.BubbleMetadata.Builder(pendingIntent,
78                 Icon.createWithResource(context, R.drawable.black)).build();
79         if (testCase != TEST_MESSAGING) {
80             nb.setCategory(Notification.CATEGORY_CALL);
81         }
82         nb.setShortcutId(BUBBLE_SHORTCUT_ID_DYNAMIC);
83         nb.setBubbleMetadata(data);
84         return nb.build();
85     }
86 }
87