12f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian/*
22f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * Copyright (C) 2017 The Android Open Source Project
32f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian *
42f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * Licensed under the Apache License, Version 2.0 (the "License");
52f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * you may not use this file except in compliance with the License.
62f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * You may obtain a copy of the License at
72f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian *
82f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian *      http://www.apache.org/licenses/LICENSE-2.0
92f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian *
102f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * Unless required by applicable law or agreed to in writing, software
112f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * distributed under the License is distributed on an "AS IS" BASIS,
122f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * See the License for the specific language governing permissions and
142f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian * limitations under the License
152f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian */
162f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
172f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianpackage com.android.dialer.app.calllog;
182f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
192f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.app.Notification;
202f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.app.NotificationManager;
212f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.app.PendingIntent;
222f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.content.ComponentName;
232f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.content.Context;
242f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.content.Intent;
252f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.graphics.Bitmap;
262f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.net.Uri;
272f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.os.Build.VERSION;
282f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.os.Build.VERSION_CODES;
292f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.service.notification.StatusBarNotification;
302f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.support.annotation.NonNull;
312f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.support.annotation.Nullable;
322f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.support.v4.os.BuildCompat;
332f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.telecom.PhoneAccount;
342f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.telecom.PhoneAccountHandle;
352f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.telephony.TelephonyManager;
362f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport android.text.TextUtils;
372f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.contacts.common.util.ContactDisplayUtils;
382f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.app.DialtactsActivity;
392f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.app.R;
402f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall;
412f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.app.contactinfo.ContactPhotoLoader;
422f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.app.list.DialtactsPagerAdapter;
432f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.common.LogUtil;
442f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.logging.DialerImpression;
452f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.logging.Logger;
462f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.notification.NotificationChannelManager;
472f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.phonenumbercache.ContactInfo;
482f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport com.android.dialer.telecom.TelecomUtil;
492f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport java.util.List;
502f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianimport java.util.Map;
512f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
522f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian/** Shows a notification in the status bar for visual voicemail. */
532f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanianfinal class VisualVoicemailNotifier {
542f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static final String NOTIFICATION_TAG_PREFIX = "VisualVoicemail_";
552f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static final String NOTIFICATION_GROUP = "VisualVoicemail";
562f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static final int NOTIFICATION_ID = 1;
572f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
582f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  public static void showNotifications(
592f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context,
602f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull List<NewCall> newCalls,
612f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Map<String, ContactInfo> contactInfos,
622f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @Nullable String callers) {
632f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    LogUtil.enterBlock("VisualVoicemailNotifier.showNotifications");
642f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    PendingIntent deleteIntent =
652f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        CallLogNotificationsService.createMarkAllNewVoicemailsAsOldIntent(context);
662f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    String contentTitle =
672f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        context
682f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .getResources()
692f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .getQuantityString(
702f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                R.plurals.notification_voicemail_title, newCalls.size(), newCalls.size());
712f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    Notification.Builder groupSummary =
722f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        createNotificationBuilder(context)
732f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setContentTitle(contentTitle)
742f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setContentText(callers)
752f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setDeleteIntent(deleteIntent)
762f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setGroupSummary(true)
772f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setContentIntent(newVoicemailIntent(context, null));
782f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
792f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (BuildCompat.isAtLeastO()) {
802f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      groupSummary.setGroupAlertBehavior(Notification.GROUP_ALERT_CHILDREN);
812f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      PhoneAccountHandle handle = getAccountForCall(context, newCalls.get(0));
822f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      groupSummary.setChannelId(NotificationChannelManager.getVoicemailChannelId(context, handle));
832f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
842f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
852f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
862f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    notificationManager.notify(
872f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        getNotificationTagForGroupSummary(), NOTIFICATION_ID, groupSummary.build());
882f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
892f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    for (NewCall voicemail : newCalls) {
902f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      notificationManager.notify(
912f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          getNotificationTagForVoicemail(voicemail),
922f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          NOTIFICATION_ID,
932f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          createNotificationForVoicemail(context, voicemail, contactInfos));
942f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
952f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
962f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
972f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  public static void cancelAllVoicemailNotifications(@NonNull Context context) {
982f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    LogUtil.enterBlock("VisualVoicemailNotifier.cancelAllVoicemailNotifications");
992f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
1002f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    for (StatusBarNotification notification : notificationManager.getActiveNotifications()) {
1012f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      String tag = notification.getTag();
1022f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (tag != null && tag.startsWith(NOTIFICATION_TAG_PREFIX)) {
1032f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        notificationManager.cancel(tag, notification.getId());
1042f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
1052f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1062f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1072f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1082f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  public static void cancelSingleVoicemailNotification(
1092f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context, @Nullable Uri voicemailUri) {
1102f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    LogUtil.enterBlock("VisualVoicemailNotifier.cancelSingleVoicemailNotification");
1112f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (voicemailUri == null) {
1122f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      LogUtil.e("VisualVoicemailNotifier.cancelSingleVoicemailNotification", "uri is null");
1132f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      return;
1142f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1152f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
1162f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    String voicemailTag = getNotificationTagForUri(voicemailUri);
1172f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    String summaryTag = getNotificationTagForGroupSummary();
1182f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    int notificationCount = 0;
1192f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1202f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    for (StatusBarNotification notification : notificationManager.getActiveNotifications()) {
1212f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      String currentTag = notification.getTag();
1222f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (currentTag == null) {
1232f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        continue;
1242f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
1252f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (currentTag.equals(voicemailTag)) {
1262f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        notificationManager.cancel(notification.getTag(), notification.getId());
1272f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      } else if (currentTag.startsWith(NOTIFICATION_TAG_PREFIX) && !currentTag.equals(summaryTag)) {
1282f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        notificationCount++;
1292f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
1302f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1312f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1322f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (notificationCount == 0) {
1332f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      // There are no more visual voicemail notifications. Remove the summary notification too.
1342f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      notificationManager.cancel(summaryTag, NOTIFICATION_ID);
1352f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1362f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1372f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1382f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static String getNotificationTagForVoicemail(@NonNull NewCall voicemail) {
1392f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return getNotificationTagForUri(voicemail.voicemailUri);
1402f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1412f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1422f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static String getNotificationTagForUri(@NonNull Uri voicemailUri) {
1432f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return NOTIFICATION_TAG_PREFIX + voicemailUri;
1442f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1452f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1462f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static String getNotificationTagForGroupSummary() {
1472f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return NOTIFICATION_TAG_PREFIX + "GroupSummary";
1482f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1492f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1502f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static Notification.Builder createNotificationBuilder(@NonNull Context context) {
1512f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return new Notification.Builder(context)
1522f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        .setSmallIcon(android.R.drawable.stat_notify_voicemail)
1532f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        .setColor(context.getColor(R.color.dialer_theme_color))
1542f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        .setGroup(NOTIFICATION_GROUP)
1552f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        .setOnlyAlertOnce(true)
1562f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        .setAutoCancel(true);
1572f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
1582f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1592f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static Notification createNotificationForVoicemail(
1602f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context,
1612f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull NewCall voicemail,
1622f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Map<String, ContactInfo> contactInfos) {
1632f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    PhoneAccountHandle handle = getAccountForCall(context, voicemail);
1642f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    ContactInfo contactInfo = contactInfos.get(voicemail.number);
1652f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1662f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    Notification.Builder builder =
1672f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        createNotificationBuilder(context)
1682f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setContentTitle(
1692f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                context
1702f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                    .getResources()
1712f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                    .getQuantityString(R.plurals.notification_voicemail_title, 1, 1))
1722f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setContentText(
1732f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                ContactDisplayUtils.getTtsSpannedPhoneNumber(
1742f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                    context.getResources(),
1752f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                    R.string.notification_new_voicemail_ticker,
1762f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian                    contactInfo.name))
1772f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setWhen(voicemail.dateMs)
1782f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setSound(getVoicemailRingtoneUri(context, handle))
1792f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            .setDefaults(getNotificationDefaultFlags(context, handle));
1802f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1812f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (voicemail.voicemailUri != null) {
1822f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      builder.setDeleteIntent(
1832f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          CallLogNotificationsService.createMarkSingleNewVoicemailAsOldIntent(
1842f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian              context, voicemail.voicemailUri));
1852f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1862f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1872f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (BuildCompat.isAtLeastO()) {
1882f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      builder.setChannelId(NotificationChannelManager.getVoicemailChannelId(context, handle));
1892f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1902f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
1912f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    ContactPhotoLoader loader = new ContactPhotoLoader(context, contactInfo);
1922f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    Bitmap photoIcon = loader.loadPhotoIcon();
1932f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (photoIcon != null) {
1942f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      builder.setLargeIcon(photoIcon);
1952f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
1962f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (!TextUtils.isEmpty(voicemail.transcription)) {
1972f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      Logger.get(context)
1982f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          .logImpression(DialerImpression.Type.VVM_NOTIFICATION_CREATED_WITH_TRANSCRIPTION);
1992f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      builder.setStyle(new Notification.BigTextStyle().bigText(voicemail.transcription));
2002f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2012f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    builder.setContentIntent(newVoicemailIntent(context, voicemail));
2022f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    Logger.get(context).logImpression(DialerImpression.Type.VVM_NOTIFICATION_CREATED);
2032f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return builder.build();
2042f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2052f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2062f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  @Nullable
2072f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static Uri getVoicemailRingtoneUri(
2082f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context, @Nullable PhoneAccountHandle handle) {
2092f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (VERSION.SDK_INT < VERSION_CODES.N) {
2102f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      return null;
2112f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2122f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (handle == null) {
2132f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      LogUtil.i("VisualVoicemailNotifier.getVoicemailRingtoneUri", "null handle, getting fallback");
2142f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      handle = getFallbackAccount(context);
2152f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (handle == null) {
2162f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        LogUtil.i(
2172f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            "VisualVoicemailNotifier.getVoicemailRingtoneUri",
2182f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            "no fallback handle, using null (default) ringtone");
2192f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        return null;
2202f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
2212f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2222f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return context.getSystemService(TelephonyManager.class).getVoicemailRingtoneUri(handle);
2232f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2242f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2252f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static int getNotificationDefaultFlags(
2262f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context, @Nullable PhoneAccountHandle handle) {
2272f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (VERSION.SDK_INT < VERSION_CODES.N) {
2282f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      return Notification.DEFAULT_ALL;
2292f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2302f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (handle == null) {
2312f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      LogUtil.i(
2322f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian          "VisualVoicemailNotifier.getNotificationDefaultFlags", "null handle, getting fallback");
2332f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      handle = getFallbackAccount(context);
2342f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (handle == null) {
2352f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        LogUtil.i(
2362f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            "VisualVoicemailNotifier.getNotificationDefaultFlags",
2372f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian            "no fallback handle, using default vibration");
2382f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        return Notification.DEFAULT_ALL;
2392f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
2402f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2412f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (context.getSystemService(TelephonyManager.class).isVoicemailVibrationEnabled(handle)) {
2422f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      return Notification.DEFAULT_VIBRATE;
2432f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2442f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return 0;
2452f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2462f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2472f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private static PendingIntent newVoicemailIntent(
2482f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context, @Nullable NewCall voicemail) {
2492f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    Intent intent =
2502f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        DialtactsActivity.getShowTabIntent(context, DialtactsPagerAdapter.TAB_INDEX_VOICEMAIL);
2512f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    // TODO (b/35486204): scroll to this voicemail
2522f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (voicemail != null) {
2532f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      intent.setData(voicemail.voicemailUri);
2542f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2552f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    intent.putExtra(DialtactsActivity.EXTRA_CLEAR_NEW_VOICEMAILS, true);
2562f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
2572f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2582f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2592f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  /**
2602f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   * Gets a phone account for the given call entry. This could be null if SIM associated with the
2612f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   * entry is no longer in the device or for other reasons (for example, modem reboot).
2622f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   */
2632f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  @Nullable
2642f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  public static PhoneAccountHandle getAccountForCall(
2652f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      @NonNull Context context, @Nullable NewCall call) {
2662f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (call == null || call.accountComponentName == null || call.accountId == null) {
2672f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      return null;
2682f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2692f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return new PhoneAccountHandle(
2702f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        ComponentName.unflattenFromString(call.accountComponentName), call.accountId);
2712f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2722f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2732f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  /**
2742f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   * Gets any available phone account that can be used to get sound settings for voicemail. This is
2752f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   * only called if the phone account for the voicemail entry can't be found.
2762f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian   */
2772f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  @Nullable
2782f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  public static PhoneAccountHandle getFallbackAccount(@NonNull Context context) {
2792f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    PhoneAccountHandle handle =
2802f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL);
2812f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    if (handle == null) {
2822f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      List<PhoneAccountHandle> handles = TelecomUtil.getCallCapablePhoneAccounts(context);
2832f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      if (!handles.isEmpty()) {
2842f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian        handle = handles.get(0);
2852f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian      }
2862f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    }
2872f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian    return handle;
2882f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  }
2892f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian
2902f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian  private VisualVoicemailNotifier() {}
2912f1c7586bcce334ca69022eb8dc6d8965ceb6a05Eric Erfanian}
292