1 /* 2 * Copyright (C) 2018 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.textclassifier.notification; 18 19 import android.app.Notification; 20 import android.app.Notification.Style; 21 import android.app.RemoteInput; 22 import android.service.notification.StatusBarNotification; 23 import java.util.Objects; 24 25 final class NotificationUtils { 26 27 /** 28 * Returns whether the given status bar notification is showing some incoming messages. 29 * 30 * @see Notification#CATEGORY_MESSAGE 31 * @see Notification.MessagingStyle 32 */ isMessaging(StatusBarNotification statusBarNotification)33 static boolean isMessaging(StatusBarNotification statusBarNotification) { 34 return isCategory(statusBarNotification, Notification.CATEGORY_MESSAGE) 35 || isPublicVersionCategory(statusBarNotification, Notification.CATEGORY_MESSAGE) 36 || hasStyle(statusBarNotification, Notification.MessagingStyle.class); 37 } 38 39 /** 40 * Returns whether the given status bar notification has an reply button that allows user to do 41 * inline reply. 42 */ hasInlineReply(StatusBarNotification statusBarNotification)43 static boolean hasInlineReply(StatusBarNotification statusBarNotification) { 44 Notification.Action[] actions = statusBarNotification.getNotification().actions; 45 if (actions == null) { 46 return false; 47 } 48 for (Notification.Action action : actions) { 49 RemoteInput[] remoteInputs = action.getRemoteInputs(); 50 if (remoteInputs == null) { 51 continue; 52 } 53 for (RemoteInput remoteInput : remoteInputs) { 54 if (remoteInput.getAllowFreeFormInput()) { 55 return true; 56 } 57 } 58 } 59 return false; 60 } 61 hasStyle( StatusBarNotification statusBarNotification, Class<? extends Style> targetStyle)62 private static boolean hasStyle( 63 StatusBarNotification statusBarNotification, Class<? extends Style> targetStyle) { 64 String templateClass = 65 statusBarNotification.getNotification().extras.getString(Notification.EXTRA_TEMPLATE); 66 return targetStyle.getName().equals(templateClass); 67 } 68 isCategory(StatusBarNotification statusBarNotification, String category)69 private static boolean isCategory(StatusBarNotification statusBarNotification, String category) { 70 return Objects.equals(statusBarNotification.getNotification().category, category); 71 } 72 isPublicVersionCategory( StatusBarNotification statusBarNotification, String category)73 private static boolean isPublicVersionCategory( 74 StatusBarNotification statusBarNotification, String category) { 75 Notification publicVersion = statusBarNotification.getNotification().publicVersion; 76 return publicVersion != null && isCategoryInternal(publicVersion, category); 77 } 78 isCategoryInternal(Notification notification, String category)79 private static boolean isCategoryInternal(Notification notification, String category) { 80 return Objects.equals(notification.category, category); 81 } 82 NotificationUtils()83 private NotificationUtils() {} 84 } 85