1 /* 2 * Copyright (C) 2016 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.car.cluster.sample; 18 19 import android.app.Notification; 20 import android.graphics.Bitmap; 21 import android.service.notification.StatusBarNotification; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.core.app.NotificationCompat.CarExtender; 26 import androidx.core.app.NotificationCompat.CarExtender.UnreadConversation; 27 28 /** 29 * Convert a {@link CarExtender} notification into a {@link MessageContactDetails} 30 */ 31 public class MessagingConverter { 32 private static final String TAG = DebugUtil.getTag(MessagingConverter.class); 33 34 public static boolean canConvert(StatusBarNotification sbn) { 35 Notification notification = sbn.getNotification(); 36 if (notification == null) { 37 if (DebugUtil.DEBUG) { 38 Log.d(TAG, "Notification is empty."); 39 } 40 return false; 41 } 42 CarExtender ce = new CarExtender(sbn.getNotification()); 43 if (ce.getUnreadConversation() == null) { 44 if (DebugUtil.DEBUG) { 45 Log.d(TAG, "Notification with no messaging component."); 46 } 47 return false; 48 } 49 50 CarExtender.UnreadConversation uc = ce.getUnreadConversation(); 51 String[] messages = uc.getMessages(); 52 if (messages == null || messages.length == 0) { 53 Log.w(TAG, "Car message notification with no messages."); 54 return false; 55 } 56 57 if (TextUtils.isEmpty(uc.getParticipant())) { 58 Log.w(TAG, "Car message notification with no participant."); 59 return false; 60 } 61 62 if (uc.getReplyPendingIntent() == null) { 63 Log.w(TAG, "Car message notification with no reply intent."); 64 return false; 65 } 66 67 for (String m : messages) { 68 if (m == null) { 69 Log.w(TAG, "Car message with null text."); 70 return false; 71 } 72 } 73 return true; 74 } 75 76 public static MessageContactDetails convert(StatusBarNotification sbn) { 77 CarExtender ce = new CarExtender(sbn.getNotification()); 78 UnreadConversation uc = ce.getUnreadConversation(); 79 80 Bitmap largeIcon = ce.getLargeIcon(); 81 if (largeIcon == null) { 82 largeIcon = sbn.getNotification().largeIcon; 83 } 84 String name = uc.getParticipant(); 85 86 return new MessageContactDetails(largeIcon, name); 87 } 88 89 public static class MessageContactDetails { 90 private final Bitmap mContactImage; 91 private final String mContactName; 92 93 private MessageContactDetails(Bitmap contactImage, String contactName) { 94 mContactImage = contactImage; 95 mContactName = contactName; 96 } 97 98 public Bitmap getContactImage() { 99 return mContactImage; 100 } 101 102 public String getContactName() { 103 return mContactName; 104 } 105 } 106 } 107