blob: 92ffae37ce4dfe85ad772259e1b7a97029ba23b0 [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;
wangqicf61ca02017-08-31 15:32:55 -070043import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080044import android.support.annotation.ColorRes;
45import android.support.annotation.NonNull;
46import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070047import android.support.annotation.RequiresPermission;
Eric Erfanianccca3152017-02-22 16:32:36 -080048import android.support.annotation.StringRes;
49import android.support.annotation.VisibleForTesting;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070050import android.support.v4.os.BuildCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080051import android.telecom.Call.Details;
52import android.telecom.PhoneAccount;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import android.telecom.TelecomManager;
55import android.text.BidiFormatter;
56import android.text.Spannable;
57import android.text.SpannableString;
58import android.text.TextDirectionHeuristics;
59import android.text.TextUtils;
60import android.text.style.ForegroundColorSpan;
61import com.android.contacts.common.ContactsUtils;
62import com.android.contacts.common.ContactsUtils.UserType;
63import com.android.contacts.common.preference.ContactsPreferences;
Eric Erfanianccca3152017-02-22 16:32:36 -080064import com.android.contacts.common.util.ContactDisplayUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070065import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070067import com.android.dialer.configprovider.ConfigProviderBindings;
68import com.android.dialer.contactphoto.BitmapUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070069import com.android.dialer.enrichedcall.EnrichedCallManager;
70import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070071import com.android.dialer.lettertile.LetterTileDrawable;
72import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070073import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070074import com.android.dialer.notification.DialerNotificationManager;
75import com.android.dialer.notification.NotificationChannelId;
Eric Erfanian90508232017-03-24 09:31:16 -070076import com.android.dialer.oem.MotorolaUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080077import com.android.dialer.util.DrawableConverter;
78import com.android.incallui.ContactInfoCache.ContactCacheEntry;
79import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
80import com.android.incallui.InCallPresenter.InCallState;
81import com.android.incallui.async.PausableExecutorImpl;
82import com.android.incallui.call.CallList;
83import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080084import com.android.incallui.call.DialerCallListener;
85import com.android.incallui.ringtone.DialerRingtoneManager;
86import com.android.incallui.ringtone.InCallTonePlayer;
87import com.android.incallui.ringtone.ToneGeneratorFactory;
Eric Erfanian90508232017-03-24 09:31:16 -070088import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070089import java.util.List;
90import java.util.Locale;
Eric Erfanianccca3152017-02-22 16:32:36 -080091import java.util.Objects;
92
93/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -070094public class StatusBarNotifier
95 implements InCallPresenter.InCallStateListener, EnrichedCallManager.StateChangedListener {
Eric Erfanianccca3152017-02-22 16:32:36 -080096
Eric Erfanian2ca43182017-08-31 06:57:16 -070097 private static final String NOTIFICATION_TAG = "STATUS_BAR_NOTIFIER";
98 private static final int NOTIFICATION_ID = 1;
99
Eric Erfanianccca3152017-02-22 16:32:36 -0800100 // Notification types
101 // Indicates that no notification is currently showing.
102 private static final int NOTIFICATION_NONE = 0;
103 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700104 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800105 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700106 private static final int NOTIFICATION_INCOMING_CALL = 2;
107 // Notification for incoming calls in the case where there is already an active call.
108 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
109 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800110
Eric Erfanianccca3152017-02-22 16:32:36 -0800111 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
112
113 private final Context mContext;
114 private final ContactInfoCache mContactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800115 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 Erfanian2ca43182017-08-31 06:57:16 -0700127 mContext = Assert.isNotNull(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800128 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
129 mContactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800130 mDialerRingtoneManager =
131 new DialerRingtoneManager(
132 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
133 CallList.getInstance());
134 mCurrentNotification = NOTIFICATION_NONE;
135 }
136
137 /**
138 * Should only be called from a irrecoverable state where it is necessary to dismiss all
139 * notifications.
140 */
Eric Erfanian2ca43182017-08-31 06:57:16 -0700141 static void clearAllCallNotifications(Context context) {
142 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700143 "StatusBarNotifier.clearAllCallNotifications",
144 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800145
Eric Erfanian2ca43182017-08-31 06:57:16 -0700146 DialerNotificationManager.cancel(context, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanianccca3152017-02-22 16:32:36 -0800147 }
148
149 private static int getWorkStringFromPersonalString(int resId) {
150 if (resId == R.string.notification_ongoing_call) {
151 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800152 } else if (resId == R.string.notification_incoming_call) {
153 return R.string.notification_incoming_work_call;
154 } else {
155 return resId;
156 }
157 }
158
159 /**
160 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
161 * context.
162 */
163 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
164 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
165 return PendingIntent.getBroadcast(context, 0, intent, 0);
166 }
167
168 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
169 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700170 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800171 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700172 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800173 updateNotification(callList);
174 }
175
Eric Erfaniand8046e52017-04-06 09:41:50 -0700176 @Override
177 public void onEnrichedCallStateChanged() {
178 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
179 updateNotification(CallList.getInstance());
180 }
181
Eric Erfanianccca3152017-02-22 16:32:36 -0800182 /**
183 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
184 * to a new incoming call.
185 *
186 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
187 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
188 * foreground activity is marked as "immersive".
189 *
190 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
191 * ringing connection" event from the telephony layer.)
192 *
193 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
194 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
195 * we'll simply update or cancel the in-call notification based on the current phone state.
196 *
197 * @see #updateInCallNotification(CallList)
198 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700199 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700200 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800201 updateInCallNotification(callList);
202 }
203
204 /**
205 * Take down the in-call notification.
206 *
207 * @see #updateInCallNotification(CallList)
208 */
209 private void cancelNotification() {
210 if (mStatusBarCallListener != null) {
211 setStatusBarCallListener(null);
212 }
213 if (mCurrentNotification != NOTIFICATION_NONE) {
Eric Erfanian8369df02017-05-03 10:27:13 -0700214 LogUtil.i("StatusBarNotifier.cancelNotification", "cancel");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700215 DialerNotificationManager.cancel(mContext, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanianccca3152017-02-22 16:32:36 -0800216 }
217 mCurrentNotification = NOTIFICATION_NONE;
218 }
219
220 /**
221 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
222 * status bar notification based on the current telephony state, or cancels the notification if
223 * the phone is totally idle.
224 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700225 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800226 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700227 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800228
229 final DialerCall call = getCallToShow(callList);
230
231 if (call != null) {
232 showNotification(callList, call);
233 } else {
234 cancelNotification();
235 }
236 }
237
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700238 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800239 private void showNotification(final CallList callList, final DialerCall call) {
wangqicf61ca02017-08-31 15:32:55 -0700240 Trace.beginSection("StatusBarNotifier.showNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800241 final boolean isIncoming =
242 (call.getState() == DialerCall.State.INCOMING
243 || call.getState() == DialerCall.State.CALL_WAITING);
244 setStatusBarCallListener(new StatusBarCallListener(call));
245
246 // we make a call to the contact info cache to query for supplemental data to what the
247 // call provides. This includes the contact name and photo.
248 // This callback will always get called immediately and synchronously with whatever data
249 // it has available, and may make a subsequent call later (same thread) if it had to
250 // call into the contacts provider for more data.
251 mContactInfoCache.findInfo(
252 call,
253 isIncoming,
254 new ContactInfoCacheCallback() {
255 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700256 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800257 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
258 DialerCall call = callList.getCallById(callId);
259 if (call != null) {
260 call.getLogState().contactLookupResult = entry.contactLookupResult;
261 buildAndSendNotification(callList, call, entry);
262 }
263 }
264
265 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700266 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800267 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
268 DialerCall call = callList.getCallById(callId);
269 if (call != null) {
270 buildAndSendNotification(callList, call, entry);
271 }
272 }
273 });
wangqicf61ca02017-08-31 15:32:55 -0700274 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 }
276
277 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700278 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 private void buildAndSendNotification(
280 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
wangqicf61ca02017-08-31 15:32:55 -0700281 Trace.beginSection("StatusBarNotifier.buildAndSendNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800282 // This can get called to update an existing notification after contact information has come
283 // back. However, it can happen much later. Before we continue, we need to make sure that
284 // the call being passed in is still the one we want to show in the notification.
285 final DialerCall call = getCallToShow(callList);
286 if (call == null || !call.getId().equals(originalCall.getId())) {
wangqicf61ca02017-08-31 15:32:55 -0700287 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800288 return;
289 }
290
wangqicf61ca02017-08-31 15:32:55 -0700291 Trace.beginSection("prepare work");
Eric Erfanianccca3152017-02-22 16:32:36 -0800292 final int callState = call.getState();
293
294 // Check if data has changed; if nothing is different, don't issue another notification.
295 final int iconResId = getIconToDisplay(call);
Eric Erfanian83b20212017-05-31 08:53:10 -0700296 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800297 final String content = getContentString(call, contactInfo.userType);
298 final String contentTitle = getContentTitle(contactInfo, call);
299
300 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700301 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700302 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800303 final int notificationType;
304 if (callState == DialerCall.State.INCOMING
305 || callState == DialerCall.State.CALL_WAITING
306 || isVideoUpgradeRequest) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700307 if (ConfigProviderBindings.get(mContext)
308 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
309 notificationType =
310 InCallPresenter.getInstance().isShowingInCallUi()
311 ? NOTIFICATION_INCOMING_CALL_QUIET
312 : NOTIFICATION_INCOMING_CALL;
313 } else {
314 boolean alreadyActive =
315 callList.getActiveOrBackgroundCall() != null
316 && InCallPresenter.getInstance().isShowingInCallUi();
317 notificationType =
318 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
319 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800320 } else {
321 notificationType = NOTIFICATION_IN_CALL;
322 }
wangqicf61ca02017-08-31 15:32:55 -0700323 Trace.endSection(); // prepare work
Eric Erfanianccca3152017-02-22 16:32:36 -0800324
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)) {
wangqicf61ca02017-08-31 15:32:55 -0700333 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800334 return;
335 }
336
337 if (largeIcon != null) {
338 largeIcon = getRoundedIcon(largeIcon);
339 }
340
341 // This builder is used for the notification shown when the device is locked and the user
342 // has set their notification settings to 'hide sensitive content'
343 // {@see Notification.Builder#setPublicVersion}.
344 Notification.Builder publicBuilder = new Notification.Builder(mContext);
345 publicBuilder
346 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700347 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800348 // Hide work call state for the lock screen notification
349 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
350 setNotificationWhen(call, callState, publicBuilder);
351
352 // Builder for the notification shown when the device is unlocked or the user has set their
353 // notification settings to 'show all notification content'.
354 final Notification.Builder builder = getNotificationBuilder();
355 builder.setPublicVersion(publicBuilder.build());
356
357 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700358 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800359
360 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700361 PhoneAccountHandle accountHandle = call.getAccountHandle();
362 if (accountHandle == null) {
363 accountHandle = getAnyPhoneAccount();
364 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700365
366 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
367 switch (notificationType) {
368 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700369 if (BuildCompat.isAtLeastO()) {
370 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
371 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700372 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
373 // Set the notification category and bump the priority for incoming calls
374 builder.setCategory(Notification.CATEGORY_CALL);
375 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700376 builder.setPriority(Notification.PRIORITY_MAX);
377 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
378 LogUtil.i(
379 "StatusBarNotifier.buildAndSendNotification",
380 "Canceling old notification so this one can be noisy");
381 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
382 // notification (if there is one) so the fullScreenIntent or HUN will show
Eric Erfanian2ca43182017-08-31 06:57:16 -0700383 DialerNotificationManager.cancel(mContext, NOTIFICATION_TAG, NOTIFICATION_ID);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700384 }
385 break;
386 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700387 if (BuildCompat.isAtLeastO()) {
388 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
389 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700390 break;
391 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700392 if (BuildCompat.isAtLeastO()) {
393 publicBuilder.setColorized(true);
394 builder.setColorized(true);
395 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
396 }
397 break;
398 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700399 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800400 }
401
402 // Set the content
403 builder.setContentText(content);
404 builder.setSmallIcon(iconResId);
405 builder.setContentTitle(contentTitle);
406 builder.setLargeIcon(largeIcon);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700407 builder.setColor(
408 mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()));
Eric Erfanianccca3152017-02-22 16:32:36 -0800409
410 if (isVideoUpgradeRequest) {
411 builder.setUsesChronometer(false);
412 addDismissUpgradeRequestAction(builder);
413 addAcceptUpgradeRequestAction(builder);
414 } else {
415 createIncomingCallNotification(call, callState, builder);
416 }
417
418 addPersonReference(builder, contactInfo, call);
419
wangqicf61ca02017-08-31 15:32:55 -0700420 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800421 // Fire off the notification
422 Notification notification = builder.build();
423
424 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
425 notification.flags |= Notification.FLAG_INSISTENT;
426 notification.sound = contactInfo.contactRingtoneUri;
427 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
428 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
429 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
430 notification.audioAttributes = audioAttributes.build();
431 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
432 notification.vibrate = VIBRATE_PATTERN;
433 }
434 }
435 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700436 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800437 mDialerRingtoneManager.playCallWaitingTone();
438 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800439
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700440 LogUtil.i(
441 "StatusBarNotifier.buildAndSendNotification",
442 "displaying notification for " + notificationType);
443
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 try {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700445 DialerNotificationManager.notify(mContext, NOTIFICATION_TAG, NOTIFICATION_ID, notification);
Eric Erfanianccca3152017-02-22 16:32:36 -0800446 } catch (RuntimeException e) {
447 // TODO(b/34744003): Move the memory stats into silent feedback PSD.
448 ActivityManager activityManager = mContext.getSystemService(ActivityManager.class);
449 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
450 activityManager.getMemoryInfo(memoryInfo);
451 throw new RuntimeException(
452 String.format(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700453 Locale.US,
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)",
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700455 contactInfo.photoType,
456 memoryInfo.lowMemory,
457 memoryInfo.availMem),
Eric Erfanianccca3152017-02-22 16:32:36 -0800458 e);
459 }
wangqicf61ca02017-08-31 15:32:55 -0700460 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800461 call.getLatencyReport().onNotificationShown();
462 mCurrentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700463 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800464 }
465
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700466 @Nullable
467 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
468 private PhoneAccountHandle getAnyPhoneAccount() {
469 PhoneAccountHandle accountHandle;
470 TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
471 accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
472 if (accountHandle == null) {
473 List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
474 if (!accountHandles.isEmpty()) {
475 accountHandle = accountHandles.get(0);
476 }
477 }
478 return accountHandle;
479 }
480
Eric Erfanianccca3152017-02-22 16:32:36 -0800481 private void createIncomingCallNotification(
482 DialerCall call, int state, Notification.Builder builder) {
483 setNotificationWhen(call, state, builder);
484
485 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
486 if (state == DialerCall.State.ACTIVE
487 || state == DialerCall.State.ONHOLD
488 || DialerCall.State.isDialing(state)) {
489 addHangupAction(builder);
490 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
491 addDismissAction(builder);
492 if (call.isVideoCall()) {
493 addVideoCallAction(builder);
494 } else {
495 addAnswerAction(builder);
496 }
497 }
498 }
499
500 /**
501 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
502 * duration of the call. For all other states, the notification will automatically show the time
503 * at which the notification was created.
504 */
505 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
506 if (state == DialerCall.State.ACTIVE) {
507 builder.setUsesChronometer(true);
508 builder.setWhen(call.getConnectTimeMillis());
509 } else {
510 builder.setUsesChronometer(false);
511 }
512 }
513
514 /**
515 * Checks the new notification data and compares it against any notification that we are already
516 * displaying. If the data is exactly the same, we return false so that we do not issue a new
517 * notification for the exact same data.
518 */
519 private boolean checkForChangeAndSaveData(
520 int icon,
521 String content,
522 Bitmap largeIcon,
523 String contentTitle,
524 int state,
525 int notificationType,
Eric Erfanian8369df02017-05-03 10:27:13 -0700526 Uri ringtone) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800527
528 // The two are different:
529 // if new title is not null, it should be different from saved version OR
530 // if new title is null, the saved version should not be null
531 final boolean contentTitleChanged =
532 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
533 || (contentTitle == null && mSavedContentTitle != null);
534
Eric Erfanian8369df02017-05-03 10:27:13 -0700535 boolean largeIconChanged =
536 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
537
Eric Erfanianccca3152017-02-22 16:32:36 -0800538 // any change means we are definitely updating
539 boolean retval =
540 (mSavedIcon != icon)
541 || !Objects.equals(mSavedContent, content)
542 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700543 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800544 || contentTitleChanged
Eric Erfanian8369df02017-05-03 10:27:13 -0700545 || !Objects.equals(mRingtone, ringtone);
Eric Erfanianccca3152017-02-22 16:32:36 -0800546
547 // If we aren't showing a notification right now or the notification type is changing,
548 // definitely do an update.
549 if (mCurrentNotification != notificationType) {
550 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700551 LogUtil.d(
552 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800553 }
554 retval = true;
555 }
556
557 mSavedIcon = icon;
558 mSavedContent = content;
559 mCallState = state;
560 mSavedLargeIcon = largeIcon;
561 mSavedContentTitle = contentTitle;
562 mRingtone = ringtone;
563
564 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700565 LogUtil.d(
566 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800567 }
568
569 return retval;
570 }
571
572 /** Returns the main string to use in the notification. */
573 @VisibleForTesting
574 @Nullable
575 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700576 if (call.isConferenceCall()) {
577 return CallerInfoUtils.getConferenceString(
578 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800579 }
580
581 String preferredName =
582 ContactDisplayUtils.getPreferredDisplayName(
583 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
584 if (TextUtils.isEmpty(preferredName)) {
585 return TextUtils.isEmpty(contactInfo.number)
586 ? null
587 : BidiFormatter.getInstance()
588 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
589 }
590 return preferredName;
591 }
592
593 private void addPersonReference(
594 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
595 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
596 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
597 // NotificationManager using it.
598 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
599 builder.addPerson(contactInfo.lookupUri.toString());
600 } else if (!TextUtils.isEmpty(call.getNumber())) {
601 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
602 }
603 }
604
605 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700606 private static Bitmap getLargeIconToDisplay(
607 Context context, ContactCacheEntry contactInfo, DialerCall call) {
608 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800609 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800610 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
611 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
612 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700613 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700614 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
615 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700616 @ContactType
617 int contactType =
618 LetterTileDrawable.getContactTypeFromPrimitives(
619 CallerInfoUtils.isVoiceMailNumber(context, call),
620 call.isSpam(),
621 contactInfo.isBusiness,
622 call.getNumberPresentation(),
623 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700624 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700625
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700626 lettertile.setCanonicalDialerLetterTileDetails(
627 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
628 contactInfo.lookupKey,
629 LetterTileDrawable.SHAPE_CIRCLE,
630 contactType);
631 largeIcon = lettertile.getBitmap(width, height);
632 }
633
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700635 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800636 largeIcon = DrawableConverter.drawableToBitmap(drawable);
637 }
638 return largeIcon;
639 }
640
641 private Bitmap getRoundedIcon(Bitmap bitmap) {
642 if (bitmap == null) {
643 return null;
644 }
645 final int height =
646 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
647 final int width =
648 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
649 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
650 }
651
652 /**
653 * Returns the appropriate icon res Id to display based on the call for which we want to display
654 * information.
655 */
656 private int getIconToDisplay(DialerCall call) {
657 // Even if both lines are in use, we only show a single item in
658 // the expanded Notifications UI. It's labeled "Ongoing call"
659 // (or "On hold" if there's only one call, and it's on hold.)
660 // Also, we don't have room to display caller-id info from two
661 // different calls. So if both lines are in use, display info
662 // from the foreground call. And if there's a ringing call,
663 // display that regardless of the state of the other calls.
664 if (call.getState() == DialerCall.State.ONHOLD) {
665 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700666 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700667 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700668 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700669 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
670 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700671 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
672 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
673 // 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 -0700674 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800675 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700676 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
677 if (ReturnToCallController.isEnabled(mContext)) {
678 return R.drawable.quantum_ic_call_vd_theme_24;
679 } else {
680 return R.drawable.on_going_call;
681 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800682 }
683
684 /** Returns the message to use with the notification. */
685 private String getContentString(DialerCall call, @UserType long userType) {
686 boolean isIncomingOrWaiting =
687 call.getState() == DialerCall.State.INCOMING
688 || call.getState() == DialerCall.State.CALL_WAITING;
689
690 if (isIncomingOrWaiting
691 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
692
693 if (!TextUtils.isEmpty(call.getChildNumber())) {
694 return mContext.getString(R.string.child_number, call.getChildNumber());
695 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
696 return call.getCallSubject();
697 }
698 }
699
700 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700701 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700703 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 }
705
706 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700707 if (call.isSpam()) {
708 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700709 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
710 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700711 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700712 resId = R.string.notification_incoming_call_wifi_template;
713
Eric Erfanianccca3152017-02-22 16:32:36 -0800714 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700715 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 }
717 } else if (call.getState() == DialerCall.State.ONHOLD) {
718 resId = R.string.notification_on_hold;
719 } else if (DialerCall.State.isDialing(call.getState())) {
720 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700721 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700722 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800723 resId = R.string.notification_requesting_video_call;
724 }
725
726 // Is the call placed through work connection service.
727 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
728 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
729 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700730 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
731 }
732
733 if (resId == R.string.notification_incoming_call_wifi_template
734 || resId == R.string.notification_ongoing_call_wifi_template) {
735 // TODO(b/64525903): Potentially apply this template logic everywhere.
736 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800737 }
738
739 return mContext.getString(resId);
740 }
741
Eric Erfanian2ca43182017-08-31 06:57:16 -0700742 private boolean shouldShowEnrichedCallNotification(Session session) {
743 if (session == null) {
744 return false;
745 }
746 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
747 }
748
Eric Erfaniand8046e52017-04-06 09:41:50 -0700749 private int getECIncomingCallText(Session session) {
750 int resId;
751 MultimediaData data = session.getMultimediaData();
752 boolean hasImage = data.hasImageData();
753 boolean hasSubject = !TextUtils.isEmpty(data.getText());
754 boolean hasMap = data.getLocation() != null;
755 if (data.isImportant()) {
756 if (hasMap) {
757 if (hasImage) {
758 if (hasSubject) {
759 resId = R.string.important_notification_incoming_call_with_photo_message_location;
760 } else {
761 resId = R.string.important_notification_incoming_call_with_photo_location;
762 }
763 } else if (hasSubject) {
764 resId = R.string.important_notification_incoming_call_with_message_location;
765 } else {
766 resId = R.string.important_notification_incoming_call_with_location;
767 }
768 } else if (hasImage) {
769 if (hasSubject) {
770 resId = R.string.important_notification_incoming_call_with_photo_message;
771 } else {
772 resId = R.string.important_notification_incoming_call_with_photo;
773 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700774 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700775 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700776 } else {
777 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700778 }
779 if (mContext.getString(resId).length() > 50) {
780 resId = R.string.important_notification_incoming_call_attachments;
781 }
782 } else {
783 if (hasMap) {
784 if (hasImage) {
785 if (hasSubject) {
786 resId = R.string.notification_incoming_call_with_photo_message_location;
787 } else {
788 resId = R.string.notification_incoming_call_with_photo_location;
789 }
790 } else if (hasSubject) {
791 resId = R.string.notification_incoming_call_with_message_location;
792 } else {
793 resId = R.string.notification_incoming_call_with_location;
794 }
795 } else if (hasImage) {
796 if (hasSubject) {
797 resId = R.string.notification_incoming_call_with_photo_message;
798 } else {
799 resId = R.string.notification_incoming_call_with_photo;
800 }
801 } else {
802 resId = R.string.notification_incoming_call_with_message;
803 }
804 }
805 if (mContext.getString(resId).length() > 50) {
806 resId = R.string.notification_incoming_call_attachments;
807 }
808 return resId;
809 }
810
Eric Erfanianccca3152017-02-22 16:32:36 -0800811 /** Gets the most relevant call to display in the notification. */
812 private DialerCall getCallToShow(CallList callList) {
813 if (callList == null) {
814 return null;
815 }
816 DialerCall call = callList.getIncomingCall();
817 if (call == null) {
818 call = callList.getOutgoingCall();
819 }
820 if (call == null) {
821 call = callList.getVideoUpgradeRequestCall();
822 }
823 if (call == null) {
824 call = callList.getActiveOrBackgroundCall();
825 }
826 return call;
827 }
828
829 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
830 Spannable spannable = new SpannableString(mContext.getText(stringRes));
831 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
832 // This will only work for cases where the Notification.Builder has a fullscreen intent set
833 // Notification.Builder that does not have a full screen intent will take the color of the
834 // app and the following leads to a no-op.
835 spannable.setSpan(
836 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
837 }
838 return spannable;
839 }
840
841 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700842 LogUtil.d(
843 "StatusBarNotifier.addAnswerAction",
844 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800845 PendingIntent answerVoicePendingIntent =
846 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
847 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700848 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700849 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700850 getActionText(
851 R.string.notification_action_answer, R.color.notification_action_accept),
852 answerVoicePendingIntent)
853 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800854 }
855
856 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700857 LogUtil.d(
858 "StatusBarNotifier.addDismissAction",
859 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 PendingIntent declinePendingIntent =
861 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
862 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700863 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700864 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700865 getActionText(
866 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
867 declinePendingIntent)
868 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800869 }
870
871 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700872 LogUtil.d(
873 "StatusBarNotifier.addHangupAction",
874 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800875 PendingIntent hangupPendingIntent =
876 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
877 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700878 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700879 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700880 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700881 hangupPendingIntent)
882 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 }
884
885 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700886 LogUtil.i(
887 "StatusBarNotifier.addVideoCallAction",
888 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800889 PendingIntent answerVideoPendingIntent =
890 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
891 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700892 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700893 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700894 getActionText(
895 R.string.notification_action_answer_video,
896 R.color.notification_action_answer_video),
897 answerVideoPendingIntent)
898 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800899 }
900
901 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700902 LogUtil.i(
903 "StatusBarNotifier.addAcceptUpgradeRequestAction",
904 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800905 PendingIntent acceptVideoPendingIntent =
906 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
907 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700908 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700909 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700910 getActionText(
911 R.string.notification_action_accept, R.color.notification_action_accept),
912 acceptVideoPendingIntent)
913 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800914 }
915
916 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700917 LogUtil.i(
918 "StatusBarNotifier.addDismissUpgradeRequestAction",
919 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800920 PendingIntent declineVideoPendingIntent =
921 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
922 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700923 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700924 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700925 getActionText(
926 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
927 declineVideoPendingIntent)
928 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 }
930
931 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700932 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800933 // Ok, we actually want to launch the incoming call
934 // UI at this point (in addition to simply posting a notification
935 // to the status bar). Setting fullScreenIntent will cause
936 // the InCallScreen to be launched immediately *unless* the
937 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700938 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800939 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800940 }
941
942 private Notification.Builder getNotificationBuilder() {
943 final Notification.Builder builder = new Notification.Builder(mContext);
944 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700945 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700946 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -0700947 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -0700948 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800949
950 return builder;
951 }
952
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700953 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800954 Intent intent =
955 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700956 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800957
Eric Erfanian2ca43182017-08-31 06:57:16 -0700958 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 if (isFullScreen) {
960 // Use a unique request code so that the pending intent isn't clobbered by the
961 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700962 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 }
964
965 // PendingIntent that can be used to launch the InCallActivity. The
966 // system fires off this intent if the user pulls down the windowshade
967 // and clicks the notification's expanded view. It's also used to
968 // launch the InCallActivity immediately when when there's an incoming
969 // call (see the "fullScreenIntent" field below).
970 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
971 }
972
973 private void setStatusBarCallListener(StatusBarCallListener listener) {
974 if (mStatusBarCallListener != null) {
975 mStatusBarCallListener.cleanup();
976 }
977 mStatusBarCallListener = listener;
978 }
979
980 private class StatusBarCallListener implements DialerCallListener {
981
982 private DialerCall mDialerCall;
983
984 StatusBarCallListener(DialerCall dialerCall) {
985 mDialerCall = dialerCall;
986 mDialerCall.addListener(this);
987 }
988
989 void cleanup() {
990 mDialerCall.removeListener(this);
991 }
992
993 @Override
994 public void onDialerCallDisconnect() {}
995
996 @Override
997 public void onDialerCallUpdate() {
998 if (CallList.getInstance().getIncomingCall() == null) {
999 mDialerRingtoneManager.stopCallWaitingTone();
1000 }
1001 }
1002
1003 @Override
1004 public void onDialerCallChildNumberChange() {}
1005
1006 @Override
1007 public void onDialerCallLastForwardedNumberChange() {}
1008
1009 @Override
1010 public void onDialerCallUpgradeToVideo() {}
1011
1012 @Override
1013 public void onWiFiToLteHandover() {}
1014
1015 @Override
1016 public void onHandoverToWifiFailure() {}
1017
Eric Erfanianc857f902017-05-15 14:05:33 -07001018 @Override
1019 public void onInternationalCallOnWifi() {}
1020
Eric Erfanian2ca43182017-08-31 06:57:16 -07001021 @Override
1022 public void onEnrichedCallSessionUpdate() {}
1023
Eric Erfanianccca3152017-02-22 16:32:36 -08001024 /**
1025 * Responds to changes in the session modification state for the call by dismissing the status
1026 * bar notification as required.
1027 */
1028 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001029 public void onDialerCallSessionModificationStateChange() {
1030 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001031 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001032 cleanup();
1033 updateNotification(CallList.getInstance());
1034 }
1035 }
1036 }
1037}