blob: a6a81c6efea73114a45c259768e33e9dafda507e [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;
122
Eric Erfaniand8046e52017-04-06 09:41:50 -0700123 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800124 Objects.requireNonNull(context);
125 mContext = context;
126 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
127 mContactInfoCache = contactInfoCache;
128 mNotificationManager = context.getSystemService(NotificationManager.class);
129 mDialerRingtoneManager =
130 new DialerRingtoneManager(
131 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
132 CallList.getInstance());
133 mCurrentNotification = NOTIFICATION_NONE;
134 }
135
136 /**
137 * Should only be called from a irrecoverable state where it is necessary to dismiss all
138 * notifications.
139 */
140 static void clearAllCallNotifications(Context backupContext) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700141 LogUtil.i(
142 "StatusBarNotifier.clearAllCallNotifications",
143 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800144
145 NotificationManager notificationManager =
146 backupContext.getSystemService(NotificationManager.class);
147 notificationManager.cancel(NOTIFICATION_IN_CALL);
148 notificationManager.cancel(NOTIFICATION_INCOMING_CALL);
149 }
150
151 private static int getWorkStringFromPersonalString(int resId) {
152 if (resId == R.string.notification_ongoing_call) {
153 return R.string.notification_ongoing_work_call;
154 } else if (resId == R.string.notification_ongoing_call_wifi) {
155 return R.string.notification_ongoing_work_call_wifi;
156 } else if (resId == R.string.notification_incoming_call_wifi) {
157 return R.string.notification_incoming_work_call_wifi;
158 } else if (resId == R.string.notification_incoming_call) {
159 return R.string.notification_incoming_work_call;
160 } else {
161 return resId;
162 }
163 }
164
165 /**
166 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
167 * context.
168 */
169 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
170 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
171 return PendingIntent.getBroadcast(context, 0, intent, 0);
172 }
173
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700174 private static void setColorized(@NonNull Builder builder) {
175 if (BuildCompat.isAtLeastO()) {
176 builder.setColorized(true);
177 }
178 }
179
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
181 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700182 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800183 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700184 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800185 updateNotification(callList);
186 }
187
Eric Erfaniand8046e52017-04-06 09:41:50 -0700188 @Override
189 public void onEnrichedCallStateChanged() {
190 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
191 updateNotification(CallList.getInstance());
192 }
193
Eric Erfanianccca3152017-02-22 16:32:36 -0800194 /**
195 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
196 * to a new incoming call.
197 *
198 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
199 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
200 * foreground activity is marked as "immersive".
201 *
202 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
203 * ringing connection" event from the telephony layer.)
204 *
205 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
206 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
207 * we'll simply update or cancel the in-call notification based on the current phone state.
208 *
209 * @see #updateInCallNotification(CallList)
210 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700211 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700212 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800213 updateInCallNotification(callList);
214 }
215
216 /**
217 * Take down the in-call notification.
218 *
219 * @see #updateInCallNotification(CallList)
220 */
221 private void cancelNotification() {
222 if (mStatusBarCallListener != null) {
223 setStatusBarCallListener(null);
224 }
225 if (mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700226 LogUtil.i("StatusBarNotifier.cancelNotification", "cancel");
Eric Erfanianccca3152017-02-22 16:32:36 -0800227 mNotificationManager.cancel(mCurrentNotification);
228 }
229 mCurrentNotification = NOTIFICATION_NONE;
230 }
231
232 /**
233 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
234 * status bar notification based on the current telephony state, or cancels the notification if
235 * the phone is totally idle.
236 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700237 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800238 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700239 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800240
241 final DialerCall call = getCallToShow(callList);
242
243 if (call != null) {
244 showNotification(callList, call);
245 } else {
246 cancelNotification();
247 }
248 }
249
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700250 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800251 private void showNotification(final CallList callList, final DialerCall call) {
252 final boolean isIncoming =
253 (call.getState() == DialerCall.State.INCOMING
254 || call.getState() == DialerCall.State.CALL_WAITING);
255 setStatusBarCallListener(new StatusBarCallListener(call));
256
257 // we make a call to the contact info cache to query for supplemental data to what the
258 // call provides. This includes the contact name and photo.
259 // This callback will always get called immediately and synchronously with whatever data
260 // it has available, and may make a subsequent call later (same thread) if it had to
261 // call into the contacts provider for more data.
262 mContactInfoCache.findInfo(
263 call,
264 isIncoming,
265 new ContactInfoCacheCallback() {
266 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700267 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800268 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
269 DialerCall call = callList.getCallById(callId);
270 if (call != null) {
271 call.getLogState().contactLookupResult = entry.contactLookupResult;
272 buildAndSendNotification(callList, call, entry);
273 }
274 }
275
276 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700277 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800278 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
279 DialerCall call = callList.getCallById(callId);
280 if (call != null) {
281 buildAndSendNotification(callList, call, entry);
282 }
283 }
284 });
285 }
286
287 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700288 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800289 private void buildAndSendNotification(
290 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
291 // This can get called to update an existing notification after contact information has come
292 // back. However, it can happen much later. Before we continue, we need to make sure that
293 // the call being passed in is still the one we want to show in the notification.
294 final DialerCall call = getCallToShow(callList);
295 if (call == null || !call.getId().equals(originalCall.getId())) {
296 return;
297 }
298
299 final int callState = call.getState();
300
301 // Check if data has changed; if nothing is different, don't issue another notification.
302 final int iconResId = getIconToDisplay(call);
303 Bitmap largeIcon = getLargeIconToDisplay(contactInfo, call);
304 final String content = getContentString(call, contactInfo.userType);
305 final String contentTitle = getContentTitle(contactInfo, call);
306
307 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700308 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700309 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800310 final int notificationType;
311 if (callState == DialerCall.State.INCOMING
312 || callState == DialerCall.State.CALL_WAITING
313 || isVideoUpgradeRequest) {
314 notificationType = NOTIFICATION_INCOMING_CALL;
315 } else {
316 notificationType = NOTIFICATION_IN_CALL;
317 }
318
319 if (!checkForChangeAndSaveData(
320 iconResId,
321 content,
322 largeIcon,
323 contentTitle,
324 callState,
325 notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700326 contactInfo.contactRingtoneUri)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800327 return;
328 }
329
330 if (largeIcon != null) {
331 largeIcon = getRoundedIcon(largeIcon);
332 }
333
334 // This builder is used for the notification shown when the device is locked and the user
335 // has set their notification settings to 'hide sensitive content'
336 // {@see Notification.Builder#setPublicVersion}.
337 Notification.Builder publicBuilder = new Notification.Builder(mContext);
338 publicBuilder
339 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700340 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800341 // Hide work call state for the lock screen notification
342 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700343 setColorized(publicBuilder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800344 setNotificationWhen(call, callState, publicBuilder);
345
346 // Builder for the notification shown when the device is unlocked or the user has set their
347 // notification settings to 'show all notification content'.
348 final Notification.Builder builder = getNotificationBuilder();
349 builder.setPublicVersion(publicBuilder.build());
350
351 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700352 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800353
354 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700355 PhoneAccountHandle accountHandle = call.getAccountHandle();
356 if (accountHandle == null) {
357 accountHandle = getAnyPhoneAccount();
358 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800359 if (notificationType == NOTIFICATION_INCOMING_CALL) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700360 NotificationChannelManager.applyChannel(
361 builder, mContext, Channel.INCOMING_CALL, accountHandle);
Eric Erfanian8369df02017-05-03 10:27:13 -0700362 configureFullScreenIntent(
363 builder, createLaunchPendingIntent(true /* isFullScreen */), callList, call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700364 // Set the notification category and bump the priority for incoming calls
Eric Erfanianccca3152017-02-22 16:32:36 -0800365 builder.setCategory(Notification.CATEGORY_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700366 builder.setPriority(Notification.PRIORITY_MAX);
367 } else {
368 NotificationChannelManager.applyChannel(
369 builder, mContext, Channel.ONGOING_CALL, accountHandle);
Eric Erfanianccca3152017-02-22 16:32:36 -0800370 }
371
372 // Set the content
373 builder.setContentText(content);
374 builder.setSmallIcon(iconResId);
375 builder.setContentTitle(contentTitle);
376 builder.setLargeIcon(largeIcon);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700377 builder.setColor(
378 mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()));
379 setColorized(builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800380
381 if (isVideoUpgradeRequest) {
382 builder.setUsesChronometer(false);
383 addDismissUpgradeRequestAction(builder);
384 addAcceptUpgradeRequestAction(builder);
385 } else {
386 createIncomingCallNotification(call, callState, builder);
387 }
388
389 addPersonReference(builder, contactInfo, call);
390
391 // Fire off the notification
392 Notification notification = builder.build();
393
394 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
395 notification.flags |= Notification.FLAG_INSISTENT;
396 notification.sound = contactInfo.contactRingtoneUri;
397 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
398 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
399 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
400 notification.audioAttributes = audioAttributes.build();
401 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
402 notification.vibrate = VIBRATE_PATTERN;
403 }
404 }
405 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700406 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800407 mDialerRingtoneManager.playCallWaitingTone();
408 }
409 if (mCurrentNotification != notificationType && mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700410 LogUtil.i(
411 "StatusBarNotifier.buildAndSendNotification",
412 "previous notification already showing - cancelling " + mCurrentNotification);
Eric Erfanianccca3152017-02-22 16:32:36 -0800413 mNotificationManager.cancel(mCurrentNotification);
414 }
415
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700416 LogUtil.i(
417 "StatusBarNotifier.buildAndSendNotification",
418 "displaying notification for " + notificationType);
419
Eric Erfanianccca3152017-02-22 16:32:36 -0800420 try {
421 mNotificationManager.notify(notificationType, notification);
422 } catch (RuntimeException e) {
423 // TODO(b/34744003): Move the memory stats into silent feedback PSD.
424 ActivityManager activityManager = mContext.getSystemService(ActivityManager.class);
425 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
426 activityManager.getMemoryInfo(memoryInfo);
427 throw new RuntimeException(
428 String.format(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700429 Locale.US,
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)",
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700431 contactInfo.photoType,
432 memoryInfo.lowMemory,
433 memoryInfo.availMem),
Eric Erfanianccca3152017-02-22 16:32:36 -0800434 e);
435 }
436 call.getLatencyReport().onNotificationShown();
437 mCurrentNotification = notificationType;
438 }
439
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700440 @Nullable
441 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
442 private PhoneAccountHandle getAnyPhoneAccount() {
443 PhoneAccountHandle accountHandle;
444 TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
445 accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
446 if (accountHandle == null) {
447 List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
448 if (!accountHandles.isEmpty()) {
449 accountHandle = accountHandles.get(0);
450 }
451 }
452 return accountHandle;
453 }
454
Eric Erfanianccca3152017-02-22 16:32:36 -0800455 private void createIncomingCallNotification(
456 DialerCall call, int state, Notification.Builder builder) {
457 setNotificationWhen(call, state, builder);
458
459 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
460 if (state == DialerCall.State.ACTIVE
461 || state == DialerCall.State.ONHOLD
462 || DialerCall.State.isDialing(state)) {
463 addHangupAction(builder);
464 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
465 addDismissAction(builder);
466 if (call.isVideoCall()) {
467 addVideoCallAction(builder);
468 } else {
469 addAnswerAction(builder);
470 }
471 }
472 }
473
474 /**
475 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
476 * duration of the call. For all other states, the notification will automatically show the time
477 * at which the notification was created.
478 */
479 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
480 if (state == DialerCall.State.ACTIVE) {
481 builder.setUsesChronometer(true);
482 builder.setWhen(call.getConnectTimeMillis());
483 } else {
484 builder.setUsesChronometer(false);
485 }
486 }
487
488 /**
489 * Checks the new notification data and compares it against any notification that we are already
490 * displaying. If the data is exactly the same, we return false so that we do not issue a new
491 * notification for the exact same data.
492 */
493 private boolean checkForChangeAndSaveData(
494 int icon,
495 String content,
496 Bitmap largeIcon,
497 String contentTitle,
498 int state,
499 int notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700500 Uri ringtone) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800501
502 // The two are different:
503 // if new title is not null, it should be different from saved version OR
504 // if new title is null, the saved version should not be null
505 final boolean contentTitleChanged =
506 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
507 || (contentTitle == null && mSavedContentTitle != null);
508
Eric Erfanian8369df02017-05-03 10:27:13 -0700509 boolean largeIconChanged =
510 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
511
Eric Erfanianccca3152017-02-22 16:32:36 -0800512 // any change means we are definitely updating
513 boolean retval =
514 (mSavedIcon != icon)
515 || !Objects.equals(mSavedContent, content)
516 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700517 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800518 || contentTitleChanged
Eric Erfanian8369df02017-05-03 10:27:13 -0700519 || !Objects.equals(mRingtone, ringtone);
Eric Erfanianccca3152017-02-22 16:32:36 -0800520
521 // If we aren't showing a notification right now or the notification type is changing,
522 // definitely do an update.
523 if (mCurrentNotification != notificationType) {
524 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700525 LogUtil.d(
526 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 }
528 retval = true;
529 }
530
531 mSavedIcon = icon;
532 mSavedContent = content;
533 mCallState = state;
534 mSavedLargeIcon = largeIcon;
535 mSavedContentTitle = contentTitle;
536 mRingtone = ringtone;
537
538 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700539 LogUtil.d(
540 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800541 }
542
543 return retval;
544 }
545
546 /** Returns the main string to use in the notification. */
547 @VisibleForTesting
548 @Nullable
549 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
550 if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) {
551 return mContext.getResources().getString(R.string.conference_call_name);
552 }
553
554 String preferredName =
555 ContactDisplayUtils.getPreferredDisplayName(
556 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
557 if (TextUtils.isEmpty(preferredName)) {
558 return TextUtils.isEmpty(contactInfo.number)
559 ? null
560 : BidiFormatter.getInstance()
561 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
562 }
563 return preferredName;
564 }
565
566 private void addPersonReference(
567 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
568 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
569 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
570 // NotificationManager using it.
571 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
572 builder.addPerson(contactInfo.lookupUri.toString());
573 } else if (!TextUtils.isEmpty(call.getNumber())) {
574 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
575 }
576 }
577
578 /** Gets a large icon from the contact info object to display in the notification. */
579 private Bitmap getLargeIconToDisplay(ContactCacheEntry contactInfo, DialerCall call) {
580 Bitmap largeIcon = null;
581 if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) {
582 largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.img_conference);
583 }
584 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
585 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
586 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700587 if (contactInfo.photo == null) {
588 int width =
589 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
590 int height =
591 (int)
592 mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
593 int contactType = LetterTileDrawable.TYPE_DEFAULT;
594 LetterTileDrawable lettertile = new LetterTileDrawable(mContext.getResources());
595
596 // TODO: Deduplicate across Dialer. b/36195917
597 if (CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
598 contactType = LetterTileDrawable.TYPE_VOICEMAIL;
599 } else if (contactInfo.isBusiness) {
600 contactType = LetterTileDrawable.TYPE_BUSINESS;
601 } else if (call.getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED) {
602 contactType = LetterTileDrawable.TYPE_GENERIC_AVATAR;
603 }
604 lettertile.setCanonicalDialerLetterTileDetails(
605 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
606 contactInfo.lookupKey,
607 LetterTileDrawable.SHAPE_CIRCLE,
608 contactType);
609 largeIcon = lettertile.getBitmap(width, height);
610 }
611
Eric Erfanianccca3152017-02-22 16:32:36 -0800612 if (call.isSpam()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700613 Drawable drawable =
614 mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800615 largeIcon = DrawableConverter.drawableToBitmap(drawable);
616 }
617 return largeIcon;
618 }
619
620 private Bitmap getRoundedIcon(Bitmap bitmap) {
621 if (bitmap == null) {
622 return null;
623 }
624 final int height =
625 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
626 final int width =
627 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
628 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
629 }
630
631 /**
632 * Returns the appropriate icon res Id to display based on the call for which we want to display
633 * information.
634 */
635 private int getIconToDisplay(DialerCall call) {
636 // Even if both lines are in use, we only show a single item in
637 // the expanded Notifications UI. It's labeled "Ongoing call"
638 // (or "On hold" if there's only one call, and it's on hold.)
639 // Also, we don't have room to display caller-id info from two
640 // different calls. So if both lines are in use, display info
641 // from the foreground call. And if there's a ringing call,
642 // display that regardless of the state of the other calls.
643 if (call.getState() == DialerCall.State.ONHOLD) {
644 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700645 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700646 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700647 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700648 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
649 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
650 // Normally when a call is ongoing the status bar displays an icon of a phone with animated
651 // lines. This is a helpful hint for users so they know how to get back to the call.
652 // For Sprint HD calls, we replace this icon with an icon of a phone with a HD badge.
653 // This is a carrier requirement.
654 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800655 }
656 return R.anim.on_going_call;
657 }
658
659 /** Returns the message to use with the notification. */
660 private String getContentString(DialerCall call, @UserType long userType) {
661 boolean isIncomingOrWaiting =
662 call.getState() == DialerCall.State.INCOMING
663 || call.getState() == DialerCall.State.CALL_WAITING;
664
665 if (isIncomingOrWaiting
666 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
667
668 if (!TextUtils.isEmpty(call.getChildNumber())) {
669 return mContext.getString(R.string.child_number, call.getChildNumber());
670 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
671 return call.getCallSubject();
672 }
673 }
674
675 int resId = R.string.notification_ongoing_call;
676 if (call.hasProperty(Details.PROPERTY_WIFI)) {
677 resId = R.string.notification_ongoing_call_wifi;
678 }
679
680 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700681 EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager();
682 Session session = null;
683 if (call.getNumber() != null) {
684 session = manager.getSession(call.getUniqueCallId(), call.getNumber());
685 }
686
687 if (call.isSpam()) {
688 resId = R.string.notification_incoming_spam_call;
689 } else if (session != null) {
690 resId = getECIncomingCallText(session);
691 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800692 resId = R.string.notification_incoming_call_wifi;
693 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700694 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800695 }
696 } else if (call.getState() == DialerCall.State.ONHOLD) {
697 resId = R.string.notification_on_hold;
698 } else if (DialerCall.State.isDialing(call.getState())) {
699 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700700 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700701 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 resId = R.string.notification_requesting_video_call;
703 }
704
705 // Is the call placed through work connection service.
706 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
707 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
708 resId = getWorkStringFromPersonalString(resId);
709 }
710
711 return mContext.getString(resId);
712 }
713
Eric Erfaniand8046e52017-04-06 09:41:50 -0700714 private int getECIncomingCallText(Session session) {
715 int resId;
716 MultimediaData data = session.getMultimediaData();
717 boolean hasImage = data.hasImageData();
718 boolean hasSubject = !TextUtils.isEmpty(data.getText());
719 boolean hasMap = data.getLocation() != null;
720 if (data.isImportant()) {
721 if (hasMap) {
722 if (hasImage) {
723 if (hasSubject) {
724 resId = R.string.important_notification_incoming_call_with_photo_message_location;
725 } else {
726 resId = R.string.important_notification_incoming_call_with_photo_location;
727 }
728 } else if (hasSubject) {
729 resId = R.string.important_notification_incoming_call_with_message_location;
730 } else {
731 resId = R.string.important_notification_incoming_call_with_location;
732 }
733 } else if (hasImage) {
734 if (hasSubject) {
735 resId = R.string.important_notification_incoming_call_with_photo_message;
736 } else {
737 resId = R.string.important_notification_incoming_call_with_photo;
738 }
739 } else {
740 resId = R.string.important_notification_incoming_call_with_message;
741 }
742 if (mContext.getString(resId).length() > 50) {
743 resId = R.string.important_notification_incoming_call_attachments;
744 }
745 } else {
746 if (hasMap) {
747 if (hasImage) {
748 if (hasSubject) {
749 resId = R.string.notification_incoming_call_with_photo_message_location;
750 } else {
751 resId = R.string.notification_incoming_call_with_photo_location;
752 }
753 } else if (hasSubject) {
754 resId = R.string.notification_incoming_call_with_message_location;
755 } else {
756 resId = R.string.notification_incoming_call_with_location;
757 }
758 } else if (hasImage) {
759 if (hasSubject) {
760 resId = R.string.notification_incoming_call_with_photo_message;
761 } else {
762 resId = R.string.notification_incoming_call_with_photo;
763 }
764 } else {
765 resId = R.string.notification_incoming_call_with_message;
766 }
767 }
768 if (mContext.getString(resId).length() > 50) {
769 resId = R.string.notification_incoming_call_attachments;
770 }
771 return resId;
772 }
773
Eric Erfanianccca3152017-02-22 16:32:36 -0800774 /** Gets the most relevant call to display in the notification. */
775 private DialerCall getCallToShow(CallList callList) {
776 if (callList == null) {
777 return null;
778 }
779 DialerCall call = callList.getIncomingCall();
780 if (call == null) {
781 call = callList.getOutgoingCall();
782 }
783 if (call == null) {
784 call = callList.getVideoUpgradeRequestCall();
785 }
786 if (call == null) {
787 call = callList.getActiveOrBackgroundCall();
788 }
789 return call;
790 }
791
792 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
793 Spannable spannable = new SpannableString(mContext.getText(stringRes));
794 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
795 // This will only work for cases where the Notification.Builder has a fullscreen intent set
796 // Notification.Builder that does not have a full screen intent will take the color of the
797 // app and the following leads to a no-op.
798 spannable.setSpan(
799 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
800 }
801 return spannable;
802 }
803
804 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700805 LogUtil.d(
806 "StatusBarNotifier.addAnswerAction",
807 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800808 PendingIntent answerVoicePendingIntent =
809 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700810 // We put animation resources in "anim" folder instead of "drawable", which causes Android
811 // Studio to complain.
812 // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc?
813 //noinspection ResourceType
Eric Erfanianccca3152017-02-22 16:32:36 -0800814 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700815 new Notification.Action.Builder(
816 Icon.createWithResource(mContext, R.anim.on_going_call),
817 getActionText(
818 R.string.notification_action_answer, R.color.notification_action_accept),
819 answerVoicePendingIntent)
820 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800821 }
822
823 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700824 LogUtil.d(
825 "StatusBarNotifier.addDismissAction",
826 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800827 PendingIntent declinePendingIntent =
828 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
829 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700830 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700831 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700832 getActionText(
833 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
834 declinePendingIntent)
835 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800836 }
837
838 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839 LogUtil.d(
840 "StatusBarNotifier.addHangupAction",
841 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800842 PendingIntent hangupPendingIntent =
843 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
844 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700845 new Notification.Action.Builder(
846 Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp),
847 getActionText(
848 R.string.notification_action_end_call, R.color.notification_action_end_call),
849 hangupPendingIntent)
850 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800851 }
852
853 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700854 LogUtil.i(
855 "StatusBarNotifier.addVideoCallAction",
856 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 PendingIntent answerVideoPendingIntent =
858 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
859 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700860 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700861 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700862 getActionText(
863 R.string.notification_action_answer_video,
864 R.color.notification_action_answer_video),
865 answerVideoPendingIntent)
866 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800867 }
868
869 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700870 LogUtil.i(
871 "StatusBarNotifier.addAcceptUpgradeRequestAction",
872 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 PendingIntent acceptVideoPendingIntent =
874 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
875 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700876 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700877 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700878 getActionText(
879 R.string.notification_action_accept, R.color.notification_action_accept),
880 acceptVideoPendingIntent)
881 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800882 }
883
884 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700885 LogUtil.i(
886 "StatusBarNotifier.addDismissUpgradeRequestAction",
887 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800888 PendingIntent declineVideoPendingIntent =
889 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
890 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700891 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700892 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700893 getActionText(
894 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
895 declineVideoPendingIntent)
896 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 }
898
899 /** Adds fullscreen intent to the builder. */
900 private void configureFullScreenIntent(
901 Notification.Builder builder, PendingIntent intent, CallList callList, DialerCall call) {
902 // Ok, we actually want to launch the incoming call
903 // UI at this point (in addition to simply posting a notification
904 // to the status bar). Setting fullScreenIntent will cause
905 // the InCallScreen to be launched immediately *unless* the
906 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700907 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800908 builder.setFullScreenIntent(intent, true);
909
910 // Ugly hack alert:
911 //
912 // The NotificationManager has the (undocumented) behavior
913 // that it will *ignore* the fullScreenIntent field if you
914 // post a new Notification that matches the ID of one that's
915 // already active. Unfortunately this is exactly what happens
916 // when you get an incoming call-waiting call: the
917 // "ongoing call" notification is already visible, so the
918 // InCallScreen won't get launched in this case!
919 // (The result: if you bail out of the in-call UI while on a
920 // call and then get a call-waiting call, the incoming call UI
921 // won't come up automatically.)
922 //
923 // The workaround is to just notice this exact case (this is a
924 // call-waiting call *and* the InCallScreen is not in the
925 // foreground) and manually cancel the in-call notification
926 // before (re)posting it.
927 //
928 // TODO: there should be a cleaner way of avoiding this
929 // problem (see discussion in bug 3184149.)
930
931 // If a call is onhold during an incoming call, the call actually comes in as
932 // INCOMING. For that case *and* traditional call-waiting, we want to
933 // cancel the notification.
934 boolean isCallWaiting =
935 (call.getState() == DialerCall.State.CALL_WAITING
936 || (call.getState() == DialerCall.State.INCOMING
937 && callList.getBackgroundCall() != null));
938
939 if (isCallWaiting) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700940 LogUtil.i(
941 "StatusBarNotifier.configureFullScreenIntent",
942 "updateInCallNotification: call-waiting! force relaunch...");
Eric Erfanianccca3152017-02-22 16:32:36 -0800943 // Cancel the IN_CALL_NOTIFICATION immediately before
944 // (re)posting it; this seems to force the
945 // NotificationManager to launch the fullScreenIntent.
946 mNotificationManager.cancel(NOTIFICATION_IN_CALL);
947 }
948 }
949
950 private Notification.Builder getNotificationBuilder() {
951 final Notification.Builder builder = new Notification.Builder(mContext);
952 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700953 builder.setOnlyAlertOnce(true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800954
955 return builder;
956 }
957
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700958 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 Intent intent =
960 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700961 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800962
963 int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
964 if (isFullScreen) {
965 // Use a unique request code so that the pending intent isn't clobbered by the
966 // non-full screen pending intent.
967 requestCode = PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
968 }
969
970 // PendingIntent that can be used to launch the InCallActivity. The
971 // system fires off this intent if the user pulls down the windowshade
972 // and clicks the notification's expanded view. It's also used to
973 // launch the InCallActivity immediately when when there's an incoming
974 // call (see the "fullScreenIntent" field below).
975 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
976 }
977
978 private void setStatusBarCallListener(StatusBarCallListener listener) {
979 if (mStatusBarCallListener != null) {
980 mStatusBarCallListener.cleanup();
981 }
982 mStatusBarCallListener = listener;
983 }
984
985 private class StatusBarCallListener implements DialerCallListener {
986
987 private DialerCall mDialerCall;
988
989 StatusBarCallListener(DialerCall dialerCall) {
990 mDialerCall = dialerCall;
991 mDialerCall.addListener(this);
992 }
993
994 void cleanup() {
995 mDialerCall.removeListener(this);
996 }
997
998 @Override
999 public void onDialerCallDisconnect() {}
1000
1001 @Override
1002 public void onDialerCallUpdate() {
1003 if (CallList.getInstance().getIncomingCall() == null) {
1004 mDialerRingtoneManager.stopCallWaitingTone();
1005 }
1006 }
1007
1008 @Override
1009 public void onDialerCallChildNumberChange() {}
1010
1011 @Override
1012 public void onDialerCallLastForwardedNumberChange() {}
1013
1014 @Override
1015 public void onDialerCallUpgradeToVideo() {}
1016
1017 @Override
1018 public void onWiFiToLteHandover() {}
1019
1020 @Override
1021 public void onHandoverToWifiFailure() {}
1022
1023 /**
1024 * Responds to changes in the session modification state for the call by dismissing the status
1025 * bar notification as required.
1026 */
1027 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001028 public void onDialerCallSessionModificationStateChange() {
1029 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001030 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001031 cleanup();
1032 updateNotification(CallList.getInstance());
1033 }
1034 }
1035 }
1036}