1 /* 2 * Copyright (C) 2024 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.platform.systemui_tapl.controller 18 19 import android.R 20 import android.app.Notification 21 import android.app.PendingIntent 22 import android.content.Intent 23 import android.platform.systemui_tapl.ui.Notification.Companion.NOTIFICATION_BIG_TEXT 24 import android.platform.uiautomatorhelpers.DeviceHelpers 25 26 object BigTextNotificationController { 27 /** Id of the high importance channel created by the controller. */ 28 private const val NOTIFICATION_CHANNEL_HIGH_IMPORTANCE_ID = "test_channel_id_high_importance" 29 private const val NOTIFICATION_CHANNEL_DEFAULT_IMPORTANCE_ID = 30 "test_channel_id_default_importance" 31 private const val NOTIFICATION_CONTENT_TEXT = "Test notification content" 32 private const val NOTIFICATION_TITLE_TEXT = "TEST NOTIFICATION" 33 34 /** 35 * Posts a notification using [Notification.BigTextStyle]. 36 * 37 * @param pkg Default contentIntent launches this App to when clicking on notification. 38 * @param highImportance Whether to post the notification with high importance. 39 * @param collapsedText Text content when the notification is collapsed. 40 * @param expandedText Text content when the notification is expanded. 41 * @param title Title of the notification. 42 * @param contentIntent The contentIntent when the notification is clicked. 43 * @param addActionButton When true, add a default action button to the notification. 44 * @param actions The list of actions attached to the notification. 45 * @return The [NotificationIdentity] that represents the posted notification. 46 */ 47 @JvmOverloads 48 @JvmStatic postBigTextNotificationnull49 fun postBigTextNotification( 50 pkg: String?, 51 highImportance: Boolean = false, 52 collapsedText: String = NOTIFICATION_CONTENT_TEXT, 53 expandedText: String = NOTIFICATION_BIG_TEXT, 54 title: String = NOTIFICATION_TITLE_TEXT, 55 contentIntent: PendingIntent? = null, 56 publicVersion: Notification? = null, 57 addActionButton: Boolean = false, 58 allowAutoGroup: Boolean = false, 59 vararg actions: Notification.Action, 60 ): NotificationIdentity { 61 val context = DeviceHelpers.context 62 val channelId = 63 if (highImportance) NOTIFICATION_CHANNEL_HIGH_IMPORTANCE_ID 64 else NOTIFICATION_CHANNEL_DEFAULT_IMPORTANCE_ID 65 val notificationController = NotificationController.get() 66 67 val builder = 68 Notification.Builder(context, channelId) 69 .setStyle(Notification.BigTextStyle().bigText(expandedText)) 70 .setSmallIcon(R.drawable.stat_notify_chat) 71 .setContentText(collapsedText) 72 .setContentTitle(title) 73 .setCategory(Notification.CATEGORY_SYSTEM) 74 .setGroupSummary(false) 75 .setActions(*actions) 76 77 if (addActionButton) { 78 builder.addAction(notificationController.defaultActionBuilder.build()) 79 } 80 81 publicVersion?.let(builder::setPublicVersion) 82 val pendingIntent = 83 if (pkg != null && contentIntent == null) 84 PendingIntent.getActivity( 85 /* context= */ context, 86 /* requestCode= */ 0, 87 /* intent= */ context.packageManager.getLaunchIntentForPackage(pkg), 88 /* flags= */ Intent.FLAG_ACTIVITY_NEW_TASK or PendingIntent.FLAG_IMMUTABLE, 89 ) 90 else contentIntent 91 pendingIntent?.let { builder.setContentIntent(it) } 92 93 if (allowAutoGroup) { 94 notificationController.postNotificationNoGroup(builder) 95 } else { 96 notificationController.postNotification(builder) 97 } 98 99 return NotificationIdentity( 100 type = NotificationIdentity.Type.BIG_TEXT, 101 title = title, 102 text = collapsedText, 103 summary = null, 104 textWhenExpanded = expandedText, 105 contentIsVisibleInCollapsedState = true, 106 pkg = pkg, 107 hasAction = actions.isNotEmpty() or addActionButton, 108 ) 109 } 110 } 111