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.Activity; 20 import android.app.Notification; 21 import android.app.NotificationManager; 22 import android.app.PendingIntent; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.graphics.drawable.Icon; 26 import android.os.Bundle; 27 import android.provider.Telephony; 28 import android.util.Log; 29 30 /** 31 * Used by NotificationManagerTest for testing policy around bubbles. 32 */ 33 public class BubblesTestActivity extends Activity { 34 final String TAG = BubblesTestActivity.class.getSimpleName(); 35 36 // Should be same as wht NotificationManagerTest is using 37 private static final String NOTIFICATION_CHANNEL_ID = "NotificationManagerTest"; 38 39 public static final String BUBBLE_ACTIVITY_OPENED = 40 "android.app.stubs.BUBBLE_ACTIVITY_OPENED"; 41 public static final int BUBBLE_NOTIF_ID = 1; 42 43 @Override 44 public void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.main); 47 48 Intent i = new Intent(BUBBLE_ACTIVITY_OPENED); 49 sendBroadcast(i); 50 } 51 52 /** 53 * Sends a bubble notification that would only be allowed to bubble when the app is 54 * foreground. 55 */ 56 public void sendBubble(int i) { 57 Context context = getApplicationContext(); 58 59 final Intent intent = new Intent(context, BubblesTestActivity.class); 60 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP 61 | Intent.FLAG_ACTIVITY_CLEAR_TOP); 62 intent.setAction(Intent.ACTION_MAIN); 63 final PendingIntent pendingIntent = 64 PendingIntent.getActivity(context, 0, intent, 0); 65 66 Notification.BubbleMetadata data = new Notification.BubbleMetadata.Builder() 67 .setIcon(Icon.createWithResource(context, R.drawable.black)) 68 .setIntent(pendingIntent) 69 .build(); 70 Notification n = new Notification.Builder(context, NOTIFICATION_CHANNEL_ID) 71 .setSmallIcon(R.drawable.black) 72 .setWhen(System.currentTimeMillis()) 73 .setContentTitle("notify#" + BUBBLE_NOTIF_ID) 74 .setContentText("This is #" + BUBBLE_NOTIF_ID + "notification ") 75 .setContentIntent(pendingIntent) 76 .setBubbleMetadata(data) 77 .build(); 78 79 NotificationManager noMan = (NotificationManager) context.getSystemService( 80 Context.NOTIFICATION_SERVICE); 81 noMan.notify(BUBBLE_NOTIF_ID, n); 82 Log.d(TAG, "posting bubble: " + n + ", " + i); 83 } 84 } 85