blob: 6ddba16139d74fb7397045477ef9e764faa249b0 [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);
twyend1d1d0c2017-10-05 17:34:43 -0700302 final CharSequence content = getContentString(call, contactInfo.userType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800303 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,
twyend1d1d0c2017-10-05 17:34:43 -0700332 content.toString(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800333 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. */
twyend1d1d0c2017-10-05 17:34:43 -0700684 private CharSequence getContentString(DialerCall call, @UserType long userType) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800685 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;
twyend1d1d0c2017-10-05 17:34:43 -0700712 } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts()) {
713 return getMultiSimIncomingText(call);
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;
calderwoodra1dc2cea2017-09-20 16:30:41 -0700719 } else if (call.isVideoCall()) {
720 resId =
721 call.getVideoTech().isPaused()
722 ? R.string.notification_ongoing_paused_video_call
723 : R.string.notification_ongoing_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800724 } else if (DialerCall.State.isDialing(call.getState())) {
725 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700726 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700727 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800728 resId = R.string.notification_requesting_video_call;
729 }
730
731 // Is the call placed through work connection service.
732 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
733 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
734 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700735 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
736 }
737
738 if (resId == R.string.notification_incoming_call_wifi_template
739 || resId == R.string.notification_ongoing_call_wifi_template) {
740 // TODO(b/64525903): Potentially apply this template logic everywhere.
741 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800742 }
743
744 return mContext.getString(resId);
745 }
746
Eric Erfanian2ca43182017-08-31 06:57:16 -0700747 private boolean shouldShowEnrichedCallNotification(Session session) {
748 if (session == null) {
749 return false;
750 }
751 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
752 }
753
Eric Erfaniand8046e52017-04-06 09:41:50 -0700754 private int getECIncomingCallText(Session session) {
755 int resId;
756 MultimediaData data = session.getMultimediaData();
757 boolean hasImage = data.hasImageData();
758 boolean hasSubject = !TextUtils.isEmpty(data.getText());
759 boolean hasMap = data.getLocation() != null;
760 if (data.isImportant()) {
761 if (hasMap) {
762 if (hasImage) {
763 if (hasSubject) {
764 resId = R.string.important_notification_incoming_call_with_photo_message_location;
765 } else {
766 resId = R.string.important_notification_incoming_call_with_photo_location;
767 }
768 } else if (hasSubject) {
769 resId = R.string.important_notification_incoming_call_with_message_location;
770 } else {
771 resId = R.string.important_notification_incoming_call_with_location;
772 }
773 } else if (hasImage) {
774 if (hasSubject) {
775 resId = R.string.important_notification_incoming_call_with_photo_message;
776 } else {
777 resId = R.string.important_notification_incoming_call_with_photo;
778 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700779 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700780 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700781 } else {
782 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700783 }
784 if (mContext.getString(resId).length() > 50) {
785 resId = R.string.important_notification_incoming_call_attachments;
786 }
787 } else {
788 if (hasMap) {
789 if (hasImage) {
790 if (hasSubject) {
791 resId = R.string.notification_incoming_call_with_photo_message_location;
792 } else {
793 resId = R.string.notification_incoming_call_with_photo_location;
794 }
795 } else if (hasSubject) {
796 resId = R.string.notification_incoming_call_with_message_location;
797 } else {
798 resId = R.string.notification_incoming_call_with_location;
799 }
800 } else if (hasImage) {
801 if (hasSubject) {
802 resId = R.string.notification_incoming_call_with_photo_message;
803 } else {
804 resId = R.string.notification_incoming_call_with_photo;
805 }
806 } else {
807 resId = R.string.notification_incoming_call_with_message;
808 }
809 }
810 if (mContext.getString(resId).length() > 50) {
811 resId = R.string.notification_incoming_call_attachments;
812 }
813 return resId;
814 }
815
twyend1d1d0c2017-10-05 17:34:43 -0700816 private CharSequence getMultiSimIncomingText(DialerCall call) {
817 PhoneAccount phoneAccount =
818 mContext.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
819 SpannableString string =
820 new SpannableString(
821 mContext.getString(
822 R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
823 int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
824 int accountEnd = accountStart + phoneAccount.getLabel().length();
825
826 string.setSpan(
827 new ForegroundColorSpan(phoneAccount.getHighlightColor()),
828 accountStart,
829 accountEnd,
830 Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
831 return string;
832 }
833
Eric Erfanianccca3152017-02-22 16:32:36 -0800834 /** Gets the most relevant call to display in the notification. */
835 private DialerCall getCallToShow(CallList callList) {
836 if (callList == null) {
837 return null;
838 }
839 DialerCall call = callList.getIncomingCall();
840 if (call == null) {
841 call = callList.getOutgoingCall();
842 }
843 if (call == null) {
844 call = callList.getVideoUpgradeRequestCall();
845 }
846 if (call == null) {
847 call = callList.getActiveOrBackgroundCall();
848 }
849 return call;
850 }
851
852 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
853 Spannable spannable = new SpannableString(mContext.getText(stringRes));
854 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
855 // This will only work for cases where the Notification.Builder has a fullscreen intent set
856 // Notification.Builder that does not have a full screen intent will take the color of the
857 // app and the following leads to a no-op.
858 spannable.setSpan(
859 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
860 }
861 return spannable;
862 }
863
864 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700865 LogUtil.d(
866 "StatusBarNotifier.addAnswerAction",
867 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800868 PendingIntent answerVoicePendingIntent =
869 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
870 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700871 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700872 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700873 getActionText(
874 R.string.notification_action_answer, R.color.notification_action_accept),
875 answerVoicePendingIntent)
876 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 }
878
879 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700880 LogUtil.d(
881 "StatusBarNotifier.addDismissAction",
882 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 PendingIntent declinePendingIntent =
884 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
885 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700886 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700887 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700888 getActionText(
889 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
890 declinePendingIntent)
891 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800892 }
893
894 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700895 LogUtil.d(
896 "StatusBarNotifier.addHangupAction",
897 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800898 PendingIntent hangupPendingIntent =
899 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
900 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700901 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700902 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700903 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700904 hangupPendingIntent)
905 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800906 }
907
yuegb26c1ae2017-09-18 16:59:16 -0700908 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
909 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
910 == CallAudioState.ROUTE_BLUETOOTH) {
911 // Don't add speaker button if bluetooth is connected
912 return;
913 }
914 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
915 addSpeakerOffAction(builder);
916 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
917 addSpeakerOnAction(builder);
918 }
919 }
920
921 private void addSpeakerOnAction(Notification.Builder builder) {
922 LogUtil.d(
923 "StatusBarNotifier.addSpeakerOnAction",
924 "will show \"Speaker on\" action in the ongoing active call Notification");
925 PendingIntent speakerOnPendingIntent =
926 createNotificationPendingIntent(mContext, ACTION_TURN_ON_SPEAKER);
927 builder.addAction(
928 new Notification.Action.Builder(
929 Icon.createWithResource(mContext, R.drawable.quantum_ic_volume_up_white_24),
930 mContext.getText(R.string.notification_action_speaker_on),
931 speakerOnPendingIntent)
932 .build());
933 }
934
935 private void addSpeakerOffAction(Notification.Builder builder) {
936 LogUtil.d(
937 "StatusBarNotifier.addSpeakerOffAction",
938 "will show \"Speaker off\" action in the ongoing active call Notification");
939 PendingIntent speakerOffPendingIntent =
940 createNotificationPendingIntent(mContext, ACTION_TURN_OFF_SPEAKER);
941 builder.addAction(
942 new Notification.Action.Builder(
943 Icon.createWithResource(mContext, R.drawable.quantum_ic_phone_in_talk_white_24),
944 mContext.getText(R.string.notification_action_speaker_off),
945 speakerOffPendingIntent)
946 .build());
947 }
948
Eric Erfanianccca3152017-02-22 16:32:36 -0800949 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700950 LogUtil.i(
951 "StatusBarNotifier.addVideoCallAction",
952 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800953 PendingIntent answerVideoPendingIntent =
954 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
955 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700956 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700957 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700958 getActionText(
959 R.string.notification_action_answer_video,
960 R.color.notification_action_answer_video),
961 answerVideoPendingIntent)
962 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 }
964
965 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700966 LogUtil.i(
967 "StatusBarNotifier.addAcceptUpgradeRequestAction",
968 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800969 PendingIntent acceptVideoPendingIntent =
970 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
971 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700972 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700973 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700974 getActionText(
975 R.string.notification_action_accept, R.color.notification_action_accept),
976 acceptVideoPendingIntent)
977 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800978 }
979
980 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700981 LogUtil.i(
982 "StatusBarNotifier.addDismissUpgradeRequestAction",
983 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800984 PendingIntent declineVideoPendingIntent =
985 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
986 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700987 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700988 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700989 getActionText(
990 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
991 declineVideoPendingIntent)
992 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800993 }
994
995 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700996 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800997 // Ok, we actually want to launch the incoming call
998 // UI at this point (in addition to simply posting a notification
999 // to the status bar). Setting fullScreenIntent will cause
1000 // the InCallScreen to be launched immediately *unless* the
1001 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001002 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001003 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 }
1005
1006 private Notification.Builder getNotificationBuilder() {
1007 final Notification.Builder builder = new Notification.Builder(mContext);
1008 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001009 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -07001010 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -07001011 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -07001012 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -08001013
1014 return builder;
1015 }
1016
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001017 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 Intent intent =
1019 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001020 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001021
Eric Erfanian2ca43182017-08-31 06:57:16 -07001022 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001023 if (isFullScreen) {
1024 // Use a unique request code so that the pending intent isn't clobbered by the
1025 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -07001026 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001027 }
1028
1029 // PendingIntent that can be used to launch the InCallActivity. The
1030 // system fires off this intent if the user pulls down the windowshade
1031 // and clicks the notification's expanded view. It's also used to
1032 // launch the InCallActivity immediately when when there's an incoming
1033 // call (see the "fullScreenIntent" field below).
1034 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
1035 }
1036
1037 private void setStatusBarCallListener(StatusBarCallListener listener) {
1038 if (mStatusBarCallListener != null) {
1039 mStatusBarCallListener.cleanup();
1040 }
1041 mStatusBarCallListener = listener;
1042 }
1043
twyend1d1d0c2017-10-05 17:34:43 -07001044 @SuppressWarnings("MissingPermission")
1045 private boolean hasMultiplePhoneAccounts() {
1046 return mContext.getSystemService(TelecomManager.class).getCallCapablePhoneAccounts().size() > 1;
1047 }
1048
yuegb26c1ae2017-09-18 16:59:16 -07001049 @Override
1050 public void onAudioStateChanged(CallAudioState audioState) {
1051 if (CallList.getInstance().getActiveOrBackgroundCall() == null) {
1052 // We only care about speaker mode when in call
1053 return;
1054 }
1055
1056 updateNotification(CallList.getInstance());
1057 }
1058
Eric Erfanianccca3152017-02-22 16:32:36 -08001059 private class StatusBarCallListener implements DialerCallListener {
1060
1061 private DialerCall mDialerCall;
1062
1063 StatusBarCallListener(DialerCall dialerCall) {
1064 mDialerCall = dialerCall;
1065 mDialerCall.addListener(this);
1066 }
1067
1068 void cleanup() {
1069 mDialerCall.removeListener(this);
1070 }
1071
1072 @Override
1073 public void onDialerCallDisconnect() {}
1074
1075 @Override
1076 public void onDialerCallUpdate() {
1077 if (CallList.getInstance().getIncomingCall() == null) {
1078 mDialerRingtoneManager.stopCallWaitingTone();
1079 }
1080 }
1081
1082 @Override
1083 public void onDialerCallChildNumberChange() {}
1084
1085 @Override
1086 public void onDialerCallLastForwardedNumberChange() {}
1087
1088 @Override
1089 public void onDialerCallUpgradeToVideo() {}
1090
1091 @Override
1092 public void onWiFiToLteHandover() {}
1093
1094 @Override
1095 public void onHandoverToWifiFailure() {}
1096
Eric Erfanianc857f902017-05-15 14:05:33 -07001097 @Override
1098 public void onInternationalCallOnWifi() {}
1099
Eric Erfanian2ca43182017-08-31 06:57:16 -07001100 @Override
1101 public void onEnrichedCallSessionUpdate() {}
1102
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 /**
1104 * Responds to changes in the session modification state for the call by dismissing the status
1105 * bar notification as required.
1106 */
1107 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001108 public void onDialerCallSessionModificationStateChange() {
1109 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001110 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001111 cleanup();
1112 updateNotification(CallList.getInstance());
1113 }
1114 }
1115 }
1116}