diff options
Diffstat (limited to 'java/com/android/dialer/app/calllog/MissedCallNotifier.java')
-rw-r--r-- | java/com/android/dialer/app/calllog/MissedCallNotifier.java | 116 |
1 files changed, 86 insertions, 30 deletions
diff --git a/java/com/android/dialer/app/calllog/MissedCallNotifier.java b/java/com/android/dialer/app/calllog/MissedCallNotifier.java index dd13298bc..e0e3fdf3f 100644 --- a/java/com/android/dialer/app/calllog/MissedCallNotifier.java +++ b/java/com/android/dialer/app/calllog/MissedCallNotifier.java @@ -30,11 +30,13 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.support.annotation.WorkerThread; +import android.support.v4.os.BuildCompat; import android.support.v4.os.UserManagerCompat; import android.support.v4.util.Pair; import android.text.BidiFormatter; import android.text.TextDirectionHeuristics; import android.text.TextUtils; +import android.util.ArraySet; import com.android.contacts.common.ContactsUtils; import com.android.contacts.common.compat.PhoneNumberUtilsCompat; import com.android.dialer.app.DialtactsActivity; @@ -46,23 +48,20 @@ import com.android.dialer.callintent.CallInitiationType; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.common.LogUtil; import com.android.dialer.common.concurrent.DialerExecutor.Worker; -import com.android.dialer.notification.NotificationChannelManager; -import com.android.dialer.notification.NotificationChannelManager.Channel; +import com.android.dialer.notification.NotificationChannelId; import com.android.dialer.phonenumbercache.ContactInfo; import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.util.DialerUtils; import com.android.dialer.util.IntentUtil; -import java.util.HashSet; import java.util.List; import java.util.Set; /** Creates a notification for calls that the user missed (neither answered nor rejected). */ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { - /** The tag used to identify notifications from this class. */ - static final String NOTIFICATION_TAG = "MissedCallNotifier"; - /** The identifier of the notification of new missed calls. */ - private static final int NOTIFICATION_ID = R.id.notification_missed_call; + static final String NOTIFICATION_TAG_PREFIX = "MissedCall_"; + static final String NOTIFICATION_GROUP = "MissedCall"; + private static final int NOTIFICATION_ID = 1; private final Context context; private final CallLogNotificationsQueryHelper callLogNotificationsQueryHelper; @@ -104,7 +103,8 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { if ((newCalls != null && newCalls.isEmpty()) || count == 0) { // No calls to notify about: clear the notification. - CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, null); + CallLogNotificationsQueryHelper.markAllMissedCallsInCallLogAsRead(context); + cancelAllMissedCallNotifications(context); return; } @@ -146,7 +146,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { null, System.currentTimeMillis()); - //TODO: look up caller ID that is not in contacts. + // TODO: look up caller ID that is not in contacts. ContactInfo contactInfo = callLogNotificationsQueryHelper.getContactInfo( call.number, call.numberPresentation, call.countryIso); @@ -181,36 +181,39 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { publicSummaryBuilder .setContentTitle(context.getText(titleResId)) .setContentIntent(createCallLogPendingIntent()) - .setDeleteIntent(createClearMissedCallsPendingIntent(null)); + .setDeleteIntent( + CallLogNotificationsService.createCancelAllMissedCallsPendingIntent(context)); // Create the notification summary suitable for display when sensitive information is showing. groupSummary .setContentTitle(context.getText(titleResId)) .setContentText(expandedText) .setContentIntent(createCallLogPendingIntent()) - .setDeleteIntent(createClearMissedCallsPendingIntent(null)) + .setDeleteIntent( + CallLogNotificationsService.createCancelAllMissedCallsPendingIntent(context)) .setGroupSummary(useCallList) .setOnlyAlertOnce(useCallList) .setPublicVersion(publicSummaryBuilder.build()); - - NotificationChannelManager.applyChannel(groupSummary, context, Channel.MISSED_CALL, null); + if (BuildCompat.isAtLeastO()) { + groupSummary.setChannelId(NotificationChannelId.MISSED_CALL); + } Notification notification = groupSummary.build(); configureLedOnNotification(notification); LogUtil.i("MissedCallNotifier.updateMissedCallNotification", "adding missed call notification"); - getNotificationMgr().notify(NOTIFICATION_TAG, NOTIFICATION_ID, notification); + getNotificationMgr().notify(getNotificationTagForGroupSummary(), NOTIFICATION_ID, notification); if (useCallList) { // Do not repost active notifications to prevent erasing post call notes. NotificationManager manager = getNotificationMgr(); - Set<String> activeTags = new HashSet<>(); + Set<String> activeTags = new ArraySet<>(); for (StatusBarNotification activeNotification : manager.getActiveNotifications()) { activeTags.add(activeNotification.getTag()); } for (NewCall call : newCalls) { - String callTag = call.callsUri.toString(); + String callTag = getNotificationTagForCall(call); if (!activeTags.contains(callTag)) { manager.notify(callTag, NOTIFICATION_ID, getNotificationForCall(call, null)); } @@ -218,6 +221,59 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { } } + public static void cancelAllMissedCallNotifications(@NonNull Context context) { + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + for (StatusBarNotification notification : notificationManager.getActiveNotifications()) { + String tag = notification.getTag(); + if (tag != null && tag.startsWith(NOTIFICATION_TAG_PREFIX)) { + notificationManager.cancel(tag, notification.getId()); + } + } + } + + public static void cancelSingleMissedCallNotification( + @NonNull Context context, @Nullable Uri callUri) { + if (callUri == null) { + LogUtil.e( + "MissedCallNotifier.cancelSingleMissedCallNotification", + "unable to cancel notification, uri is null"); + return; + } + NotificationManager notificationManager = context.getSystemService(NotificationManager.class); + String callTag = getNotificationTagForCallUri(callUri); + String summaryTag = getNotificationTagForGroupSummary(); + int notificationCount = 0; + + for (StatusBarNotification notification : notificationManager.getActiveNotifications()) { + String currentTag = notification.getTag(); + if (currentTag == null) { + continue; + } + if (currentTag.equals(callTag)) { + notificationManager.cancel(notification.getTag(), notification.getId()); + } else if (currentTag.startsWith(NOTIFICATION_TAG_PREFIX) && !currentTag.equals(summaryTag)) { + notificationCount++; + } + } + + if (notificationCount == 0) { + // There are no more missed call notifications. Remove the summary notification too. + notificationManager.cancel(summaryTag, NOTIFICATION_ID); + } + } + + private static String getNotificationTagForGroupSummary() { + return NOTIFICATION_TAG_PREFIX + "GroupSummary"; + } + + private static String getNotificationTagForCall(@NonNull NewCall call) { + return getNotificationTagForCallUri(call.callsUri); + } + + private static String getNotificationTagForCallUri(@NonNull Uri callUri) { + return NOTIFICATION_TAG_PREFIX + callUri; + } + public void insertPostCallNotification(@NonNull String number, @NonNull String note) { List<NewCall> newCalls = callLogNotificationsQueryHelper.getNewMissedCalls(); if (newCalls != null && !newCalls.isEmpty()) { @@ -226,7 +282,9 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { // Update the first notification that matches our post call note sender. getNotificationMgr() .notify( - call.callsUri.toString(), NOTIFICATION_ID, getNotificationForCall(call, note)); + getNotificationTagForCall(call), + NOTIFICATION_ID, + getNotificationForCall(call, note)); break; } } @@ -308,7 +366,7 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { private Notification.Builder createNotificationBuilder() { return new Notification.Builder(context) - .setGroup(NOTIFICATION_TAG) + .setGroup(NOTIFICATION_GROUP) .setSmallIcon(android.R.drawable.stat_notify_missed_call) .setColor(context.getResources().getColor(R.color.dialer_theme_color, null)) .setAutoCancel(true) @@ -321,10 +379,14 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { Builder builder = createNotificationBuilder() .setWhen(call.dateMs) - .setDeleteIntent(createClearMissedCallsPendingIntent(call.callsUri)) + .setDeleteIntent( + CallLogNotificationsService.createCancelSingleMissedCallPendingIntent( + context, call.callsUri)) .setContentIntent(createCallLogPendingIntent(call.callsUri)); + if (BuildCompat.isAtLeastO()) { + builder.setChannelId(NotificationChannelId.MISSED_CALL); + } - NotificationChannelManager.applyChannel(builder, context, Channel.MISSED_CALL, null); return builder; } @@ -332,7 +394,8 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { @WorkerThread public void callBackFromMissedCall(String number, Uri callUri) { closeSystemDialogs(context); - CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, callUri); + CallLogNotificationsQueryHelper.markSingleMissedCallInCallLogAsRead(context, callUri); + cancelSingleMissedCallNotification(context, callUri); DialerUtils.startActivityWithErrorToast( context, new CallIntentBuilder(number, CallInitiationType.Type.MISSED_CALL_NOTIFICATION) @@ -343,7 +406,8 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { /** Trigger an intent to send an sms from a missed call number. */ public void sendSmsFromMissedCall(String number, Uri callUri) { closeSystemDialogs(context); - CallLogNotificationsQueryHelper.removeMissedCallNotifications(context, callUri); + CallLogNotificationsQueryHelper.markSingleMissedCallInCallLogAsRead(context, callUri); + cancelSingleMissedCallNotification(context, callUri); DialerUtils.startActivityWithErrorToast( context, IntentUtil.getSendSmsIntent(number).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } @@ -371,14 +435,6 @@ public class MissedCallNotifier implements Worker<Pair<Integer, String>, Void> { return PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); } - /** Creates a pending intent that marks all new missed calls as old. */ - private PendingIntent createClearMissedCallsPendingIntent(@Nullable Uri callUri) { - Intent intent = new Intent(context, CallLogNotificationsService.class); - intent.setAction(CallLogNotificationsService.ACTION_MARK_NEW_MISSED_CALLS_AS_OLD); - intent.setData(callUri); - return PendingIntent.getService(context, 0, intent, 0); - } - private PendingIntent createCallBackPendingIntent(String number, @NonNull Uri callUri) { Intent intent = new Intent(context, CallLogNotificationsService.class); intent.setAction(CallLogNotificationsService.ACTION_CALL_BACK_FROM_MISSED_CALL_NOTIFICATION); |