blob: 4fb402caa304461ef6ffc4c50513a4f91888e2c5 [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 Erfanian10b34a52017-05-04 08:23:17 -0700100 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800101 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700102 private static final int NOTIFICATION_INCOMING_CALL = 2;
103 // Notification for incoming calls in the case where there is already an active call.
104 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
105 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800106
107 private static final int PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN = 0;
108 private static final int PENDING_INTENT_REQUEST_CODE_FULL_SCREEN = 1;
109
110 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
111
112 private final Context mContext;
113 private final ContactInfoCache mContactInfoCache;
114 private final NotificationManager mNotificationManager;
115 private final DialerRingtoneManager mDialerRingtoneManager;
116 @Nullable private ContactsPreferences mContactsPreferences;
117 private int mCurrentNotification = NOTIFICATION_NONE;
118 private int mCallState = DialerCall.State.INVALID;
119 private int mSavedIcon = 0;
120 private String mSavedContent = null;
121 private Bitmap mSavedLargeIcon;
122 private String mSavedContentTitle;
123 private Uri mRingtone;
124 private StatusBarCallListener mStatusBarCallListener;
125
Eric Erfaniand8046e52017-04-06 09:41:50 -0700126 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 Objects.requireNonNull(context);
128 mContext = context;
129 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
130 mContactInfoCache = contactInfoCache;
131 mNotificationManager = context.getSystemService(NotificationManager.class);
132 mDialerRingtoneManager =
133 new DialerRingtoneManager(
134 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
135 CallList.getInstance());
136 mCurrentNotification = NOTIFICATION_NONE;
137 }
138
139 /**
140 * Should only be called from a irrecoverable state where it is necessary to dismiss all
141 * notifications.
142 */
143 static void clearAllCallNotifications(Context backupContext) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700144 LogUtil.i(
145 "StatusBarNotifier.clearAllCallNotifications",
146 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800147
148 NotificationManager notificationManager =
149 backupContext.getSystemService(NotificationManager.class);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700150 notificationManager.cancel(R.id.notification_ongoing_call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800151 }
152
153 private static int getWorkStringFromPersonalString(int resId) {
154 if (resId == R.string.notification_ongoing_call) {
155 return R.string.notification_ongoing_work_call;
156 } else if (resId == R.string.notification_ongoing_call_wifi) {
157 return R.string.notification_ongoing_work_call_wifi;
158 } else if (resId == R.string.notification_incoming_call_wifi) {
159 return R.string.notification_incoming_work_call_wifi;
160 } else if (resId == R.string.notification_incoming_call) {
161 return R.string.notification_incoming_work_call;
162 } else {
163 return resId;
164 }
165 }
166
167 /**
168 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
169 * context.
170 */
171 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
172 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
173 return PendingIntent.getBroadcast(context, 0, intent, 0);
174 }
175
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700176 private static void setColorized(@NonNull Builder builder) {
177 if (BuildCompat.isAtLeastO()) {
178 builder.setColorized(true);
179 }
180 }
181
Eric Erfanianccca3152017-02-22 16:32:36 -0800182 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
183 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700184 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800185 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700186 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 updateNotification(callList);
188 }
189
Eric Erfaniand8046e52017-04-06 09:41:50 -0700190 @Override
191 public void onEnrichedCallStateChanged() {
192 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
193 updateNotification(CallList.getInstance());
194 }
195
Eric Erfanianccca3152017-02-22 16:32:36 -0800196 /**
197 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
198 * to a new incoming call.
199 *
200 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
201 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
202 * foreground activity is marked as "immersive".
203 *
204 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
205 * ringing connection" event from the telephony layer.)
206 *
207 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
208 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
209 * we'll simply update or cancel the in-call notification based on the current phone state.
210 *
211 * @see #updateInCallNotification(CallList)
212 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700213 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700214 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800215 updateInCallNotification(callList);
216 }
217
218 /**
219 * Take down the in-call notification.
220 *
221 * @see #updateInCallNotification(CallList)
222 */
223 private void cancelNotification() {
224 if (mStatusBarCallListener != null) {
225 setStatusBarCallListener(null);
226 }
227 if (mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700228 LogUtil.i("StatusBarNotifier.cancelNotification", "cancel");
Eric Erfanian10b34a52017-05-04 08:23:17 -0700229 mNotificationManager.cancel(R.id.notification_ongoing_call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800230 }
231 mCurrentNotification = NOTIFICATION_NONE;
232 }
233
234 /**
235 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
236 * status bar notification based on the current telephony state, or cancels the notification if
237 * the phone is totally idle.
238 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700239 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800240 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700241 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800242
243 final DialerCall call = getCallToShow(callList);
244
245 if (call != null) {
246 showNotification(callList, call);
247 } else {
248 cancelNotification();
249 }
250 }
251
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700252 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800253 private void showNotification(final CallList callList, final DialerCall call) {
254 final boolean isIncoming =
255 (call.getState() == DialerCall.State.INCOMING
256 || call.getState() == DialerCall.State.CALL_WAITING);
257 setStatusBarCallListener(new StatusBarCallListener(call));
258
259 // we make a call to the contact info cache to query for supplemental data to what the
260 // call provides. This includes the contact name and photo.
261 // This callback will always get called immediately and synchronously with whatever data
262 // it has available, and may make a subsequent call later (same thread) if it had to
263 // call into the contacts provider for more data.
264 mContactInfoCache.findInfo(
265 call,
266 isIncoming,
267 new ContactInfoCacheCallback() {
268 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700269 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800270 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
271 DialerCall call = callList.getCallById(callId);
272 if (call != null) {
273 call.getLogState().contactLookupResult = entry.contactLookupResult;
274 buildAndSendNotification(callList, call, entry);
275 }
276 }
277
278 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700279 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800280 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
281 DialerCall call = callList.getCallById(callId);
282 if (call != null) {
283 buildAndSendNotification(callList, call, entry);
284 }
285 }
286 });
287 }
288
289 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700290 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800291 private void buildAndSendNotification(
292 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
293 // This can get called to update an existing notification after contact information has come
294 // back. However, it can happen much later. Before we continue, we need to make sure that
295 // the call being passed in is still the one we want to show in the notification.
296 final DialerCall call = getCallToShow(callList);
297 if (call == null || !call.getId().equals(originalCall.getId())) {
298 return;
299 }
300
301 final int callState = call.getState();
302
303 // Check if data has changed; if nothing is different, don't issue another notification.
304 final int iconResId = getIconToDisplay(call);
305 Bitmap largeIcon = getLargeIconToDisplay(contactInfo, call);
306 final String content = getContentString(call, contactInfo.userType);
307 final String contentTitle = getContentTitle(contactInfo, call);
308
309 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700310 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700311 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800312 final int notificationType;
313 if (callState == DialerCall.State.INCOMING
314 || callState == DialerCall.State.CALL_WAITING
315 || isVideoUpgradeRequest) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700316 boolean alreadyActive =
317 callList.getActiveOrBackgroundCall() != null
318 && InCallPresenter.getInstance().isShowingInCallUi();
319 notificationType =
320 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
Eric Erfanianccca3152017-02-22 16:32:36 -0800321 } else {
322 notificationType = NOTIFICATION_IN_CALL;
323 }
324
325 if (!checkForChangeAndSaveData(
326 iconResId,
327 content,
328 largeIcon,
329 contentTitle,
330 callState,
331 notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700332 contactInfo.contactRingtoneUri)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800333 return;
334 }
335
336 if (largeIcon != null) {
337 largeIcon = getRoundedIcon(largeIcon);
338 }
339
340 // This builder is used for the notification shown when the device is locked and the user
341 // has set their notification settings to 'hide sensitive content'
342 // {@see Notification.Builder#setPublicVersion}.
343 Notification.Builder publicBuilder = new Notification.Builder(mContext);
344 publicBuilder
345 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700346 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800347 // Hide work call state for the lock screen notification
348 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
349 setNotificationWhen(call, callState, publicBuilder);
350
351 // Builder for the notification shown when the device is unlocked or the user has set their
352 // notification settings to 'show all notification content'.
353 final Notification.Builder builder = getNotificationBuilder();
354 builder.setPublicVersion(publicBuilder.build());
355
356 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700357 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800358
359 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700360 PhoneAccountHandle accountHandle = call.getAccountHandle();
361 if (accountHandle == null) {
362 accountHandle = getAnyPhoneAccount();
363 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700364
365 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
366 switch (notificationType) {
367 case NOTIFICATION_INCOMING_CALL:
368 NotificationChannelManager.applyChannel(
369 builder, mContext, Channel.INCOMING_CALL, accountHandle);
370 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
371 // Set the notification category and bump the priority for incoming calls
372 builder.setCategory(Notification.CATEGORY_CALL);
373 // This will be ignored on O+ and handled by the channel
374 //noinspection deprecation
375 builder.setPriority(Notification.PRIORITY_MAX);
376 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
377 LogUtil.i(
378 "StatusBarNotifier.buildAndSendNotification",
379 "Canceling old notification so this one can be noisy");
380 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
381 // notification (if there is one) so the fullScreenIntent or HUN will show
382 mNotificationManager.cancel(R.id.notification_ongoing_call);
383 }
384 break;
385 case NOTIFICATION_INCOMING_CALL_QUIET:
386 NotificationChannelManager.applyChannel(
387 builder, mContext, Channel.ONGOING_CALL, accountHandle);
388 break;
389 case NOTIFICATION_IN_CALL:
390 setColorized(publicBuilder);
391 setColorized(builder);
392 NotificationChannelManager.applyChannel(
393 builder, mContext, Channel.ONGOING_CALL, accountHandle);
394 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800395 }
396
397 // Set the content
398 builder.setContentText(content);
399 builder.setSmallIcon(iconResId);
400 builder.setContentTitle(contentTitle);
401 builder.setLargeIcon(largeIcon);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700402 builder.setColor(
403 mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800404
405 if (isVideoUpgradeRequest) {
406 builder.setUsesChronometer(false);
407 addDismissUpgradeRequestAction(builder);
408 addAcceptUpgradeRequestAction(builder);
409 } else {
410 createIncomingCallNotification(call, callState, builder);
411 }
412
413 addPersonReference(builder, contactInfo, call);
414
415 // Fire off the notification
416 Notification notification = builder.build();
417
418 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
419 notification.flags |= Notification.FLAG_INSISTENT;
420 notification.sound = contactInfo.contactRingtoneUri;
421 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
422 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
423 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
424 notification.audioAttributes = audioAttributes.build();
425 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
426 notification.vibrate = VIBRATE_PATTERN;
427 }
428 }
429 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700430 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800431 mDialerRingtoneManager.playCallWaitingTone();
432 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800433
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700434 LogUtil.i(
435 "StatusBarNotifier.buildAndSendNotification",
436 "displaying notification for " + notificationType);
437
Eric Erfanianccca3152017-02-22 16:32:36 -0800438 try {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700439 mNotificationManager.notify(R.id.notification_ongoing_call, notification);
Eric Erfanianccca3152017-02-22 16:32:36 -0800440 } catch (RuntimeException e) {
441 // TODO(b/34744003): Move the memory stats into silent feedback PSD.
442 ActivityManager activityManager = mContext.getSystemService(ActivityManager.class);
443 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
444 activityManager.getMemoryInfo(memoryInfo);
445 throw new RuntimeException(
446 String.format(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700447 Locale.US,
Eric Erfanianccca3152017-02-22 16:32:36 -0800448 "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)",
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700449 contactInfo.photoType,
450 memoryInfo.lowMemory,
451 memoryInfo.availMem),
Eric Erfanianccca3152017-02-22 16:32:36 -0800452 e);
453 }
454 call.getLatencyReport().onNotificationShown();
455 mCurrentNotification = notificationType;
456 }
457
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700458 @Nullable
459 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
460 private PhoneAccountHandle getAnyPhoneAccount() {
461 PhoneAccountHandle accountHandle;
462 TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
463 accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
464 if (accountHandle == null) {
465 List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
466 if (!accountHandles.isEmpty()) {
467 accountHandle = accountHandles.get(0);
468 }
469 }
470 return accountHandle;
471 }
472
Eric Erfanianccca3152017-02-22 16:32:36 -0800473 private void createIncomingCallNotification(
474 DialerCall call, int state, Notification.Builder builder) {
475 setNotificationWhen(call, state, builder);
476
477 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
478 if (state == DialerCall.State.ACTIVE
479 || state == DialerCall.State.ONHOLD
480 || DialerCall.State.isDialing(state)) {
481 addHangupAction(builder);
482 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
483 addDismissAction(builder);
484 if (call.isVideoCall()) {
485 addVideoCallAction(builder);
486 } else {
487 addAnswerAction(builder);
488 }
489 }
490 }
491
492 /**
493 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
494 * duration of the call. For all other states, the notification will automatically show the time
495 * at which the notification was created.
496 */
497 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
498 if (state == DialerCall.State.ACTIVE) {
499 builder.setUsesChronometer(true);
500 builder.setWhen(call.getConnectTimeMillis());
501 } else {
502 builder.setUsesChronometer(false);
503 }
504 }
505
506 /**
507 * Checks the new notification data and compares it against any notification that we are already
508 * displaying. If the data is exactly the same, we return false so that we do not issue a new
509 * notification for the exact same data.
510 */
511 private boolean checkForChangeAndSaveData(
512 int icon,
513 String content,
514 Bitmap largeIcon,
515 String contentTitle,
516 int state,
517 int notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700518 Uri ringtone) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800519
520 // The two are different:
521 // if new title is not null, it should be different from saved version OR
522 // if new title is null, the saved version should not be null
523 final boolean contentTitleChanged =
524 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
525 || (contentTitle == null && mSavedContentTitle != null);
526
Eric Erfanian8369df02017-05-03 10:27:13 -0700527 boolean largeIconChanged =
528 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
529
Eric Erfanianccca3152017-02-22 16:32:36 -0800530 // any change means we are definitely updating
531 boolean retval =
532 (mSavedIcon != icon)
533 || !Objects.equals(mSavedContent, content)
534 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700535 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 || contentTitleChanged
Eric Erfanian8369df02017-05-03 10:27:13 -0700537 || !Objects.equals(mRingtone, ringtone);
Eric Erfanianccca3152017-02-22 16:32:36 -0800538
539 // If we aren't showing a notification right now or the notification type is changing,
540 // definitely do an update.
541 if (mCurrentNotification != notificationType) {
542 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700543 LogUtil.d(
544 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 }
546 retval = true;
547 }
548
549 mSavedIcon = icon;
550 mSavedContent = content;
551 mCallState = state;
552 mSavedLargeIcon = largeIcon;
553 mSavedContentTitle = contentTitle;
554 mRingtone = ringtone;
555
556 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700557 LogUtil.d(
558 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800559 }
560
561 return retval;
562 }
563
564 /** Returns the main string to use in the notification. */
565 @VisibleForTesting
566 @Nullable
567 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
568 if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) {
569 return mContext.getResources().getString(R.string.conference_call_name);
570 }
571
572 String preferredName =
573 ContactDisplayUtils.getPreferredDisplayName(
574 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
575 if (TextUtils.isEmpty(preferredName)) {
576 return TextUtils.isEmpty(contactInfo.number)
577 ? null
578 : BidiFormatter.getInstance()
579 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
580 }
581 return preferredName;
582 }
583
584 private void addPersonReference(
585 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
586 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
587 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
588 // NotificationManager using it.
589 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
590 builder.addPerson(contactInfo.lookupUri.toString());
591 } else if (!TextUtils.isEmpty(call.getNumber())) {
592 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
593 }
594 }
595
596 /** Gets a large icon from the contact info object to display in the notification. */
597 private Bitmap getLargeIconToDisplay(ContactCacheEntry contactInfo, DialerCall call) {
598 Bitmap largeIcon = null;
599 if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) {
600 largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.img_conference);
601 }
602 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
603 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
604 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700605 if (contactInfo.photo == null) {
606 int width =
607 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
608 int height =
609 (int)
610 mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
611 int contactType = LetterTileDrawable.TYPE_DEFAULT;
612 LetterTileDrawable lettertile = new LetterTileDrawable(mContext.getResources());
613
614 // TODO: Deduplicate across Dialer. b/36195917
615 if (CallerInfoUtils.isVoiceMailNumber(mContext, call)) {
616 contactType = LetterTileDrawable.TYPE_VOICEMAIL;
617 } else if (contactInfo.isBusiness) {
618 contactType = LetterTileDrawable.TYPE_BUSINESS;
619 } else if (call.getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED) {
620 contactType = LetterTileDrawable.TYPE_GENERIC_AVATAR;
621 }
622 lettertile.setCanonicalDialerLetterTileDetails(
623 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
624 contactInfo.lookupKey,
625 LetterTileDrawable.SHAPE_CIRCLE,
626 contactType);
627 largeIcon = lettertile.getBitmap(width, height);
628 }
629
Eric Erfanianccca3152017-02-22 16:32:36 -0800630 if (call.isSpam()) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700631 Drawable drawable =
632 mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800633 largeIcon = DrawableConverter.drawableToBitmap(drawable);
634 }
635 return largeIcon;
636 }
637
638 private Bitmap getRoundedIcon(Bitmap bitmap) {
639 if (bitmap == null) {
640 return null;
641 }
642 final int height =
643 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
644 final int width =
645 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
646 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
647 }
648
649 /**
650 * Returns the appropriate icon res Id to display based on the call for which we want to display
651 * information.
652 */
653 private int getIconToDisplay(DialerCall call) {
654 // Even if both lines are in use, we only show a single item in
655 // the expanded Notifications UI. It's labeled "Ongoing call"
656 // (or "On hold" if there's only one call, and it's on hold.)
657 // Also, we don't have room to display caller-id info from two
658 // different calls. So if both lines are in use, display info
659 // from the foreground call. And if there's a ringing call,
660 // display that regardless of the state of the other calls.
661 if (call.getState() == DialerCall.State.ONHOLD) {
662 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700663 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700664 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700665 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700666 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
667 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
668 // Normally when a call is ongoing the status bar displays an icon of a phone with animated
669 // lines. This is a helpful hint for users so they know how to get back to the call.
670 // For Sprint HD calls, we replace this icon with an icon of a phone with a HD badge.
671 // This is a carrier requirement.
672 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800673 }
674 return R.anim.on_going_call;
675 }
676
677 /** Returns the message to use with the notification. */
678 private String getContentString(DialerCall call, @UserType long userType) {
679 boolean isIncomingOrWaiting =
680 call.getState() == DialerCall.State.INCOMING
681 || call.getState() == DialerCall.State.CALL_WAITING;
682
683 if (isIncomingOrWaiting
684 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
685
686 if (!TextUtils.isEmpty(call.getChildNumber())) {
687 return mContext.getString(R.string.child_number, call.getChildNumber());
688 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
689 return call.getCallSubject();
690 }
691 }
692
693 int resId = R.string.notification_ongoing_call;
694 if (call.hasProperty(Details.PROPERTY_WIFI)) {
695 resId = R.string.notification_ongoing_call_wifi;
696 }
697
698 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700699 EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager();
700 Session session = null;
701 if (call.getNumber() != null) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700702 session =
703 manager.getSession(
704 call.getUniqueCallId(),
705 call.getNumber(),
706 manager.createIncomingCallComposerFilter());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700707 }
708
709 if (call.isSpam()) {
710 resId = R.string.notification_incoming_spam_call;
711 } else if (session != null) {
712 resId = getECIncomingCallText(session);
713 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 resId = R.string.notification_incoming_call_wifi;
715 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700716 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800717 }
718 } else if (call.getState() == DialerCall.State.ONHOLD) {
719 resId = R.string.notification_on_hold;
720 } else if (DialerCall.State.isDialing(call.getState())) {
721 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700722 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700723 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800724 resId = R.string.notification_requesting_video_call;
725 }
726
727 // Is the call placed through work connection service.
728 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
729 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
730 resId = getWorkStringFromPersonalString(resId);
731 }
732
733 return mContext.getString(resId);
734 }
735
Eric Erfaniand8046e52017-04-06 09:41:50 -0700736 private int getECIncomingCallText(Session session) {
737 int resId;
738 MultimediaData data = session.getMultimediaData();
739 boolean hasImage = data.hasImageData();
740 boolean hasSubject = !TextUtils.isEmpty(data.getText());
741 boolean hasMap = data.getLocation() != null;
742 if (data.isImportant()) {
743 if (hasMap) {
744 if (hasImage) {
745 if (hasSubject) {
746 resId = R.string.important_notification_incoming_call_with_photo_message_location;
747 } else {
748 resId = R.string.important_notification_incoming_call_with_photo_location;
749 }
750 } else if (hasSubject) {
751 resId = R.string.important_notification_incoming_call_with_message_location;
752 } else {
753 resId = R.string.important_notification_incoming_call_with_location;
754 }
755 } else if (hasImage) {
756 if (hasSubject) {
757 resId = R.string.important_notification_incoming_call_with_photo_message;
758 } else {
759 resId = R.string.important_notification_incoming_call_with_photo;
760 }
761 } else {
762 resId = R.string.important_notification_incoming_call_with_message;
763 }
764 if (mContext.getString(resId).length() > 50) {
765 resId = R.string.important_notification_incoming_call_attachments;
766 }
767 } else {
768 if (hasMap) {
769 if (hasImage) {
770 if (hasSubject) {
771 resId = R.string.notification_incoming_call_with_photo_message_location;
772 } else {
773 resId = R.string.notification_incoming_call_with_photo_location;
774 }
775 } else if (hasSubject) {
776 resId = R.string.notification_incoming_call_with_message_location;
777 } else {
778 resId = R.string.notification_incoming_call_with_location;
779 }
780 } else if (hasImage) {
781 if (hasSubject) {
782 resId = R.string.notification_incoming_call_with_photo_message;
783 } else {
784 resId = R.string.notification_incoming_call_with_photo;
785 }
786 } else {
787 resId = R.string.notification_incoming_call_with_message;
788 }
789 }
790 if (mContext.getString(resId).length() > 50) {
791 resId = R.string.notification_incoming_call_attachments;
792 }
793 return resId;
794 }
795
Eric Erfanianccca3152017-02-22 16:32:36 -0800796 /** Gets the most relevant call to display in the notification. */
797 private DialerCall getCallToShow(CallList callList) {
798 if (callList == null) {
799 return null;
800 }
801 DialerCall call = callList.getIncomingCall();
802 if (call == null) {
803 call = callList.getOutgoingCall();
804 }
805 if (call == null) {
806 call = callList.getVideoUpgradeRequestCall();
807 }
808 if (call == null) {
809 call = callList.getActiveOrBackgroundCall();
810 }
811 return call;
812 }
813
814 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
815 Spannable spannable = new SpannableString(mContext.getText(stringRes));
816 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
817 // This will only work for cases where the Notification.Builder has a fullscreen intent set
818 // Notification.Builder that does not have a full screen intent will take the color of the
819 // app and the following leads to a no-op.
820 spannable.setSpan(
821 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
822 }
823 return spannable;
824 }
825
826 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700827 LogUtil.d(
828 "StatusBarNotifier.addAnswerAction",
829 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800830 PendingIntent answerVoicePendingIntent =
831 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700832 // We put animation resources in "anim" folder instead of "drawable", which causes Android
833 // Studio to complain.
834 // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc?
835 //noinspection ResourceType
Eric Erfanianccca3152017-02-22 16:32:36 -0800836 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700837 new Notification.Action.Builder(
838 Icon.createWithResource(mContext, R.anim.on_going_call),
839 getActionText(
840 R.string.notification_action_answer, R.color.notification_action_accept),
841 answerVoicePendingIntent)
842 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800843 }
844
845 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700846 LogUtil.d(
847 "StatusBarNotifier.addDismissAction",
848 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800849 PendingIntent declinePendingIntent =
850 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
851 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700852 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700853 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700854 getActionText(
855 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
856 declinePendingIntent)
857 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 }
859
860 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700861 LogUtil.d(
862 "StatusBarNotifier.addHangupAction",
863 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800864 PendingIntent hangupPendingIntent =
865 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
866 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700867 new Notification.Action.Builder(
868 Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700869 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700870 hangupPendingIntent)
871 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800872 }
873
874 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700875 LogUtil.i(
876 "StatusBarNotifier.addVideoCallAction",
877 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800878 PendingIntent answerVideoPendingIntent =
879 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
880 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700881 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700882 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700883 getActionText(
884 R.string.notification_action_answer_video,
885 R.color.notification_action_answer_video),
886 answerVideoPendingIntent)
887 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800888 }
889
890 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700891 LogUtil.i(
892 "StatusBarNotifier.addAcceptUpgradeRequestAction",
893 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800894 PendingIntent acceptVideoPendingIntent =
895 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
896 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700897 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700898 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700899 getActionText(
900 R.string.notification_action_accept, R.color.notification_action_accept),
901 acceptVideoPendingIntent)
902 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 }
904
905 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700906 LogUtil.i(
907 "StatusBarNotifier.addDismissUpgradeRequestAction",
908 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800909 PendingIntent declineVideoPendingIntent =
910 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
911 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700912 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700913 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700914 getActionText(
915 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
916 declineVideoPendingIntent)
917 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800918 }
919
920 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700921 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800922 // Ok, we actually want to launch the incoming call
923 // UI at this point (in addition to simply posting a notification
924 // to the status bar). Setting fullScreenIntent will cause
925 // the InCallScreen to be launched immediately *unless* the
926 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700927 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800928 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 }
930
931 private Notification.Builder getNotificationBuilder() {
932 final Notification.Builder builder = new Notification.Builder(mContext);
933 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700934 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700935 // This will be ignored on O+ and handled by the channel
936 //noinspection deprecation
937 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800938
939 return builder;
940 }
941
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700942 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800943 Intent intent =
944 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700945 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800946
947 int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
948 if (isFullScreen) {
949 // Use a unique request code so that the pending intent isn't clobbered by the
950 // non-full screen pending intent.
951 requestCode = PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
952 }
953
954 // PendingIntent that can be used to launch the InCallActivity. The
955 // system fires off this intent if the user pulls down the windowshade
956 // and clicks the notification's expanded view. It's also used to
957 // launch the InCallActivity immediately when when there's an incoming
958 // call (see the "fullScreenIntent" field below).
959 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
960 }
961
962 private void setStatusBarCallListener(StatusBarCallListener listener) {
963 if (mStatusBarCallListener != null) {
964 mStatusBarCallListener.cleanup();
965 }
966 mStatusBarCallListener = listener;
967 }
968
969 private class StatusBarCallListener implements DialerCallListener {
970
971 private DialerCall mDialerCall;
972
973 StatusBarCallListener(DialerCall dialerCall) {
974 mDialerCall = dialerCall;
975 mDialerCall.addListener(this);
976 }
977
978 void cleanup() {
979 mDialerCall.removeListener(this);
980 }
981
982 @Override
983 public void onDialerCallDisconnect() {}
984
985 @Override
986 public void onDialerCallUpdate() {
987 if (CallList.getInstance().getIncomingCall() == null) {
988 mDialerRingtoneManager.stopCallWaitingTone();
989 }
990 }
991
992 @Override
993 public void onDialerCallChildNumberChange() {}
994
995 @Override
996 public void onDialerCallLastForwardedNumberChange() {}
997
998 @Override
999 public void onDialerCallUpgradeToVideo() {}
1000
1001 @Override
1002 public void onWiFiToLteHandover() {}
1003
1004 @Override
1005 public void onHandoverToWifiFailure() {}
1006
1007 /**
1008 * Responds to changes in the session modification state for the call by dismissing the status
1009 * bar notification as required.
1010 */
1011 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001012 public void onDialerCallSessionModificationStateChange() {
1013 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001014 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001015 cleanup();
1016 updateNotification(CallList.getInstance());
1017 }
1018 }
1019 }
1020}