1 /* 2 * Copyright (C) 2020 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.systemui.statusbar.notification; 18 19 import android.app.INotificationManager; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.os.RemoteException; 25 import android.os.UserHandle; 26 import android.text.TextUtils; 27 import android.util.Slog; 28 29 import com.android.systemui.statusbar.notification.collection.NotificationEntry; 30 31 /** 32 * Helps SystemUI create notification channels. 33 */ 34 public class NotificationChannelHelper { 35 private static final String TAG = "NotificationChannelHelper"; 36 37 /** Creates a conversation channel based on the shortcut info or notification title. */ createConversationChannelIfNeeded( Context context, INotificationManager notificationManager, NotificationEntry entry, NotificationChannel channel)38 public static NotificationChannel createConversationChannelIfNeeded( 39 Context context, 40 INotificationManager notificationManager, 41 NotificationEntry entry, 42 NotificationChannel channel) { 43 if (!TextUtils.isEmpty(channel.getConversationId())) { 44 return channel; 45 } 46 final String conversationId = entry.getSbn().getShortcutId(); 47 final String pkg = entry.getSbn().getPackageName(); 48 final int appUid = entry.getSbn().getUid(); 49 if (TextUtils.isEmpty(conversationId) || TextUtils.isEmpty(pkg) 50 || entry.getRanking().getShortcutInfo() == null) { 51 return channel; 52 } 53 54 // If this channel is not already a customized conversation channel, create 55 // a custom channel 56 try { 57 channel.setName(getName(entry)); 58 notificationManager.createConversationNotificationChannelForPackage( 59 pkg, appUid, entry.getSbn().getKey(), channel, 60 conversationId); 61 channel = notificationManager.getConversationNotificationChannel( 62 context.getOpPackageName(), UserHandle.getUserId(appUid), pkg, 63 channel.getId(), false, conversationId); 64 } catch (RemoteException e) { 65 Slog.e(TAG, "Could not create conversation channel", e); 66 } 67 return channel; 68 } 69 getName(NotificationEntry entry)70 private static CharSequence getName(NotificationEntry entry) { 71 if (entry.getRanking().getShortcutInfo().getLabel() != null) { 72 return entry.getRanking().getShortcutInfo().getLabel().toString(); 73 } 74 Bundle extras = entry.getSbn().getNotification().extras; 75 CharSequence nameString = extras.getCharSequence(Notification.EXTRA_CONVERSATION_TITLE); 76 if (TextUtils.isEmpty(nameString)) { 77 nameString = extras.getCharSequence(Notification.EXTRA_TITLE); 78 } 79 if (TextUtils.isEmpty(nameString)) { 80 nameString = "fallback"; 81 } 82 return nameString; 83 } 84 } 85