1 /* 2 * Copyright (C) 2017 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.dialer.app.calllog; 18 19 import android.annotation.TargetApi; 20 import android.app.Notification; 21 import android.app.PendingIntent; 22 import android.content.Context; 23 import android.os.Build.VERSION_CODES; 24 import android.support.annotation.NonNull; 25 import android.support.v4.os.BuildCompat; 26 import android.telecom.PhoneAccount; 27 import android.telecom.PhoneAccountHandle; 28 import android.telecom.TelecomManager; 29 import android.telephony.CarrierConfigManager; 30 import android.telephony.TelephonyManager; 31 import android.text.TextUtils; 32 import com.android.dialer.app.R; 33 import com.android.dialer.common.Assert; 34 import com.android.dialer.common.LogUtil; 35 import com.android.dialer.location.GeoUtil; 36 import com.android.dialer.notification.DialerNotificationManager; 37 import com.android.dialer.notification.NotificationChannelManager; 38 import com.android.dialer.phonenumberutil.PhoneNumberHelper; 39 import com.android.dialer.telecom.TelecomUtil; 40 41 /** Shows a notification in the status bar for legacy vociemail. */ 42 @TargetApi(VERSION_CODES.O) 43 public final class LegacyVoicemailNotifier { 44 private static final String NOTIFICATION_TAG_PREFIX = "LegacyVoicemail_"; 45 private static final String NOTIFICATION_TAG = "LegacyVoicemail"; 46 private static final int NOTIFICATION_ID = 1; 47 48 /** 49 * Replicates how packages/services/Telephony/NotificationMgr.java handles legacy voicemail 50 * notification. The notification will not be stackable because no information is available for 51 * individual voicemails. 52 */ showNotification( @onNull Context context, @NonNull PhoneAccountHandle handle, int count, String voicemailNumber, PendingIntent callVoicemailIntent, PendingIntent voicemailSettingsIntent, boolean isRefresh)53 public static void showNotification( 54 @NonNull Context context, 55 @NonNull PhoneAccountHandle handle, 56 int count, 57 String voicemailNumber, 58 PendingIntent callVoicemailIntent, 59 PendingIntent voicemailSettingsIntent, 60 boolean isRefresh) { 61 LogUtil.enterBlock("LegacyVoicemailNotifier.showNotification"); 62 Assert.isNotNull(handle); 63 Assert.checkArgument(BuildCompat.isAtLeastO()); 64 65 TelephonyManager pinnedTelephonyManager = 66 context.getSystemService(TelephonyManager.class).createForPhoneAccountHandle(handle); 67 if (pinnedTelephonyManager == null) { 68 LogUtil.e("LegacyVoicemailNotifier.showNotification", "invalid PhoneAccountHandle"); 69 return; 70 } 71 72 Notification notification = 73 createNotification( 74 context, 75 pinnedTelephonyManager, 76 handle, 77 count, 78 voicemailNumber, 79 callVoicemailIntent, 80 voicemailSettingsIntent, 81 isRefresh); 82 DialerNotificationManager.notify( 83 context, getNotificationTag(context, handle), NOTIFICATION_ID, notification); 84 } 85 86 @NonNull createNotification( @onNull Context context, @NonNull TelephonyManager pinnedTelephonyManager, @NonNull PhoneAccountHandle handle, int count, String voicemailNumber, PendingIntent callVoicemailIntent, PendingIntent voicemailSettingsIntent, boolean isRefresh)87 private static Notification createNotification( 88 @NonNull Context context, 89 @NonNull TelephonyManager pinnedTelephonyManager, 90 @NonNull PhoneAccountHandle handle, 91 int count, 92 String voicemailNumber, 93 PendingIntent callVoicemailIntent, 94 PendingIntent voicemailSettingsIntent, 95 boolean isRefresh) { 96 String notificationTitle = 97 context 98 .getResources() 99 .getQuantityString(R.plurals.notification_voicemail_title, count, count); 100 boolean isOngoing = 101 pinnedTelephonyManager 102 .getCarrierConfig() 103 .getBoolean(CarrierConfigManager.KEY_VOICEMAIL_NOTIFICATION_PERSISTENT_BOOL); 104 105 String contentText; 106 PendingIntent contentIntent; 107 if (!TextUtils.isEmpty(voicemailNumber) && callVoicemailIntent != null) { 108 contentText = getNotificationText(context, handle, voicemailNumber); 109 contentIntent = callVoicemailIntent; 110 } else { 111 contentText = context.getString(R.string.notification_voicemail_no_vm_number); 112 contentIntent = voicemailSettingsIntent; 113 } 114 115 Notification.Builder builder = 116 new Notification.Builder(context) 117 .setSmallIcon(android.R.drawable.stat_notify_voicemail) 118 .setColor(context.getColor(R.color.dialer_theme_color)) 119 .setWhen(System.currentTimeMillis()) 120 .setContentTitle(notificationTitle) 121 .setContentText(contentText) 122 .setContentIntent(contentIntent) 123 .setSound(pinnedTelephonyManager.getVoicemailRingtoneUri(handle)) 124 .setOngoing(isOngoing) 125 .setOnlyAlertOnce(isRefresh) 126 .setChannelId(NotificationChannelManager.getVoicemailChannelId(context, handle)) 127 .setDeleteIntent( 128 CallLogNotificationsService.createLegacyVoicemailDismissedPendingIntent( 129 context, handle)); 130 131 if (pinnedTelephonyManager.isVoicemailVibrationEnabled(handle)) { 132 builder.setDefaults(Notification.DEFAULT_VIBRATE); 133 } 134 135 return builder.build(); 136 } 137 138 @NonNull getNotificationText( @onNull Context context, PhoneAccountHandle handle, String voicemailNumber)139 private static String getNotificationText( 140 @NonNull Context context, PhoneAccountHandle handle, String voicemailNumber) { 141 if (TelecomUtil.getCallCapablePhoneAccounts(context).size() > 1) { 142 TelecomManager telecomManager = context.getSystemService(TelecomManager.class); 143 PhoneAccount phoneAccount = telecomManager.getPhoneAccount(handle); 144 return phoneAccount.getShortDescription().toString(); 145 } else { 146 return String.format( 147 context.getString(R.string.notification_voicemail_text_format), 148 PhoneNumberHelper.formatNumber( 149 context, voicemailNumber, GeoUtil.getCurrentCountryIso(context))); 150 } 151 } 152 cancelNotification( @onNull Context context, @NonNull PhoneAccountHandle phoneAccountHandle)153 public static void cancelNotification( 154 @NonNull Context context, @NonNull PhoneAccountHandle phoneAccountHandle) { 155 LogUtil.enterBlock("LegacyVoicemailNotifier.cancelNotification"); 156 Assert.checkArgument(BuildCompat.isAtLeastO()); 157 Assert.isNotNull(phoneAccountHandle); 158 DialerNotificationManager.cancel( 159 context, getNotificationTag(context, phoneAccountHandle), NOTIFICATION_ID); 160 } 161 162 @NonNull getNotificationTag( @onNull Context context, @NonNull PhoneAccountHandle phoneAccountHandle)163 private static String getNotificationTag( 164 @NonNull Context context, @NonNull PhoneAccountHandle phoneAccountHandle) { 165 if (context.getSystemService(TelephonyManager.class).getPhoneCount() <= 1) { 166 return NOTIFICATION_TAG; 167 } 168 return NOTIFICATION_TAG_PREFIX + phoneAccountHandle.getId(); 169 } 170 LegacyVoicemailNotifier()171 private LegacyVoicemailNotifier() {} 172 } 173