Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.incallui; |
| 18 | |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 19 | import static android.telecom.Call.Details.PROPERTY_HIGH_DEF_AUDIO; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 20 | import static com.android.contacts.common.compat.CallCompat.Details.PROPERTY_ENTERPRISE_CALL; |
| 21 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST; |
| 22 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VIDEO_INCOMING_CALL; |
| 23 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VOICE_INCOMING_CALL; |
| 24 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_INCOMING_CALL; |
| 25 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST; |
| 26 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_HANG_UP_ONGOING_CALL; |
| 27 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 28 | import android.Manifest; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 29 | import android.app.ActivityManager; |
| 30 | import android.app.Notification; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 31 | import android.app.Notification.Builder; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 32 | import android.app.NotificationManager; |
| 33 | import android.app.PendingIntent; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.BitmapFactory; |
| 38 | import android.graphics.drawable.BitmapDrawable; |
| 39 | import android.graphics.drawable.Drawable; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 40 | import android.graphics.drawable.Icon; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 41 | import android.media.AudioAttributes; |
| 42 | import android.net.Uri; |
| 43 | import android.os.Build.VERSION; |
| 44 | import android.os.Build.VERSION_CODES; |
| 45 | import android.support.annotation.ColorRes; |
| 46 | import android.support.annotation.NonNull; |
| 47 | import android.support.annotation.Nullable; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 48 | import android.support.annotation.RequiresPermission; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | import android.support.annotation.StringRes; |
| 50 | import android.support.annotation.VisibleForTesting; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 51 | import android.support.v4.os.BuildCompat; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 52 | import android.telecom.Call.Details; |
| 53 | import android.telecom.PhoneAccount; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 54 | import android.telecom.PhoneAccountHandle; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 55 | import android.telecom.TelecomManager; |
| 56 | import android.text.BidiFormatter; |
| 57 | import android.text.Spannable; |
| 58 | import android.text.SpannableString; |
| 59 | import android.text.TextDirectionHeuristics; |
| 60 | import android.text.TextUtils; |
| 61 | import android.text.style.ForegroundColorSpan; |
| 62 | import com.android.contacts.common.ContactsUtils; |
| 63 | import com.android.contacts.common.ContactsUtils.UserType; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 64 | import com.android.contacts.common.lettertiles.LetterTileDrawable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | import com.android.contacts.common.preference.ContactsPreferences; |
| 66 | import com.android.contacts.common.util.BitmapUtil; |
| 67 | import com.android.contacts.common.util.ContactDisplayUtils; |
| 68 | import com.android.dialer.common.LogUtil; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 69 | import com.android.dialer.enrichedcall.EnrichedCallComponent; |
| 70 | import com.android.dialer.enrichedcall.EnrichedCallManager; |
| 71 | import com.android.dialer.enrichedcall.Session; |
| 72 | import com.android.dialer.multimedia.MultimediaData; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 73 | import com.android.dialer.notification.NotificationChannelManager; |
| 74 | import com.android.dialer.notification.NotificationChannelManager.Channel; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 75 | import com.android.dialer.oem.MotorolaUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 76 | import com.android.dialer.util.DrawableConverter; |
| 77 | import com.android.incallui.ContactInfoCache.ContactCacheEntry; |
| 78 | import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback; |
| 79 | import com.android.incallui.InCallPresenter.InCallState; |
| 80 | import com.android.incallui.async.PausableExecutorImpl; |
| 81 | import com.android.incallui.call.CallList; |
| 82 | import com.android.incallui.call.DialerCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 83 | import com.android.incallui.call.DialerCallListener; |
| 84 | import com.android.incallui.ringtone.DialerRingtoneManager; |
| 85 | import com.android.incallui.ringtone.InCallTonePlayer; |
| 86 | import com.android.incallui.ringtone.ToneGeneratorFactory; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 87 | import com.android.incallui.videotech.utils.SessionModificationState; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 88 | import java.util.List; |
| 89 | import java.util.Locale; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 90 | import java.util.Objects; |
| 91 | |
| 92 | /** This class adds Notifications to the status bar for the in-call experience. */ |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 93 | public class StatusBarNotifier |
| 94 | implements InCallPresenter.InCallStateListener, EnrichedCallManager.StateChangedListener { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 95 | |
| 96 | // Notification types |
| 97 | // Indicates that no notification is currently showing. |
| 98 | private static final int NOTIFICATION_NONE = 0; |
| 99 | // Notification for an active call. This is non-interruptive, but cannot be dismissed. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 100 | private static final int NOTIFICATION_IN_CALL = R.id.notification_ongoing_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 101 | // Notification for incoming calls. This is interruptive and will show up as a HUN. |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 102 | private static final int NOTIFICATION_INCOMING_CALL = R.id.notification_incoming_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 103 | |
| 104 | private static final int PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN = 0; |
| 105 | private static final int PENDING_INTENT_REQUEST_CODE_FULL_SCREEN = 1; |
| 106 | |
| 107 | private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000}; |
| 108 | |
| 109 | private final Context mContext; |
| 110 | private final ContactInfoCache mContactInfoCache; |
| 111 | private final NotificationManager mNotificationManager; |
| 112 | private final DialerRingtoneManager mDialerRingtoneManager; |
| 113 | @Nullable private ContactsPreferences mContactsPreferences; |
| 114 | private int mCurrentNotification = NOTIFICATION_NONE; |
| 115 | private int mCallState = DialerCall.State.INVALID; |
| 116 | private int mSavedIcon = 0; |
| 117 | private String mSavedContent = null; |
| 118 | private Bitmap mSavedLargeIcon; |
| 119 | private String mSavedContentTitle; |
| 120 | private Uri mRingtone; |
| 121 | private StatusBarCallListener mStatusBarCallListener; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 122 | private boolean mShowFullScreenIntent; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 123 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 124 | public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 125 | Objects.requireNonNull(context); |
| 126 | mContext = context; |
| 127 | mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext); |
| 128 | mContactInfoCache = contactInfoCache; |
| 129 | mNotificationManager = context.getSystemService(NotificationManager.class); |
| 130 | mDialerRingtoneManager = |
| 131 | new DialerRingtoneManager( |
| 132 | new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()), |
| 133 | CallList.getInstance()); |
| 134 | mCurrentNotification = NOTIFICATION_NONE; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Should only be called from a irrecoverable state where it is necessary to dismiss all |
| 139 | * notifications. |
| 140 | */ |
| 141 | static void clearAllCallNotifications(Context backupContext) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 142 | LogUtil.i( |
| 143 | "StatusBarNotifier.clearAllCallNotifications", |
| 144 | "something terrible happened, clear all InCall notifications"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 145 | |
| 146 | NotificationManager notificationManager = |
| 147 | backupContext.getSystemService(NotificationManager.class); |
| 148 | notificationManager.cancel(NOTIFICATION_IN_CALL); |
| 149 | notificationManager.cancel(NOTIFICATION_INCOMING_CALL); |
| 150 | } |
| 151 | |
| 152 | private static int getWorkStringFromPersonalString(int resId) { |
| 153 | if (resId == R.string.notification_ongoing_call) { |
| 154 | return R.string.notification_ongoing_work_call; |
| 155 | } else if (resId == R.string.notification_ongoing_call_wifi) { |
| 156 | return R.string.notification_ongoing_work_call_wifi; |
| 157 | } else if (resId == R.string.notification_incoming_call_wifi) { |
| 158 | return R.string.notification_incoming_work_call_wifi; |
| 159 | } else if (resId == R.string.notification_incoming_call) { |
| 160 | return R.string.notification_incoming_work_call; |
| 161 | } else { |
| 162 | return resId; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Returns PendingIntent for answering a phone call. This will typically be used from Notification |
| 168 | * context. |
| 169 | */ |
| 170 | private static PendingIntent createNotificationPendingIntent(Context context, String action) { |
| 171 | final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class); |
| 172 | return PendingIntent.getBroadcast(context, 0, intent, 0); |
| 173 | } |
| 174 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 175 | private static void setColorized(@NonNull Builder builder) { |
| 176 | if (BuildCompat.isAtLeastO()) { |
| 177 | builder.setColorized(true); |
| 178 | } |
| 179 | } |
| 180 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 181 | /** Creates notifications according to the state we receive from {@link InCallPresenter}. */ |
| 182 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 183 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 184 | public void onStateChange(InCallState oldState, InCallState newState, CallList callList) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 185 | LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 186 | updateNotification(callList); |
| 187 | } |
| 188 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 189 | @Override |
| 190 | public void onEnrichedCallStateChanged() { |
| 191 | LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged"); |
| 192 | updateNotification(CallList.getInstance()); |
| 193 | } |
| 194 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 195 | /** |
| 196 | * Updates the phone app's status bar notification *and* launches the incoming call UI in response |
| 197 | * to a new incoming call. |
| 198 | * |
| 199 | * <p>If an incoming call is ringing (or call-waiting), the notification will also include a |
| 200 | * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current |
| 201 | * foreground activity is marked as "immersive". |
| 202 | * |
| 203 | * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new |
| 204 | * ringing connection" event from the telephony layer.) |
| 205 | * |
| 206 | * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or, |
| 207 | * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case, |
| 208 | * we'll simply update or cancel the in-call notification based on the current phone state. |
| 209 | * |
| 210 | * @see #updateInCallNotification(CallList) |
| 211 | */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 212 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 213 | public void updateNotification(CallList callList) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 214 | updateInCallNotification(callList); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Take down the in-call notification. |
| 219 | * |
| 220 | * @see #updateInCallNotification(CallList) |
| 221 | */ |
| 222 | private void cancelNotification() { |
| 223 | if (mStatusBarCallListener != null) { |
| 224 | setStatusBarCallListener(null); |
| 225 | } |
| 226 | if (mCurrentNotification != NOTIFICATION_NONE) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 227 | LogUtil.d("StatusBarNotifier.cancelNotification", "cancel"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 228 | mNotificationManager.cancel(mCurrentNotification); |
| 229 | } |
| 230 | mCurrentNotification = NOTIFICATION_NONE; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's |
| 235 | * status bar notification based on the current telephony state, or cancels the notification if |
| 236 | * the phone is totally idle. |
| 237 | */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 238 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 239 | private void updateInCallNotification(CallList callList) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 240 | LogUtil.d("StatusBarNotifier.updateInCallNotification", ""); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 241 | |
| 242 | final DialerCall call = getCallToShow(callList); |
| 243 | |
| 244 | if (call != null) { |
| 245 | showNotification(callList, call); |
| 246 | } else { |
| 247 | cancelNotification(); |
| 248 | } |
| 249 | } |
| 250 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 251 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 252 | private void showNotification(final CallList callList, final DialerCall call) { |
| 253 | final boolean isIncoming = |
| 254 | (call.getState() == DialerCall.State.INCOMING |
| 255 | || call.getState() == DialerCall.State.CALL_WAITING); |
| 256 | setStatusBarCallListener(new StatusBarCallListener(call)); |
| 257 | |
| 258 | // we make a call to the contact info cache to query for supplemental data to what the |
| 259 | // call provides. This includes the contact name and photo. |
| 260 | // This callback will always get called immediately and synchronously with whatever data |
| 261 | // it has available, and may make a subsequent call later (same thread) if it had to |
| 262 | // call into the contacts provider for more data. |
| 263 | mContactInfoCache.findInfo( |
| 264 | call, |
| 265 | isIncoming, |
| 266 | new ContactInfoCacheCallback() { |
| 267 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 268 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 269 | public void onContactInfoComplete(String callId, ContactCacheEntry entry) { |
| 270 | DialerCall call = callList.getCallById(callId); |
| 271 | if (call != null) { |
| 272 | call.getLogState().contactLookupResult = entry.contactLookupResult; |
| 273 | buildAndSendNotification(callList, call, entry); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 278 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 279 | public void onImageLoadComplete(String callId, ContactCacheEntry entry) { |
| 280 | DialerCall call = callList.getCallById(callId); |
| 281 | if (call != null) { |
| 282 | buildAndSendNotification(callList, call, entry); |
| 283 | } |
| 284 | } |
| 285 | }); |
| 286 | } |
| 287 | |
| 288 | /** Sets up the main Ui for the notification */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 289 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 290 | private void buildAndSendNotification( |
| 291 | CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) { |
| 292 | // This can get called to update an existing notification after contact information has come |
| 293 | // back. However, it can happen much later. Before we continue, we need to make sure that |
| 294 | // the call being passed in is still the one we want to show in the notification. |
| 295 | final DialerCall call = getCallToShow(callList); |
| 296 | if (call == null || !call.getId().equals(originalCall.getId())) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | final int callState = call.getState(); |
| 301 | |
| 302 | // Check if data has changed; if nothing is different, don't issue another notification. |
| 303 | final int iconResId = getIconToDisplay(call); |
| 304 | Bitmap largeIcon = getLargeIconToDisplay(contactInfo, call); |
| 305 | final String content = getContentString(call, contactInfo.userType); |
| 306 | final String contentTitle = getContentTitle(contactInfo, call); |
| 307 | |
| 308 | final boolean isVideoUpgradeRequest = |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 309 | call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 310 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 311 | final int notificationType; |
| 312 | if (callState == DialerCall.State.INCOMING |
| 313 | || callState == DialerCall.State.CALL_WAITING |
| 314 | || isVideoUpgradeRequest) { |
| 315 | notificationType = NOTIFICATION_INCOMING_CALL; |
| 316 | } else { |
| 317 | notificationType = NOTIFICATION_IN_CALL; |
| 318 | } |
| 319 | |
| 320 | if (!checkForChangeAndSaveData( |
| 321 | iconResId, |
| 322 | content, |
| 323 | largeIcon, |
| 324 | contentTitle, |
| 325 | callState, |
| 326 | notificationType, |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 327 | contactInfo.contactRingtoneUri, |
| 328 | InCallPresenter.getInstance().shouldShowFullScreenNotification())) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | if (largeIcon != null) { |
| 333 | largeIcon = getRoundedIcon(largeIcon); |
| 334 | } |
| 335 | |
| 336 | // This builder is used for the notification shown when the device is locked and the user |
| 337 | // has set their notification settings to 'hide sensitive content' |
| 338 | // {@see Notification.Builder#setPublicVersion}. |
| 339 | Notification.Builder publicBuilder = new Notification.Builder(mContext); |
| 340 | publicBuilder |
| 341 | .setSmallIcon(iconResId) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 342 | .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 343 | // Hide work call state for the lock screen notification |
| 344 | .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT)); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 345 | setColorized(publicBuilder); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 346 | setNotificationWhen(call, callState, publicBuilder); |
| 347 | |
| 348 | // Builder for the notification shown when the device is unlocked or the user has set their |
| 349 | // notification settings to 'show all notification content'. |
| 350 | final Notification.Builder builder = getNotificationBuilder(); |
| 351 | builder.setPublicVersion(publicBuilder.build()); |
| 352 | |
| 353 | // Set up the main intent to send the user to the in-call screen |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 354 | builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 355 | |
| 356 | // Set the intent as a full screen intent as well if a call is incoming |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 357 | PhoneAccountHandle accountHandle = call.getAccountHandle(); |
| 358 | if (accountHandle == null) { |
| 359 | accountHandle = getAnyPhoneAccount(); |
| 360 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 361 | if (notificationType == NOTIFICATION_INCOMING_CALL) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 362 | NotificationChannelManager.applyChannel( |
| 363 | builder, mContext, Channel.INCOMING_CALL, accountHandle); |
| 364 | if (InCallPresenter.getInstance().shouldShowFullScreenNotification()) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 365 | configureFullScreenIntent( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 366 | builder, createLaunchPendingIntent(true /* isFullScreen */), callList, call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 367 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 368 | // Set the notification category and bump the priority for incoming calls |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 369 | builder.setCategory(Notification.CATEGORY_CALL); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 370 | builder.setPriority(Notification.PRIORITY_MAX); |
| 371 | } else { |
| 372 | NotificationChannelManager.applyChannel( |
| 373 | builder, mContext, Channel.ONGOING_CALL, accountHandle); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // Set the content |
| 377 | builder.setContentText(content); |
| 378 | builder.setSmallIcon(iconResId); |
| 379 | builder.setContentTitle(contentTitle); |
| 380 | builder.setLargeIcon(largeIcon); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 381 | builder.setColor( |
| 382 | mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())); |
| 383 | setColorized(builder); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 384 | |
| 385 | if (isVideoUpgradeRequest) { |
| 386 | builder.setUsesChronometer(false); |
| 387 | addDismissUpgradeRequestAction(builder); |
| 388 | addAcceptUpgradeRequestAction(builder); |
| 389 | } else { |
| 390 | createIncomingCallNotification(call, callState, builder); |
| 391 | } |
| 392 | |
| 393 | addPersonReference(builder, contactInfo, call); |
| 394 | |
| 395 | // Fire off the notification |
| 396 | Notification notification = builder.build(); |
| 397 | |
| 398 | if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) { |
| 399 | notification.flags |= Notification.FLAG_INSISTENT; |
| 400 | notification.sound = contactInfo.contactRingtoneUri; |
| 401 | AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder(); |
| 402 | audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC); |
| 403 | audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE); |
| 404 | notification.audioAttributes = audioAttributes.build(); |
| 405 | if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) { |
| 406 | notification.vibrate = VIBRATE_PATTERN; |
| 407 | } |
| 408 | } |
| 409 | if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 410 | LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 411 | mDialerRingtoneManager.playCallWaitingTone(); |
| 412 | } |
| 413 | if (mCurrentNotification != notificationType && mCurrentNotification != NOTIFICATION_NONE) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 414 | LogUtil.i( |
| 415 | "StatusBarNotifier.buildAndSendNotification", |
| 416 | "previous notification already showing - cancelling " + mCurrentNotification); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 417 | mNotificationManager.cancel(mCurrentNotification); |
| 418 | } |
| 419 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 420 | LogUtil.i( |
| 421 | "StatusBarNotifier.buildAndSendNotification", |
| 422 | "displaying notification for " + notificationType); |
| 423 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 424 | try { |
| 425 | mNotificationManager.notify(notificationType, notification); |
| 426 | } catch (RuntimeException e) { |
| 427 | // TODO(b/34744003): Move the memory stats into silent feedback PSD. |
| 428 | ActivityManager activityManager = mContext.getSystemService(ActivityManager.class); |
| 429 | ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); |
| 430 | activityManager.getMemoryInfo(memoryInfo); |
| 431 | throw new RuntimeException( |
| 432 | String.format( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 433 | Locale.US, |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 434 | "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)", |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 435 | contactInfo.photoType, |
| 436 | memoryInfo.lowMemory, |
| 437 | memoryInfo.availMem), |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 438 | e); |
| 439 | } |
| 440 | call.getLatencyReport().onNotificationShown(); |
| 441 | mCurrentNotification = notificationType; |
| 442 | } |
| 443 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 444 | @Nullable |
| 445 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
| 446 | private PhoneAccountHandle getAnyPhoneAccount() { |
| 447 | PhoneAccountHandle accountHandle; |
| 448 | TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class); |
| 449 | accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL); |
| 450 | if (accountHandle == null) { |
| 451 | List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts(); |
| 452 | if (!accountHandles.isEmpty()) { |
| 453 | accountHandle = accountHandles.get(0); |
| 454 | } |
| 455 | } |
| 456 | return accountHandle; |
| 457 | } |
| 458 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 459 | private void createIncomingCallNotification( |
| 460 | DialerCall call, int state, Notification.Builder builder) { |
| 461 | setNotificationWhen(call, state, builder); |
| 462 | |
| 463 | // Add hang up option for any active calls (active | onhold), outgoing calls (dialing). |
| 464 | if (state == DialerCall.State.ACTIVE |
| 465 | || state == DialerCall.State.ONHOLD |
| 466 | || DialerCall.State.isDialing(state)) { |
| 467 | addHangupAction(builder); |
| 468 | } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) { |
| 469 | addDismissAction(builder); |
| 470 | if (call.isVideoCall()) { |
| 471 | addVideoCallAction(builder); |
| 472 | } else { |
| 473 | addAnswerAction(builder); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Sets the notification's when section as needed. For active calls, this is explicitly set as the |
| 480 | * duration of the call. For all other states, the notification will automatically show the time |
| 481 | * at which the notification was created. |
| 482 | */ |
| 483 | private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) { |
| 484 | if (state == DialerCall.State.ACTIVE) { |
| 485 | builder.setUsesChronometer(true); |
| 486 | builder.setWhen(call.getConnectTimeMillis()); |
| 487 | } else { |
| 488 | builder.setUsesChronometer(false); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Checks the new notification data and compares it against any notification that we are already |
| 494 | * displaying. If the data is exactly the same, we return false so that we do not issue a new |
| 495 | * notification for the exact same data. |
| 496 | */ |
| 497 | private boolean checkForChangeAndSaveData( |
| 498 | int icon, |
| 499 | String content, |
| 500 | Bitmap largeIcon, |
| 501 | String contentTitle, |
| 502 | int state, |
| 503 | int notificationType, |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 504 | Uri ringtone, |
| 505 | boolean showFullScreenIntent) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 506 | |
| 507 | // The two are different: |
| 508 | // if new title is not null, it should be different from saved version OR |
| 509 | // if new title is null, the saved version should not be null |
| 510 | final boolean contentTitleChanged = |
| 511 | (contentTitle != null && !contentTitle.equals(mSavedContentTitle)) |
| 512 | || (contentTitle == null && mSavedContentTitle != null); |
| 513 | |
| 514 | // any change means we are definitely updating |
| 515 | boolean retval = |
| 516 | (mSavedIcon != icon) |
| 517 | || !Objects.equals(mSavedContent, content) |
| 518 | || (mCallState != state) |
| 519 | || (mSavedLargeIcon != largeIcon) |
| 520 | || contentTitleChanged |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 521 | || !Objects.equals(mRingtone, ringtone) |
| 522 | || mShowFullScreenIntent != showFullScreenIntent; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 523 | |
| 524 | // If we aren't showing a notification right now or the notification type is changing, |
| 525 | // definitely do an update. |
| 526 | if (mCurrentNotification != notificationType) { |
| 527 | if (mCurrentNotification == NOTIFICATION_NONE) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 528 | LogUtil.d( |
| 529 | "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 530 | } |
| 531 | retval = true; |
| 532 | } |
| 533 | |
| 534 | mSavedIcon = icon; |
| 535 | mSavedContent = content; |
| 536 | mCallState = state; |
| 537 | mSavedLargeIcon = largeIcon; |
| 538 | mSavedContentTitle = contentTitle; |
| 539 | mRingtone = ringtone; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 540 | mShowFullScreenIntent = showFullScreenIntent; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 541 | |
| 542 | if (retval) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 543 | LogUtil.d( |
| 544 | "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return retval; |
| 548 | } |
| 549 | |
| 550 | /** Returns the main string to use in the notification. */ |
| 551 | @VisibleForTesting |
| 552 | @Nullable |
| 553 | String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) { |
| 554 | if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) { |
| 555 | return mContext.getResources().getString(R.string.conference_call_name); |
| 556 | } |
| 557 | |
| 558 | String preferredName = |
| 559 | ContactDisplayUtils.getPreferredDisplayName( |
| 560 | contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences); |
| 561 | if (TextUtils.isEmpty(preferredName)) { |
| 562 | return TextUtils.isEmpty(contactInfo.number) |
| 563 | ? null |
| 564 | : BidiFormatter.getInstance() |
| 565 | .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR); |
| 566 | } |
| 567 | return preferredName; |
| 568 | } |
| 569 | |
| 570 | private void addPersonReference( |
| 571 | Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) { |
| 572 | // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed. |
| 573 | // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid |
| 574 | // NotificationManager using it. |
| 575 | if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) { |
| 576 | builder.addPerson(contactInfo.lookupUri.toString()); |
| 577 | } else if (!TextUtils.isEmpty(call.getNumber())) { |
| 578 | builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString()); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /** Gets a large icon from the contact info object to display in the notification. */ |
| 583 | private Bitmap getLargeIconToDisplay(ContactCacheEntry contactInfo, DialerCall call) { |
| 584 | Bitmap largeIcon = null; |
| 585 | if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) { |
| 586 | largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.img_conference); |
| 587 | } |
| 588 | if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) { |
| 589 | largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap(); |
| 590 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 591 | if (contactInfo.photo == null) { |
| 592 | int width = |
| 593 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width); |
| 594 | int height = |
| 595 | (int) |
| 596 | mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height); |
| 597 | int contactType = LetterTileDrawable.TYPE_DEFAULT; |
| 598 | LetterTileDrawable lettertile = new LetterTileDrawable(mContext.getResources()); |
| 599 | |
| 600 | // TODO: Deduplicate across Dialer. b/36195917 |
| 601 | if (CallerInfoUtils.isVoiceMailNumber(mContext, call)) { |
| 602 | contactType = LetterTileDrawable.TYPE_VOICEMAIL; |
| 603 | } else if (contactInfo.isBusiness) { |
| 604 | contactType = LetterTileDrawable.TYPE_BUSINESS; |
| 605 | } else if (call.getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED) { |
| 606 | contactType = LetterTileDrawable.TYPE_GENERIC_AVATAR; |
| 607 | } |
| 608 | lettertile.setCanonicalDialerLetterTileDetails( |
| 609 | contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary, |
| 610 | contactInfo.lookupKey, |
| 611 | LetterTileDrawable.SHAPE_CIRCLE, |
| 612 | contactType); |
| 613 | largeIcon = lettertile.getBitmap(width, height); |
| 614 | } |
| 615 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 616 | if (call.isSpam()) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 617 | Drawable drawable = |
| 618 | mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 619 | largeIcon = DrawableConverter.drawableToBitmap(drawable); |
| 620 | } |
| 621 | return largeIcon; |
| 622 | } |
| 623 | |
| 624 | private Bitmap getRoundedIcon(Bitmap bitmap) { |
| 625 | if (bitmap == null) { |
| 626 | return null; |
| 627 | } |
| 628 | final int height = |
| 629 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height); |
| 630 | final int width = |
| 631 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width); |
| 632 | return BitmapUtil.getRoundedBitmap(bitmap, width, height); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Returns the appropriate icon res Id to display based on the call for which we want to display |
| 637 | * information. |
| 638 | */ |
| 639 | private int getIconToDisplay(DialerCall call) { |
| 640 | // Even if both lines are in use, we only show a single item in |
| 641 | // the expanded Notifications UI. It's labeled "Ongoing call" |
| 642 | // (or "On hold" if there's only one call, and it's on hold.) |
| 643 | // Also, we don't have room to display caller-id info from two |
| 644 | // different calls. So if both lines are in use, display info |
| 645 | // from the foreground call. And if there's a ringing call, |
| 646 | // display that regardless of the state of the other calls. |
| 647 | if (call.getState() == DialerCall.State.ONHOLD) { |
| 648 | return R.drawable.ic_phone_paused_white_24dp; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 649 | } else if (call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 650 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 651 | return R.drawable.quantum_ic_videocam_white_24; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 652 | } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO) |
| 653 | && MotorolaUtils.shouldShowHdIconInNotification(mContext)) { |
| 654 | // Normally when a call is ongoing the status bar displays an icon of a phone with animated |
| 655 | // lines. This is a helpful hint for users so they know how to get back to the call. |
| 656 | // For Sprint HD calls, we replace this icon with an icon of a phone with a HD badge. |
| 657 | // This is a carrier requirement. |
| 658 | return R.drawable.ic_hd_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 659 | } |
| 660 | return R.anim.on_going_call; |
| 661 | } |
| 662 | |
| 663 | /** Returns the message to use with the notification. */ |
| 664 | private String getContentString(DialerCall call, @UserType long userType) { |
| 665 | boolean isIncomingOrWaiting = |
| 666 | call.getState() == DialerCall.State.INCOMING |
| 667 | || call.getState() == DialerCall.State.CALL_WAITING; |
| 668 | |
| 669 | if (isIncomingOrWaiting |
| 670 | && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) { |
| 671 | |
| 672 | if (!TextUtils.isEmpty(call.getChildNumber())) { |
| 673 | return mContext.getString(R.string.child_number, call.getChildNumber()); |
| 674 | } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) { |
| 675 | return call.getCallSubject(); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | int resId = R.string.notification_ongoing_call; |
| 680 | if (call.hasProperty(Details.PROPERTY_WIFI)) { |
| 681 | resId = R.string.notification_ongoing_call_wifi; |
| 682 | } |
| 683 | |
| 684 | if (isIncomingOrWaiting) { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 685 | EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager(); |
| 686 | Session session = null; |
| 687 | if (call.getNumber() != null) { |
| 688 | session = manager.getSession(call.getUniqueCallId(), call.getNumber()); |
| 689 | } |
| 690 | |
| 691 | if (call.isSpam()) { |
| 692 | resId = R.string.notification_incoming_spam_call; |
| 693 | } else if (session != null) { |
| 694 | resId = getECIncomingCallText(session); |
| 695 | } else if (call.hasProperty(Details.PROPERTY_WIFI)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 696 | resId = R.string.notification_incoming_call_wifi; |
| 697 | } else { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 698 | resId = R.string.notification_incoming_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 699 | } |
| 700 | } else if (call.getState() == DialerCall.State.ONHOLD) { |
| 701 | resId = R.string.notification_on_hold; |
| 702 | } else if (DialerCall.State.isDialing(call.getState())) { |
| 703 | resId = R.string.notification_dialing; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 704 | } else if (call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 705 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 706 | resId = R.string.notification_requesting_video_call; |
| 707 | } |
| 708 | |
| 709 | // Is the call placed through work connection service. |
| 710 | boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL); |
| 711 | if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) { |
| 712 | resId = getWorkStringFromPersonalString(resId); |
| 713 | } |
| 714 | |
| 715 | return mContext.getString(resId); |
| 716 | } |
| 717 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 718 | private int getECIncomingCallText(Session session) { |
| 719 | int resId; |
| 720 | MultimediaData data = session.getMultimediaData(); |
| 721 | boolean hasImage = data.hasImageData(); |
| 722 | boolean hasSubject = !TextUtils.isEmpty(data.getText()); |
| 723 | boolean hasMap = data.getLocation() != null; |
| 724 | if (data.isImportant()) { |
| 725 | if (hasMap) { |
| 726 | if (hasImage) { |
| 727 | if (hasSubject) { |
| 728 | resId = R.string.important_notification_incoming_call_with_photo_message_location; |
| 729 | } else { |
| 730 | resId = R.string.important_notification_incoming_call_with_photo_location; |
| 731 | } |
| 732 | } else if (hasSubject) { |
| 733 | resId = R.string.important_notification_incoming_call_with_message_location; |
| 734 | } else { |
| 735 | resId = R.string.important_notification_incoming_call_with_location; |
| 736 | } |
| 737 | } else if (hasImage) { |
| 738 | if (hasSubject) { |
| 739 | resId = R.string.important_notification_incoming_call_with_photo_message; |
| 740 | } else { |
| 741 | resId = R.string.important_notification_incoming_call_with_photo; |
| 742 | } |
| 743 | } else { |
| 744 | resId = R.string.important_notification_incoming_call_with_message; |
| 745 | } |
| 746 | if (mContext.getString(resId).length() > 50) { |
| 747 | resId = R.string.important_notification_incoming_call_attachments; |
| 748 | } |
| 749 | } else { |
| 750 | if (hasMap) { |
| 751 | if (hasImage) { |
| 752 | if (hasSubject) { |
| 753 | resId = R.string.notification_incoming_call_with_photo_message_location; |
| 754 | } else { |
| 755 | resId = R.string.notification_incoming_call_with_photo_location; |
| 756 | } |
| 757 | } else if (hasSubject) { |
| 758 | resId = R.string.notification_incoming_call_with_message_location; |
| 759 | } else { |
| 760 | resId = R.string.notification_incoming_call_with_location; |
| 761 | } |
| 762 | } else if (hasImage) { |
| 763 | if (hasSubject) { |
| 764 | resId = R.string.notification_incoming_call_with_photo_message; |
| 765 | } else { |
| 766 | resId = R.string.notification_incoming_call_with_photo; |
| 767 | } |
| 768 | } else { |
| 769 | resId = R.string.notification_incoming_call_with_message; |
| 770 | } |
| 771 | } |
| 772 | if (mContext.getString(resId).length() > 50) { |
| 773 | resId = R.string.notification_incoming_call_attachments; |
| 774 | } |
| 775 | return resId; |
| 776 | } |
| 777 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 778 | /** Gets the most relevant call to display in the notification. */ |
| 779 | private DialerCall getCallToShow(CallList callList) { |
| 780 | if (callList == null) { |
| 781 | return null; |
| 782 | } |
| 783 | DialerCall call = callList.getIncomingCall(); |
| 784 | if (call == null) { |
| 785 | call = callList.getOutgoingCall(); |
| 786 | } |
| 787 | if (call == null) { |
| 788 | call = callList.getVideoUpgradeRequestCall(); |
| 789 | } |
| 790 | if (call == null) { |
| 791 | call = callList.getActiveOrBackgroundCall(); |
| 792 | } |
| 793 | return call; |
| 794 | } |
| 795 | |
| 796 | private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) { |
| 797 | Spannable spannable = new SpannableString(mContext.getText(stringRes)); |
| 798 | if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { |
| 799 | // This will only work for cases where the Notification.Builder has a fullscreen intent set |
| 800 | // Notification.Builder that does not have a full screen intent will take the color of the |
| 801 | // app and the following leads to a no-op. |
| 802 | spannable.setSpan( |
| 803 | new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0); |
| 804 | } |
| 805 | return spannable; |
| 806 | } |
| 807 | |
| 808 | private void addAnswerAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 809 | LogUtil.d( |
| 810 | "StatusBarNotifier.addAnswerAction", |
| 811 | "will show \"answer\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 812 | PendingIntent answerVoicePendingIntent = |
| 813 | createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 814 | // We put animation resources in "anim" folder instead of "drawable", which causes Android |
| 815 | // Studio to complain. |
| 816 | // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc? |
| 817 | //noinspection ResourceType |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 818 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 819 | new Notification.Action.Builder( |
| 820 | Icon.createWithResource(mContext, R.anim.on_going_call), |
| 821 | getActionText( |
| 822 | R.string.notification_action_answer, R.color.notification_action_accept), |
| 823 | answerVoicePendingIntent) |
| 824 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | private void addDismissAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 828 | LogUtil.d( |
| 829 | "StatusBarNotifier.addDismissAction", |
| 830 | "will show \"decline\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 831 | PendingIntent declinePendingIntent = |
| 832 | createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL); |
| 833 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 834 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 835 | Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 836 | getActionText( |
| 837 | R.string.notification_action_dismiss, R.color.notification_action_dismiss), |
| 838 | declinePendingIntent) |
| 839 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | private void addHangupAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 843 | LogUtil.d( |
| 844 | "StatusBarNotifier.addHangupAction", |
| 845 | "will show \"hang-up\" action in the ongoing active call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 846 | PendingIntent hangupPendingIntent = |
| 847 | createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL); |
| 848 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 849 | new Notification.Action.Builder( |
| 850 | Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp), |
| 851 | getActionText( |
| 852 | R.string.notification_action_end_call, R.color.notification_action_end_call), |
| 853 | hangupPendingIntent) |
| 854 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | private void addVideoCallAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 858 | LogUtil.i( |
| 859 | "StatusBarNotifier.addVideoCallAction", |
| 860 | "will show \"video\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 861 | PendingIntent answerVideoPendingIntent = |
| 862 | createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL); |
| 863 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 864 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 865 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 866 | getActionText( |
| 867 | R.string.notification_action_answer_video, |
| 868 | R.color.notification_action_answer_video), |
| 869 | answerVideoPendingIntent) |
| 870 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | private void addAcceptUpgradeRequestAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 874 | LogUtil.i( |
| 875 | "StatusBarNotifier.addAcceptUpgradeRequestAction", |
| 876 | "will show \"accept upgrade\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 877 | PendingIntent acceptVideoPendingIntent = |
| 878 | createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST); |
| 879 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 880 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 881 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 882 | getActionText( |
| 883 | R.string.notification_action_accept, R.color.notification_action_accept), |
| 884 | acceptVideoPendingIntent) |
| 885 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | private void addDismissUpgradeRequestAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 889 | LogUtil.i( |
| 890 | "StatusBarNotifier.addDismissUpgradeRequestAction", |
| 891 | "will show \"dismiss upgrade\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 892 | PendingIntent declineVideoPendingIntent = |
| 893 | createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST); |
| 894 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 895 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame^] | 896 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 897 | getActionText( |
| 898 | R.string.notification_action_dismiss, R.color.notification_action_dismiss), |
| 899 | declineVideoPendingIntent) |
| 900 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** Adds fullscreen intent to the builder. */ |
| 904 | private void configureFullScreenIntent( |
| 905 | Notification.Builder builder, PendingIntent intent, CallList callList, DialerCall call) { |
| 906 | // Ok, we actually want to launch the incoming call |
| 907 | // UI at this point (in addition to simply posting a notification |
| 908 | // to the status bar). Setting fullScreenIntent will cause |
| 909 | // the InCallScreen to be launched immediately *unless* the |
| 910 | // current foreground activity is marked as "immersive". |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 911 | LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 912 | builder.setFullScreenIntent(intent, true); |
| 913 | |
| 914 | // Ugly hack alert: |
| 915 | // |
| 916 | // The NotificationManager has the (undocumented) behavior |
| 917 | // that it will *ignore* the fullScreenIntent field if you |
| 918 | // post a new Notification that matches the ID of one that's |
| 919 | // already active. Unfortunately this is exactly what happens |
| 920 | // when you get an incoming call-waiting call: the |
| 921 | // "ongoing call" notification is already visible, so the |
| 922 | // InCallScreen won't get launched in this case! |
| 923 | // (The result: if you bail out of the in-call UI while on a |
| 924 | // call and then get a call-waiting call, the incoming call UI |
| 925 | // won't come up automatically.) |
| 926 | // |
| 927 | // The workaround is to just notice this exact case (this is a |
| 928 | // call-waiting call *and* the InCallScreen is not in the |
| 929 | // foreground) and manually cancel the in-call notification |
| 930 | // before (re)posting it. |
| 931 | // |
| 932 | // TODO: there should be a cleaner way of avoiding this |
| 933 | // problem (see discussion in bug 3184149.) |
| 934 | |
| 935 | // If a call is onhold during an incoming call, the call actually comes in as |
| 936 | // INCOMING. For that case *and* traditional call-waiting, we want to |
| 937 | // cancel the notification. |
| 938 | boolean isCallWaiting = |
| 939 | (call.getState() == DialerCall.State.CALL_WAITING |
| 940 | || (call.getState() == DialerCall.State.INCOMING |
| 941 | && callList.getBackgroundCall() != null)); |
| 942 | |
| 943 | if (isCallWaiting) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 944 | LogUtil.i( |
| 945 | "StatusBarNotifier.configureFullScreenIntent", |
| 946 | "updateInCallNotification: call-waiting! force relaunch..."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 947 | // Cancel the IN_CALL_NOTIFICATION immediately before |
| 948 | // (re)posting it; this seems to force the |
| 949 | // NotificationManager to launch the fullScreenIntent. |
| 950 | mNotificationManager.cancel(NOTIFICATION_IN_CALL); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | private Notification.Builder getNotificationBuilder() { |
| 955 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 956 | builder.setOngoing(true); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 957 | builder.setOnlyAlertOnce(true); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 958 | |
| 959 | return builder; |
| 960 | } |
| 961 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 962 | private PendingIntent createLaunchPendingIntent(boolean isFullScreen) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 963 | Intent intent = |
| 964 | InCallActivity.getIntent( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 965 | mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 966 | |
| 967 | int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN; |
| 968 | if (isFullScreen) { |
| 969 | // Use a unique request code so that the pending intent isn't clobbered by the |
| 970 | // non-full screen pending intent. |
| 971 | requestCode = PENDING_INTENT_REQUEST_CODE_FULL_SCREEN; |
| 972 | } |
| 973 | |
| 974 | // PendingIntent that can be used to launch the InCallActivity. The |
| 975 | // system fires off this intent if the user pulls down the windowshade |
| 976 | // and clicks the notification's expanded view. It's also used to |
| 977 | // launch the InCallActivity immediately when when there's an incoming |
| 978 | // call (see the "fullScreenIntent" field below). |
| 979 | return PendingIntent.getActivity(mContext, requestCode, intent, 0); |
| 980 | } |
| 981 | |
| 982 | private void setStatusBarCallListener(StatusBarCallListener listener) { |
| 983 | if (mStatusBarCallListener != null) { |
| 984 | mStatusBarCallListener.cleanup(); |
| 985 | } |
| 986 | mStatusBarCallListener = listener; |
| 987 | } |
| 988 | |
| 989 | private class StatusBarCallListener implements DialerCallListener { |
| 990 | |
| 991 | private DialerCall mDialerCall; |
| 992 | |
| 993 | StatusBarCallListener(DialerCall dialerCall) { |
| 994 | mDialerCall = dialerCall; |
| 995 | mDialerCall.addListener(this); |
| 996 | } |
| 997 | |
| 998 | void cleanup() { |
| 999 | mDialerCall.removeListener(this); |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public void onDialerCallDisconnect() {} |
| 1004 | |
| 1005 | @Override |
| 1006 | public void onDialerCallUpdate() { |
| 1007 | if (CallList.getInstance().getIncomingCall() == null) { |
| 1008 | mDialerRingtoneManager.stopCallWaitingTone(); |
| 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | @Override |
| 1013 | public void onDialerCallChildNumberChange() {} |
| 1014 | |
| 1015 | @Override |
| 1016 | public void onDialerCallLastForwardedNumberChange() {} |
| 1017 | |
| 1018 | @Override |
| 1019 | public void onDialerCallUpgradeToVideo() {} |
| 1020 | |
| 1021 | @Override |
| 1022 | public void onWiFiToLteHandover() {} |
| 1023 | |
| 1024 | @Override |
| 1025 | public void onHandoverToWifiFailure() {} |
| 1026 | |
| 1027 | /** |
| 1028 | * Responds to changes in the session modification state for the call by dismissing the status |
| 1029 | * bar notification as required. |
| 1030 | */ |
| 1031 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1032 | public void onDialerCallSessionModificationStateChange() { |
| 1033 | if (mDialerCall.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 1034 | == SessionModificationState.NO_REQUEST) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1035 | cleanup(); |
| 1036 | updateNotification(CallList.getInstance()); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | } |