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