blob: db416d861a2d5021a3c100639019592676dc92c6 [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;
55import android.telecom.TelecomManager;
56import android.text.BidiFormatter;
57import android.text.Spannable;
58import android.text.SpannableString;
59import android.text.TextDirectionHeuristics;
60import android.text.TextUtils;
61import android.text.style.ForegroundColorSpan;
62import com.android.contacts.common.ContactsUtils;
63import com.android.contacts.common.ContactsUtils.UserType;
64import com.android.contacts.common.preference.ContactsPreferences;
Eric Erfanianccca3152017-02-22 16:32:36 -080065import com.android.contacts.common.util.ContactDisplayUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070066import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080067import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070068import com.android.dialer.configprovider.ConfigProviderBindings;
69import com.android.dialer.contactphoto.BitmapUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070070import com.android.dialer.enrichedcall.EnrichedCallManager;
71import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070072import com.android.dialer.lettertile.LetterTileDrawable;
73import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070074import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070075import com.android.dialer.notification.NotificationChannelId;
Eric Erfanian90508232017-03-24 09:31:16 -070076import com.android.dialer.oem.MotorolaUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080077import com.android.dialer.util.DrawableConverter;
78import com.android.incallui.ContactInfoCache.ContactCacheEntry;
79import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
80import com.android.incallui.InCallPresenter.InCallState;
81import com.android.incallui.async.PausableExecutorImpl;
yuegb26c1ae2017-09-18 16:59:16 -070082import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080083import com.android.incallui.call.CallList;
84import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080085import com.android.incallui.call.DialerCallListener;
yueg01a964d2017-10-03 15:25:41 -070086import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080087import com.android.incallui.ringtone.DialerRingtoneManager;
88import com.android.incallui.ringtone.InCallTonePlayer;
89import com.android.incallui.ringtone.ToneGeneratorFactory;
Eric Erfanian90508232017-03-24 09:31:16 -070090import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080091import java.util.Objects;
92
93/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -070094public class StatusBarNotifier
yuegb26c1ae2017-09-18 16:59:16 -070095 implements InCallPresenter.InCallStateListener,
96 EnrichedCallManager.StateChangedListener,
wangqic8cf79e2017-10-17 09:21:00 -070097 ContactInfoCacheCallback {
Eric Erfanianccca3152017-02-22 16:32:36 -080098
Eric Erfanian2ca43182017-08-31 06:57:16 -070099 private static final int NOTIFICATION_ID = 1;
100
Eric Erfanianccca3152017-02-22 16:32:36 -0800101 // Notification types
102 // Indicates that no notification is currently showing.
103 private static final int NOTIFICATION_NONE = 0;
104 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700105 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800106 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700107 private static final int NOTIFICATION_INCOMING_CALL = 2;
108 // Notification for incoming calls in the case where there is already an active call.
109 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
110 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800111
Eric Erfanianccca3152017-02-22 16:32:36 -0800112 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
113
114 private final Context mContext;
115 private final ContactInfoCache mContactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800116 private final DialerRingtoneManager mDialerRingtoneManager;
117 @Nullable private ContactsPreferences mContactsPreferences;
118 private int mCurrentNotification = NOTIFICATION_NONE;
119 private int mCallState = DialerCall.State.INVALID;
120 private int mSavedIcon = 0;
121 private String mSavedContent = null;
122 private Bitmap mSavedLargeIcon;
123 private String mSavedContentTitle;
yuegb26c1ae2017-09-18 16:59:16 -0700124 private CallAudioState savedCallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800125 private Uri mRingtone;
126 private StatusBarCallListener mStatusBarCallListener;
127
Eric Erfaniand8046e52017-04-06 09:41:50 -0700128 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
wangqic8cf79e2017-10-17 09:21:00 -0700129 Trace.beginSection("StatusBarNotifier.Constructor");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700130 mContext = Assert.isNotNull(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800131 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
132 mContactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800133 mDialerRingtoneManager =
134 new DialerRingtoneManager(
135 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
136 CallList.getInstance());
137 mCurrentNotification = NOTIFICATION_NONE;
wangqic8cf79e2017-10-17 09:21:00 -0700138 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800139 }
140
141 /**
142 * Should only be called from a irrecoverable state where it is necessary to dismiss all
143 * notifications.
144 */
yueg01a964d2017-10-03 15:25:41 -0700145 static void clearAllCallNotifications() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700146 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700147 "StatusBarNotifier.clearAllCallNotifications",
148 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800149
yueg01a964d2017-10-03 15:25:41 -0700150 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800151 }
152
153 private static int getWorkStringFromPersonalString(int resId) {
154 if (resId == R.string.notification_ongoing_call) {
155 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800156 } else if (resId == R.string.notification_incoming_call) {
157 return R.string.notification_incoming_work_call;
158 } else {
159 return resId;
160 }
161 }
162
163 /**
164 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
165 * context.
166 */
167 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
168 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
169 return PendingIntent.getBroadcast(context, 0, intent, 0);
170 }
171
172 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
173 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700174 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800175 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700176 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
wangqic8cf79e2017-10-17 09:21:00 -0700177 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800178 }
179
Eric Erfaniand8046e52017-04-06 09:41:50 -0700180 @Override
181 public void onEnrichedCallStateChanged() {
182 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
wangqic8cf79e2017-10-17 09:21:00 -0700183 updateNotification();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700184 }
185
Eric Erfanianccca3152017-02-22 16:32:36 -0800186 /**
187 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
188 * to a new incoming call.
189 *
190 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
191 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
192 * foreground activity is marked as "immersive".
193 *
194 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
195 * ringing connection" event from the telephony layer.)
196 *
197 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
198 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
199 * we'll simply update or cancel the in-call notification based on the current phone state.
200 *
wangqic8cf79e2017-10-17 09:21:00 -0700201 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800202 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700203 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700204 public void updateNotification() {
205 updateInCallNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800206 }
207
208 /**
209 * Take down the in-call notification.
210 *
wangqic8cf79e2017-10-17 09:21:00 -0700211 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800212 */
213 private void cancelNotification() {
214 if (mStatusBarCallListener != null) {
215 setStatusBarCallListener(null);
216 }
217 if (mCurrentNotification != NOTIFICATION_NONE) {
yueg01a964d2017-10-03 15:25:41 -0700218 TelecomAdapter.getInstance().stopForegroundNotification();
219 mCurrentNotification = NOTIFICATION_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800220 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800221 }
222
223 /**
224 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
225 * status bar notification based on the current telephony state, or cancels the notification if
226 * the phone is totally idle.
227 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700228 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700229 private void updateInCallNotification() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700230 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800231
wangqic8cf79e2017-10-17 09:21:00 -0700232 final DialerCall call = getCallToShow(CallList.getInstance());
Eric Erfanianccca3152017-02-22 16:32:36 -0800233
234 if (call != null) {
wangqic8cf79e2017-10-17 09:21:00 -0700235 showNotification(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800236 } else {
237 cancelNotification();
238 }
239 }
240
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700241 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700242 private void showNotification(final DialerCall call) {
wangqicf61ca02017-08-31 15:32:55 -0700243 Trace.beginSection("StatusBarNotifier.showNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800244 final boolean isIncoming =
245 (call.getState() == DialerCall.State.INCOMING
246 || call.getState() == DialerCall.State.CALL_WAITING);
247 setStatusBarCallListener(new StatusBarCallListener(call));
248
249 // we make a call to the contact info cache to query for supplemental data to what the
250 // call provides. This includes the contact name and photo.
251 // This callback will always get called immediately and synchronously with whatever data
252 // it has available, and may make a subsequent call later (same thread) if it had to
253 // call into the contacts provider for more data.
wangqic8cf79e2017-10-17 09:21:00 -0700254 mContactInfoCache.findInfo(call, isIncoming, this);
wangqicf61ca02017-08-31 15:32:55 -0700255 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800256 }
257
258 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700259 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800260 private void buildAndSendNotification(
261 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
wangqicf61ca02017-08-31 15:32:55 -0700262 Trace.beginSection("StatusBarNotifier.buildAndSendNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800263 // This can get called to update an existing notification after contact information has come
264 // back. However, it can happen much later. Before we continue, we need to make sure that
265 // the call being passed in is still the one we want to show in the notification.
266 final DialerCall call = getCallToShow(callList);
267 if (call == null || !call.getId().equals(originalCall.getId())) {
wangqicf61ca02017-08-31 15:32:55 -0700268 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800269 return;
270 }
271
wangqicf61ca02017-08-31 15:32:55 -0700272 Trace.beginSection("prepare work");
Eric Erfanianccca3152017-02-22 16:32:36 -0800273 final int callState = call.getState();
yuegb26c1ae2017-09-18 16:59:16 -0700274 final CallAudioState callAudioState = AudioModeProvider.getInstance().getAudioState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800275
wangqic8cf79e2017-10-17 09:21:00 -0700276 Trace.beginSection("read icon and strings");
Eric Erfanianccca3152017-02-22 16:32:36 -0800277 // Check if data has changed; if nothing is different, don't issue another notification.
278 final int iconResId = getIconToDisplay(call);
Eric Erfanian83b20212017-05-31 08:53:10 -0700279 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
twyend1d1d0c2017-10-05 17:34:43 -0700280 final CharSequence content = getContentString(call, contactInfo.userType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800281 final String contentTitle = getContentTitle(contactInfo, call);
wangqic8cf79e2017-10-17 09:21:00 -0700282 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800283
284 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700285 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700286 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800287 final int notificationType;
288 if (callState == DialerCall.State.INCOMING
289 || callState == DialerCall.State.CALL_WAITING
290 || isVideoUpgradeRequest) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700291 if (ConfigProviderBindings.get(mContext)
292 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
293 notificationType =
294 InCallPresenter.getInstance().isShowingInCallUi()
295 ? NOTIFICATION_INCOMING_CALL_QUIET
296 : NOTIFICATION_INCOMING_CALL;
297 } else {
298 boolean alreadyActive =
299 callList.getActiveOrBackgroundCall() != null
300 && InCallPresenter.getInstance().isShowingInCallUi();
301 notificationType =
302 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
303 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800304 } else {
305 notificationType = NOTIFICATION_IN_CALL;
306 }
wangqicf61ca02017-08-31 15:32:55 -0700307 Trace.endSection(); // prepare work
Eric Erfanianccca3152017-02-22 16:32:36 -0800308
309 if (!checkForChangeAndSaveData(
310 iconResId,
twyend1d1d0c2017-10-05 17:34:43 -0700311 content.toString(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800312 largeIcon,
313 contentTitle,
314 callState,
315 notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700316 contactInfo.contactRingtoneUri,
317 callAudioState)) {
wangqicf61ca02017-08-31 15:32:55 -0700318 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800319 return;
320 }
321
322 if (largeIcon != null) {
323 largeIcon = getRoundedIcon(largeIcon);
324 }
325
326 // This builder is used for the notification shown when the device is locked and the user
327 // has set their notification settings to 'hide sensitive content'
328 // {@see Notification.Builder#setPublicVersion}.
329 Notification.Builder publicBuilder = new Notification.Builder(mContext);
330 publicBuilder
331 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700332 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800333 // Hide work call state for the lock screen notification
334 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
335 setNotificationWhen(call, callState, publicBuilder);
336
337 // Builder for the notification shown when the device is unlocked or the user has set their
338 // notification settings to 'show all notification content'.
339 final Notification.Builder builder = getNotificationBuilder();
340 builder.setPublicVersion(publicBuilder.build());
341
342 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700343 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800344
Eric Erfanian10b34a52017-05-04 08:23:17 -0700345 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
346 switch (notificationType) {
347 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700348 if (BuildCompat.isAtLeastO()) {
349 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
350 }
wangqic8cf79e2017-10-17 09:21:00 -0700351 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700352 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
353 // Set the notification category and bump the priority for incoming calls
354 builder.setCategory(Notification.CATEGORY_CALL);
355 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700356 builder.setPriority(Notification.PRIORITY_MAX);
357 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
358 LogUtil.i(
359 "StatusBarNotifier.buildAndSendNotification",
360 "Canceling old notification so this one can be noisy");
361 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
362 // notification (if there is one) so the fullScreenIntent or HUN will show
yueg01a964d2017-10-03 15:25:41 -0700363 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700364 }
365 break;
366 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700367 if (BuildCompat.isAtLeastO()) {
368 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
369 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700370 break;
371 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700372 if (BuildCompat.isAtLeastO()) {
373 publicBuilder.setColorized(true);
374 builder.setColorized(true);
375 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
376 }
377 break;
378 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700379 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800380 }
381
382 // Set the content
383 builder.setContentText(content);
384 builder.setSmallIcon(iconResId);
385 builder.setContentTitle(contentTitle);
386 builder.setLargeIcon(largeIcon);
yueg01a964d2017-10-03 15:25:41 -0700387 builder.setColor(InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800388
389 if (isVideoUpgradeRequest) {
390 builder.setUsesChronometer(false);
391 addDismissUpgradeRequestAction(builder);
392 addAcceptUpgradeRequestAction(builder);
393 } else {
yuegb26c1ae2017-09-18 16:59:16 -0700394 createIncomingCallNotification(call, callState, callAudioState, builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800395 }
396
397 addPersonReference(builder, contactInfo, call);
398
wangqicf61ca02017-08-31 15:32:55 -0700399 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800400 // Fire off the notification
401 Notification notification = builder.build();
402
403 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
404 notification.flags |= Notification.FLAG_INSISTENT;
405 notification.sound = contactInfo.contactRingtoneUri;
406 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
407 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
408 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
409 notification.audioAttributes = audioAttributes.build();
410 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
411 notification.vibrate = VIBRATE_PATTERN;
412 }
413 }
414 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700415 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800416 mDialerRingtoneManager.playCallWaitingTone();
417 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800418
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700419 LogUtil.i(
420 "StatusBarNotifier.buildAndSendNotification",
421 "displaying notification for " + notificationType);
422
yueg01a964d2017-10-03 15:25:41 -0700423 // If a notification exists, this will only update it.
424 TelecomAdapter.getInstance().startForegroundNotification(NOTIFICATION_ID, notification);
425
wangqicf61ca02017-08-31 15:32:55 -0700426 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800427 call.getLatencyReport().onNotificationShown();
428 mCurrentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700429 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 }
431
432 private void createIncomingCallNotification(
yuegb26c1ae2017-09-18 16:59:16 -0700433 DialerCall call, int state, CallAudioState callAudioState, Notification.Builder builder) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800434 setNotificationWhen(call, state, builder);
435
436 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
437 if (state == DialerCall.State.ACTIVE
438 || state == DialerCall.State.ONHOLD
439 || DialerCall.State.isDialing(state)) {
440 addHangupAction(builder);
yuegb26c1ae2017-09-18 16:59:16 -0700441 addSpeakerAction(builder, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800442 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
443 addDismissAction(builder);
444 if (call.isVideoCall()) {
445 addVideoCallAction(builder);
446 } else {
447 addAnswerAction(builder);
448 }
449 }
450 }
451
452 /**
453 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
454 * duration of the call. For all other states, the notification will automatically show the time
455 * at which the notification was created.
456 */
457 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
458 if (state == DialerCall.State.ACTIVE) {
459 builder.setUsesChronometer(true);
460 builder.setWhen(call.getConnectTimeMillis());
461 } else {
462 builder.setUsesChronometer(false);
463 }
464 }
465
466 /**
467 * Checks the new notification data and compares it against any notification that we are already
468 * displaying. If the data is exactly the same, we return false so that we do not issue a new
469 * notification for the exact same data.
470 */
471 private boolean checkForChangeAndSaveData(
472 int icon,
473 String content,
474 Bitmap largeIcon,
475 String contentTitle,
476 int state,
477 int notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700478 Uri ringtone,
479 CallAudioState callAudioState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800480
481 // The two are different:
482 // if new title is not null, it should be different from saved version OR
483 // if new title is null, the saved version should not be null
484 final boolean contentTitleChanged =
485 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
486 || (contentTitle == null && mSavedContentTitle != null);
487
Eric Erfanian8369df02017-05-03 10:27:13 -0700488 boolean largeIconChanged =
489 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
490
Eric Erfanianccca3152017-02-22 16:32:36 -0800491 // any change means we are definitely updating
492 boolean retval =
493 (mSavedIcon != icon)
494 || !Objects.equals(mSavedContent, content)
495 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700496 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800497 || contentTitleChanged
yuegb26c1ae2017-09-18 16:59:16 -0700498 || !Objects.equals(mRingtone, ringtone)
499 || !Objects.equals(savedCallAudioState, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800500
wangqi9982f0d2017-10-11 17:46:07 -0700501 LogUtil.d(
502 "StatusBarNotifier.checkForChangeAndSaveData",
503 "data changed: icon: %b, content: %b, state: %b, largeIcon: %b, title: %b, ringtone: %b, "
504 + "audioState: %b, type: %b",
505 (mSavedIcon != icon),
506 !Objects.equals(mSavedContent, content),
507 (mCallState != state),
508 largeIconChanged,
509 contentTitleChanged,
510 !Objects.equals(mRingtone, ringtone),
511 !Objects.equals(savedCallAudioState, callAudioState),
512 mCurrentNotification != notificationType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800513 // If we aren't showing a notification right now or the notification type is changing,
514 // definitely do an update.
515 if (mCurrentNotification != notificationType) {
516 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700517 LogUtil.d(
518 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800519 }
520 retval = true;
521 }
522
523 mSavedIcon = icon;
524 mSavedContent = content;
525 mCallState = state;
526 mSavedLargeIcon = largeIcon;
527 mSavedContentTitle = contentTitle;
528 mRingtone = ringtone;
yuegb26c1ae2017-09-18 16:59:16 -0700529 savedCallAudioState = callAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800530
531 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700532 LogUtil.d(
533 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800534 }
535
536 return retval;
537 }
538
539 /** Returns the main string to use in the notification. */
540 @VisibleForTesting
541 @Nullable
542 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700543 if (call.isConferenceCall()) {
544 return CallerInfoUtils.getConferenceString(
545 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800546 }
547
548 String preferredName =
549 ContactDisplayUtils.getPreferredDisplayName(
550 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
551 if (TextUtils.isEmpty(preferredName)) {
552 return TextUtils.isEmpty(contactInfo.number)
553 ? null
554 : BidiFormatter.getInstance()
555 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
556 }
557 return preferredName;
558 }
559
560 private void addPersonReference(
561 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
562 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
563 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
564 // NotificationManager using it.
565 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
566 builder.addPerson(contactInfo.lookupUri.toString());
567 } else if (!TextUtils.isEmpty(call.getNumber())) {
568 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
569 }
570 }
571
572 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700573 private static Bitmap getLargeIconToDisplay(
574 Context context, ContactCacheEntry contactInfo, DialerCall call) {
wangqic8cf79e2017-10-17 09:21:00 -0700575 Trace.beginSection("StatusBarNotifier.getLargeIconToDisplay");
Eric Erfanian83b20212017-05-31 08:53:10 -0700576 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800577 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800578 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
579 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
580 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700581 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700582 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
583 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700584 @ContactType
585 int contactType =
586 LetterTileDrawable.getContactTypeFromPrimitives(
wangqi9982f0d2017-10-11 17:46:07 -0700587 call.isVoiceMailNumber(),
Eric Erfanian2ca43182017-08-31 06:57:16 -0700588 call.isSpam(),
589 contactInfo.isBusiness,
590 call.getNumberPresentation(),
591 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700592 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700593
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700594 lettertile.setCanonicalDialerLetterTileDetails(
595 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
596 contactInfo.lookupKey,
597 LetterTileDrawable.SHAPE_CIRCLE,
598 contactType);
599 largeIcon = lettertile.getBitmap(width, height);
600 }
601
Eric Erfanianccca3152017-02-22 16:32:36 -0800602 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700603 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 largeIcon = DrawableConverter.drawableToBitmap(drawable);
605 }
wangqic8cf79e2017-10-17 09:21:00 -0700606 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800607 return largeIcon;
608 }
609
610 private Bitmap getRoundedIcon(Bitmap bitmap) {
611 if (bitmap == null) {
612 return null;
613 }
614 final int height =
615 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
616 final int width =
617 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
618 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
619 }
620
621 /**
622 * Returns the appropriate icon res Id to display based on the call for which we want to display
623 * information.
624 */
Eric Erfanian2827dd12017-10-26 09:37:50 -0700625 @VisibleForTesting
626 public int getIconToDisplay(DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800627 // Even if both lines are in use, we only show a single item in
628 // the expanded Notifications UI. It's labeled "Ongoing call"
629 // (or "On hold" if there's only one call, and it's on hold.)
630 // Also, we don't have room to display caller-id info from two
631 // different calls. So if both lines are in use, display info
632 // from the foreground call. And if there's a ringing call,
633 // display that regardless of the state of the other calls.
634 if (call.getState() == DialerCall.State.ONHOLD) {
Eric Erfanian2827dd12017-10-26 09:37:50 -0700635 return R.drawable.quantum_ic_phone_paused_vd_theme_24;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700636 } else if (call.getVideoTech().getSessionModificationState()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700637 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST
638 || call.isVideoCall()) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700639 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700640 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
641 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700642 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
643 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
644 // 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 -0700645 return R.drawable.ic_hd_call;
Eric Erfanian2827dd12017-10-26 09:37:50 -0700646 } else if (call.hasProperty(Details.PROPERTY_HAS_CDMA_VOICE_PRIVACY)) {
647 return R.drawable.quantum_ic_phone_locked_vd_theme_24;
Eric Erfanianccca3152017-02-22 16:32:36 -0800648 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700649 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
Eric Erfanian938468d2017-10-24 14:05:52 -0700650 if (ReturnToCallController.isEnabled(mContext)
651 || NewReturnToCallController.isEnabled(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700652 return R.drawable.quantum_ic_call_vd_theme_24;
653 } else {
654 return R.drawable.on_going_call;
655 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800656 }
657
658 /** Returns the message to use with the notification. */
twyend1d1d0c2017-10-05 17:34:43 -0700659 private CharSequence getContentString(DialerCall call, @UserType long userType) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800660 boolean isIncomingOrWaiting =
661 call.getState() == DialerCall.State.INCOMING
662 || call.getState() == DialerCall.State.CALL_WAITING;
663
664 if (isIncomingOrWaiting
665 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
666
667 if (!TextUtils.isEmpty(call.getChildNumber())) {
668 return mContext.getString(R.string.child_number, call.getChildNumber());
669 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
670 return call.getCallSubject();
671 }
672 }
673
674 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700675 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800676 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700677 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800678 }
679
680 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700681 if (call.isSpam()) {
682 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700683 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
684 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700685 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700686 resId = R.string.notification_incoming_call_wifi_template;
wangqi9982f0d2017-10-11 17:46:07 -0700687 } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts(call)) {
twyend1d1d0c2017-10-05 17:34:43 -0700688 return getMultiSimIncomingText(call);
yueg45e45732017-10-09 14:35:06 -0700689 } else if (call.isVideoCall()) {
690 resId = R.string.notification_incoming_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800691 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700692 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800693 }
694 } else if (call.getState() == DialerCall.State.ONHOLD) {
695 resId = R.string.notification_on_hold;
calderwoodra1dc2cea2017-09-20 16:30:41 -0700696 } else if (call.isVideoCall()) {
697 resId =
698 call.getVideoTech().isPaused()
699 ? R.string.notification_ongoing_paused_video_call
700 : R.string.notification_ongoing_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800701 } else if (DialerCall.State.isDialing(call.getState())) {
702 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700703 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700704 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 resId = R.string.notification_requesting_video_call;
706 }
707
708 // Is the call placed through work connection service.
709 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
710 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
711 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700712 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
713 }
714
715 if (resId == R.string.notification_incoming_call_wifi_template
716 || resId == R.string.notification_ongoing_call_wifi_template) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700717 // TODO(a bug): Potentially apply this template logic everywhere.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700718 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800719 }
720
721 return mContext.getString(resId);
722 }
723
Eric Erfanian2ca43182017-08-31 06:57:16 -0700724 private boolean shouldShowEnrichedCallNotification(Session session) {
725 if (session == null) {
726 return false;
727 }
728 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
729 }
730
Eric Erfaniand8046e52017-04-06 09:41:50 -0700731 private int getECIncomingCallText(Session session) {
732 int resId;
733 MultimediaData data = session.getMultimediaData();
734 boolean hasImage = data.hasImageData();
735 boolean hasSubject = !TextUtils.isEmpty(data.getText());
736 boolean hasMap = data.getLocation() != null;
737 if (data.isImportant()) {
738 if (hasMap) {
739 if (hasImage) {
740 if (hasSubject) {
741 resId = R.string.important_notification_incoming_call_with_photo_message_location;
742 } else {
743 resId = R.string.important_notification_incoming_call_with_photo_location;
744 }
745 } else if (hasSubject) {
746 resId = R.string.important_notification_incoming_call_with_message_location;
747 } else {
748 resId = R.string.important_notification_incoming_call_with_location;
749 }
750 } else if (hasImage) {
751 if (hasSubject) {
752 resId = R.string.important_notification_incoming_call_with_photo_message;
753 } else {
754 resId = R.string.important_notification_incoming_call_with_photo;
755 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700756 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700757 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700758 } else {
759 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700760 }
761 if (mContext.getString(resId).length() > 50) {
762 resId = R.string.important_notification_incoming_call_attachments;
763 }
764 } else {
765 if (hasMap) {
766 if (hasImage) {
767 if (hasSubject) {
768 resId = R.string.notification_incoming_call_with_photo_message_location;
769 } else {
770 resId = R.string.notification_incoming_call_with_photo_location;
771 }
772 } else if (hasSubject) {
773 resId = R.string.notification_incoming_call_with_message_location;
774 } else {
775 resId = R.string.notification_incoming_call_with_location;
776 }
777 } else if (hasImage) {
778 if (hasSubject) {
779 resId = R.string.notification_incoming_call_with_photo_message;
780 } else {
781 resId = R.string.notification_incoming_call_with_photo;
782 }
783 } else {
784 resId = R.string.notification_incoming_call_with_message;
785 }
786 }
787 if (mContext.getString(resId).length() > 50) {
788 resId = R.string.notification_incoming_call_attachments;
789 }
790 return resId;
791 }
792
twyend1d1d0c2017-10-05 17:34:43 -0700793 private CharSequence getMultiSimIncomingText(DialerCall call) {
794 PhoneAccount phoneAccount =
795 mContext.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
796 SpannableString string =
797 new SpannableString(
798 mContext.getString(
799 R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
800 int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
801 int accountEnd = accountStart + phoneAccount.getLabel().length();
802
803 string.setSpan(
804 new ForegroundColorSpan(phoneAccount.getHighlightColor()),
805 accountStart,
806 accountEnd,
807 Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
808 return string;
809 }
810
Eric Erfanianccca3152017-02-22 16:32:36 -0800811 /** Gets the most relevant call to display in the notification. */
812 private DialerCall getCallToShow(CallList callList) {
813 if (callList == null) {
814 return null;
815 }
816 DialerCall call = callList.getIncomingCall();
817 if (call == null) {
818 call = callList.getOutgoingCall();
819 }
820 if (call == null) {
821 call = callList.getVideoUpgradeRequestCall();
822 }
823 if (call == null) {
824 call = callList.getActiveOrBackgroundCall();
825 }
826 return call;
827 }
828
829 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
830 Spannable spannable = new SpannableString(mContext.getText(stringRes));
831 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
832 // This will only work for cases where the Notification.Builder has a fullscreen intent set
833 // Notification.Builder that does not have a full screen intent will take the color of the
834 // app and the following leads to a no-op.
835 spannable.setSpan(
836 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
837 }
838 return spannable;
839 }
840
841 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700842 LogUtil.d(
843 "StatusBarNotifier.addAnswerAction",
844 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800845 PendingIntent answerVoicePendingIntent =
846 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
847 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700848 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700849 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700850 getActionText(
851 R.string.notification_action_answer, R.color.notification_action_accept),
852 answerVoicePendingIntent)
853 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800854 }
855
856 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700857 LogUtil.d(
858 "StatusBarNotifier.addDismissAction",
859 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800860 PendingIntent declinePendingIntent =
861 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
862 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700863 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700864 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700865 getActionText(
866 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
867 declinePendingIntent)
868 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800869 }
870
871 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700872 LogUtil.d(
873 "StatusBarNotifier.addHangupAction",
874 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800875 PendingIntent hangupPendingIntent =
876 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
877 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700878 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700879 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700880 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700881 hangupPendingIntent)
882 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 }
884
yuegb26c1ae2017-09-18 16:59:16 -0700885 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
886 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
887 == CallAudioState.ROUTE_BLUETOOTH) {
888 // Don't add speaker button if bluetooth is connected
889 return;
890 }
891 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
892 addSpeakerOffAction(builder);
893 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
894 addSpeakerOnAction(builder);
895 }
896 }
897
898 private void addSpeakerOnAction(Notification.Builder builder) {
899 LogUtil.d(
900 "StatusBarNotifier.addSpeakerOnAction",
901 "will show \"Speaker on\" action in the ongoing active call Notification");
902 PendingIntent speakerOnPendingIntent =
903 createNotificationPendingIntent(mContext, ACTION_TURN_ON_SPEAKER);
904 builder.addAction(
905 new Notification.Action.Builder(
906 Icon.createWithResource(mContext, R.drawable.quantum_ic_volume_up_white_24),
907 mContext.getText(R.string.notification_action_speaker_on),
908 speakerOnPendingIntent)
909 .build());
910 }
911
912 private void addSpeakerOffAction(Notification.Builder builder) {
913 LogUtil.d(
914 "StatusBarNotifier.addSpeakerOffAction",
915 "will show \"Speaker off\" action in the ongoing active call Notification");
916 PendingIntent speakerOffPendingIntent =
917 createNotificationPendingIntent(mContext, ACTION_TURN_OFF_SPEAKER);
918 builder.addAction(
919 new Notification.Action.Builder(
920 Icon.createWithResource(mContext, R.drawable.quantum_ic_phone_in_talk_white_24),
921 mContext.getText(R.string.notification_action_speaker_off),
922 speakerOffPendingIntent)
923 .build());
924 }
925
Eric Erfanianccca3152017-02-22 16:32:36 -0800926 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700927 LogUtil.i(
928 "StatusBarNotifier.addVideoCallAction",
929 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800930 PendingIntent answerVideoPendingIntent =
931 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
932 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700933 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700934 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700935 getActionText(
936 R.string.notification_action_answer_video,
937 R.color.notification_action_answer_video),
938 answerVideoPendingIntent)
939 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800940 }
941
942 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700943 LogUtil.i(
944 "StatusBarNotifier.addAcceptUpgradeRequestAction",
945 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800946 PendingIntent acceptVideoPendingIntent =
947 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
948 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700949 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700950 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700951 getActionText(
952 R.string.notification_action_accept, R.color.notification_action_accept),
953 acceptVideoPendingIntent)
954 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800955 }
956
957 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700958 LogUtil.i(
959 "StatusBarNotifier.addDismissUpgradeRequestAction",
960 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800961 PendingIntent declineVideoPendingIntent =
962 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
963 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700964 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700965 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700966 getActionText(
967 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
968 declineVideoPendingIntent)
969 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800970 }
971
972 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700973 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800974 // Ok, we actually want to launch the incoming call
975 // UI at this point (in addition to simply posting a notification
976 // to the status bar). Setting fullScreenIntent will cause
977 // the InCallScreen to be launched immediately *unless* the
978 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700979 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 }
982
983 private Notification.Builder getNotificationBuilder() {
984 final Notification.Builder builder = new Notification.Builder(mContext);
985 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700986 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700987 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -0700988 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -0700989 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800990
991 return builder;
992 }
993
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700994 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800995 Intent intent =
996 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700997 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800998
Eric Erfanian2ca43182017-08-31 06:57:16 -0700999 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001000 if (isFullScreen) {
1001 // Use a unique request code so that the pending intent isn't clobbered by the
1002 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -07001003 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001004 }
1005
1006 // PendingIntent that can be used to launch the InCallActivity. The
1007 // system fires off this intent if the user pulls down the windowshade
1008 // and clicks the notification's expanded view. It's also used to
1009 // launch the InCallActivity immediately when when there's an incoming
1010 // call (see the "fullScreenIntent" field below).
1011 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
1012 }
1013
1014 private void setStatusBarCallListener(StatusBarCallListener listener) {
1015 if (mStatusBarCallListener != null) {
1016 mStatusBarCallListener.cleanup();
1017 }
1018 mStatusBarCallListener = listener;
1019 }
1020
wangqi9982f0d2017-10-11 17:46:07 -07001021 private boolean hasMultiplePhoneAccounts(DialerCall call) {
1022 if (call.getCallCapableAccounts() == null) {
1023 return false;
1024 }
1025 return call.getCallCapableAccounts().size() > 1;
twyend1d1d0c2017-10-05 17:34:43 -07001026 }
1027
yuegb26c1ae2017-09-18 16:59:16 -07001028 @Override
wangqic8cf79e2017-10-17 09:21:00 -07001029 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1030 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1031 DialerCall call = CallList.getInstance().getCallById(callId);
1032 if (call != null) {
1033 call.getLogState().contactLookupResult = entry.contactLookupResult;
1034 buildAndSendNotification(CallList.getInstance(), call, entry);
1035 }
1036 }
1037
1038 @Override
1039 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1040 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1041 DialerCall call = CallList.getInstance().getCallById(callId);
1042 if (call != null) {
1043 buildAndSendNotification(CallList.getInstance(), call, entry);
1044 }
yuegb26c1ae2017-09-18 16:59:16 -07001045 }
1046
Eric Erfanianccca3152017-02-22 16:32:36 -08001047 private class StatusBarCallListener implements DialerCallListener {
1048
1049 private DialerCall mDialerCall;
1050
1051 StatusBarCallListener(DialerCall dialerCall) {
1052 mDialerCall = dialerCall;
1053 mDialerCall.addListener(this);
1054 }
1055
1056 void cleanup() {
1057 mDialerCall.removeListener(this);
1058 }
1059
1060 @Override
1061 public void onDialerCallDisconnect() {}
1062
1063 @Override
1064 public void onDialerCallUpdate() {
1065 if (CallList.getInstance().getIncomingCall() == null) {
1066 mDialerRingtoneManager.stopCallWaitingTone();
1067 }
1068 }
1069
1070 @Override
1071 public void onDialerCallChildNumberChange() {}
1072
1073 @Override
1074 public void onDialerCallLastForwardedNumberChange() {}
1075
1076 @Override
1077 public void onDialerCallUpgradeToVideo() {}
1078
1079 @Override
1080 public void onWiFiToLteHandover() {}
1081
1082 @Override
1083 public void onHandoverToWifiFailure() {}
1084
Eric Erfanianc857f902017-05-15 14:05:33 -07001085 @Override
1086 public void onInternationalCallOnWifi() {}
1087
Eric Erfanian2ca43182017-08-31 06:57:16 -07001088 @Override
1089 public void onEnrichedCallSessionUpdate() {}
1090
Eric Erfanianccca3152017-02-22 16:32:36 -08001091 /**
1092 * Responds to changes in the session modification state for the call by dismissing the status
1093 * bar notification as required.
1094 */
1095 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001096 public void onDialerCallSessionModificationStateChange() {
1097 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001098 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001099 cleanup();
wangqic8cf79e2017-10-17 09:21:00 -07001100 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001101 }
1102 }
1103 }
1104}