blob: 86ad9ffe514e2ccd5c15a47d440138a24b7f0023 [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;
yuegb26c1ae2017-09-18 16:59:16 -070027import static com.android.incallui.NotificationBroadcastReceiver.ACTION_TURN_OFF_SPEAKER;
28import static com.android.incallui.NotificationBroadcastReceiver.ACTION_TURN_ON_SPEAKER;
Eric Erfanianccca3152017-02-22 16:32:36 -080029
Eric Erfaniand5e47f62017-03-15 14:41:07 -070030import android.Manifest;
Eric Erfanianccca3152017-02-22 16:32:36 -080031import android.app.Notification;
Eric Erfanianccca3152017-02-22 16:32:36 -080032import android.app.PendingIntent;
33import android.content.Context;
34import android.content.Intent;
Eric Erfanian83b20212017-05-31 08:53:10 -070035import android.content.res.Resources;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.graphics.Bitmap;
Eric Erfanianccca3152017-02-22 16:32:36 -080037import android.graphics.drawable.BitmapDrawable;
38import android.graphics.drawable.Drawable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070039import android.graphics.drawable.Icon;
Eric Erfanianccca3152017-02-22 16:32:36 -080040import android.media.AudioAttributes;
41import android.net.Uri;
42import android.os.Build.VERSION;
43import android.os.Build.VERSION_CODES;
wangqicf61ca02017-08-31 15:32:55 -070044import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import android.support.annotation.ColorRes;
46import android.support.annotation.NonNull;
47import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070048import android.support.annotation.RequiresPermission;
Eric Erfanianccca3152017-02-22 16:32:36 -080049import android.support.annotation.StringRes;
50import android.support.annotation.VisibleForTesting;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070051import android.support.v4.os.BuildCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080052import android.telecom.Call.Details;
yuegb26c1ae2017-09-18 16:59:16 -070053import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import android.telecom.PhoneAccount;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070055import android.telecom.PhoneAccountHandle;
Eric Erfanianccca3152017-02-22 16:32:36 -080056import android.telecom.TelecomManager;
57import android.text.BidiFormatter;
58import android.text.Spannable;
59import android.text.SpannableString;
60import android.text.TextDirectionHeuristics;
61import android.text.TextUtils;
62import android.text.style.ForegroundColorSpan;
63import com.android.contacts.common.ContactsUtils;
64import com.android.contacts.common.ContactsUtils.UserType;
65import com.android.contacts.common.preference.ContactsPreferences;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.contacts.common.util.ContactDisplayUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070067import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070069import com.android.dialer.configprovider.ConfigProviderBindings;
70import com.android.dialer.contactphoto.BitmapUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070071import com.android.dialer.enrichedcall.EnrichedCallManager;
72import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070073import com.android.dialer.lettertile.LetterTileDrawable;
74import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070075import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070076import com.android.dialer.notification.NotificationChannelId;
Eric Erfanian90508232017-03-24 09:31:16 -070077import com.android.dialer.oem.MotorolaUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import com.android.dialer.util.DrawableConverter;
79import com.android.incallui.ContactInfoCache.ContactCacheEntry;
80import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
81import com.android.incallui.InCallPresenter.InCallState;
82import com.android.incallui.async.PausableExecutorImpl;
yuegb26c1ae2017-09-18 16:59:16 -070083import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080084import com.android.incallui.call.CallList;
85import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080086import com.android.incallui.call.DialerCallListener;
yueg01a964d2017-10-03 15:25:41 -070087import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080088import com.android.incallui.ringtone.DialerRingtoneManager;
89import com.android.incallui.ringtone.InCallTonePlayer;
90import com.android.incallui.ringtone.ToneGeneratorFactory;
Eric Erfanian90508232017-03-24 09:31:16 -070091import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070092import java.util.List;
Eric Erfanianccca3152017-02-22 16:32:36 -080093import java.util.Objects;
94
95/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -070096public class StatusBarNotifier
yuegb26c1ae2017-09-18 16:59:16 -070097 implements InCallPresenter.InCallStateListener,
98 EnrichedCallManager.StateChangedListener,
99 AudioModeProvider.AudioModeListener {
Eric Erfanianccca3152017-02-22 16:32:36 -0800100
Eric Erfanian2ca43182017-08-31 06:57:16 -0700101 private static final int NOTIFICATION_ID = 1;
102
Eric Erfanianccca3152017-02-22 16:32:36 -0800103 // Notification types
104 // Indicates that no notification is currently showing.
105 private static final int NOTIFICATION_NONE = 0;
106 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700107 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800108 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700109 private static final int NOTIFICATION_INCOMING_CALL = 2;
110 // Notification for incoming calls in the case where there is already an active call.
111 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
112 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800113
Eric Erfanianccca3152017-02-22 16:32:36 -0800114 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
115
116 private final Context mContext;
117 private final ContactInfoCache mContactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800118 private final DialerRingtoneManager mDialerRingtoneManager;
119 @Nullable private ContactsPreferences mContactsPreferences;
120 private int mCurrentNotification = NOTIFICATION_NONE;
121 private int mCallState = DialerCall.State.INVALID;
122 private int mSavedIcon = 0;
123 private String mSavedContent = null;
124 private Bitmap mSavedLargeIcon;
125 private String mSavedContentTitle;
yuegb26c1ae2017-09-18 16:59:16 -0700126 private CallAudioState savedCallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 private Uri mRingtone;
128 private StatusBarCallListener mStatusBarCallListener;
129
Eric Erfaniand8046e52017-04-06 09:41:50 -0700130 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700131 mContext = Assert.isNotNull(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800132 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
133 mContactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800134 mDialerRingtoneManager =
135 new DialerRingtoneManager(
136 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
137 CallList.getInstance());
138 mCurrentNotification = NOTIFICATION_NONE;
yuegb26c1ae2017-09-18 16:59:16 -0700139 AudioModeProvider.getInstance().addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -0800140 }
141
142 /**
143 * Should only be called from a irrecoverable state where it is necessary to dismiss all
144 * notifications.
145 */
yueg01a964d2017-10-03 15:25:41 -0700146 static void clearAllCallNotifications() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700147 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700148 "StatusBarNotifier.clearAllCallNotifications",
149 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800150
yueg01a964d2017-10-03 15:25:41 -0700151 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800152 }
153
154 private static int getWorkStringFromPersonalString(int resId) {
155 if (resId == R.string.notification_ongoing_call) {
156 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800157 } else if (resId == R.string.notification_incoming_call) {
158 return R.string.notification_incoming_work_call;
159 } else {
160 return resId;
161 }
162 }
163
164 /**
165 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
166 * context.
167 */
168 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
169 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
170 return PendingIntent.getBroadcast(context, 0, intent, 0);
171 }
172
173 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
174 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700175 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800176 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700177 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800178 updateNotification(callList);
179 }
180
Eric Erfaniand8046e52017-04-06 09:41:50 -0700181 @Override
182 public void onEnrichedCallStateChanged() {
183 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
184 updateNotification(CallList.getInstance());
185 }
186
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 /**
188 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
189 * to a new incoming call.
190 *
191 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
192 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
193 * foreground activity is marked as "immersive".
194 *
195 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
196 * ringing connection" event from the telephony layer.)
197 *
198 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
199 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
200 * we'll simply update or cancel the in-call notification based on the current phone state.
201 *
202 * @see #updateInCallNotification(CallList)
203 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700204 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfaniand8046e52017-04-06 09:41:50 -0700205 public void updateNotification(CallList callList) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800206 updateInCallNotification(callList);
207 }
208
209 /**
210 * Take down the in-call notification.
211 *
212 * @see #updateInCallNotification(CallList)
213 */
214 private void cancelNotification() {
215 if (mStatusBarCallListener != null) {
216 setStatusBarCallListener(null);
217 }
218 if (mCurrentNotification != NOTIFICATION_NONE) {
yueg01a964d2017-10-03 15:25:41 -0700219 TelecomAdapter.getInstance().stopForegroundNotification();
220 mCurrentNotification = NOTIFICATION_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800221 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800222 }
223
224 /**
225 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
226 * status bar notification based on the current telephony state, or cancels the notification if
227 * the phone is totally idle.
228 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700229 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800230 private void updateInCallNotification(CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700231 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800232
233 final DialerCall call = getCallToShow(callList);
234
235 if (call != null) {
236 showNotification(callList, call);
237 } else {
238 cancelNotification();
239 }
240 }
241
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700242 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800243 private void showNotification(final CallList callList, final DialerCall call) {
wangqicf61ca02017-08-31 15:32:55 -0700244 Trace.beginSection("StatusBarNotifier.showNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800245 final boolean isIncoming =
246 (call.getState() == DialerCall.State.INCOMING
247 || call.getState() == DialerCall.State.CALL_WAITING);
248 setStatusBarCallListener(new StatusBarCallListener(call));
249
250 // we make a call to the contact info cache to query for supplemental data to what the
251 // call provides. This includes the contact name and photo.
252 // This callback will always get called immediately and synchronously with whatever data
253 // it has available, and may make a subsequent call later (same thread) if it had to
254 // call into the contacts provider for more data.
255 mContactInfoCache.findInfo(
256 call,
257 isIncoming,
258 new ContactInfoCacheCallback() {
259 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700260 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800261 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
262 DialerCall call = callList.getCallById(callId);
263 if (call != null) {
264 call.getLogState().contactLookupResult = entry.contactLookupResult;
265 buildAndSendNotification(callList, call, entry);
266 }
267 }
268
269 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700270 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
272 DialerCall call = callList.getCallById(callId);
273 if (call != null) {
274 buildAndSendNotification(callList, call, entry);
275 }
276 }
277 });
wangqicf61ca02017-08-31 15:32:55 -0700278 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 }
280
281 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700282 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800283 private void buildAndSendNotification(
284 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
wangqicf61ca02017-08-31 15:32:55 -0700285 Trace.beginSection("StatusBarNotifier.buildAndSendNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800286 // This can get called to update an existing notification after contact information has come
287 // back. However, it can happen much later. Before we continue, we need to make sure that
288 // the call being passed in is still the one we want to show in the notification.
289 final DialerCall call = getCallToShow(callList);
290 if (call == null || !call.getId().equals(originalCall.getId())) {
wangqicf61ca02017-08-31 15:32:55 -0700291 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800292 return;
293 }
294
wangqicf61ca02017-08-31 15:32:55 -0700295 Trace.beginSection("prepare work");
Eric Erfanianccca3152017-02-22 16:32:36 -0800296 final int callState = call.getState();
yuegb26c1ae2017-09-18 16:59:16 -0700297 final CallAudioState callAudioState = AudioModeProvider.getInstance().getAudioState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800298
299 // Check if data has changed; if nothing is different, don't issue another notification.
300 final int iconResId = getIconToDisplay(call);
Eric Erfanian83b20212017-05-31 08:53:10 -0700301 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800302 final String content = getContentString(call, contactInfo.userType);
303 final String contentTitle = getContentTitle(contactInfo, call);
304
305 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700306 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700307 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800308 final int notificationType;
309 if (callState == DialerCall.State.INCOMING
310 || callState == DialerCall.State.CALL_WAITING
311 || isVideoUpgradeRequest) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700312 if (ConfigProviderBindings.get(mContext)
313 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
314 notificationType =
315 InCallPresenter.getInstance().isShowingInCallUi()
316 ? NOTIFICATION_INCOMING_CALL_QUIET
317 : NOTIFICATION_INCOMING_CALL;
318 } else {
319 boolean alreadyActive =
320 callList.getActiveOrBackgroundCall() != null
321 && InCallPresenter.getInstance().isShowingInCallUi();
322 notificationType =
323 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
324 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800325 } else {
326 notificationType = NOTIFICATION_IN_CALL;
327 }
wangqicf61ca02017-08-31 15:32:55 -0700328 Trace.endSection(); // prepare work
Eric Erfanianccca3152017-02-22 16:32:36 -0800329
330 if (!checkForChangeAndSaveData(
331 iconResId,
332 content,
333 largeIcon,
334 contentTitle,
335 callState,
336 notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700337 contactInfo.contactRingtoneUri,
338 callAudioState)) {
wangqicf61ca02017-08-31 15:32:55 -0700339 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800340 return;
341 }
342
343 if (largeIcon != null) {
344 largeIcon = getRoundedIcon(largeIcon);
345 }
346
347 // This builder is used for the notification shown when the device is locked and the user
348 // has set their notification settings to 'hide sensitive content'
349 // {@see Notification.Builder#setPublicVersion}.
350 Notification.Builder publicBuilder = new Notification.Builder(mContext);
351 publicBuilder
352 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700353 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800354 // Hide work call state for the lock screen notification
355 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
356 setNotificationWhen(call, callState, publicBuilder);
357
358 // Builder for the notification shown when the device is unlocked or the user has set their
359 // notification settings to 'show all notification content'.
360 final Notification.Builder builder = getNotificationBuilder();
361 builder.setPublicVersion(publicBuilder.build());
362
363 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700364 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800365
366 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700367 PhoneAccountHandle accountHandle = call.getAccountHandle();
368 if (accountHandle == null) {
369 accountHandle = getAnyPhoneAccount();
370 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700371
372 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
373 switch (notificationType) {
374 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700375 if (BuildCompat.isAtLeastO()) {
376 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
377 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700378 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
379 // Set the notification category and bump the priority for incoming calls
380 builder.setCategory(Notification.CATEGORY_CALL);
381 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700382 builder.setPriority(Notification.PRIORITY_MAX);
383 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
384 LogUtil.i(
385 "StatusBarNotifier.buildAndSendNotification",
386 "Canceling old notification so this one can be noisy");
387 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
388 // notification (if there is one) so the fullScreenIntent or HUN will show
yueg01a964d2017-10-03 15:25:41 -0700389 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700390 }
391 break;
392 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700393 if (BuildCompat.isAtLeastO()) {
394 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
395 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700396 break;
397 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700398 if (BuildCompat.isAtLeastO()) {
399 publicBuilder.setColorized(true);
400 builder.setColorized(true);
401 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
402 }
yueg01a964d2017-10-03 15:25:41 -0700403 // This will be ignored on O+ and handled by the channel
404 builder.setPriority(Notification.PRIORITY_MAX);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700405 break;
406 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700407 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800408 }
409
410 // Set the content
411 builder.setContentText(content);
412 builder.setSmallIcon(iconResId);
413 builder.setContentTitle(contentTitle);
414 builder.setLargeIcon(largeIcon);
yueg01a964d2017-10-03 15:25:41 -0700415 builder.setColor(InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800416
417 if (isVideoUpgradeRequest) {
418 builder.setUsesChronometer(false);
419 addDismissUpgradeRequestAction(builder);
420 addAcceptUpgradeRequestAction(builder);
421 } else {
yuegb26c1ae2017-09-18 16:59:16 -0700422 createIncomingCallNotification(call, callState, callAudioState, builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800423 }
424
425 addPersonReference(builder, contactInfo, call);
426
wangqicf61ca02017-08-31 15:32:55 -0700427 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800428 // Fire off the notification
429 Notification notification = builder.build();
430
431 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
432 notification.flags |= Notification.FLAG_INSISTENT;
433 notification.sound = contactInfo.contactRingtoneUri;
434 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
435 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
436 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
437 notification.audioAttributes = audioAttributes.build();
438 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
439 notification.vibrate = VIBRATE_PATTERN;
440 }
441 }
442 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700443 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 mDialerRingtoneManager.playCallWaitingTone();
445 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800446
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700447 LogUtil.i(
448 "StatusBarNotifier.buildAndSendNotification",
449 "displaying notification for " + notificationType);
450
yueg01a964d2017-10-03 15:25:41 -0700451 // If a notification exists, this will only update it.
452 TelecomAdapter.getInstance().startForegroundNotification(NOTIFICATION_ID, notification);
453
wangqicf61ca02017-08-31 15:32:55 -0700454 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800455 call.getLatencyReport().onNotificationShown();
456 mCurrentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700457 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800458 }
459
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700460 @Nullable
461 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
462 private PhoneAccountHandle getAnyPhoneAccount() {
463 PhoneAccountHandle accountHandle;
464 TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
465 accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL);
466 if (accountHandle == null) {
467 List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
468 if (!accountHandles.isEmpty()) {
469 accountHandle = accountHandles.get(0);
470 }
471 }
472 return accountHandle;
473 }
474
Eric Erfanianccca3152017-02-22 16:32:36 -0800475 private void createIncomingCallNotification(
yuegb26c1ae2017-09-18 16:59:16 -0700476 DialerCall call, int state, CallAudioState callAudioState, Notification.Builder builder) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800477 setNotificationWhen(call, state, builder);
478
479 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
480 if (state == DialerCall.State.ACTIVE
481 || state == DialerCall.State.ONHOLD
482 || DialerCall.State.isDialing(state)) {
483 addHangupAction(builder);
yuegb26c1ae2017-09-18 16:59:16 -0700484 addSpeakerAction(builder, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800485 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
486 addDismissAction(builder);
487 if (call.isVideoCall()) {
488 addVideoCallAction(builder);
489 } else {
490 addAnswerAction(builder);
491 }
492 }
493 }
494
495 /**
496 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
497 * duration of the call. For all other states, the notification will automatically show the time
498 * at which the notification was created.
499 */
500 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
501 if (state == DialerCall.State.ACTIVE) {
502 builder.setUsesChronometer(true);
503 builder.setWhen(call.getConnectTimeMillis());
504 } else {
505 builder.setUsesChronometer(false);
506 }
507 }
508
509 /**
510 * Checks the new notification data and compares it against any notification that we are already
511 * displaying. If the data is exactly the same, we return false so that we do not issue a new
512 * notification for the exact same data.
513 */
514 private boolean checkForChangeAndSaveData(
515 int icon,
516 String content,
517 Bitmap largeIcon,
518 String contentTitle,
519 int state,
520 int notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700521 Uri ringtone,
522 CallAudioState callAudioState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800523
524 // The two are different:
525 // if new title is not null, it should be different from saved version OR
526 // if new title is null, the saved version should not be null
527 final boolean contentTitleChanged =
528 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
529 || (contentTitle == null && mSavedContentTitle != null);
530
Eric Erfanian8369df02017-05-03 10:27:13 -0700531 boolean largeIconChanged =
532 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
533
Eric Erfanianccca3152017-02-22 16:32:36 -0800534 // any change means we are definitely updating
535 boolean retval =
536 (mSavedIcon != icon)
537 || !Objects.equals(mSavedContent, content)
538 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700539 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800540 || contentTitleChanged
yuegb26c1ae2017-09-18 16:59:16 -0700541 || !Objects.equals(mRingtone, ringtone)
542 || !Objects.equals(savedCallAudioState, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800543
544 // If we aren't showing a notification right now or the notification type is changing,
545 // definitely do an update.
546 if (mCurrentNotification != notificationType) {
547 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700548 LogUtil.d(
549 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800550 }
551 retval = true;
552 }
553
554 mSavedIcon = icon;
555 mSavedContent = content;
556 mCallState = state;
557 mSavedLargeIcon = largeIcon;
558 mSavedContentTitle = contentTitle;
559 mRingtone = ringtone;
yuegb26c1ae2017-09-18 16:59:16 -0700560 savedCallAudioState = callAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800561
562 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700563 LogUtil.d(
564 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800565 }
566
567 return retval;
568 }
569
570 /** Returns the main string to use in the notification. */
571 @VisibleForTesting
572 @Nullable
573 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700574 if (call.isConferenceCall()) {
575 return CallerInfoUtils.getConferenceString(
576 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800577 }
578
579 String preferredName =
580 ContactDisplayUtils.getPreferredDisplayName(
581 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
582 if (TextUtils.isEmpty(preferredName)) {
583 return TextUtils.isEmpty(contactInfo.number)
584 ? null
585 : BidiFormatter.getInstance()
586 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
587 }
588 return preferredName;
589 }
590
591 private void addPersonReference(
592 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
593 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
594 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
595 // NotificationManager using it.
596 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
597 builder.addPerson(contactInfo.lookupUri.toString());
598 } else if (!TextUtils.isEmpty(call.getNumber())) {
599 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
600 }
601 }
602
603 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700604 private static Bitmap getLargeIconToDisplay(
605 Context context, ContactCacheEntry contactInfo, DialerCall call) {
606 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800607 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800608 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
609 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
610 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700611 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700612 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
613 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700614 @ContactType
615 int contactType =
616 LetterTileDrawable.getContactTypeFromPrimitives(
617 CallerInfoUtils.isVoiceMailNumber(context, call),
618 call.isSpam(),
619 contactInfo.isBusiness,
620 call.getNumberPresentation(),
621 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700622 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700623
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700624 lettertile.setCanonicalDialerLetterTileDetails(
625 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
626 contactInfo.lookupKey,
627 LetterTileDrawable.SHAPE_CIRCLE,
628 contactType);
629 largeIcon = lettertile.getBitmap(width, height);
630 }
631
Eric Erfanianccca3152017-02-22 16:32:36 -0800632 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700633 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800634 largeIcon = DrawableConverter.drawableToBitmap(drawable);
635 }
636 return largeIcon;
637 }
638
639 private Bitmap getRoundedIcon(Bitmap bitmap) {
640 if (bitmap == null) {
641 return null;
642 }
643 final int height =
644 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
645 final int width =
646 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
647 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
648 }
649
650 /**
651 * Returns the appropriate icon res Id to display based on the call for which we want to display
652 * information.
653 */
654 private int getIconToDisplay(DialerCall call) {
655 // Even if both lines are in use, we only show a single item in
656 // the expanded Notifications UI. It's labeled "Ongoing call"
657 // (or "On hold" if there's only one call, and it's on hold.)
658 // Also, we don't have room to display caller-id info from two
659 // different calls. So if both lines are in use, display info
660 // from the foreground call. And if there's a ringing call,
661 // display that regardless of the state of the other calls.
662 if (call.getState() == DialerCall.State.ONHOLD) {
663 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700664 } else if (call.getVideoTech().getSessionModificationState()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700665 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST
666 || call.isVideoCall()) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700667 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700668 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
669 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700670 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
671 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
672 // 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 -0700673 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800674 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700675 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
676 if (ReturnToCallController.isEnabled(mContext)) {
677 return R.drawable.quantum_ic_call_vd_theme_24;
678 } else {
679 return R.drawable.on_going_call;
680 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800681 }
682
683 /** Returns the message to use with the notification. */
684 private String getContentString(DialerCall call, @UserType long userType) {
685 boolean isIncomingOrWaiting =
686 call.getState() == DialerCall.State.INCOMING
687 || call.getState() == DialerCall.State.CALL_WAITING;
688
689 if (isIncomingOrWaiting
690 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
691
692 if (!TextUtils.isEmpty(call.getChildNumber())) {
693 return mContext.getString(R.string.child_number, call.getChildNumber());
694 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
695 return call.getCallSubject();
696 }
697 }
698
699 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700700 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800701 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700702 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 }
704
705 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700706 if (call.isSpam()) {
707 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700708 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
709 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700710 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700711 resId = R.string.notification_incoming_call_wifi_template;
712
Eric Erfanianccca3152017-02-22 16:32:36 -0800713 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700714 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800715 }
716 } else if (call.getState() == DialerCall.State.ONHOLD) {
717 resId = R.string.notification_on_hold;
calderwoodra1dc2cea2017-09-20 16:30:41 -0700718 } else if (call.isVideoCall()) {
719 resId =
720 call.getVideoTech().isPaused()
721 ? R.string.notification_ongoing_paused_video_call
722 : R.string.notification_ongoing_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800723 } else if (DialerCall.State.isDialing(call.getState())) {
724 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700725 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700726 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800727 resId = R.string.notification_requesting_video_call;
728 }
729
730 // Is the call placed through work connection service.
731 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
732 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
733 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700734 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
735 }
736
737 if (resId == R.string.notification_incoming_call_wifi_template
738 || resId == R.string.notification_ongoing_call_wifi_template) {
739 // TODO(b/64525903): Potentially apply this template logic everywhere.
740 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800741 }
742
743 return mContext.getString(resId);
744 }
745
Eric Erfanian2ca43182017-08-31 06:57:16 -0700746 private boolean shouldShowEnrichedCallNotification(Session session) {
747 if (session == null) {
748 return false;
749 }
750 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
751 }
752
Eric Erfaniand8046e52017-04-06 09:41:50 -0700753 private int getECIncomingCallText(Session session) {
754 int resId;
755 MultimediaData data = session.getMultimediaData();
756 boolean hasImage = data.hasImageData();
757 boolean hasSubject = !TextUtils.isEmpty(data.getText());
758 boolean hasMap = data.getLocation() != null;
759 if (data.isImportant()) {
760 if (hasMap) {
761 if (hasImage) {
762 if (hasSubject) {
763 resId = R.string.important_notification_incoming_call_with_photo_message_location;
764 } else {
765 resId = R.string.important_notification_incoming_call_with_photo_location;
766 }
767 } else if (hasSubject) {
768 resId = R.string.important_notification_incoming_call_with_message_location;
769 } else {
770 resId = R.string.important_notification_incoming_call_with_location;
771 }
772 } else if (hasImage) {
773 if (hasSubject) {
774 resId = R.string.important_notification_incoming_call_with_photo_message;
775 } else {
776 resId = R.string.important_notification_incoming_call_with_photo;
777 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700778 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700779 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700780 } else {
781 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700782 }
783 if (mContext.getString(resId).length() > 50) {
784 resId = R.string.important_notification_incoming_call_attachments;
785 }
786 } else {
787 if (hasMap) {
788 if (hasImage) {
789 if (hasSubject) {
790 resId = R.string.notification_incoming_call_with_photo_message_location;
791 } else {
792 resId = R.string.notification_incoming_call_with_photo_location;
793 }
794 } else if (hasSubject) {
795 resId = R.string.notification_incoming_call_with_message_location;
796 } else {
797 resId = R.string.notification_incoming_call_with_location;
798 }
799 } else if (hasImage) {
800 if (hasSubject) {
801 resId = R.string.notification_incoming_call_with_photo_message;
802 } else {
803 resId = R.string.notification_incoming_call_with_photo;
804 }
805 } else {
806 resId = R.string.notification_incoming_call_with_message;
807 }
808 }
809 if (mContext.getString(resId).length() > 50) {
810 resId = R.string.notification_incoming_call_attachments;
811 }
812 return resId;
813 }
814
Eric Erfanianccca3152017-02-22 16:32:36 -0800815 /** Gets the most relevant call to display in the notification. */
816 private DialerCall getCallToShow(CallList callList) {
817 if (callList == null) {
818 return null;
819 }
820 DialerCall call = callList.getIncomingCall();
821 if (call == null) {
822 call = callList.getOutgoingCall();
823 }
824 if (call == null) {
825 call = callList.getVideoUpgradeRequestCall();
826 }
827 if (call == null) {
828 call = callList.getActiveOrBackgroundCall();
829 }
830 return call;
831 }
832
833 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
834 Spannable spannable = new SpannableString(mContext.getText(stringRes));
835 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
836 // This will only work for cases where the Notification.Builder has a fullscreen intent set
837 // Notification.Builder that does not have a full screen intent will take the color of the
838 // app and the following leads to a no-op.
839 spannable.setSpan(
840 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
841 }
842 return spannable;
843 }
844
845 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700846 LogUtil.d(
847 "StatusBarNotifier.addAnswerAction",
848 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800849 PendingIntent answerVoicePendingIntent =
850 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
851 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700852 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700853 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700854 getActionText(
855 R.string.notification_action_answer, R.color.notification_action_accept),
856 answerVoicePendingIntent)
857 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800858 }
859
860 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700861 LogUtil.d(
862 "StatusBarNotifier.addDismissAction",
863 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800864 PendingIntent declinePendingIntent =
865 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
866 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700867 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700868 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700869 getActionText(
870 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
871 declinePendingIntent)
872 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800873 }
874
875 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700876 LogUtil.d(
877 "StatusBarNotifier.addHangupAction",
878 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800879 PendingIntent hangupPendingIntent =
880 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
881 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700882 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700883 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700884 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700885 hangupPendingIntent)
886 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800887 }
888
yuegb26c1ae2017-09-18 16:59:16 -0700889 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
890 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
891 == CallAudioState.ROUTE_BLUETOOTH) {
892 // Don't add speaker button if bluetooth is connected
893 return;
894 }
895 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
896 addSpeakerOffAction(builder);
897 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
898 addSpeakerOnAction(builder);
899 }
900 }
901
902 private void addSpeakerOnAction(Notification.Builder builder) {
903 LogUtil.d(
904 "StatusBarNotifier.addSpeakerOnAction",
905 "will show \"Speaker on\" action in the ongoing active call Notification");
906 PendingIntent speakerOnPendingIntent =
907 createNotificationPendingIntent(mContext, ACTION_TURN_ON_SPEAKER);
908 builder.addAction(
909 new Notification.Action.Builder(
910 Icon.createWithResource(mContext, R.drawable.quantum_ic_volume_up_white_24),
911 mContext.getText(R.string.notification_action_speaker_on),
912 speakerOnPendingIntent)
913 .build());
914 }
915
916 private void addSpeakerOffAction(Notification.Builder builder) {
917 LogUtil.d(
918 "StatusBarNotifier.addSpeakerOffAction",
919 "will show \"Speaker off\" action in the ongoing active call Notification");
920 PendingIntent speakerOffPendingIntent =
921 createNotificationPendingIntent(mContext, ACTION_TURN_OFF_SPEAKER);
922 builder.addAction(
923 new Notification.Action.Builder(
924 Icon.createWithResource(mContext, R.drawable.quantum_ic_phone_in_talk_white_24),
925 mContext.getText(R.string.notification_action_speaker_off),
926 speakerOffPendingIntent)
927 .build());
928 }
929
Eric Erfanianccca3152017-02-22 16:32:36 -0800930 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700931 LogUtil.i(
932 "StatusBarNotifier.addVideoCallAction",
933 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800934 PendingIntent answerVideoPendingIntent =
935 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
936 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700937 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700938 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700939 getActionText(
940 R.string.notification_action_answer_video,
941 R.color.notification_action_answer_video),
942 answerVideoPendingIntent)
943 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800944 }
945
946 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700947 LogUtil.i(
948 "StatusBarNotifier.addAcceptUpgradeRequestAction",
949 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 PendingIntent acceptVideoPendingIntent =
951 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
952 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700953 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700954 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700955 getActionText(
956 R.string.notification_action_accept, R.color.notification_action_accept),
957 acceptVideoPendingIntent)
958 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800959 }
960
961 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700962 LogUtil.i(
963 "StatusBarNotifier.addDismissUpgradeRequestAction",
964 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800965 PendingIntent declineVideoPendingIntent =
966 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
967 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700968 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700969 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700970 getActionText(
971 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
972 declineVideoPendingIntent)
973 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 }
975
976 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700977 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800978 // Ok, we actually want to launch the incoming call
979 // UI at this point (in addition to simply posting a notification
980 // to the status bar). Setting fullScreenIntent will cause
981 // the InCallScreen to be launched immediately *unless* the
982 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700983 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800985 }
986
987 private Notification.Builder getNotificationBuilder() {
988 final Notification.Builder builder = new Notification.Builder(mContext);
989 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700990 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700991 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -0700992 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -0700993 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800994
995 return builder;
996 }
997
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700998 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800999 Intent intent =
1000 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001001 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001002
Eric Erfanian2ca43182017-08-31 06:57:16 -07001003 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 if (isFullScreen) {
1005 // Use a unique request code so that the pending intent isn't clobbered by the
1006 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -07001007 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001008 }
1009
1010 // PendingIntent that can be used to launch the InCallActivity. The
1011 // system fires off this intent if the user pulls down the windowshade
1012 // and clicks the notification's expanded view. It's also used to
1013 // launch the InCallActivity immediately when when there's an incoming
1014 // call (see the "fullScreenIntent" field below).
1015 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
1016 }
1017
1018 private void setStatusBarCallListener(StatusBarCallListener listener) {
1019 if (mStatusBarCallListener != null) {
1020 mStatusBarCallListener.cleanup();
1021 }
1022 mStatusBarCallListener = listener;
1023 }
1024
yuegb26c1ae2017-09-18 16:59:16 -07001025 @Override
1026 public void onAudioStateChanged(CallAudioState audioState) {
1027 if (CallList.getInstance().getActiveOrBackgroundCall() == null) {
1028 // We only care about speaker mode when in call
1029 return;
1030 }
1031
1032 updateNotification(CallList.getInstance());
1033 }
1034
Eric Erfanianccca3152017-02-22 16:32:36 -08001035 private class StatusBarCallListener implements DialerCallListener {
1036
1037 private DialerCall mDialerCall;
1038
1039 StatusBarCallListener(DialerCall dialerCall) {
1040 mDialerCall = dialerCall;
1041 mDialerCall.addListener(this);
1042 }
1043
1044 void cleanup() {
1045 mDialerCall.removeListener(this);
1046 }
1047
1048 @Override
1049 public void onDialerCallDisconnect() {}
1050
1051 @Override
1052 public void onDialerCallUpdate() {
1053 if (CallList.getInstance().getIncomingCall() == null) {
1054 mDialerRingtoneManager.stopCallWaitingTone();
1055 }
1056 }
1057
1058 @Override
1059 public void onDialerCallChildNumberChange() {}
1060
1061 @Override
1062 public void onDialerCallLastForwardedNumberChange() {}
1063
1064 @Override
1065 public void onDialerCallUpgradeToVideo() {}
1066
1067 @Override
1068 public void onWiFiToLteHandover() {}
1069
1070 @Override
1071 public void onHandoverToWifiFailure() {}
1072
Eric Erfanianc857f902017-05-15 14:05:33 -07001073 @Override
1074 public void onInternationalCallOnWifi() {}
1075
Eric Erfanian2ca43182017-08-31 06:57:16 -07001076 @Override
1077 public void onEnrichedCallSessionUpdate() {}
1078
Eric Erfanianccca3152017-02-22 16:32:36 -08001079 /**
1080 * Responds to changes in the session modification state for the call by dismissing the status
1081 * bar notification as required.
1082 */
1083 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001084 public void onDialerCallSessionModificationStateChange() {
1085 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001086 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001087 cleanup();
1088 updateNotification(CallList.getInstance());
1089 }
1090 }
1091 }
1092}