blob: 229f08c8e28eb33ced6c1a91f4ebec9d61e71007 [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;
Eric Erfanian83b20212017-05-31 08:53:10 -070036import android.content.res.Resources;
Eric Erfanianccca3152017-02-22 16:32:36 -080037import android.graphics.Bitmap;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import 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 Erfanian91ce7d22017-06-05 13:35:02 -070065import com.android.contacts.common.lettertiles.LetterTileDrawable.ContactType;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.contacts.common.preference.ContactsPreferences;
67import com.android.contacts.common.util.BitmapUtil;
68import com.android.contacts.common.util.ContactDisplayUtils;
69import com.android.dialer.common.LogUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070070import 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);
Eric Erfanian83b20212017-05-31 08:53:10 -0700305 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800306 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) {
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700568 if (call.isConferenceCall()) {
569 return CallerInfoUtils.getConferenceString(
570 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800571 }
572
573 String preferredName =
574 ContactDisplayUtils.getPreferredDisplayName(
575 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
576 if (TextUtils.isEmpty(preferredName)) {
577 return TextUtils.isEmpty(contactInfo.number)
578 ? null
579 : BidiFormatter.getInstance()
580 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
581 }
582 return preferredName;
583 }
584
585 private void addPersonReference(
586 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
587 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
588 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
589 // NotificationManager using it.
590 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
591 builder.addPerson(contactInfo.lookupUri.toString());
592 } else if (!TextUtils.isEmpty(call.getNumber())) {
593 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
594 }
595 }
596
597 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700598 private static Bitmap getLargeIconToDisplay(
599 Context context, ContactCacheEntry contactInfo, DialerCall call) {
600 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800601 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 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) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700606 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
607 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700608 @ContactType
609 int contactType =
610 LetterTileDrawable.getContactTypeFromPrimitives(
611 CallerInfoUtils.isVoiceMailNumber(context, call),
612 call.isSpam(),
613 contactInfo.isBusiness,
614 call.getNumberPresentation(),
615 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700616 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700617
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700618 lettertile.setCanonicalDialerLetterTileDetails(
619 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
620 contactInfo.lookupKey,
621 LetterTileDrawable.SHAPE_CIRCLE,
622 contactType);
623 largeIcon = lettertile.getBitmap(width, height);
624 }
625
Eric Erfanianccca3152017-02-22 16:32:36 -0800626 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700627 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800628 largeIcon = DrawableConverter.drawableToBitmap(drawable);
629 }
630 return largeIcon;
631 }
632
633 private Bitmap getRoundedIcon(Bitmap bitmap) {
634 if (bitmap == null) {
635 return null;
636 }
637 final int height =
638 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
639 final int width =
640 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
641 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
642 }
643
644 /**
645 * Returns the appropriate icon res Id to display based on the call for which we want to display
646 * information.
647 */
648 private int getIconToDisplay(DialerCall call) {
649 // Even if both lines are in use, we only show a single item in
650 // the expanded Notifications UI. It's labeled "Ongoing call"
651 // (or "On hold" if there's only one call, and it's on hold.)
652 // Also, we don't have room to display caller-id info from two
653 // different calls. So if both lines are in use, display info
654 // from the foreground call. And if there's a ringing call,
655 // display that regardless of the state of the other calls.
656 if (call.getState() == DialerCall.State.ONHOLD) {
657 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700658 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700659 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700660 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700661 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
662 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
663 // Normally when a call is ongoing the status bar displays an icon of a phone with animated
664 // lines. This is a helpful hint for users so they know how to get back to the call.
665 // For Sprint HD calls, we replace this icon with an icon of a phone with a HD badge.
666 // This is a carrier requirement.
667 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800668 }
669 return R.anim.on_going_call;
670 }
671
672 /** Returns the message to use with the notification. */
673 private String getContentString(DialerCall call, @UserType long userType) {
674 boolean isIncomingOrWaiting =
675 call.getState() == DialerCall.State.INCOMING
676 || call.getState() == DialerCall.State.CALL_WAITING;
677
678 if (isIncomingOrWaiting
679 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
680
681 if (!TextUtils.isEmpty(call.getChildNumber())) {
682 return mContext.getString(R.string.child_number, call.getChildNumber());
683 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
684 return call.getCallSubject();
685 }
686 }
687
688 int resId = R.string.notification_ongoing_call;
689 if (call.hasProperty(Details.PROPERTY_WIFI)) {
690 resId = R.string.notification_ongoing_call_wifi;
691 }
692
693 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700694 if (call.isSpam()) {
695 resId = R.string.notification_incoming_spam_call;
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700696 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
697 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700698 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800699 resId = R.string.notification_incoming_call_wifi;
700 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700701 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 }
703 } else if (call.getState() == DialerCall.State.ONHOLD) {
704 resId = R.string.notification_on_hold;
705 } else if (DialerCall.State.isDialing(call.getState())) {
706 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700707 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700708 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800709 resId = R.string.notification_requesting_video_call;
710 }
711
712 // Is the call placed through work connection service.
713 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
714 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
715 resId = getWorkStringFromPersonalString(resId);
716 }
717
718 return mContext.getString(resId);
719 }
720
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700721 private boolean shouldShowEnrichedCallNotification(Session session) {
722 if (session == null) {
723 return false;
724 }
725 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
726 }
727
Eric Erfaniand8046e52017-04-06 09:41:50 -0700728 private int getECIncomingCallText(Session session) {
729 int resId;
730 MultimediaData data = session.getMultimediaData();
731 boolean hasImage = data.hasImageData();
732 boolean hasSubject = !TextUtils.isEmpty(data.getText());
733 boolean hasMap = data.getLocation() != null;
734 if (data.isImportant()) {
735 if (hasMap) {
736 if (hasImage) {
737 if (hasSubject) {
738 resId = R.string.important_notification_incoming_call_with_photo_message_location;
739 } else {
740 resId = R.string.important_notification_incoming_call_with_photo_location;
741 }
742 } else if (hasSubject) {
743 resId = R.string.important_notification_incoming_call_with_message_location;
744 } else {
745 resId = R.string.important_notification_incoming_call_with_location;
746 }
747 } else if (hasImage) {
748 if (hasSubject) {
749 resId = R.string.important_notification_incoming_call_with_photo_message;
750 } else {
751 resId = R.string.important_notification_incoming_call_with_photo;
752 }
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700753 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700754 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian91ce7d22017-06-05 13:35:02 -0700755 } else {
756 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700757 }
758 if (mContext.getString(resId).length() > 50) {
759 resId = R.string.important_notification_incoming_call_attachments;
760 }
761 } else {
762 if (hasMap) {
763 if (hasImage) {
764 if (hasSubject) {
765 resId = R.string.notification_incoming_call_with_photo_message_location;
766 } else {
767 resId = R.string.notification_incoming_call_with_photo_location;
768 }
769 } else if (hasSubject) {
770 resId = R.string.notification_incoming_call_with_message_location;
771 } else {
772 resId = R.string.notification_incoming_call_with_location;
773 }
774 } else if (hasImage) {
775 if (hasSubject) {
776 resId = R.string.notification_incoming_call_with_photo_message;
777 } else {
778 resId = R.string.notification_incoming_call_with_photo;
779 }
780 } else {
781 resId = R.string.notification_incoming_call_with_message;
782 }
783 }
784 if (mContext.getString(resId).length() > 50) {
785 resId = R.string.notification_incoming_call_attachments;
786 }
787 return resId;
788 }
789
Eric Erfanianccca3152017-02-22 16:32:36 -0800790 /** Gets the most relevant call to display in the notification. */
791 private DialerCall getCallToShow(CallList callList) {
792 if (callList == null) {
793 return null;
794 }
795 DialerCall call = callList.getIncomingCall();
796 if (call == null) {
797 call = callList.getOutgoingCall();
798 }
799 if (call == null) {
800 call = callList.getVideoUpgradeRequestCall();
801 }
802 if (call == null) {
803 call = callList.getActiveOrBackgroundCall();
804 }
805 return call;
806 }
807
808 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
809 Spannable spannable = new SpannableString(mContext.getText(stringRes));
810 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
811 // This will only work for cases where the Notification.Builder has a fullscreen intent set
812 // Notification.Builder that does not have a full screen intent will take the color of the
813 // app and the following leads to a no-op.
814 spannable.setSpan(
815 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
816 }
817 return spannable;
818 }
819
820 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700821 LogUtil.d(
822 "StatusBarNotifier.addAnswerAction",
823 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800824 PendingIntent answerVoicePendingIntent =
825 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700826 // We put animation resources in "anim" folder instead of "drawable", which causes Android
827 // Studio to complain.
828 // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc?
829 //noinspection ResourceType
Eric Erfanianccca3152017-02-22 16:32:36 -0800830 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700831 new Notification.Action.Builder(
832 Icon.createWithResource(mContext, R.anim.on_going_call),
833 getActionText(
834 R.string.notification_action_answer, R.color.notification_action_accept),
835 answerVoicePendingIntent)
836 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800837 }
838
839 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700840 LogUtil.d(
841 "StatusBarNotifier.addDismissAction",
842 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800843 PendingIntent declinePendingIntent =
844 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
845 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700846 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700847 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700848 getActionText(
849 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
850 declinePendingIntent)
851 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800852 }
853
854 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700855 LogUtil.d(
856 "StatusBarNotifier.addHangupAction",
857 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 PendingIntent hangupPendingIntent =
859 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
860 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700861 new Notification.Action.Builder(
862 Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700863 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700864 hangupPendingIntent)
865 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800866 }
867
868 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700869 LogUtil.i(
870 "StatusBarNotifier.addVideoCallAction",
871 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800872 PendingIntent answerVideoPendingIntent =
873 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
874 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700875 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700876 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700877 getActionText(
878 R.string.notification_action_answer_video,
879 R.color.notification_action_answer_video),
880 answerVideoPendingIntent)
881 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800882 }
883
884 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700885 LogUtil.i(
886 "StatusBarNotifier.addAcceptUpgradeRequestAction",
887 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800888 PendingIntent acceptVideoPendingIntent =
889 createNotificationPendingIntent(mContext, ACTION_ACCEPT_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_accept, R.color.notification_action_accept),
895 acceptVideoPendingIntent)
896 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800897 }
898
899 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700900 LogUtil.i(
901 "StatusBarNotifier.addDismissUpgradeRequestAction",
902 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800903 PendingIntent declineVideoPendingIntent =
904 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
905 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700906 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700907 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700908 getActionText(
909 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
910 declineVideoPendingIntent)
911 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800912 }
913
914 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700915 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800916 // Ok, we actually want to launch the incoming call
917 // UI at this point (in addition to simply posting a notification
918 // to the status bar). Setting fullScreenIntent will cause
919 // the InCallScreen to be launched immediately *unless* the
920 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700921 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800922 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800923 }
924
925 private Notification.Builder getNotificationBuilder() {
926 final Notification.Builder builder = new Notification.Builder(mContext);
927 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700928 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700929 // This will be ignored on O+ and handled by the channel
930 //noinspection deprecation
931 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800932
933 return builder;
934 }
935
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700936 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 Intent intent =
938 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700939 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800940
941 int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
942 if (isFullScreen) {
943 // Use a unique request code so that the pending intent isn't clobbered by the
944 // non-full screen pending intent.
945 requestCode = PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
946 }
947
948 // PendingIntent that can be used to launch the InCallActivity. The
949 // system fires off this intent if the user pulls down the windowshade
950 // and clicks the notification's expanded view. It's also used to
951 // launch the InCallActivity immediately when when there's an incoming
952 // call (see the "fullScreenIntent" field below).
953 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
954 }
955
956 private void setStatusBarCallListener(StatusBarCallListener listener) {
957 if (mStatusBarCallListener != null) {
958 mStatusBarCallListener.cleanup();
959 }
960 mStatusBarCallListener = listener;
961 }
962
963 private class StatusBarCallListener implements DialerCallListener {
964
965 private DialerCall mDialerCall;
966
967 StatusBarCallListener(DialerCall dialerCall) {
968 mDialerCall = dialerCall;
969 mDialerCall.addListener(this);
970 }
971
972 void cleanup() {
973 mDialerCall.removeListener(this);
974 }
975
976 @Override
977 public void onDialerCallDisconnect() {}
978
979 @Override
980 public void onDialerCallUpdate() {
981 if (CallList.getInstance().getIncomingCall() == null) {
982 mDialerRingtoneManager.stopCallWaitingTone();
983 }
984 }
985
986 @Override
987 public void onDialerCallChildNumberChange() {}
988
989 @Override
990 public void onDialerCallLastForwardedNumberChange() {}
991
992 @Override
993 public void onDialerCallUpgradeToVideo() {}
994
995 @Override
996 public void onWiFiToLteHandover() {}
997
998 @Override
999 public void onHandoverToWifiFailure() {}
1000
Eric Erfanianc857f902017-05-15 14:05:33 -07001001 @Override
1002 public void onInternationalCallOnWifi() {}
1003
Eric Erfanian91ce7d22017-06-05 13:35:02 -07001004 @Override
1005 public void onEnrichedCallSessionUpdate() {}
1006
Eric Erfanianccca3152017-02-22 16:32:36 -08001007 /**
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}