blob: 405eede346fbffee22f54bf38b41912d196e0b89 [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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
17package com.android.incallui;
18
Eric Erfanian90508232017-03-24 09:31:16 -070019import static android.telecom.Call.Details.PROPERTY_HIGH_DEF_AUDIO;
Eric Erfanianccca3152017-02-22 16:32:36 -080020import static com.android.contacts.common.compat.CallCompat.Details.PROPERTY_ENTERPRISE_CALL;
21import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST;
22import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VIDEO_INCOMING_CALL;
23import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VOICE_INCOMING_CALL;
24import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_INCOMING_CALL;
25import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST;
26import static com.android.incallui.NotificationBroadcastReceiver.ACTION_HANG_UP_ONGOING_CALL;
27
Eric Erfaniand5e47f62017-03-15 14:41:07 -070028import android.Manifest;
Eric Erfanianccca3152017-02-22 16:32:36 -080029import android.app.ActivityManager;
30import android.app.Notification;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070031import android.app.Notification.Builder;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.app.NotificationManager;
33import android.app.PendingIntent;
34import android.content.Context;
35import android.content.Intent;
36import android.graphics.Bitmap;
37import android.graphics.BitmapFactory;
38import android.graphics.drawable.BitmapDrawable;
39import android.graphics.drawable.Drawable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070040import android.graphics.drawable.Icon;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import android.media.AudioAttributes;
42import android.net.Uri;
43import android.os.Build.VERSION;
44import android.os.Build.VERSION_CODES;
45import android.support.annotation.ColorRes;
46import android.support.annotation.NonNull;
47import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070048import android.support.annotation.RequiresPermission;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import android.support.annotation.StringRes;
50import android.support.annotation.VisibleForTesting;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070051import android.support.v4.os.BuildCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import android.telecom.Call.Details;
53import android.telecom.PhoneAccount;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070054import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080055import android.telecom.TelecomManager;
56import android.text.BidiFormatter;
57import android.text.Spannable;
58import android.text.SpannableString;
59import android.text.TextDirectionHeuristics;
60import android.text.TextUtils;
61import android.text.style.ForegroundColorSpan;
62import com.android.contacts.common.ContactsUtils;
63import com.android.contacts.common.ContactsUtils.UserType;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070064import com.android.contacts.common.lettertiles.LetterTileDrawable;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.contacts.common.preference.ContactsPreferences;
66import com.android.contacts.common.util.BitmapUtil;
67import com.android.contacts.common.util.ContactDisplayUtils;
68import com.android.dialer.common.LogUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070069import com.android.dialer.enrichedcall.EnrichedCallComponent;
70import com.android.dialer.enrichedcall.EnrichedCallManager;
71import com.android.dialer.enrichedcall.Session;
72import com.android.dialer.multimedia.MultimediaData;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070073import com.android.dialer.notification.NotificationChannelManager;
74import com.android.dialer.notification.NotificationChannelManager.Channel;
Eric Erfanian90508232017-03-24 09:31:16 -070075import com.android.dialer.oem.MotorolaUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080076import com.android.dialer.util.DrawableConverter;
77import com.android.incallui.ContactInfoCache.ContactCacheEntry;
78import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
79import com.android.incallui.InCallPresenter.InCallState;
80import com.android.incallui.async.PausableExecutorImpl;
81import com.android.incallui.call.CallList;
82import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080083import com.android.incallui.call.DialerCallListener;
84import com.android.incallui.ringtone.DialerRingtoneManager;
85import com.android.incallui.ringtone.InCallTonePlayer;
86import com.android.incallui.ringtone.ToneGeneratorFactory;
Eric Erfanian90508232017-03-24 09:31:16 -070087import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070088import java.util.List;
89import java.util.Locale;
Eric Erfanianccca3152017-02-22 16:32:36 -080090import java.util.Objects;
91
92/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -070093public class StatusBarNotifier
94 implements InCallPresenter.InCallStateListener, EnrichedCallManager.StateChangedListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080095
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 Erfaniand5e47f62017-03-15 14:41:07 -0700100 private static final int NOTIFICATION_IN_CALL = R.id.notification_ongoing_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800101 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700102 private static final int NOTIFICATION_INCOMING_CALL = R.id.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800103
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 Erfaniand5e47f62017-03-15 14:41:07 -0700122 private boolean mShowFullScreenIntent;
Eric Erfanianccca3152017-02-22 16:32:36 -0800123
Eric Erfaniand8046e52017-04-06 09:41:50 -0700124 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800125 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 Erfaniand5e47f62017-03-15 14:41:07 -0700142 LogUtil.i(
143 "StatusBarNotifier.clearAllCallNotifications",
144 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800145
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 Erfaniand5e47f62017-03-15 14:41:07 -0700175 private static void setColorized(@NonNull Builder builder) {
176 if (BuildCompat.isAtLeastO()) {
177 builder.setColorized(true);
178 }
179 }
180
Eric Erfanianccca3152017-02-22 16:32:36 -0800181 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
182 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700183 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700185 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800186 updateNotification(callList);
187 }
188
Eric Erfaniand8046e52017-04-06 09:41:50 -0700189 @Override
190 public void onEnrichedCallStateChanged() {
191 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
192 updateNotification(CallList.getInstance());
193 }
194
Eric Erfanianccca3152017-02-22 16:32:36 -0800195 /**
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 Erfaniand5e47f62017-03-15 14:41:07 -0700212 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700213 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800214 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 Erfaniand5e47f62017-03-15 14:41:07 -0700227 LogUtil.d("StatusBarNotifier.cancelNotification", "cancel");
Eric Erfanianccca3152017-02-22 16:32:36 -0800228 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 Erfaniand5e47f62017-03-15 14:41:07 -0700238 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800239 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700240 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800241
242 final DialerCall call = getCallToShow(callList);
243
244 if (call != null) {
245 showNotification(callList, call);
246 } else {
247 cancelNotification();
248 }
249 }
250
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700251 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800252 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 Erfaniand5e47f62017-03-15 14:41:07 -0700268 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800269 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 Erfaniand5e47f62017-03-15 14:41:07 -0700278 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 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 Erfaniand5e47f62017-03-15 14:41:07 -0700289 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800290 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 Erfaniand5e47f62017-03-15 14:41:07 -0700309 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700310 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800311 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 Erfaniand5e47f62017-03-15 14:41:07 -0700327 contactInfo.contactRingtoneUri,
328 InCallPresenter.getInstance().shouldShowFullScreenNotification())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800329 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 Erfaniand5e47f62017-03-15 14:41:07 -0700342 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800343 // Hide work call state for the lock screen notification
344 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700345 setColorized(publicBuilder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800346 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 Erfaniand5e47f62017-03-15 14:41:07 -0700354 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800355
356 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700357 PhoneAccountHandle accountHandle = call.getAccountHandle();
358 if (accountHandle == null) {
359 accountHandle = getAnyPhoneAccount();
360 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800361 if (notificationType == NOTIFICATION_INCOMING_CALL) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700362 NotificationChannelManager.applyChannel(
363 builder, mContext, Channel.INCOMING_CALL, accountHandle);
364 if (InCallPresenter.getInstance().shouldShowFullScreenNotification()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800365 configureFullScreenIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700366 builder, createLaunchPendingIntent(true /* isFullScreen */), callList, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800367 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700368 // Set the notification category and bump the priority for incoming calls
Eric Erfanianccca3152017-02-22 16:32:36 -0800369 builder.setCategory(Notification.CATEGORY_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700370 builder.setPriority(Notification.PRIORITY_MAX);
371 } else {
372 NotificationChannelManager.applyChannel(
373 builder, mContext, Channel.ONGOING_CALL, accountHandle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800374 }
375
376 // Set the content
377 builder.setContentText(content);
378 builder.setSmallIcon(iconResId);
379 builder.setContentTitle(contentTitle);
380 builder.setLargeIcon(largeIcon);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700381 builder.setColor(
382 mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()));
383 setColorized(builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800384
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 Erfaniand5e47f62017-03-15 14:41:07 -0700410 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 mDialerRingtoneManager.playCallWaitingTone();
412 }
413 if (mCurrentNotification != notificationType && mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700414 LogUtil.i(
415 "StatusBarNotifier.buildAndSendNotification",
416 "previous notification already showing - cancelling " + mCurrentNotification);
Eric Erfanianccca3152017-02-22 16:32:36 -0800417 mNotificationManager.cancel(mCurrentNotification);
418 }
419
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700420 LogUtil.i(
421 "StatusBarNotifier.buildAndSendNotification",
422 "displaying notification for " + notificationType);
423
Eric Erfanianccca3152017-02-22 16:32:36 -0800424 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 Erfaniand5e47f62017-03-15 14:41:07 -0700433 Locale.US,
Eric Erfanianccca3152017-02-22 16:32:36 -0800434 "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)",
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700435 contactInfo.photoType,
436 memoryInfo.lowMemory,
437 memoryInfo.availMem),
Eric Erfanianccca3152017-02-22 16:32:36 -0800438 e);
439 }
440 call.getLatencyReport().onNotificationShown();
441 mCurrentNotification = notificationType;
442 }
443
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700444 @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 Erfanianccca3152017-02-22 16:32:36 -0800459 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 Erfaniand5e47f62017-03-15 14:41:07 -0700504 Uri ringtone,
505 boolean showFullScreenIntent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800506
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 Erfaniand5e47f62017-03-15 14:41:07 -0700521 || !Objects.equals(mRingtone, ringtone)
522 || mShowFullScreenIntent != showFullScreenIntent;
Eric Erfanianccca3152017-02-22 16:32:36 -0800523
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 Erfaniand5e47f62017-03-15 14:41:07 -0700528 LogUtil.d(
529 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 }
531 retval = true;
532 }
533
534 mSavedIcon = icon;
535 mSavedContent = content;
536 mCallState = state;
537 mSavedLargeIcon = largeIcon;
538 mSavedContentTitle = contentTitle;
539 mRingtone = ringtone;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700540 mShowFullScreenIntent = showFullScreenIntent;
Eric Erfanianccca3152017-02-22 16:32:36 -0800541
542 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700543 LogUtil.d(
544 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 }
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 Erfaniand5e47f62017-03-15 14:41:07 -0700591 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 Erfanianccca3152017-02-22 16:32:36 -0800616 if (call.isSpam()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700617 Drawable drawable =
618 mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800619 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 Erfaniand5e47f62017-03-15 14:41:07 -0700649 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700650 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700651 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700652 } 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 Erfanianccca3152017-02-22 16:32:36 -0800659 }
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 Erfaniand8046e52017-04-06 09:41:50 -0700685 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 Erfanianccca3152017-02-22 16:32:36 -0800696 resId = R.string.notification_incoming_call_wifi;
697 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700698 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800699 }
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 Erfaniand5e47f62017-03-15 14:41:07 -0700704 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700705 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800706 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 Erfaniand8046e52017-04-06 09:41:50 -0700718 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 Erfanianccca3152017-02-22 16:32:36 -0800778 /** 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 Erfaniand5e47f62017-03-15 14:41:07 -0700809 LogUtil.d(
810 "StatusBarNotifier.addAnswerAction",
811 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800812 PendingIntent answerVoicePendingIntent =
813 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700814 // 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 Erfanianccca3152017-02-22 16:32:36 -0800818 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700819 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 Erfanianccca3152017-02-22 16:32:36 -0800825 }
826
827 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700828 LogUtil.d(
829 "StatusBarNotifier.addDismissAction",
830 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800831 PendingIntent declinePendingIntent =
832 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
833 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700834 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700835 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700836 getActionText(
837 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
838 declinePendingIntent)
839 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800840 }
841
842 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700843 LogUtil.d(
844 "StatusBarNotifier.addHangupAction",
845 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800846 PendingIntent hangupPendingIntent =
847 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
848 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700849 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 Erfanianccca3152017-02-22 16:32:36 -0800855 }
856
857 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700858 LogUtil.i(
859 "StatusBarNotifier.addVideoCallAction",
860 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800861 PendingIntent answerVideoPendingIntent =
862 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
863 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700864 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700865 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700866 getActionText(
867 R.string.notification_action_answer_video,
868 R.color.notification_action_answer_video),
869 answerVideoPendingIntent)
870 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800871 }
872
873 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700874 LogUtil.i(
875 "StatusBarNotifier.addAcceptUpgradeRequestAction",
876 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 PendingIntent acceptVideoPendingIntent =
878 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
879 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700880 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700881 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700882 getActionText(
883 R.string.notification_action_accept, R.color.notification_action_accept),
884 acceptVideoPendingIntent)
885 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800886 }
887
888 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700889 LogUtil.i(
890 "StatusBarNotifier.addDismissUpgradeRequestAction",
891 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800892 PendingIntent declineVideoPendingIntent =
893 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
894 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700895 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700896 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700897 getActionText(
898 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
899 declineVideoPendingIntent)
900 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800901 }
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 Erfaniand5e47f62017-03-15 14:41:07 -0700911 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800912 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 Erfaniand5e47f62017-03-15 14:41:07 -0700944 LogUtil.i(
945 "StatusBarNotifier.configureFullScreenIntent",
946 "updateInCallNotification: call-waiting! force relaunch...");
Eric Erfanianccca3152017-02-22 16:32:36 -0800947 // 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 Erfaniand5e47f62017-03-15 14:41:07 -0700957 builder.setOnlyAlertOnce(true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800958
959 return builder;
960 }
961
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700962 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 Intent intent =
964 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700965 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800966
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 Erfaniand5e47f62017-03-15 14:41:07 -07001032 public void onDialerCallSessionModificationStateChange() {
1033 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001034 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 cleanup();
1036 updateNotification(CallList.getInstance());
1037 }
1038 }
1039 }
1040}