blob: 4ce43935e3b96a4fbb38ed5bb25e5d278d4a3ee9 [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 AudioModeProvider.AudioModeListener,
98 ContactInfoCacheCallback {
Eric Erfanianccca3152017-02-22 16:32:36 -080099
Eric Erfanian2ca43182017-08-31 06:57:16 -0700100 private static final int NOTIFICATION_ID = 1;
101
Eric Erfanianccca3152017-02-22 16:32:36 -0800102 // Notification types
103 // Indicates that no notification is currently showing.
104 private static final int NOTIFICATION_NONE = 0;
105 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700106 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800107 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700108 private static final int NOTIFICATION_INCOMING_CALL = 2;
109 // Notification for incoming calls in the case where there is already an active call.
110 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
111 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800112
Eric Erfanianccca3152017-02-22 16:32:36 -0800113 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
114
115 private final Context mContext;
116 private final ContactInfoCache mContactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800117 private final DialerRingtoneManager mDialerRingtoneManager;
118 @Nullable private ContactsPreferences mContactsPreferences;
119 private int mCurrentNotification = NOTIFICATION_NONE;
120 private int mCallState = DialerCall.State.INVALID;
121 private int mSavedIcon = 0;
122 private String mSavedContent = null;
123 private Bitmap mSavedLargeIcon;
124 private String mSavedContentTitle;
yuegb26c1ae2017-09-18 16:59:16 -0700125 private CallAudioState savedCallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800126 private Uri mRingtone;
127 private StatusBarCallListener mStatusBarCallListener;
128
Eric Erfaniand8046e52017-04-06 09:41:50 -0700129 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
wangqic8cf79e2017-10-17 09:21:00 -0700130 Trace.beginSection("StatusBarNotifier.Constructor");
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);
wangqic8cf79e2017-10-17 09:21:00 -0700140 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800141 }
142
143 /**
144 * Should only be called from a irrecoverable state where it is necessary to dismiss all
145 * notifications.
146 */
yueg01a964d2017-10-03 15:25:41 -0700147 static void clearAllCallNotifications() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700148 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700149 "StatusBarNotifier.clearAllCallNotifications",
150 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800151
yueg01a964d2017-10-03 15:25:41 -0700152 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800153 }
154
155 private static int getWorkStringFromPersonalString(int resId) {
156 if (resId == R.string.notification_ongoing_call) {
157 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800158 } else if (resId == R.string.notification_incoming_call) {
159 return R.string.notification_incoming_work_call;
160 } else {
161 return resId;
162 }
163 }
164
165 /**
166 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
167 * context.
168 */
169 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
170 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
171 return PendingIntent.getBroadcast(context, 0, intent, 0);
172 }
173
174 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
175 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700176 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800177 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700178 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
wangqic8cf79e2017-10-17 09:21:00 -0700179 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800180 }
181
Eric Erfaniand8046e52017-04-06 09:41:50 -0700182 @Override
183 public void onEnrichedCallStateChanged() {
184 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
wangqic8cf79e2017-10-17 09:21:00 -0700185 updateNotification();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700186 }
187
Eric Erfanianccca3152017-02-22 16:32:36 -0800188 /**
189 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
190 * to a new incoming call.
191 *
192 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
193 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
194 * foreground activity is marked as "immersive".
195 *
196 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
197 * ringing connection" event from the telephony layer.)
198 *
199 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
200 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
201 * we'll simply update or cancel the in-call notification based on the current phone state.
202 *
wangqic8cf79e2017-10-17 09:21:00 -0700203 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800204 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700205 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700206 public void updateNotification() {
207 updateInCallNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800208 }
209
210 /**
211 * Take down the in-call notification.
212 *
wangqic8cf79e2017-10-17 09:21:00 -0700213 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800214 */
215 private void cancelNotification() {
216 if (mStatusBarCallListener != null) {
217 setStatusBarCallListener(null);
218 }
219 if (mCurrentNotification != NOTIFICATION_NONE) {
yueg01a964d2017-10-03 15:25:41 -0700220 TelecomAdapter.getInstance().stopForegroundNotification();
221 mCurrentNotification = NOTIFICATION_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800222 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800223 }
224
225 /**
226 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
227 * status bar notification based on the current telephony state, or cancels the notification if
228 * the phone is totally idle.
229 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700230 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700231 private void updateInCallNotification() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700232 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800233
wangqic8cf79e2017-10-17 09:21:00 -0700234 final DialerCall call = getCallToShow(CallList.getInstance());
Eric Erfanianccca3152017-02-22 16:32:36 -0800235
236 if (call != null) {
wangqic8cf79e2017-10-17 09:21:00 -0700237 showNotification(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800238 } else {
239 cancelNotification();
240 }
241 }
242
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700243 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700244 private void showNotification(final DialerCall call) {
wangqicf61ca02017-08-31 15:32:55 -0700245 Trace.beginSection("StatusBarNotifier.showNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800246 final boolean isIncoming =
247 (call.getState() == DialerCall.State.INCOMING
248 || call.getState() == DialerCall.State.CALL_WAITING);
249 setStatusBarCallListener(new StatusBarCallListener(call));
250
251 // we make a call to the contact info cache to query for supplemental data to what the
252 // call provides. This includes the contact name and photo.
253 // This callback will always get called immediately and synchronously with whatever data
254 // it has available, and may make a subsequent call later (same thread) if it had to
255 // call into the contacts provider for more data.
wangqic8cf79e2017-10-17 09:21:00 -0700256 mContactInfoCache.findInfo(call, isIncoming, this);
wangqicf61ca02017-08-31 15:32:55 -0700257 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800258 }
259
260 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700261 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800262 private void buildAndSendNotification(
263 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
wangqicf61ca02017-08-31 15:32:55 -0700264 Trace.beginSection("StatusBarNotifier.buildAndSendNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800265 // This can get called to update an existing notification after contact information has come
266 // back. However, it can happen much later. Before we continue, we need to make sure that
267 // the call being passed in is still the one we want to show in the notification.
268 final DialerCall call = getCallToShow(callList);
269 if (call == null || !call.getId().equals(originalCall.getId())) {
wangqicf61ca02017-08-31 15:32:55 -0700270 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800271 return;
272 }
273
wangqicf61ca02017-08-31 15:32:55 -0700274 Trace.beginSection("prepare work");
Eric Erfanianccca3152017-02-22 16:32:36 -0800275 final int callState = call.getState();
yuegb26c1ae2017-09-18 16:59:16 -0700276 final CallAudioState callAudioState = AudioModeProvider.getInstance().getAudioState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800277
wangqic8cf79e2017-10-17 09:21:00 -0700278 Trace.beginSection("read icon and strings");
Eric Erfanianccca3152017-02-22 16:32:36 -0800279 // Check if data has changed; if nothing is different, don't issue another notification.
280 final int iconResId = getIconToDisplay(call);
Eric Erfanian83b20212017-05-31 08:53:10 -0700281 Bitmap largeIcon = getLargeIconToDisplay(mContext, contactInfo, call);
twyend1d1d0c2017-10-05 17:34:43 -0700282 final CharSequence content = getContentString(call, contactInfo.userType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800283 final String contentTitle = getContentTitle(contactInfo, call);
wangqic8cf79e2017-10-17 09:21:00 -0700284 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800285
286 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700287 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700288 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800289 final int notificationType;
290 if (callState == DialerCall.State.INCOMING
291 || callState == DialerCall.State.CALL_WAITING
292 || isVideoUpgradeRequest) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700293 if (ConfigProviderBindings.get(mContext)
294 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
295 notificationType =
296 InCallPresenter.getInstance().isShowingInCallUi()
297 ? NOTIFICATION_INCOMING_CALL_QUIET
298 : NOTIFICATION_INCOMING_CALL;
299 } else {
300 boolean alreadyActive =
301 callList.getActiveOrBackgroundCall() != null
302 && InCallPresenter.getInstance().isShowingInCallUi();
303 notificationType =
304 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
305 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800306 } else {
307 notificationType = NOTIFICATION_IN_CALL;
308 }
wangqicf61ca02017-08-31 15:32:55 -0700309 Trace.endSection(); // prepare work
Eric Erfanianccca3152017-02-22 16:32:36 -0800310
311 if (!checkForChangeAndSaveData(
312 iconResId,
twyend1d1d0c2017-10-05 17:34:43 -0700313 content.toString(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800314 largeIcon,
315 contentTitle,
316 callState,
317 notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700318 contactInfo.contactRingtoneUri,
319 callAudioState)) {
wangqicf61ca02017-08-31 15:32:55 -0700320 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800321 return;
322 }
323
324 if (largeIcon != null) {
325 largeIcon = getRoundedIcon(largeIcon);
326 }
327
328 // This builder is used for the notification shown when the device is locked and the user
329 // has set their notification settings to 'hide sensitive content'
330 // {@see Notification.Builder#setPublicVersion}.
331 Notification.Builder publicBuilder = new Notification.Builder(mContext);
332 publicBuilder
333 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700334 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800335 // Hide work call state for the lock screen notification
336 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
337 setNotificationWhen(call, callState, publicBuilder);
338
339 // Builder for the notification shown when the device is unlocked or the user has set their
340 // notification settings to 'show all notification content'.
341 final Notification.Builder builder = getNotificationBuilder();
342 builder.setPublicVersion(publicBuilder.build());
343
344 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700345 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800346
Eric Erfanian10b34a52017-05-04 08:23:17 -0700347 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
348 switch (notificationType) {
349 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700350 if (BuildCompat.isAtLeastO()) {
351 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
352 }
wangqic8cf79e2017-10-17 09:21:00 -0700353 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700354 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
355 // Set the notification category and bump the priority for incoming calls
356 builder.setCategory(Notification.CATEGORY_CALL);
357 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700358 builder.setPriority(Notification.PRIORITY_MAX);
359 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
360 LogUtil.i(
361 "StatusBarNotifier.buildAndSendNotification",
362 "Canceling old notification so this one can be noisy");
363 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
364 // notification (if there is one) so the fullScreenIntent or HUN will show
yueg01a964d2017-10-03 15:25:41 -0700365 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700366 }
367 break;
368 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700369 if (BuildCompat.isAtLeastO()) {
370 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
371 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700372 break;
373 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700374 if (BuildCompat.isAtLeastO()) {
375 publicBuilder.setColorized(true);
376 builder.setColorized(true);
377 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
378 }
379 break;
380 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700381 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800382 }
383
384 // Set the content
385 builder.setContentText(content);
386 builder.setSmallIcon(iconResId);
387 builder.setContentTitle(contentTitle);
388 builder.setLargeIcon(largeIcon);
yueg01a964d2017-10-03 15:25:41 -0700389 builder.setColor(InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800390
391 if (isVideoUpgradeRequest) {
392 builder.setUsesChronometer(false);
393 addDismissUpgradeRequestAction(builder);
394 addAcceptUpgradeRequestAction(builder);
395 } else {
yuegb26c1ae2017-09-18 16:59:16 -0700396 createIncomingCallNotification(call, callState, callAudioState, builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800397 }
398
399 addPersonReference(builder, contactInfo, call);
400
wangqicf61ca02017-08-31 15:32:55 -0700401 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800402 // Fire off the notification
403 Notification notification = builder.build();
404
405 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
406 notification.flags |= Notification.FLAG_INSISTENT;
407 notification.sound = contactInfo.contactRingtoneUri;
408 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
409 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
410 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
411 notification.audioAttributes = audioAttributes.build();
412 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
413 notification.vibrate = VIBRATE_PATTERN;
414 }
415 }
416 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700417 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800418 mDialerRingtoneManager.playCallWaitingTone();
419 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800420
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700421 LogUtil.i(
422 "StatusBarNotifier.buildAndSendNotification",
423 "displaying notification for " + notificationType);
424
yueg01a964d2017-10-03 15:25:41 -0700425 // If a notification exists, this will only update it.
426 TelecomAdapter.getInstance().startForegroundNotification(NOTIFICATION_ID, notification);
427
wangqicf61ca02017-08-31 15:32:55 -0700428 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800429 call.getLatencyReport().onNotificationShown();
430 mCurrentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700431 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800432 }
433
434 private void createIncomingCallNotification(
yuegb26c1ae2017-09-18 16:59:16 -0700435 DialerCall call, int state, CallAudioState callAudioState, Notification.Builder builder) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800436 setNotificationWhen(call, state, builder);
437
438 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
439 if (state == DialerCall.State.ACTIVE
440 || state == DialerCall.State.ONHOLD
441 || DialerCall.State.isDialing(state)) {
442 addHangupAction(builder);
yuegb26c1ae2017-09-18 16:59:16 -0700443 addSpeakerAction(builder, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800444 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
445 addDismissAction(builder);
446 if (call.isVideoCall()) {
447 addVideoCallAction(builder);
448 } else {
449 addAnswerAction(builder);
450 }
451 }
452 }
453
454 /**
455 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
456 * duration of the call. For all other states, the notification will automatically show the time
457 * at which the notification was created.
458 */
459 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
460 if (state == DialerCall.State.ACTIVE) {
461 builder.setUsesChronometer(true);
462 builder.setWhen(call.getConnectTimeMillis());
463 } else {
464 builder.setUsesChronometer(false);
465 }
466 }
467
468 /**
469 * Checks the new notification data and compares it against any notification that we are already
470 * displaying. If the data is exactly the same, we return false so that we do not issue a new
471 * notification for the exact same data.
472 */
473 private boolean checkForChangeAndSaveData(
474 int icon,
475 String content,
476 Bitmap largeIcon,
477 String contentTitle,
478 int state,
479 int notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700480 Uri ringtone,
481 CallAudioState callAudioState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800482
483 // The two are different:
484 // if new title is not null, it should be different from saved version OR
485 // if new title is null, the saved version should not be null
486 final boolean contentTitleChanged =
487 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
488 || (contentTitle == null && mSavedContentTitle != null);
489
Eric Erfanian8369df02017-05-03 10:27:13 -0700490 boolean largeIconChanged =
491 mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon);
492
Eric Erfanianccca3152017-02-22 16:32:36 -0800493 // any change means we are definitely updating
494 boolean retval =
495 (mSavedIcon != icon)
496 || !Objects.equals(mSavedContent, content)
497 || (mCallState != state)
Eric Erfanian8369df02017-05-03 10:27:13 -0700498 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800499 || contentTitleChanged
yuegb26c1ae2017-09-18 16:59:16 -0700500 || !Objects.equals(mRingtone, ringtone)
501 || !Objects.equals(savedCallAudioState, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800502
wangqi9982f0d2017-10-11 17:46:07 -0700503 LogUtil.d(
504 "StatusBarNotifier.checkForChangeAndSaveData",
505 "data changed: icon: %b, content: %b, state: %b, largeIcon: %b, title: %b, ringtone: %b, "
506 + "audioState: %b, type: %b",
507 (mSavedIcon != icon),
508 !Objects.equals(mSavedContent, content),
509 (mCallState != state),
510 largeIconChanged,
511 contentTitleChanged,
512 !Objects.equals(mRingtone, ringtone),
513 !Objects.equals(savedCallAudioState, callAudioState),
514 mCurrentNotification != notificationType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800515 // If we aren't showing a notification right now or the notification type is changing,
516 // definitely do an update.
517 if (mCurrentNotification != notificationType) {
518 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700519 LogUtil.d(
520 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800521 }
522 retval = true;
523 }
524
525 mSavedIcon = icon;
526 mSavedContent = content;
527 mCallState = state;
528 mSavedLargeIcon = largeIcon;
529 mSavedContentTitle = contentTitle;
530 mRingtone = ringtone;
yuegb26c1ae2017-09-18 16:59:16 -0700531 savedCallAudioState = callAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800532
533 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700534 LogUtil.d(
535 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800536 }
537
538 return retval;
539 }
540
541 /** Returns the main string to use in the notification. */
542 @VisibleForTesting
543 @Nullable
544 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700545 if (call.isConferenceCall()) {
546 return CallerInfoUtils.getConferenceString(
547 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800548 }
549
550 String preferredName =
551 ContactDisplayUtils.getPreferredDisplayName(
552 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
553 if (TextUtils.isEmpty(preferredName)) {
554 return TextUtils.isEmpty(contactInfo.number)
555 ? null
556 : BidiFormatter.getInstance()
557 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
558 }
559 return preferredName;
560 }
561
562 private void addPersonReference(
563 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
564 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
565 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
566 // NotificationManager using it.
567 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
568 builder.addPerson(contactInfo.lookupUri.toString());
569 } else if (!TextUtils.isEmpty(call.getNumber())) {
570 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
571 }
572 }
573
574 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700575 private static Bitmap getLargeIconToDisplay(
576 Context context, ContactCacheEntry contactInfo, DialerCall call) {
wangqic8cf79e2017-10-17 09:21:00 -0700577 Trace.beginSection("StatusBarNotifier.getLargeIconToDisplay");
Eric Erfanian83b20212017-05-31 08:53:10 -0700578 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800579 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800580 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
581 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
582 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700583 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700584 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
585 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700586 @ContactType
587 int contactType =
588 LetterTileDrawable.getContactTypeFromPrimitives(
wangqi9982f0d2017-10-11 17:46:07 -0700589 call.isVoiceMailNumber(),
Eric Erfanian2ca43182017-08-31 06:57:16 -0700590 call.isSpam(),
591 contactInfo.isBusiness,
592 call.getNumberPresentation(),
593 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700594 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700595
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700596 lettertile.setCanonicalDialerLetterTileDetails(
597 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
598 contactInfo.lookupKey,
599 LetterTileDrawable.SHAPE_CIRCLE,
600 contactType);
601 largeIcon = lettertile.getBitmap(width, height);
602 }
603
Eric Erfanianccca3152017-02-22 16:32:36 -0800604 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700605 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800606 largeIcon = DrawableConverter.drawableToBitmap(drawable);
607 }
wangqic8cf79e2017-10-17 09:21:00 -0700608 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800609 return largeIcon;
610 }
611
612 private Bitmap getRoundedIcon(Bitmap bitmap) {
613 if (bitmap == null) {
614 return null;
615 }
616 final int height =
617 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
618 final int width =
619 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
620 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
621 }
622
623 /**
624 * Returns the appropriate icon res Id to display based on the call for which we want to display
625 * information.
626 */
627 private int getIconToDisplay(DialerCall call) {
628 // Even if both lines are in use, we only show a single item in
629 // the expanded Notifications UI. It's labeled "Ongoing call"
630 // (or "On hold" if there's only one call, and it's on hold.)
631 // Also, we don't have room to display caller-id info from two
632 // different calls. So if both lines are in use, display info
633 // from the foreground call. And if there's a ringing call,
634 // display that regardless of the state of the other calls.
635 if (call.getState() == DialerCall.State.ONHOLD) {
636 return R.drawable.ic_phone_paused_white_24dp;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700637 } else if (call.getVideoTech().getSessionModificationState()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700638 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST
639 || call.isVideoCall()) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700640 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700641 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
642 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700643 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
644 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
645 // 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 -0700646 return R.drawable.ic_hd_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800647 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700648 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
Eric Erfanian938468d2017-10-24 14:05:52 -0700649 if (ReturnToCallController.isEnabled(mContext)
650 || NewReturnToCallController.isEnabled(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700651 return R.drawable.quantum_ic_call_vd_theme_24;
652 } else {
653 return R.drawable.on_going_call;
654 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800655 }
656
657 /** Returns the message to use with the notification. */
twyend1d1d0c2017-10-05 17:34:43 -0700658 private CharSequence getContentString(DialerCall call, @UserType long userType) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800659 boolean isIncomingOrWaiting =
660 call.getState() == DialerCall.State.INCOMING
661 || call.getState() == DialerCall.State.CALL_WAITING;
662
663 if (isIncomingOrWaiting
664 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
665
666 if (!TextUtils.isEmpty(call.getChildNumber())) {
667 return mContext.getString(R.string.child_number, call.getChildNumber());
668 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
669 return call.getCallSubject();
670 }
671 }
672
673 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700674 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800675 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700676 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800677 }
678
679 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700680 if (call.isSpam()) {
681 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700682 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
683 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700684 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700685 resId = R.string.notification_incoming_call_wifi_template;
wangqi9982f0d2017-10-11 17:46:07 -0700686 } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts(call)) {
twyend1d1d0c2017-10-05 17:34:43 -0700687 return getMultiSimIncomingText(call);
yueg45e45732017-10-09 14:35:06 -0700688 } else if (call.isVideoCall()) {
689 resId = R.string.notification_incoming_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800690 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700691 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800692 }
693 } else if (call.getState() == DialerCall.State.ONHOLD) {
694 resId = R.string.notification_on_hold;
calderwoodra1dc2cea2017-09-20 16:30:41 -0700695 } else if (call.isVideoCall()) {
696 resId =
697 call.getVideoTech().isPaused()
698 ? R.string.notification_ongoing_paused_video_call
699 : R.string.notification_ongoing_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800700 } else if (DialerCall.State.isDialing(call.getState())) {
701 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700702 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700703 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 resId = R.string.notification_requesting_video_call;
705 }
706
707 // Is the call placed through work connection service.
708 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
709 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
710 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700711 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
712 }
713
714 if (resId == R.string.notification_incoming_call_wifi_template
715 || resId == R.string.notification_ongoing_call_wifi_template) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700716 // TODO(a bug): Potentially apply this template logic everywhere.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700717 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 }
719
720 return mContext.getString(resId);
721 }
722
Eric Erfanian2ca43182017-08-31 06:57:16 -0700723 private boolean shouldShowEnrichedCallNotification(Session session) {
724 if (session == null) {
725 return false;
726 }
727 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
728 }
729
Eric Erfaniand8046e52017-04-06 09:41:50 -0700730 private int getECIncomingCallText(Session session) {
731 int resId;
732 MultimediaData data = session.getMultimediaData();
733 boolean hasImage = data.hasImageData();
734 boolean hasSubject = !TextUtils.isEmpty(data.getText());
735 boolean hasMap = data.getLocation() != null;
736 if (data.isImportant()) {
737 if (hasMap) {
738 if (hasImage) {
739 if (hasSubject) {
740 resId = R.string.important_notification_incoming_call_with_photo_message_location;
741 } else {
742 resId = R.string.important_notification_incoming_call_with_photo_location;
743 }
744 } else if (hasSubject) {
745 resId = R.string.important_notification_incoming_call_with_message_location;
746 } else {
747 resId = R.string.important_notification_incoming_call_with_location;
748 }
749 } else if (hasImage) {
750 if (hasSubject) {
751 resId = R.string.important_notification_incoming_call_with_photo_message;
752 } else {
753 resId = R.string.important_notification_incoming_call_with_photo;
754 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700755 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700756 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700757 } else {
758 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700759 }
760 if (mContext.getString(resId).length() > 50) {
761 resId = R.string.important_notification_incoming_call_attachments;
762 }
763 } else {
764 if (hasMap) {
765 if (hasImage) {
766 if (hasSubject) {
767 resId = R.string.notification_incoming_call_with_photo_message_location;
768 } else {
769 resId = R.string.notification_incoming_call_with_photo_location;
770 }
771 } else if (hasSubject) {
772 resId = R.string.notification_incoming_call_with_message_location;
773 } else {
774 resId = R.string.notification_incoming_call_with_location;
775 }
776 } else if (hasImage) {
777 if (hasSubject) {
778 resId = R.string.notification_incoming_call_with_photo_message;
779 } else {
780 resId = R.string.notification_incoming_call_with_photo;
781 }
782 } else {
783 resId = R.string.notification_incoming_call_with_message;
784 }
785 }
786 if (mContext.getString(resId).length() > 50) {
787 resId = R.string.notification_incoming_call_attachments;
788 }
789 return resId;
790 }
791
twyend1d1d0c2017-10-05 17:34:43 -0700792 private CharSequence getMultiSimIncomingText(DialerCall call) {
793 PhoneAccount phoneAccount =
794 mContext.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
795 SpannableString string =
796 new SpannableString(
797 mContext.getString(
798 R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
799 int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
800 int accountEnd = accountStart + phoneAccount.getLabel().length();
801
802 string.setSpan(
803 new ForegroundColorSpan(phoneAccount.getHighlightColor()),
804 accountStart,
805 accountEnd,
806 Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
807 return string;
808 }
809
Eric Erfanianccca3152017-02-22 16:32:36 -0800810 /** Gets the most relevant call to display in the notification. */
811 private DialerCall getCallToShow(CallList callList) {
812 if (callList == null) {
813 return null;
814 }
815 DialerCall call = callList.getIncomingCall();
816 if (call == null) {
817 call = callList.getOutgoingCall();
818 }
819 if (call == null) {
820 call = callList.getVideoUpgradeRequestCall();
821 }
822 if (call == null) {
823 call = callList.getActiveOrBackgroundCall();
824 }
825 return call;
826 }
827
828 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
829 Spannable spannable = new SpannableString(mContext.getText(stringRes));
830 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
831 // This will only work for cases where the Notification.Builder has a fullscreen intent set
832 // Notification.Builder that does not have a full screen intent will take the color of the
833 // app and the following leads to a no-op.
834 spannable.setSpan(
835 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
836 }
837 return spannable;
838 }
839
840 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700841 LogUtil.d(
842 "StatusBarNotifier.addAnswerAction",
843 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800844 PendingIntent answerVoicePendingIntent =
845 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
846 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700847 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700848 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700849 getActionText(
850 R.string.notification_action_answer, R.color.notification_action_accept),
851 answerVoicePendingIntent)
852 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800853 }
854
855 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700856 LogUtil.d(
857 "StatusBarNotifier.addDismissAction",
858 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800859 PendingIntent declinePendingIntent =
860 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
861 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700862 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700863 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700864 getActionText(
865 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
866 declinePendingIntent)
867 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800868 }
869
870 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700871 LogUtil.d(
872 "StatusBarNotifier.addHangupAction",
873 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800874 PendingIntent hangupPendingIntent =
875 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
876 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700877 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700878 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700879 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700880 hangupPendingIntent)
881 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800882 }
883
yuegb26c1ae2017-09-18 16:59:16 -0700884 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
885 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
886 == CallAudioState.ROUTE_BLUETOOTH) {
887 // Don't add speaker button if bluetooth is connected
888 return;
889 }
890 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
891 addSpeakerOffAction(builder);
892 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
893 addSpeakerOnAction(builder);
894 }
895 }
896
897 private void addSpeakerOnAction(Notification.Builder builder) {
898 LogUtil.d(
899 "StatusBarNotifier.addSpeakerOnAction",
900 "will show \"Speaker on\" action in the ongoing active call Notification");
901 PendingIntent speakerOnPendingIntent =
902 createNotificationPendingIntent(mContext, ACTION_TURN_ON_SPEAKER);
903 builder.addAction(
904 new Notification.Action.Builder(
905 Icon.createWithResource(mContext, R.drawable.quantum_ic_volume_up_white_24),
906 mContext.getText(R.string.notification_action_speaker_on),
907 speakerOnPendingIntent)
908 .build());
909 }
910
911 private void addSpeakerOffAction(Notification.Builder builder) {
912 LogUtil.d(
913 "StatusBarNotifier.addSpeakerOffAction",
914 "will show \"Speaker off\" action in the ongoing active call Notification");
915 PendingIntent speakerOffPendingIntent =
916 createNotificationPendingIntent(mContext, ACTION_TURN_OFF_SPEAKER);
917 builder.addAction(
918 new Notification.Action.Builder(
919 Icon.createWithResource(mContext, R.drawable.quantum_ic_phone_in_talk_white_24),
920 mContext.getText(R.string.notification_action_speaker_off),
921 speakerOffPendingIntent)
922 .build());
923 }
924
Eric Erfanianccca3152017-02-22 16:32:36 -0800925 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700926 LogUtil.i(
927 "StatusBarNotifier.addVideoCallAction",
928 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800929 PendingIntent answerVideoPendingIntent =
930 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
931 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700932 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700933 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700934 getActionText(
935 R.string.notification_action_answer_video,
936 R.color.notification_action_answer_video),
937 answerVideoPendingIntent)
938 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800939 }
940
941 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700942 LogUtil.i(
943 "StatusBarNotifier.addAcceptUpgradeRequestAction",
944 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800945 PendingIntent acceptVideoPendingIntent =
946 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
947 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700948 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700949 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700950 getActionText(
951 R.string.notification_action_accept, R.color.notification_action_accept),
952 acceptVideoPendingIntent)
953 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800954 }
955
956 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700957 LogUtil.i(
958 "StatusBarNotifier.addDismissUpgradeRequestAction",
959 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800960 PendingIntent declineVideoPendingIntent =
961 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
962 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700963 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700964 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700965 getActionText(
966 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
967 declineVideoPendingIntent)
968 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800969 }
970
971 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700972 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800973 // Ok, we actually want to launch the incoming call
974 // UI at this point (in addition to simply posting a notification
975 // to the status bar). Setting fullScreenIntent will cause
976 // the InCallScreen to be launched immediately *unless* the
977 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700978 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800979 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800980 }
981
982 private Notification.Builder getNotificationBuilder() {
983 final Notification.Builder builder = new Notification.Builder(mContext);
984 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700985 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700986 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -0700987 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -0700988 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -0800989
990 return builder;
991 }
992
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700993 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800994 Intent intent =
995 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700996 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -0800997
Eric Erfanian2ca43182017-08-31 06:57:16 -0700998 int requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -0800999 if (isFullScreen) {
1000 // Use a unique request code so that the pending intent isn't clobbered by the
1001 // non-full screen pending intent.
Eric Erfanian2ca43182017-08-31 06:57:16 -07001002 requestCode = InCallActivity.PENDING_INTENT_REQUEST_CODE_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001003 }
1004
1005 // PendingIntent that can be used to launch the InCallActivity. The
1006 // system fires off this intent if the user pulls down the windowshade
1007 // and clicks the notification's expanded view. It's also used to
1008 // launch the InCallActivity immediately when when there's an incoming
1009 // call (see the "fullScreenIntent" field below).
1010 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
1011 }
1012
1013 private void setStatusBarCallListener(StatusBarCallListener listener) {
1014 if (mStatusBarCallListener != null) {
1015 mStatusBarCallListener.cleanup();
1016 }
1017 mStatusBarCallListener = listener;
1018 }
1019
wangqi9982f0d2017-10-11 17:46:07 -07001020 private boolean hasMultiplePhoneAccounts(DialerCall call) {
1021 if (call.getCallCapableAccounts() == null) {
1022 return false;
1023 }
1024 return call.getCallCapableAccounts().size() > 1;
twyend1d1d0c2017-10-05 17:34:43 -07001025 }
1026
yuegb26c1ae2017-09-18 16:59:16 -07001027 @Override
1028 public void onAudioStateChanged(CallAudioState audioState) {
wangqic8cf79e2017-10-17 09:21:00 -07001029 updateNotification();
1030 }
1031
1032 @Override
1033 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1034 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1035 DialerCall call = CallList.getInstance().getCallById(callId);
1036 if (call != null) {
1037 call.getLogState().contactLookupResult = entry.contactLookupResult;
1038 buildAndSendNotification(CallList.getInstance(), call, entry);
1039 }
1040 }
1041
1042 @Override
1043 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1044 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1045 DialerCall call = CallList.getInstance().getCallById(callId);
1046 if (call != null) {
1047 buildAndSendNotification(CallList.getInstance(), call, entry);
1048 }
yuegb26c1ae2017-09-18 16:59:16 -07001049 }
1050
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 private class StatusBarCallListener implements DialerCallListener {
1052
1053 private DialerCall mDialerCall;
1054
1055 StatusBarCallListener(DialerCall dialerCall) {
1056 mDialerCall = dialerCall;
1057 mDialerCall.addListener(this);
1058 }
1059
1060 void cleanup() {
1061 mDialerCall.removeListener(this);
1062 }
1063
1064 @Override
1065 public void onDialerCallDisconnect() {}
1066
1067 @Override
1068 public void onDialerCallUpdate() {
1069 if (CallList.getInstance().getIncomingCall() == null) {
1070 mDialerRingtoneManager.stopCallWaitingTone();
1071 }
1072 }
1073
1074 @Override
1075 public void onDialerCallChildNumberChange() {}
1076
1077 @Override
1078 public void onDialerCallLastForwardedNumberChange() {}
1079
1080 @Override
1081 public void onDialerCallUpgradeToVideo() {}
1082
1083 @Override
1084 public void onWiFiToLteHandover() {}
1085
1086 @Override
1087 public void onHandoverToWifiFailure() {}
1088
Eric Erfanianc857f902017-05-15 14:05:33 -07001089 @Override
1090 public void onInternationalCallOnWifi() {}
1091
Eric Erfanian2ca43182017-08-31 06:57:16 -07001092 @Override
1093 public void onEnrichedCallSessionUpdate() {}
1094
Eric Erfanianccca3152017-02-22 16:32:36 -08001095 /**
1096 * Responds to changes in the session modification state for the call by dismissing the status
1097 * bar notification as required.
1098 */
1099 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001100 public void onDialerCallSessionModificationStateChange() {
1101 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001102 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001103 cleanup();
wangqic8cf79e2017-10-17 09:21:00 -07001104 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001105 }
1106 }
1107 }
1108}