blob: e6969c1475fc9033f289f6cb72fb99c291b15337 [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 Erfanianccca3152017-02-22 16:32:36 -080031import android.app.PendingIntent;
32import android.content.Context;
33import android.content.Intent;
Eric Erfanian83b20212017-05-31 08:53:10 -070034import android.content.res.Resources;
Eric Erfanianccca3152017-02-22 16:32:36 -080035import android.graphics.Bitmap;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.graphics.drawable.BitmapDrawable;
37import android.graphics.drawable.Drawable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070038import android.graphics.drawable.Icon;
Eric Erfanianccca3152017-02-22 16:32:36 -080039import android.media.AudioAttributes;
40import android.net.Uri;
41import android.os.Build.VERSION;
42import android.os.Build.VERSION_CODES;
43import android.support.annotation.ColorRes;
44import android.support.annotation.NonNull;
45import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070046import android.support.annotation.RequiresPermission;
Eric Erfanianccca3152017-02-22 16:32:36 -080047import android.support.annotation.StringRes;
48import android.support.annotation.VisibleForTesting;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070049import android.support.v4.os.BuildCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080050import android.telecom.Call.Details;
51import android.telecom.PhoneAccount;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070052import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080053import android.telecom.TelecomManager;
54import android.text.BidiFormatter;
55import android.text.Spannable;
56import android.text.SpannableString;
57import android.text.TextDirectionHeuristics;
58import android.text.TextUtils;
59import android.text.style.ForegroundColorSpan;
60import com.android.contacts.common.ContactsUtils;
61import com.android.contacts.common.ContactsUtils.UserType;
62import com.android.contacts.common.preference.ContactsPreferences;
Eric Erfanianccca3152017-02-22 16:32:36 -080063import com.android.contacts.common.util.ContactDisplayUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070064import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070066import com.android.dialer.configprovider.ConfigProviderBindings;
67import com.android.dialer.contactphoto.BitmapUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070068import com.android.dialer.enrichedcall.EnrichedCallManager;
69import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070070import com.android.dialer.lettertile.LetterTileDrawable;
71import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070072import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070073import com.android.dialer.notification.DialerNotificationManager;
74import com.android.dialer.notification.NotificationChannelId;
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
Eric Erfanian2ca43182017-08-31 06:57:16 -070096 private static final String NOTIFICATION_TAG = "STATUS_BAR_NOTIFIER";
97 private static final int NOTIFICATION_ID = 1;
98
Eric Erfanianccca3152017-02-22 16:32:36 -080099 // Notification types
100 // Indicates that no notification is currently showing.
101 private static final int NOTIFICATION_NONE = 0;
102 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700103 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800104 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700105 private static final int NOTIFICATION_INCOMING_CALL = 2;
106 // Notification for incoming calls in the case where there is already an active call.
107 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
108 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800109
Eric Erfanianccca3152017-02-22 16:32:36 -0800110 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
111
112 private final Context mContext;
113 private final ContactInfoCache mContactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 private final DialerRingtoneManager mDialerRingtoneManager;
115 @Nullable private ContactsPreferences mContactsPreferences;
116 private int mCurrentNotification = NOTIFICATION_NONE;
117 private int mCallState = DialerCall.State.INVALID;
118 private int mSavedIcon = 0;
119 private String mSavedContent = null;
120 private Bitmap mSavedLargeIcon;
121 private String mSavedContentTitle;
122 private Uri mRingtone;
123 private StatusBarCallListener mStatusBarCallListener;
124
Eric Erfaniand8046e52017-04-06 09:41:50 -0700125 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700126 mContext = Assert.isNotNull(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
128 mContactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800129 mDialerRingtoneManager =
130 new DialerRingtoneManager(
131 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
132 CallList.getInstance());
133 mCurrentNotification = NOTIFICATION_NONE;
134 }
135
136 /**
137 * Should only be called from a irrecoverable state where it is necessary to dismiss all
138 * notifications.
139 */
Eric Erfanian2ca43182017-08-31 06:57:16 -0700140 static void clearAllCallNotifications(Context context) {
141 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700142 "StatusBarNotifier.clearAllCallNotifications",
143 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800144
Eric Erfanian2ca43182017-08-31 06:57:16 -0700145 DialerNotificationManager.cancel(context, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanianccca3152017-02-22 16:32:36 -0800146 }
147
148 private static int getWorkStringFromPersonalString(int resId) {
149 if (resId == R.string.notification_ongoing_call) {
150 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800151 } else if (resId == R.string.notification_incoming_call) {
152 return R.string.notification_incoming_work_call;
153 } else {
154 return resId;
155 }
156 }
157
158 /**
159 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
160 * context.
161 */
162 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
163 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
164 return PendingIntent.getBroadcast(context, 0, intent, 0);
165 }
166
167 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
168 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700169 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800170 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700171 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800172 updateNotification(callList);
173 }
174
Eric Erfaniand8046e52017-04-06 09:41:50 -0700175 @Override
176 public void onEnrichedCallStateChanged() {
177 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
178 updateNotification(CallList.getInstance());
179 }
180
Eric Erfanianccca3152017-02-22 16:32:36 -0800181 /**
182 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
183 * to a new incoming call.
184 *
185 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
186 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
187 * foreground activity is marked as "immersive".
188 *
189 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
190 * ringing connection" event from the telephony layer.)
191 *
192 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
193 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
194 * we'll simply update or cancel the in-call notification based on the current phone state.
195 *
196 * @see #updateInCallNotification(CallList)
197 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700198 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700199 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800200 updateInCallNotification(callList);
201 }
202
203 /**
204 * Take down the in-call notification.
205 *
206 * @see #updateInCallNotification(CallList)
207 */
208 private void cancelNotification() {
209 if (mStatusBarCallListener != null) {
210 setStatusBarCallListener(null);
211 }
212 if (mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700213 LogUtil.i("StatusBarNotifier.cancelNotification", "cancel");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700214 DialerNotificationManager.cancel(mContext, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanianccca3152017-02-22 16:32:36 -0800215 }
216 mCurrentNotification = NOTIFICATION_NONE;
217 }
218
219 /**
220 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
221 * status bar notification based on the current telephony state, or cancels the notification if
222 * the phone is totally idle.
223 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700224 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800225 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700226 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800227
228 final DialerCall call = getCallToShow(callList);
229
230 if (call != null) {
231 showNotification(callList, call);
232 } else {
233 cancelNotification();
234 }
235 }
236
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700237 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800238 private void showNotification(final CallList callList, final DialerCall call) {
239 final boolean isIncoming =
240 (call.getState() == DialerCall.State.INCOMING
241 || call.getState() == DialerCall.State.CALL_WAITING);
242 setStatusBarCallListener(new StatusBarCallListener(call));
243
244 // we make a call to the contact info cache to query for supplemental data to what the
245 // call provides. This includes the contact name and photo.
246 // This callback will always get called immediately and synchronously with whatever data
247 // it has available, and may make a subsequent call later (same thread) if it had to
248 // call into the contacts provider for more data.
249 mContactInfoCache.findInfo(
250 call,
251 isIncoming,
252 new ContactInfoCacheCallback() {
253 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700254 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800255 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
256 DialerCall call = callList.getCallById(callId);
257 if (call != null) {
258 call.getLogState().contactLookupResult = entry.contactLookupResult;
259 buildAndSendNotification(callList, call, entry);
260 }
261 }
262
263 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700264 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800265 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
266 DialerCall call = callList.getCallById(callId);
267 if (call != null) {
268 buildAndSendNotification(callList, call, entry);
269 }
270 }
271 });
272 }
273
274 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700275 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800276 private void buildAndSendNotification(
277 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
278 // This can get called to update an existing notification after contact information has come
279 // back. However, it can happen much later. Before we continue, we need to make sure that
280 // the call being passed in is still the one we want to show in the notification.
281 final DialerCall call = getCallToShow(callList);
282 if (call == null || !call.getId().equals(originalCall.getId())) {
283 return;
284 }
285
286 final int callState = call.getState();
287
288 // Check if data has changed; if nothing is different, don't issue another notification.
289 final int iconResId = getIconToDisplay(call);
Eric Erfanian83b20212017-05-31 08:53:10 -0700290 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800291 final String content = getContentString(call, contactInfo.userType);
292 final String contentTitle = getContentTitle(contactInfo, call);
293
294 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700295 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700296 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800297 final int notificationType;
298 if (callState == DialerCall.State.INCOMING
299 || callState == DialerCall.State.CALL_WAITING
300 || isVideoUpgradeRequest) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700301 if (ConfigProviderBindings.get(mContext)
302 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
303 notificationType =
304 InCallPresenter.getInstance().isShowingInCallUi()
305 ? NOTIFICATION_INCOMING_CALL_QUIET
306 : NOTIFICATION_INCOMING_CALL;
307 } else {
308 boolean alreadyActive =
309 callList.getActiveOrBackgroundCall() != null
310 && InCallPresenter.getInstance().isShowingInCallUi();
311 notificationType =
312 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
313 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800314 } else {
315 notificationType = NOTIFICATION_IN_CALL;
316 }
317
318 if (!checkForChangeAndSaveData(
319 iconResId,
320 content,
321 largeIcon,
322 contentTitle,
323 callState,
324 notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700325 contactInfo.contactRingtoneUri)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 return;
327 }
328
329 if (largeIcon != null) {
330 largeIcon = getRoundedIcon(largeIcon);
331 }
332
333 // This builder is used for the notification shown when the device is locked and the user
334 // has set their notification settings to 'hide sensitive content'
335 // {@see Notification.Builder#setPublicVersion}.
336 Notification.Builder publicBuilder = new Notification.Builder(mContext);
337 publicBuilder
338 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700339 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800340 // Hide work call state for the lock screen notification
341 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
342 setNotificationWhen(call, callState, publicBuilder);
343
344 // Builder for the notification shown when the device is unlocked or the user has set their
345 // notification settings to 'show all notification content'.
346 final Notification.Builder builder = getNotificationBuilder();
347 builder.setPublicVersion(publicBuilder.build());
348
349 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700350 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800351
352 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700353 PhoneAccountHandle accountHandle = call.getAccountHandle();
354 if (accountHandle == null) {
355 accountHandle = getAnyPhoneAccount();
356 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700357
358 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
359 switch (notificationType) {
360 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700361 if (BuildCompat.isAtLeastO()) {
362 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
363 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700364 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
365 // Set the notification category and bump the priority for incoming calls
366 builder.setCategory(Notification.CATEGORY_CALL);
367 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700368 builder.setPriority(Notification.PRIORITY_MAX);
369 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
370 LogUtil.i(
371 "StatusBarNotifier.buildAndSendNotification",
372 "Canceling old notification so this one can be noisy");
373 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
374 // notification (if there is one) so the fullScreenIntent or HUN will show
Eric Erfanian2ca43182017-08-31 06:57:16 -0700375 DialerNotificationManager.cancel(mContext, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700376 }
377 break;
378 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700379 if (BuildCompat.isAtLeastO()) {
380 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
381 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700382 break;
383 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700384 if (BuildCompat.isAtLeastO()) {
385 publicBuilder.setColorized(true);
386 builder.setColorized(true);
387 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
388 }
389 break;
390 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700391 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800392 }
393
394 // Set the content
395 builder.setContentText(content);
396 builder.setSmallIcon(iconResId);
397 builder.setContentTitle(contentTitle);
398 builder.setLargeIcon(largeIcon);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700399 builder.setColor(
400 mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800401
402 if (isVideoUpgradeRequest) {
403 builder.setUsesChronometer(false);
404 addDismissUpgradeRequestAction(builder);
405 addAcceptUpgradeRequestAction(builder);
406 } else {
407 createIncomingCallNotification(call, callState, builder);
408 }
409
410 addPersonReference(builder, contactInfo, call);
411
412 // Fire off the notification
413 Notification notification = builder.build();
414
415 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
416 notification.flags |= Notification.FLAG_INSISTENT;
417 notification.sound = contactInfo.contactRingtoneUri;
418 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
419 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
420 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
421 notification.audioAttributes = audioAttributes.build();
422 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
423 notification.vibrate = VIBRATE_PATTERN;
424 }
425 }
426 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700427 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800428 mDialerRingtoneManager.playCallWaitingTone();
429 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800430
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700431 LogUtil.i(
432 "StatusBarNotifier.buildAndSendNotification",
433 "displaying notification for " + notificationType);
434
Eric Erfanianccca3152017-02-22 16:32:36 -0800435 try {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700436 DialerNotificationManager.notify(mContext, NOTIFICATION_TAG, NOTIFICATION_ID, notification);
Eric Erfanianccca3152017-02-22 16:32:36 -0800437 } catch (RuntimeException e) {
438 // TODO(b/34744003): Move the memory stats into silent feedback PSD.
439 ActivityManager activityManager = mContext.getSystemService(ActivityManager.class);
440 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
441 activityManager.getMemoryInfo(memoryInfo);
442 throw new RuntimeException(
443 String.format(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700444 Locale.US,
Eric Erfanianccca3152017-02-22 16:32:36 -0800445 "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)",
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700446 contactInfo.photoType,
447 memoryInfo.lowMemory,
448 memoryInfo.availMem),
Eric Erfanianccca3152017-02-22 16:32:36 -0800449 e);
450 }
451 call.getLatencyReport().onNotificationShown();
452 mCurrentNotification = notificationType;
453 }
454
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700455 @Nullable
456 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
457 private PhoneAccountHandle getAnyPhoneAccount() {
458 PhoneAccountHandle accountHandle;
459 TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
460 accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
461 if (accountHandle == null) {
462 List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
463 if (!accountHandles.isEmpty()) {
464 accountHandle = accountHandles.get(0);
465 }
466 }
467 return accountHandle;
468 }
469
Eric Erfanianccca3152017-02-22 16:32:36 -0800470 private void createIncomingCallNotification(
471 DialerCall call, int state, Notification.Builder builder) {
472 setNotificationWhen(call, state, builder);
473
474 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
475 if (state == DialerCall.State.ACTIVE
476 || state == DialerCall.State.ONHOLD
477 || DialerCall.State.isDialing(state)) {
478 addHangupAction(builder);
479 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
480 addDismissAction(builder);
481 if (call.isVideoCall()) {
482 addVideoCallAction(builder);
483 } else {
484 addAnswerAction(builder);
485 }
486 }
487 }
488
489 /**
490 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
491 * duration of the call. For all other states, the notification will automatically show the time
492 * at which the notification was created.
493 */
494 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
495 if (state == DialerCall.State.ACTIVE) {
496 builder.setUsesChronometer(true);
497 builder.setWhen(call.getConnectTimeMillis());
498 } else {
499 builder.setUsesChronometer(false);
500 }
501 }
502
503 /**
504 * Checks the new notification data and compares it against any notification that we are already
505 * displaying. If the data is exactly the same, we return false so that we do not issue a new
506 * notification for the exact same data.
507 */
508 private boolean checkForChangeAndSaveData(
509 int icon,
510 String content,
511 Bitmap largeIcon,
512 String contentTitle,
513 int state,
514 int notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700515 Uri ringtone) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800516
517 // The two are different:
518 // if new title is not null, it should be different from saved version OR
519 // if new title is null, the saved version should not be null
520 final boolean contentTitleChanged =
521 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
522 || (contentTitle == null && mSavedContentTitle != null);
523
Eric Erfanian8369df02017-05-03 10:27:13 -0700524 boolean largeIconChanged =
525 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
526
Eric Erfanianccca3152017-02-22 16:32:36 -0800527 // any change means we are definitely updating
528 boolean retval =
529 (mSavedIcon != icon)
530 || !Objects.equals(mSavedContent, content)
531 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700532 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800533 || contentTitleChanged
Eric Erfanian8369df02017-05-03 10:27:13 -0700534 || !Objects.equals(mRingtone, ringtone);
Eric Erfanianccca3152017-02-22 16:32:36 -0800535
536 // If we aren't showing a notification right now or the notification type is changing,
537 // definitely do an update.
538 if (mCurrentNotification != notificationType) {
539 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700540 LogUtil.d(
541 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800542 }
543 retval = true;
544 }
545
546 mSavedIcon = icon;
547 mSavedContent = content;
548 mCallState = state;
549 mSavedLargeIcon = largeIcon;
550 mSavedContentTitle = contentTitle;
551 mRingtone = ringtone;
552
553 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700554 LogUtil.d(
555 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800556 }
557
558 return retval;
559 }
560
561 /** Returns the main string to use in the notification. */
562 @VisibleForTesting
563 @Nullable
564 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700565 if (call.isConferenceCall()) {
566 return CallerInfoUtils.getConferenceString(
567 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800568 }
569
570 String preferredName =
571 ContactDisplayUtils.getPreferredDisplayName(
572 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
573 if (TextUtils.isEmpty(preferredName)) {
574 return TextUtils.isEmpty(contactInfo.number)
575 ? null
576 : BidiFormatter.getInstance()
577 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
578 }
579 return preferredName;
580 }
581
582 private void addPersonReference(
583 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
584 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
585 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
586 // NotificationManager using it.
587 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
588 builder.addPerson(contactInfo.lookupUri.toString());
589 } else if (!TextUtils.isEmpty(call.getNumber())) {
590 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
591 }
592 }
593
594 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700595 private static Bitmap getLargeIconToDisplay(
596 Context context, ContactCacheEntry contactInfo, DialerCall call) {
597 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800598 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
600 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
601 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700602 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700603 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
604 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700605 @ContactType
606 int contactType =
607 LetterTileDrawable.getContactTypeFromPrimitives(
608 CallerInfoUtils.isVoiceMailNumber(context, call),
609 call.isSpam(),
610 contactInfo.isBusiness,
611 call.getNumberPresentation(),
612 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700613 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700614
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700615 lettertile.setCanonicalDialerLetterTileDetails(
616 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
617 contactInfo.lookupKey,
618 LetterTileDrawable.SHAPE_CIRCLE,
619 contactType);
620 largeIcon = lettertile.getBitmap(width, height);
621 }
622
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700624 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800625 largeIcon = DrawableConverter.drawableToBitmap(drawable);
626 }
627 return largeIcon;
628 }
629
630 private Bitmap getRoundedIcon(Bitmap bitmap) {
631 if (bitmap == null) {
632 return null;
633 }
634 final int height =
635 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
636 final int width =
637 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
638 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
639 }
640
641 /**
642 * Returns the appropriate icon res Id to display based on the call for which we want to display
643 * information.
644 */
645 private int getIconToDisplay(DialerCall call) {
646 // Even if both lines are in use, we only show a single item in
647 // the expanded Notifications UI. It's labeled "Ongoing call"
648 // (or "On hold" if there's only one call, and it's on hold.)
649 // Also, we don't have room to display caller-id info from two
650 // different calls. So if both lines are in use, display info
651 // from the foreground call. And if there's a ringing call,
652 // display that regardless of the state of the other calls.
653 if (call.getState() == DialerCall.State.ONHOLD) {
654 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700655 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700656 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700657 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700658 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
659 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700660 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
661 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
662 // replace this icon with an icon of a phone with a HD badge. This is a carrier requirement.
Eric Erfanian90508232017-03-24 09:31:16 -0700663 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800664 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700665 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
666 if (ReturnToCallController.isEnabled(mContext)) {
667 return R.drawable.quantum_ic_call_vd_theme_24;
668 } else {
669 return R.drawable.on_going_call;
670 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800671 }
672
673 /** Returns the message to use with the notification. */
674 private String getContentString(DialerCall call, @UserType long userType) {
675 boolean isIncomingOrWaiting =
676 call.getState() == DialerCall.State.INCOMING
677 || call.getState() == DialerCall.State.CALL_WAITING;
678
679 if (isIncomingOrWaiting
680 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
681
682 if (!TextUtils.isEmpty(call.getChildNumber())) {
683 return mContext.getString(R.string.child_number, call.getChildNumber());
684 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
685 return call.getCallSubject();
686 }
687 }
688
689 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700690 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800691 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700692 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800693 }
694
695 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700696 if (call.isSpam()) {
697 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700698 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
699 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700700 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700701 resId = R.string.notification_incoming_call_wifi_template;
702
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700704 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 }
706 } else if (call.getState() == DialerCall.State.ONHOLD) {
707 resId = R.string.notification_on_hold;
708 } else if (DialerCall.State.isDialing(call.getState())) {
709 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700710 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700711 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800712 resId = R.string.notification_requesting_video_call;
713 }
714
715 // Is the call placed through work connection service.
716 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
717 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
718 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700719 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
720 }
721
722 if (resId == R.string.notification_incoming_call_wifi_template
723 || resId == R.string.notification_ongoing_call_wifi_template) {
724 // TODO(b/64525903): Potentially apply this template logic everywhere.
725 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800726 }
727
728 return mContext.getString(resId);
729 }
730
Eric Erfanian2ca43182017-08-31 06:57:16 -0700731 private boolean shouldShowEnrichedCallNotification(Session session) {
732 if (session == null) {
733 return false;
734 }
735 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
736 }
737
Eric Erfaniand8046e52017-04-06 09:41:50 -0700738 private int getECIncomingCallText(Session session) {
739 int resId;
740 MultimediaData data = session.getMultimediaData();
741 boolean hasImage = data.hasImageData();
742 boolean hasSubject = !TextUtils.isEmpty(data.getText());
743 boolean hasMap = data.getLocation() != null;
744 if (data.isImportant()) {
745 if (hasMap) {
746 if (hasImage) {
747 if (hasSubject) {
748 resId = R.string.important_notification_incoming_call_with_photo_message_location;
749 } else {
750 resId = R.string.important_notification_incoming_call_with_photo_location;
751 }
752 } else if (hasSubject) {
753 resId = R.string.important_notification_incoming_call_with_message_location;
754 } else {
755 resId = R.string.important_notification_incoming_call_with_location;
756 }
757 } else if (hasImage) {
758 if (hasSubject) {
759 resId = R.string.important_notification_incoming_call_with_photo_message;
760 } else {
761 resId = R.string.important_notification_incoming_call_with_photo;
762 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700763 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700764 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700765 } else {
766 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700767 }
768 if (mContext.getString(resId).length() > 50) {
769 resId = R.string.important_notification_incoming_call_attachments;
770 }
771 } else {
772 if (hasMap) {
773 if (hasImage) {
774 if (hasSubject) {
775 resId = R.string.notification_incoming_call_with_photo_message_location;
776 } else {
777 resId = R.string.notification_incoming_call_with_photo_location;
778 }
779 } else if (hasSubject) {
780 resId = R.string.notification_incoming_call_with_message_location;
781 } else {
782 resId = R.string.notification_incoming_call_with_location;
783 }
784 } else if (hasImage) {
785 if (hasSubject) {
786 resId = R.string.notification_incoming_call_with_photo_message;
787 } else {
788 resId = R.string.notification_incoming_call_with_photo;
789 }
790 } else {
791 resId = R.string.notification_incoming_call_with_message;
792 }
793 }
794 if (mContext.getString(resId).length() > 50) {
795 resId = R.string.notification_incoming_call_attachments;
796 }
797 return resId;
798 }
799
Eric Erfanianccca3152017-02-22 16:32:36 -0800800 /** Gets the most relevant call to display in the notification. */
801 private DialerCall getCallToShow(CallList callList) {
802 if (callList == null) {
803 return null;
804 }
805 DialerCall call = callList.getIncomingCall();
806 if (call == null) {
807 call = callList.getOutgoingCall();
808 }
809 if (call == null) {
810 call = callList.getVideoUpgradeRequestCall();
811 }
812 if (call == null) {
813 call = callList.getActiveOrBackgroundCall();
814 }
815 return call;
816 }
817
818 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
819 Spannable spannable = new SpannableString(mContext.getText(stringRes));
820 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
821 // This will only work for cases where the Notification.Builder has a fullscreen intent set
822 // Notification.Builder that does not have a full screen intent will take the color of the
823 // app and the following leads to a no-op.
824 spannable.setSpan(
825 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
826 }
827 return spannable;
828 }
829
830 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700831 LogUtil.d(
832 "StatusBarNotifier.addAnswerAction",
833 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800834 PendingIntent answerVoicePendingIntent =
835 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
836 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700837 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700838 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700839 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(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700868 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
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
Eric Erfanian2ca43182017-08-31 06:57:16 -0700936 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -0700937 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
Eric Erfanian2ca43182017-08-31 06:57:16 -0700947 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800948 if (isFullScreen) {
949 // Use a unique request code so that the pending intent isn't clobbered by the
950 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700951 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800952 }
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
Eric Erfanianc857f902017-05-15 14:05:33 -07001007 @Override
1008 public void onInternationalCallOnWifi() {}
1009
Eric Erfanian2ca43182017-08-31 06:57:16 -07001010 @Override
1011 public void onEnrichedCallSessionUpdate() {}
1012
Eric Erfanianccca3152017-02-22 16:32:36 -08001013 /**
1014 * Responds to changes in the session modification state for the call by dismissing the status
1015 * bar notification as required.
1016 */
1017 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001018 public void onDialerCallSessionModificationStateChange() {
1019 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001020 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001021 cleanup();
1022 updateNotification(CallList.getInstance());
1023 }
1024 }
1025 }
1026}