blob: 4da858fff8ae285aa686c8f9f005c4f9f4de7cf7 [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;
roldenburgc14610d2017-10-31 12:51:06 -070056import android.telecom.VideoProfile;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import android.text.BidiFormatter;
58import android.text.Spannable;
59import android.text.SpannableString;
60import android.text.TextDirectionHeuristics;
61import android.text.TextUtils;
62import android.text.style.ForegroundColorSpan;
63import com.android.contacts.common.ContactsUtils;
64import com.android.contacts.common.ContactsUtils.UserType;
65import com.android.contacts.common.preference.ContactsPreferences;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import com.android.contacts.common.util.ContactDisplayUtils;
Eric Erfanian2ca43182017-08-31 06:57:16 -070067import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080068import com.android.dialer.common.LogUtil;
Eric Erfanian2ca43182017-08-31 06:57:16 -070069import com.android.dialer.configprovider.ConfigProviderBindings;
70import com.android.dialer.contactphoto.BitmapUtil;
Eric Erfaniand8046e52017-04-06 09:41:50 -070071import com.android.dialer.enrichedcall.EnrichedCallManager;
72import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070073import com.android.dialer.lettertile.LetterTileDrawable;
74import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070075import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070076import com.android.dialer.notification.NotificationChannelId;
Eric Erfanian90508232017-03-24 09:31:16 -070077import com.android.dialer.oem.MotorolaUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080078import com.android.dialer.util.DrawableConverter;
79import com.android.incallui.ContactInfoCache.ContactCacheEntry;
80import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
81import com.android.incallui.InCallPresenter.InCallState;
82import com.android.incallui.async.PausableExecutorImpl;
yuegb26c1ae2017-09-18 16:59:16 -070083import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080084import com.android.incallui.call.CallList;
85import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080086import com.android.incallui.call.DialerCallListener;
yueg01a964d2017-10-03 15:25:41 -070087import com.android.incallui.call.TelecomAdapter;
Eric Erfanianccca3152017-02-22 16:32:36 -080088import com.android.incallui.ringtone.DialerRingtoneManager;
89import com.android.incallui.ringtone.InCallTonePlayer;
90import com.android.incallui.ringtone.ToneGeneratorFactory;
Eric Erfanian90508232017-03-24 09:31:16 -070091import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanianccca3152017-02-22 16:32:36 -080092import java.util.Objects;
93
94/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -070095public class StatusBarNotifier
yuegb26c1ae2017-09-18 16:59:16 -070096 implements InCallPresenter.InCallStateListener,
97 EnrichedCallManager.StateChangedListener,
wangqic8cf79e2017-10-17 09:21:00 -070098 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;
roldenburgc14610d2017-10-31 12:51:06 -0700121 private int mVideoState = VideoProfile.STATE_AUDIO_ONLY;
Eric Erfanianccca3152017-02-22 16:32:36 -0800122 private int mSavedIcon = 0;
123 private String mSavedContent = null;
124 private Bitmap mSavedLargeIcon;
125 private String mSavedContentTitle;
yuegb26c1ae2017-09-18 16:59:16 -0700126 private CallAudioState savedCallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800127 private Uri mRingtone;
128 private StatusBarCallListener mStatusBarCallListener;
129
Eric Erfaniand8046e52017-04-06 09:41:50 -0700130 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
wangqic8cf79e2017-10-17 09:21:00 -0700131 Trace.beginSection("StatusBarNotifier.Constructor");
Eric Erfanian2ca43182017-08-31 06:57:16 -0700132 mContext = Assert.isNotNull(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800133 mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
134 mContactInfoCache = contactInfoCache;
Eric Erfanianccca3152017-02-22 16:32:36 -0800135 mDialerRingtoneManager =
136 new DialerRingtoneManager(
137 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
138 CallList.getInstance());
139 mCurrentNotification = NOTIFICATION_NONE;
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,
roldenburgc14610d2017-10-31 12:51:06 -0700317 call.getVideoState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800318 notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700319 contactInfo.contactRingtoneUri,
320 callAudioState)) {
wangqicf61ca02017-08-31 15:32:55 -0700321 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800322 return;
323 }
324
325 if (largeIcon != null) {
326 largeIcon = getRoundedIcon(largeIcon);
327 }
328
329 // This builder is used for the notification shown when the device is locked and the user
330 // has set their notification settings to 'hide sensitive content'
331 // {@see Notification.Builder#setPublicVersion}.
332 Notification.Builder publicBuilder = new Notification.Builder(mContext);
333 publicBuilder
334 .setSmallIcon(iconResId)
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700335 .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme()))
Eric Erfanianccca3152017-02-22 16:32:36 -0800336 // Hide work call state for the lock screen notification
337 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
338 setNotificationWhen(call, callState, publicBuilder);
339
340 // Builder for the notification shown when the device is unlocked or the user has set their
341 // notification settings to 'show all notification content'.
342 final Notification.Builder builder = getNotificationBuilder();
343 builder.setPublicVersion(publicBuilder.build());
344
345 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700346 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800347
Eric Erfanian10b34a52017-05-04 08:23:17 -0700348 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
349 switch (notificationType) {
350 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700351 if (BuildCompat.isAtLeastO()) {
352 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
353 }
wangqic8cf79e2017-10-17 09:21:00 -0700354 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700355 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
356 // Set the notification category and bump the priority for incoming calls
357 builder.setCategory(Notification.CATEGORY_CALL);
358 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700359 builder.setPriority(Notification.PRIORITY_MAX);
360 if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) {
361 LogUtil.i(
362 "StatusBarNotifier.buildAndSendNotification",
363 "Canceling old notification so this one can be noisy");
364 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
365 // notification (if there is one) so the fullScreenIntent or HUN will show
yueg01a964d2017-10-03 15:25:41 -0700366 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700367 }
368 break;
369 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700370 if (BuildCompat.isAtLeastO()) {
371 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
372 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700373 break;
374 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700375 if (BuildCompat.isAtLeastO()) {
376 publicBuilder.setColorized(true);
377 builder.setColorized(true);
378 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
379 }
380 break;
381 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700382 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800383 }
384
385 // Set the content
386 builder.setContentText(content);
387 builder.setSmallIcon(iconResId);
388 builder.setContentTitle(contentTitle);
389 builder.setLargeIcon(largeIcon);
yueg01a964d2017-10-03 15:25:41 -0700390 builder.setColor(InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800391
392 if (isVideoUpgradeRequest) {
393 builder.setUsesChronometer(false);
394 addDismissUpgradeRequestAction(builder);
395 addAcceptUpgradeRequestAction(builder);
396 } else {
yuegb26c1ae2017-09-18 16:59:16 -0700397 createIncomingCallNotification(call, callState, callAudioState, builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800398 }
399
400 addPersonReference(builder, contactInfo, call);
401
wangqicf61ca02017-08-31 15:32:55 -0700402 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800403 // Fire off the notification
404 Notification notification = builder.build();
405
406 if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
407 notification.flags |= Notification.FLAG_INSISTENT;
408 notification.sound = contactInfo.contactRingtoneUri;
409 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
410 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
411 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
412 notification.audioAttributes = audioAttributes.build();
413 if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) {
414 notification.vibrate = VIBRATE_PATTERN;
415 }
416 }
417 if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700418 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
Eric Erfanianccca3152017-02-22 16:32:36 -0800419 mDialerRingtoneManager.playCallWaitingTone();
420 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800421
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700422 LogUtil.i(
423 "StatusBarNotifier.buildAndSendNotification",
424 "displaying notification for " + notificationType);
425
yueg01a964d2017-10-03 15:25:41 -0700426 // If a notification exists, this will only update it.
427 TelecomAdapter.getInstance().startForegroundNotification(NOTIFICATION_ID, notification);
428
wangqicf61ca02017-08-31 15:32:55 -0700429 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800430 call.getLatencyReport().onNotificationShown();
431 mCurrentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700432 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800433 }
434
435 private void createIncomingCallNotification(
yuegb26c1ae2017-09-18 16:59:16 -0700436 DialerCall call, int state, CallAudioState callAudioState, Notification.Builder builder) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800437 setNotificationWhen(call, state, builder);
438
439 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
440 if (state == DialerCall.State.ACTIVE
441 || state == DialerCall.State.ONHOLD
442 || DialerCall.State.isDialing(state)) {
443 addHangupAction(builder);
yuegb26c1ae2017-09-18 16:59:16 -0700444 addSpeakerAction(builder, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800445 } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) {
446 addDismissAction(builder);
447 if (call.isVideoCall()) {
448 addVideoCallAction(builder);
449 } else {
450 addAnswerAction(builder);
451 }
452 }
453 }
454
455 /**
456 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
457 * duration of the call. For all other states, the notification will automatically show the time
458 * at which the notification was created.
459 */
460 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
461 if (state == DialerCall.State.ACTIVE) {
462 builder.setUsesChronometer(true);
463 builder.setWhen(call.getConnectTimeMillis());
464 } else {
465 builder.setUsesChronometer(false);
466 }
467 }
468
469 /**
470 * Checks the new notification data and compares it against any notification that we are already
471 * displaying. If the data is exactly the same, we return false so that we do not issue a new
472 * notification for the exact same data.
473 */
474 private boolean checkForChangeAndSaveData(
475 int icon,
476 String content,
477 Bitmap largeIcon,
478 String contentTitle,
479 int state,
roldenburgc14610d2017-10-31 12:51:06 -0700480 int videoState,
Eric Erfanianccca3152017-02-22 16:32:36 -0800481 int notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700482 Uri ringtone,
483 CallAudioState callAudioState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800484
485 // The two are different:
486 // if new title is not null, it should be different from saved version OR
487 // if new title is null, the saved version should not be null
488 final boolean contentTitleChanged =
489 (contentTitle != null && !contentTitle.equals(mSavedContentTitle))
490 || (contentTitle == null && mSavedContentTitle != null);
491
roldenburgc14610d2017-10-31 12:51:06 -0700492 boolean largeIconChanged;
493 if (mSavedLargeIcon == null) {
494 largeIconChanged = largeIcon != null;
495 } else {
496 largeIconChanged = largeIcon == null || !mSavedLargeIcon.sameAs(largeIcon);
497 }
Eric Erfanian8369df02017-05-03 10:27:13 -0700498
Eric Erfanianccca3152017-02-22 16:32:36 -0800499 // any change means we are definitely updating
500 boolean retval =
501 (mSavedIcon != icon)
502 || !Objects.equals(mSavedContent, content)
503 || (mCallState != state)
roldenburgc14610d2017-10-31 12:51:06 -0700504 || (mVideoState != videoState)
Eric Erfanian8369df02017-05-03 10:27:13 -0700505 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800506 || contentTitleChanged
yuegb26c1ae2017-09-18 16:59:16 -0700507 || !Objects.equals(mRingtone, ringtone)
508 || !Objects.equals(savedCallAudioState, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800509
wangqi9982f0d2017-10-11 17:46:07 -0700510 LogUtil.d(
511 "StatusBarNotifier.checkForChangeAndSaveData",
roldenburgc14610d2017-10-31 12:51:06 -0700512 "data changed: icon: %b, content: %b, state: %b, videoState: %b, largeIcon: %b, title: %b,"
513 + "ringtone: %b, audioState: %b, type: %b",
wangqi9982f0d2017-10-11 17:46:07 -0700514 (mSavedIcon != icon),
515 !Objects.equals(mSavedContent, content),
516 (mCallState != state),
roldenburgc14610d2017-10-31 12:51:06 -0700517 (mVideoState != videoState),
wangqi9982f0d2017-10-11 17:46:07 -0700518 largeIconChanged,
519 contentTitleChanged,
520 !Objects.equals(mRingtone, ringtone),
521 !Objects.equals(savedCallAudioState, callAudioState),
522 mCurrentNotification != notificationType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800523 // If we aren't showing a notification right now or the notification type is changing,
524 // definitely do an update.
525 if (mCurrentNotification != notificationType) {
526 if (mCurrentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700527 LogUtil.d(
528 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800529 }
530 retval = true;
531 }
532
533 mSavedIcon = icon;
534 mSavedContent = content;
535 mCallState = state;
roldenburgc14610d2017-10-31 12:51:06 -0700536 mVideoState = videoState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800537 mSavedLargeIcon = largeIcon;
538 mSavedContentTitle = contentTitle;
539 mRingtone = ringtone;
yuegb26c1ae2017-09-18 16:59:16 -0700540 savedCallAudioState = callAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800541
542 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700543 LogUtil.d(
544 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800545 }
546
547 return retval;
548 }
549
550 /** Returns the main string to use in the notification. */
551 @VisibleForTesting
552 @Nullable
553 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700554 if (call.isConferenceCall()) {
555 return CallerInfoUtils.getConferenceString(
556 mContext, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800557 }
558
559 String preferredName =
560 ContactDisplayUtils.getPreferredDisplayName(
561 contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences);
562 if (TextUtils.isEmpty(preferredName)) {
563 return TextUtils.isEmpty(contactInfo.number)
564 ? null
565 : BidiFormatter.getInstance()
566 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
567 }
568 return preferredName;
569 }
570
571 private void addPersonReference(
572 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
573 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
574 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
575 // NotificationManager using it.
576 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
577 builder.addPerson(contactInfo.lookupUri.toString());
578 } else if (!TextUtils.isEmpty(call.getNumber())) {
579 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
580 }
581 }
582
583 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700584 private static Bitmap getLargeIconToDisplay(
585 Context context, ContactCacheEntry contactInfo, DialerCall call) {
wangqic8cf79e2017-10-17 09:21:00 -0700586 Trace.beginSection("StatusBarNotifier.getLargeIconToDisplay");
Eric Erfanian83b20212017-05-31 08:53:10 -0700587 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800588 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800589 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
590 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
591 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700592 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700593 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
594 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700595 @ContactType
596 int contactType =
597 LetterTileDrawable.getContactTypeFromPrimitives(
wangqi9982f0d2017-10-11 17:46:07 -0700598 call.isVoiceMailNumber(),
Eric Erfanian2ca43182017-08-31 06:57:16 -0700599 call.isSpam(),
600 contactInfo.isBusiness,
601 call.getNumberPresentation(),
602 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700603 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700604
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700605 lettertile.setCanonicalDialerLetterTileDetails(
606 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
607 contactInfo.lookupKey,
608 LetterTileDrawable.SHAPE_CIRCLE,
609 contactType);
610 largeIcon = lettertile.getBitmap(width, height);
611 }
612
Eric Erfanianccca3152017-02-22 16:32:36 -0800613 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700614 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800615 largeIcon = DrawableConverter.drawableToBitmap(drawable);
616 }
wangqic8cf79e2017-10-17 09:21:00 -0700617 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800618 return largeIcon;
619 }
620
621 private Bitmap getRoundedIcon(Bitmap bitmap) {
622 if (bitmap == null) {
623 return null;
624 }
625 final int height =
626 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height);
627 final int width =
628 (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width);
629 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
630 }
631
632 /**
633 * Returns the appropriate icon res Id to display based on the call for which we want to display
634 * information.
635 */
Eric Erfanian2827dd12017-10-26 09:37:50 -0700636 @VisibleForTesting
637 public int getIconToDisplay(DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800638 // Even if both lines are in use, we only show a single item in
639 // the expanded Notifications UI. It's labeled "Ongoing call"
640 // (or "On hold" if there's only one call, and it's on hold.)
641 // Also, we don't have room to display caller-id info from two
642 // different calls. So if both lines are in use, display info
643 // from the foreground call. And if there's a ringing call,
644 // display that regardless of the state of the other calls.
645 if (call.getState() == DialerCall.State.ONHOLD) {
Eric Erfanian2827dd12017-10-26 09:37:50 -0700646 return R.drawable.quantum_ic_phone_paused_vd_theme_24;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700647 } else if (call.getVideoTech().getSessionModificationState()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700648 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST
649 || call.isVideoCall()) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700650 return R.drawable.quantum_ic_videocam_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700651 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
652 && MotorolaUtils.shouldShowHdIconInNotification(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700653 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
654 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
655 // 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 -0700656 return R.drawable.ic_hd_call;
Eric Erfanian2827dd12017-10-26 09:37:50 -0700657 } else if (call.hasProperty(Details.PROPERTY_HAS_CDMA_VOICE_PRIVACY)) {
658 return R.drawable.quantum_ic_phone_locked_vd_theme_24;
Eric Erfanianccca3152017-02-22 16:32:36 -0800659 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700660 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
Eric Erfanian938468d2017-10-24 14:05:52 -0700661 if (ReturnToCallController.isEnabled(mContext)
662 || NewReturnToCallController.isEnabled(mContext)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700663 return R.drawable.quantum_ic_call_vd_theme_24;
664 } else {
665 return R.drawable.on_going_call;
666 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800667 }
668
669 /** Returns the message to use with the notification. */
twyend1d1d0c2017-10-05 17:34:43 -0700670 private CharSequence getContentString(DialerCall call, @UserType long userType) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800671 boolean isIncomingOrWaiting =
672 call.getState() == DialerCall.State.INCOMING
673 || call.getState() == DialerCall.State.CALL_WAITING;
674
675 if (isIncomingOrWaiting
676 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
677
678 if (!TextUtils.isEmpty(call.getChildNumber())) {
679 return mContext.getString(R.string.child_number, call.getChildNumber());
680 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
681 return call.getCallSubject();
682 }
683 }
684
685 int resId = R.string.notification_ongoing_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700686 String wifiBrand = mContext.getString(R.string.notification_call_wifi_brand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800687 if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700688 resId = R.string.notification_ongoing_call_wifi_template;
Eric Erfanianccca3152017-02-22 16:32:36 -0800689 }
690
691 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700692 if (call.isSpam()) {
693 resId = R.string.notification_incoming_spam_call;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700694 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
695 resId = getECIncomingCallText(call.getEnrichedCallSession());
Eric Erfaniand8046e52017-04-06 09:41:50 -0700696 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700697 resId = R.string.notification_incoming_call_wifi_template;
wangqi9982f0d2017-10-11 17:46:07 -0700698 } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts(call)) {
twyend1d1d0c2017-10-05 17:34:43 -0700699 return getMultiSimIncomingText(call);
yueg45e45732017-10-09 14:35:06 -0700700 } else if (call.isVideoCall()) {
701 resId = R.string.notification_incoming_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800702 } else {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700703 resId = R.string.notification_incoming_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800704 }
705 } else if (call.getState() == DialerCall.State.ONHOLD) {
706 resId = R.string.notification_on_hold;
calderwoodra1dc2cea2017-09-20 16:30:41 -0700707 } else if (call.isVideoCall()) {
708 resId =
709 call.getVideoTech().isPaused()
710 ? R.string.notification_ongoing_paused_video_call
711 : R.string.notification_ongoing_video_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800712 } else if (DialerCall.State.isDialing(call.getState())) {
713 resId = R.string.notification_dialing;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700714 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700715 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800716 resId = R.string.notification_requesting_video_call;
717 }
718
719 // Is the call placed through work connection service.
720 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL);
721 if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) {
722 resId = getWorkStringFromPersonalString(resId);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700723 wifiBrand = mContext.getString(R.string.notification_call_wifi_work_brand);
724 }
725
726 if (resId == R.string.notification_incoming_call_wifi_template
727 || resId == R.string.notification_ongoing_call_wifi_template) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700728 // TODO(a bug): Potentially apply this template logic everywhere.
Eric Erfanian2ca43182017-08-31 06:57:16 -0700729 return mContext.getString(resId, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800730 }
731
732 return mContext.getString(resId);
733 }
734
Eric Erfanian2ca43182017-08-31 06:57:16 -0700735 private boolean shouldShowEnrichedCallNotification(Session session) {
736 if (session == null) {
737 return false;
738 }
739 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
740 }
741
Eric Erfaniand8046e52017-04-06 09:41:50 -0700742 private int getECIncomingCallText(Session session) {
743 int resId;
744 MultimediaData data = session.getMultimediaData();
745 boolean hasImage = data.hasImageData();
746 boolean hasSubject = !TextUtils.isEmpty(data.getText());
747 boolean hasMap = data.getLocation() != null;
748 if (data.isImportant()) {
749 if (hasMap) {
750 if (hasImage) {
751 if (hasSubject) {
752 resId = R.string.important_notification_incoming_call_with_photo_message_location;
753 } else {
754 resId = R.string.important_notification_incoming_call_with_photo_location;
755 }
756 } else if (hasSubject) {
757 resId = R.string.important_notification_incoming_call_with_message_location;
758 } else {
759 resId = R.string.important_notification_incoming_call_with_location;
760 }
761 } else if (hasImage) {
762 if (hasSubject) {
763 resId = R.string.important_notification_incoming_call_with_photo_message;
764 } else {
765 resId = R.string.important_notification_incoming_call_with_photo;
766 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700767 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700768 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700769 } else {
770 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700771 }
772 if (mContext.getString(resId).length() > 50) {
773 resId = R.string.important_notification_incoming_call_attachments;
774 }
775 } else {
776 if (hasMap) {
777 if (hasImage) {
778 if (hasSubject) {
779 resId = R.string.notification_incoming_call_with_photo_message_location;
780 } else {
781 resId = R.string.notification_incoming_call_with_photo_location;
782 }
783 } else if (hasSubject) {
784 resId = R.string.notification_incoming_call_with_message_location;
785 } else {
786 resId = R.string.notification_incoming_call_with_location;
787 }
788 } else if (hasImage) {
789 if (hasSubject) {
790 resId = R.string.notification_incoming_call_with_photo_message;
791 } else {
792 resId = R.string.notification_incoming_call_with_photo;
793 }
794 } else {
795 resId = R.string.notification_incoming_call_with_message;
796 }
797 }
798 if (mContext.getString(resId).length() > 50) {
799 resId = R.string.notification_incoming_call_attachments;
800 }
801 return resId;
802 }
803
twyend1d1d0c2017-10-05 17:34:43 -0700804 private CharSequence getMultiSimIncomingText(DialerCall call) {
805 PhoneAccount phoneAccount =
806 mContext.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
807 SpannableString string =
808 new SpannableString(
809 mContext.getString(
810 R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
811 int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
812 int accountEnd = accountStart + phoneAccount.getLabel().length();
813
814 string.setSpan(
815 new ForegroundColorSpan(phoneAccount.getHighlightColor()),
816 accountStart,
817 accountEnd,
818 Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
819 return string;
820 }
821
Eric Erfanianccca3152017-02-22 16:32:36 -0800822 /** Gets the most relevant call to display in the notification. */
823 private DialerCall getCallToShow(CallList callList) {
824 if (callList == null) {
825 return null;
826 }
827 DialerCall call = callList.getIncomingCall();
828 if (call == null) {
829 call = callList.getOutgoingCall();
830 }
831 if (call == null) {
832 call = callList.getVideoUpgradeRequestCall();
833 }
834 if (call == null) {
835 call = callList.getActiveOrBackgroundCall();
836 }
837 return call;
838 }
839
840 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
841 Spannable spannable = new SpannableString(mContext.getText(stringRes));
842 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
843 // This will only work for cases where the Notification.Builder has a fullscreen intent set
844 // Notification.Builder that does not have a full screen intent will take the color of the
845 // app and the following leads to a no-op.
846 spannable.setSpan(
847 new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0);
848 }
849 return spannable;
850 }
851
852 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700853 LogUtil.d(
854 "StatusBarNotifier.addAnswerAction",
855 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800856 PendingIntent answerVoicePendingIntent =
857 createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL);
858 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700859 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700860 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700861 getActionText(
862 R.string.notification_action_answer, R.color.notification_action_accept),
863 answerVoicePendingIntent)
864 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800865 }
866
867 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700868 LogUtil.d(
869 "StatusBarNotifier.addDismissAction",
870 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800871 PendingIntent declinePendingIntent =
872 createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL);
873 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700874 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700875 Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700876 getActionText(
877 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
878 declinePendingIntent)
879 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800880 }
881
882 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700883 LogUtil.d(
884 "StatusBarNotifier.addHangupAction",
885 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800886 PendingIntent hangupPendingIntent =
887 createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL);
888 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700889 new Notification.Action.Builder(
Eric Erfanian2ca43182017-08-31 06:57:16 -0700890 Icon.createWithResource(mContext, R.drawable.quantum_ic_call_end_white_24),
Eric Erfanian10b34a52017-05-04 08:23:17 -0700891 mContext.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700892 hangupPendingIntent)
893 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800894 }
895
yuegb26c1ae2017-09-18 16:59:16 -0700896 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
897 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
898 == CallAudioState.ROUTE_BLUETOOTH) {
899 // Don't add speaker button if bluetooth is connected
900 return;
901 }
902 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
903 addSpeakerOffAction(builder);
904 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
905 addSpeakerOnAction(builder);
906 }
907 }
908
909 private void addSpeakerOnAction(Notification.Builder builder) {
910 LogUtil.d(
911 "StatusBarNotifier.addSpeakerOnAction",
912 "will show \"Speaker on\" action in the ongoing active call Notification");
913 PendingIntent speakerOnPendingIntent =
914 createNotificationPendingIntent(mContext, ACTION_TURN_ON_SPEAKER);
915 builder.addAction(
916 new Notification.Action.Builder(
917 Icon.createWithResource(mContext, R.drawable.quantum_ic_volume_up_white_24),
918 mContext.getText(R.string.notification_action_speaker_on),
919 speakerOnPendingIntent)
920 .build());
921 }
922
923 private void addSpeakerOffAction(Notification.Builder builder) {
924 LogUtil.d(
925 "StatusBarNotifier.addSpeakerOffAction",
926 "will show \"Speaker off\" action in the ongoing active call Notification");
927 PendingIntent speakerOffPendingIntent =
928 createNotificationPendingIntent(mContext, ACTION_TURN_OFF_SPEAKER);
929 builder.addAction(
930 new Notification.Action.Builder(
931 Icon.createWithResource(mContext, R.drawable.quantum_ic_phone_in_talk_white_24),
932 mContext.getText(R.string.notification_action_speaker_off),
933 speakerOffPendingIntent)
934 .build());
935 }
936
Eric Erfanianccca3152017-02-22 16:32:36 -0800937 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700938 LogUtil.i(
939 "StatusBarNotifier.addVideoCallAction",
940 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800941 PendingIntent answerVideoPendingIntent =
942 createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL);
943 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700944 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700945 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700946 getActionText(
947 R.string.notification_action_answer_video,
948 R.color.notification_action_answer_video),
949 answerVideoPendingIntent)
950 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800951 }
952
953 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700954 LogUtil.i(
955 "StatusBarNotifier.addAcceptUpgradeRequestAction",
956 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800957 PendingIntent acceptVideoPendingIntent =
958 createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
959 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700960 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700961 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700962 getActionText(
963 R.string.notification_action_accept, R.color.notification_action_accept),
964 acceptVideoPendingIntent)
965 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800966 }
967
968 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700969 LogUtil.i(
970 "StatusBarNotifier.addDismissUpgradeRequestAction",
971 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800972 PendingIntent declineVideoPendingIntent =
973 createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
974 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700975 new Notification.Action.Builder(
Eric Erfaniand8046e52017-04-06 09:41:50 -0700976 Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700977 getActionText(
978 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
979 declineVideoPendingIntent)
980 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800981 }
982
983 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -0700984 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800985 // Ok, we actually want to launch the incoming call
986 // UI at this point (in addition to simply posting a notification
987 // to the status bar). Setting fullScreenIntent will cause
988 // the InCallScreen to be launched immediately *unless* the
989 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700990 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -0800991 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -0800992 }
993
994 private Notification.Builder getNotificationBuilder() {
995 final Notification.Builder builder = new Notification.Builder(mContext);
996 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700997 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -0700998 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -0700999 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -07001000 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -08001001
1002 return builder;
1003 }
1004
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001005 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001006 Intent intent =
1007 InCallActivity.getIntent(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001008 mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001009
linyuhc3968e62017-11-20 17:40:50 -08001010 int requestCode = InCallActivity.PendingIntentRequestCodes.NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001011 if (isFullScreen) {
1012 // Use a unique request code so that the pending intent isn't clobbered by the
1013 // non-full screen pending intent.
linyuhc3968e62017-11-20 17:40:50 -08001014 requestCode = InCallActivity.PendingIntentRequestCodes.FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001015 }
1016
1017 // PendingIntent that can be used to launch the InCallActivity. The
1018 // system fires off this intent if the user pulls down the windowshade
1019 // and clicks the notification's expanded view. It's also used to
1020 // launch the InCallActivity immediately when when there's an incoming
1021 // call (see the "fullScreenIntent" field below).
1022 return PendingIntent.getActivity(mContext, requestCode, intent, 0);
1023 }
1024
1025 private void setStatusBarCallListener(StatusBarCallListener listener) {
1026 if (mStatusBarCallListener != null) {
1027 mStatusBarCallListener.cleanup();
1028 }
1029 mStatusBarCallListener = listener;
1030 }
1031
wangqi9982f0d2017-10-11 17:46:07 -07001032 private boolean hasMultiplePhoneAccounts(DialerCall call) {
1033 if (call.getCallCapableAccounts() == null) {
1034 return false;
1035 }
1036 return call.getCallCapableAccounts().size() > 1;
twyend1d1d0c2017-10-05 17:34:43 -07001037 }
1038
yuegb26c1ae2017-09-18 16:59:16 -07001039 @Override
wangqic8cf79e2017-10-17 09:21:00 -07001040 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1041 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1042 DialerCall call = CallList.getInstance().getCallById(callId);
1043 if (call != null) {
1044 call.getLogState().contactLookupResult = entry.contactLookupResult;
1045 buildAndSendNotification(CallList.getInstance(), call, entry);
1046 }
1047 }
1048
1049 @Override
1050 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1051 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1052 DialerCall call = CallList.getInstance().getCallById(callId);
1053 if (call != null) {
1054 buildAndSendNotification(CallList.getInstance(), call, entry);
1055 }
yuegb26c1ae2017-09-18 16:59:16 -07001056 }
1057
Eric Erfanianccca3152017-02-22 16:32:36 -08001058 private class StatusBarCallListener implements DialerCallListener {
1059
1060 private DialerCall mDialerCall;
1061
1062 StatusBarCallListener(DialerCall dialerCall) {
1063 mDialerCall = dialerCall;
1064 mDialerCall.addListener(this);
1065 }
1066
1067 void cleanup() {
1068 mDialerCall.removeListener(this);
1069 }
1070
1071 @Override
1072 public void onDialerCallDisconnect() {}
1073
1074 @Override
1075 public void onDialerCallUpdate() {
1076 if (CallList.getInstance().getIncomingCall() == null) {
1077 mDialerRingtoneManager.stopCallWaitingTone();
1078 }
1079 }
1080
1081 @Override
1082 public void onDialerCallChildNumberChange() {}
1083
1084 @Override
1085 public void onDialerCallLastForwardedNumberChange() {}
1086
1087 @Override
1088 public void onDialerCallUpgradeToVideo() {}
1089
1090 @Override
1091 public void onWiFiToLteHandover() {}
1092
1093 @Override
1094 public void onHandoverToWifiFailure() {}
1095
Eric Erfanianc857f902017-05-15 14:05:33 -07001096 @Override
1097 public void onInternationalCallOnWifi() {}
1098
Eric Erfanian2ca43182017-08-31 06:57:16 -07001099 @Override
1100 public void onEnrichedCallSessionUpdate() {}
1101
Eric Erfanianccca3152017-02-22 16:32:36 -08001102 /**
1103 * Responds to changes in the session modification state for the call by dismissing the status
1104 * bar notification as required.
1105 */
1106 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001107 public void onDialerCallSessionModificationStateChange() {
1108 if (mDialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001109 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001110 cleanup();
wangqic8cf79e2017-10-17 09:21:00 -07001111 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001112 }
1113 }
1114 }
1115}