1 /* 2 * Copyright (C) 2022 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 com.android.server.wm.flicker.testapp; 18 19 import android.app.Activity; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.app.TaskStackBuilder; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.view.WindowManager; 28 import android.widget.Button; 29 30 public class NotificationActivity extends Activity { 31 private static final String CHANNEL_ID = "notification_channel"; 32 private static final int NOTIFICATION_ID = 1; 33 34 @Override onCreate(Bundle savedInstanceState)35 public void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 WindowManager.LayoutParams p = getWindow().getAttributes(); 38 p.layoutInDisplayCutoutMode = WindowManager.LayoutParams 39 .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; 40 getWindow().setAttributes(p); 41 setContentView(R.layout.notification_button); 42 43 Button button = findViewById(R.id.post_notification); 44 button.setOnClickListener(v -> postNotification()); 45 46 createNotificationChannel(); 47 } 48 postNotification()49 private void postNotification() { 50 Intent resultIntent = new Intent(this, NotificationActivity.class); 51 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 52 stackBuilder.addNextIntentWithParentStack(resultIntent); 53 PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, 54 new Intent(this, NotificationActivity.class), 55 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE); 56 Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID) 57 .setSmallIcon(R.drawable.ic_notification) 58 .setWhen(System.currentTimeMillis()) 59 .setContentTitle("Flicker Test Notification") 60 .setContentText("Flicker Test Notification") 61 // Set the intent that will fire when the user taps the notification 62 .setContentIntent(resultPendingIntent) 63 .setAutoCancel(true); 64 65 NotificationManager notificationManager = getSystemService(NotificationManager.class); 66 notificationManager.notify(NOTIFICATION_ID, builder.build()); 67 } 68 createNotificationChannel()69 private void createNotificationChannel() { 70 CharSequence name = "channel_name"; 71 String description = "channel_description"; 72 int importance = NotificationManager.IMPORTANCE_HIGH; 73 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); 74 channel.setDescription(description); 75 // Register the channel with the system; you can't change the importance 76 // or other notification behaviors after this 77 NotificationManager notificationManager = getSystemService(NotificationManager.class); 78 notificationManager.createNotificationChannel(channel); 79 } 80 } 81