blob: 39428512b72a368cbb8da118a97ec92600517f25 [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;
kholoud mohamed76f50972022-03-14 11:50:58 +000021import static com.android.dialer.app.DevicePolicyResources.NOTIFICATION_INCOMING_WORK_CALL_TITLE;
22import static com.android.dialer.app.DevicePolicyResources.NOTIFICATION_ONGOING_WORK_CALL_TITLE;
23import static com.android.dialer.app.DevicePolicyResources.NOTIFICATION_WIFI_WORK_CALL_LABEL;
Eric Erfanianccca3152017-02-22 16:32:36 -080024import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST;
erfanian1e4ef182018-06-11 11:05:51 -070025import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_SPEAKEASY_CALL;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VIDEO_INCOMING_CALL;
27import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VOICE_INCOMING_CALL;
28import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_INCOMING_CALL;
29import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST;
30import static com.android.incallui.NotificationBroadcastReceiver.ACTION_HANG_UP_ONGOING_CALL;
yuegb26c1ae2017-09-18 16:59:16 -070031import static com.android.incallui.NotificationBroadcastReceiver.ACTION_TURN_OFF_SPEAKER;
32import static com.android.incallui.NotificationBroadcastReceiver.ACTION_TURN_ON_SPEAKER;
Eric Erfanianccca3152017-02-22 16:32:36 -080033
Eric Erfaniand5e47f62017-03-15 14:41:07 -070034import android.Manifest;
Eric Erfanianccca3152017-02-22 16:32:36 -080035import android.app.Notification;
Eric Erfanianccca3152017-02-22 16:32:36 -080036import android.app.PendingIntent;
kholoud mohamed7be24dd2022-02-01 14:55:27 +000037import android.app.admin.DevicePolicyManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080038import android.content.Context;
39import android.content.Intent;
Eric Erfanian83b20212017-05-31 08:53:10 -070040import android.content.res.Resources;
Eric Erfanianccca3152017-02-22 16:32:36 -080041import android.graphics.Bitmap;
Eric Erfanianccca3152017-02-22 16:32:36 -080042import android.graphics.drawable.BitmapDrawable;
43import android.graphics.drawable.Drawable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070044import android.graphics.drawable.Icon;
Eric Erfanianccca3152017-02-22 16:32:36 -080045import android.media.AudioAttributes;
46import android.net.Uri;
47import android.os.Build.VERSION;
48import android.os.Build.VERSION_CODES;
wangqicf61ca02017-08-31 15:32:55 -070049import android.os.Trace;
Eric Erfanianccca3152017-02-22 16:32:36 -080050import android.support.annotation.ColorRes;
51import android.support.annotation.NonNull;
52import android.support.annotation.Nullable;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070053import android.support.annotation.RequiresPermission;
Eric Erfanianccca3152017-02-22 16:32:36 -080054import android.support.annotation.StringRes;
55import android.support.annotation.VisibleForTesting;
Eric Erfaniand5e47f62017-03-15 14:41:07 -070056import android.support.v4.os.BuildCompat;
Eric Erfanianccca3152017-02-22 16:32:36 -080057import android.telecom.Call.Details;
yuegb26c1ae2017-09-18 16:59:16 -070058import android.telecom.CallAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -080059import android.telecom.PhoneAccount;
60import android.telecom.TelecomManager;
roldenburgc14610d2017-10-31 12:51:06 -070061import android.telecom.VideoProfile;
Eric Erfanianccca3152017-02-22 16:32:36 -080062import android.text.BidiFormatter;
63import android.text.Spannable;
64import android.text.SpannableString;
erfanian1e4ef182018-06-11 11:05:51 -070065import android.text.Spanned;
Eric Erfanianccca3152017-02-22 16:32:36 -080066import android.text.TextDirectionHeuristics;
67import android.text.TextUtils;
68import android.text.style.ForegroundColorSpan;
69import com.android.contacts.common.ContactsUtils;
70import com.android.contacts.common.ContactsUtils.UserType;
Eric Erfanian2ca43182017-08-31 06:57:16 -070071import com.android.dialer.common.Assert;
Eric Erfanianccca3152017-02-22 16:32:36 -080072import com.android.dialer.common.LogUtil;
zachh190343a2018-05-31 17:30:46 -070073import com.android.dialer.configprovider.ConfigProviderComponent;
Eric Erfanian2ca43182017-08-31 06:57:16 -070074import com.android.dialer.contactphoto.BitmapUtil;
twyen5578d922018-06-25 12:36:26 -070075import com.android.dialer.contacts.ContactsComponent;
Eric Erfaniand8046e52017-04-06 09:41:50 -070076import com.android.dialer.enrichedcall.EnrichedCallManager;
77import com.android.dialer.enrichedcall.Session;
Eric Erfanian2ca43182017-08-31 06:57:16 -070078import com.android.dialer.lettertile.LetterTileDrawable;
79import com.android.dialer.lettertile.LetterTileDrawable.ContactType;
Eric Erfaniand8046e52017-04-06 09:41:50 -070080import com.android.dialer.multimedia.MultimediaData;
Eric Erfanian2ca43182017-08-31 06:57:16 -070081import com.android.dialer.notification.NotificationChannelId;
Eric Erfanian90508232017-03-24 09:31:16 -070082import com.android.dialer.oem.MotorolaUtils;
calderwoodraa93df432018-05-23 12:59:03 -070083import com.android.dialer.theme.base.ThemeComponent;
Eric Erfanianccca3152017-02-22 16:32:36 -080084import com.android.dialer.util.DrawableConverter;
85import com.android.incallui.ContactInfoCache.ContactCacheEntry;
86import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback;
87import com.android.incallui.InCallPresenter.InCallState;
88import com.android.incallui.async.PausableExecutorImpl;
yuegb26c1ae2017-09-18 16:59:16 -070089import com.android.incallui.audiomode.AudioModeProvider;
Eric Erfanianccca3152017-02-22 16:32:36 -080090import com.android.incallui.call.CallList;
91import com.android.incallui.call.DialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -080092import com.android.incallui.call.DialerCallListener;
yueg01a964d2017-10-03 15:25:41 -070093import com.android.incallui.call.TelecomAdapter;
wangqibb94ca62018-04-27 14:34:04 -070094import com.android.incallui.call.state.DialerCallState;
Eric Erfanianccca3152017-02-22 16:32:36 -080095import com.android.incallui.ringtone.DialerRingtoneManager;
96import com.android.incallui.ringtone.InCallTonePlayer;
97import com.android.incallui.ringtone.ToneGeneratorFactory;
erfanian1e4ef182018-06-11 11:05:51 -070098import com.android.incallui.speakeasy.SpeakEasyComponent;
Eric Erfanian90508232017-03-24 09:31:16 -070099import com.android.incallui.videotech.utils.SessionModificationState;
Eric Erfanian9d10e642018-07-10 18:48:42 +0000100import com.google.common.base.Optional;
Eric Erfanianccca3152017-02-22 16:32:36 -0800101import java.util.Objects;
102
103/** This class adds Notifications to the status bar for the in-call experience. */
Eric Erfaniand8046e52017-04-06 09:41:50 -0700104public class StatusBarNotifier
yuegb26c1ae2017-09-18 16:59:16 -0700105 implements InCallPresenter.InCallStateListener,
106 EnrichedCallManager.StateChangedListener,
wangqic8cf79e2017-10-17 09:21:00 -0700107 ContactInfoCacheCallback {
Eric Erfanianccca3152017-02-22 16:32:36 -0800108
Eric Erfanian2ca43182017-08-31 06:57:16 -0700109 private static final int NOTIFICATION_ID = 1;
110
Eric Erfanianccca3152017-02-22 16:32:36 -0800111 // Notification types
112 // Indicates that no notification is currently showing.
113 private static final int NOTIFICATION_NONE = 0;
114 // Notification for an active call. This is non-interruptive, but cannot be dismissed.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700115 private static final int NOTIFICATION_IN_CALL = 1;
Eric Erfanianccca3152017-02-22 16:32:36 -0800116 // Notification for incoming calls. This is interruptive and will show up as a HUN.
Eric Erfanian10b34a52017-05-04 08:23:17 -0700117 private static final int NOTIFICATION_INCOMING_CALL = 2;
118 // Notification for incoming calls in the case where there is already an active call.
119 // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL
120 private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3;
Eric Erfanianccca3152017-02-22 16:32:36 -0800121
Eric Erfanianccca3152017-02-22 16:32:36 -0800122 private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000};
123
linyuh183cb712017-12-27 17:02:37 -0800124 private final Context context;
125 private final ContactInfoCache contactInfoCache;
126 private final DialerRingtoneManager dialerRingtoneManager;
linyuh183cb712017-12-27 17:02:37 -0800127 private int currentNotification = NOTIFICATION_NONE;
wangqibb94ca62018-04-27 14:34:04 -0700128 private int callState = DialerCallState.INVALID;
linyuh183cb712017-12-27 17:02:37 -0800129 private int videoState = VideoProfile.STATE_AUDIO_ONLY;
130 private int savedIcon = 0;
131 private String savedContent = null;
132 private Bitmap savedLargeIcon;
133 private String savedContentTitle;
yuegb26c1ae2017-09-18 16:59:16 -0700134 private CallAudioState savedCallAudioState;
linyuh183cb712017-12-27 17:02:37 -0800135 private Uri ringtone;
136 private StatusBarCallListener statusBarCallListener;
Eric Erfanianccca3152017-02-22 16:32:36 -0800137
Eric Erfaniand8046e52017-04-06 09:41:50 -0700138 public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
wangqic8cf79e2017-10-17 09:21:00 -0700139 Trace.beginSection("StatusBarNotifier.Constructor");
linyuh183cb712017-12-27 17:02:37 -0800140 this.context = Assert.isNotNull(context);
linyuh183cb712017-12-27 17:02:37 -0800141 this.contactInfoCache = contactInfoCache;
142 dialerRingtoneManager =
Eric Erfanianccca3152017-02-22 16:32:36 -0800143 new DialerRingtoneManager(
144 new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()),
145 CallList.getInstance());
linyuh183cb712017-12-27 17:02:37 -0800146 currentNotification = NOTIFICATION_NONE;
wangqic8cf79e2017-10-17 09:21:00 -0700147 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800148 }
149
150 /**
151 * Should only be called from a irrecoverable state where it is necessary to dismiss all
152 * notifications.
153 */
yueg01a964d2017-10-03 15:25:41 -0700154 static void clearAllCallNotifications() {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700155 LogUtil.e(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700156 "StatusBarNotifier.clearAllCallNotifications",
157 "something terrible happened, clear all InCall notifications");
Eric Erfanianccca3152017-02-22 16:32:36 -0800158
yueg01a964d2017-10-03 15:25:41 -0700159 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800160 }
161
162 private static int getWorkStringFromPersonalString(int resId) {
163 if (resId == R.string.notification_ongoing_call) {
164 return R.string.notification_ongoing_work_call;
Eric Erfanianccca3152017-02-22 16:32:36 -0800165 } else if (resId == R.string.notification_incoming_call) {
166 return R.string.notification_incoming_work_call;
167 } else {
168 return resId;
169 }
170 }
171
172 /**
173 * Returns PendingIntent for answering a phone call. This will typically be used from Notification
174 * context.
175 */
176 private static PendingIntent createNotificationPendingIntent(Context context, String action) {
177 final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class);
178 return PendingIntent.getBroadcast(context, 0, intent, 0);
179 }
180
181 /** Creates notifications according to the state we receive from {@link InCallPresenter}. */
182 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700183 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800184 public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700185 LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState);
wangqic8cf79e2017-10-17 09:21:00 -0700186 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800187 }
188
Eric Erfaniand8046e52017-04-06 09:41:50 -0700189 @Override
190 public void onEnrichedCallStateChanged() {
191 LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged");
wangqic8cf79e2017-10-17 09:21:00 -0700192 updateNotification();
Eric Erfaniand8046e52017-04-06 09:41:50 -0700193 }
194
Eric Erfanianccca3152017-02-22 16:32:36 -0800195 /**
196 * Updates the phone app's status bar notification *and* launches the incoming call UI in response
197 * to a new incoming call.
198 *
199 * <p>If an incoming call is ringing (or call-waiting), the notification will also include a
200 * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current
201 * foreground activity is marked as "immersive".
202 *
203 * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new
204 * ringing connection" event from the telephony layer.)
205 *
206 * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or,
207 * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case,
208 * we'll simply update or cancel the in-call notification based on the current phone state.
209 *
wangqic8cf79e2017-10-17 09:21:00 -0700210 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800211 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700212 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700213 public void updateNotification() {
214 updateInCallNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -0800215 }
216
217 /**
218 * Take down the in-call notification.
219 *
wangqic8cf79e2017-10-17 09:21:00 -0700220 * @see #updateInCallNotification()
Eric Erfanianccca3152017-02-22 16:32:36 -0800221 */
222 private void cancelNotification() {
linyuh183cb712017-12-27 17:02:37 -0800223 if (statusBarCallListener != null) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800224 setStatusBarCallListener(null);
225 }
linyuh183cb712017-12-27 17:02:37 -0800226 if (currentNotification != NOTIFICATION_NONE) {
yueg01a964d2017-10-03 15:25:41 -0700227 TelecomAdapter.getInstance().stopForegroundNotification();
linyuh183cb712017-12-27 17:02:37 -0800228 currentNotification = NOTIFICATION_NONE;
Eric Erfanianccca3152017-02-22 16:32:36 -0800229 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800230 }
231
232 /**
233 * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's
234 * status bar notification based on the current telephony state, or cancels the notification if
235 * the phone is totally idle.
236 */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700237 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700238 private void updateInCallNotification() {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700239 LogUtil.d("StatusBarNotifier.updateInCallNotification", "");
Eric Erfanianccca3152017-02-22 16:32:36 -0800240
wangqic8cf79e2017-10-17 09:21:00 -0700241 final DialerCall call = getCallToShow(CallList.getInstance());
Eric Erfanianccca3152017-02-22 16:32:36 -0800242
243 if (call != null) {
wangqic8cf79e2017-10-17 09:21:00 -0700244 showNotification(call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800245 } else {
246 cancelNotification();
247 }
248 }
249
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700250 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
wangqic8cf79e2017-10-17 09:21:00 -0700251 private void showNotification(final DialerCall call) {
wangqicf61ca02017-08-31 15:32:55 -0700252 Trace.beginSection("StatusBarNotifier.showNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800253 final boolean isIncoming =
wangqibb94ca62018-04-27 14:34:04 -0700254 (call.getState() == DialerCallState.INCOMING
255 || call.getState() == DialerCallState.CALL_WAITING);
Eric Erfanianccca3152017-02-22 16:32:36 -0800256 setStatusBarCallListener(new StatusBarCallListener(call));
257
258 // we make a call to the contact info cache to query for supplemental data to what the
259 // call provides. This includes the contact name and photo.
260 // This callback will always get called immediately and synchronously with whatever data
261 // it has available, and may make a subsequent call later (same thread) if it had to
262 // call into the contacts provider for more data.
linyuh183cb712017-12-27 17:02:37 -0800263 contactInfoCache.findInfo(call, isIncoming, this);
wangqicf61ca02017-08-31 15:32:55 -0700264 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800265 }
266
267 /** Sets up the main Ui for the notification */
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700268 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
Eric Erfanianccca3152017-02-22 16:32:36 -0800269 private void buildAndSendNotification(
270 CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) {
wangqicf61ca02017-08-31 15:32:55 -0700271 Trace.beginSection("StatusBarNotifier.buildAndSendNotification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800272 // This can get called to update an existing notification after contact information has come
273 // back. However, it can happen much later. Before we continue, we need to make sure that
274 // the call being passed in is still the one we want to show in the notification.
275 final DialerCall call = getCallToShow(callList);
276 if (call == null || !call.getId().equals(originalCall.getId())) {
wangqicf61ca02017-08-31 15:32:55 -0700277 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800278 return;
279 }
280
wangqicf61ca02017-08-31 15:32:55 -0700281 Trace.beginSection("prepare work");
Eric Erfanianccca3152017-02-22 16:32:36 -0800282 final int callState = call.getState();
yuegb26c1ae2017-09-18 16:59:16 -0700283 final CallAudioState callAudioState = AudioModeProvider.getInstance().getAudioState();
Eric Erfanianccca3152017-02-22 16:32:36 -0800284
wangqic8cf79e2017-10-17 09:21:00 -0700285 Trace.beginSection("read icon and strings");
Eric Erfanianccca3152017-02-22 16:32:36 -0800286 // Check if data has changed; if nothing is different, don't issue another notification.
287 final int iconResId = getIconToDisplay(call);
linyuh183cb712017-12-27 17:02:37 -0800288 Bitmap largeIcon = getLargeIconToDisplay(context, contactInfo, call);
twyend1d1d0c2017-10-05 17:34:43 -0700289 final CharSequence content = getContentString(call, contactInfo.userType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800290 final String contentTitle = getContentTitle(contactInfo, call);
wangqic8cf79e2017-10-17 09:21:00 -0700291 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800292
293 final boolean isVideoUpgradeRequest =
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700294 call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700295 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST;
Eric Erfanianccca3152017-02-22 16:32:36 -0800296 final int notificationType;
wangqibb94ca62018-04-27 14:34:04 -0700297 if (callState == DialerCallState.INCOMING
298 || callState == DialerCallState.CALL_WAITING
Eric Erfanianccca3152017-02-22 16:32:36 -0800299 || isVideoUpgradeRequest) {
zachh190343a2018-05-31 17:30:46 -0700300 if (ConfigProviderComponent.get(context)
301 .getConfigProvider()
Eric Erfanian2ca43182017-08-31 06:57:16 -0700302 .getBoolean("quiet_incoming_call_if_ui_showing", true)) {
303 notificationType =
304 InCallPresenter.getInstance().isShowingInCallUi()
305 ? NOTIFICATION_INCOMING_CALL_QUIET
306 : NOTIFICATION_INCOMING_CALL;
307 } else {
308 boolean alreadyActive =
309 callList.getActiveOrBackgroundCall() != null
310 && InCallPresenter.getInstance().isShowingInCallUi();
311 notificationType =
312 alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL;
313 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800314 } else {
315 notificationType = NOTIFICATION_IN_CALL;
316 }
wangqicf61ca02017-08-31 15:32:55 -0700317 Trace.endSection(); // prepare work
Eric Erfanianccca3152017-02-22 16:32:36 -0800318
319 if (!checkForChangeAndSaveData(
320 iconResId,
twyend1d1d0c2017-10-05 17:34:43 -0700321 content.toString(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800322 largeIcon,
323 contentTitle,
324 callState,
roldenburgc14610d2017-10-31 12:51:06 -0700325 call.getVideoState(),
Eric Erfanianccca3152017-02-22 16:32:36 -0800326 notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700327 contactInfo.contactRingtoneUri,
328 callAudioState)) {
wangqicf61ca02017-08-31 15:32:55 -0700329 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800330 return;
331 }
332
333 if (largeIcon != null) {
334 largeIcon = getRoundedIcon(largeIcon);
335 }
336
337 // This builder is used for the notification shown when the device is locked and the user
338 // has set their notification settings to 'hide sensitive content'
339 // {@see Notification.Builder#setPublicVersion}.
linyuh183cb712017-12-27 17:02:37 -0800340 Notification.Builder publicBuilder = new Notification.Builder(context);
Eric Erfanianccca3152017-02-22 16:32:36 -0800341 publicBuilder
342 .setSmallIcon(iconResId)
calderwoodraa93df432018-05-23 12:59:03 -0700343 .setColor(ThemeComponent.get(context).theme().getColorPrimary())
Eric Erfanianccca3152017-02-22 16:32:36 -0800344 // Hide work call state for the lock screen notification
345 .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT));
346 setNotificationWhen(call, callState, publicBuilder);
347
348 // Builder for the notification shown when the device is unlocked or the user has set their
349 // notification settings to 'show all notification content'.
350 final Notification.Builder builder = getNotificationBuilder();
351 builder.setPublicVersion(publicBuilder.build());
352
353 // Set up the main intent to send the user to the in-call screen
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700354 builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */));
Eric Erfanianccca3152017-02-22 16:32:36 -0800355
Eric Erfanian10b34a52017-05-04 08:23:17 -0700356 LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType);
357 switch (notificationType) {
358 case NOTIFICATION_INCOMING_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700359 if (BuildCompat.isAtLeastO()) {
360 builder.setChannelId(NotificationChannelId.INCOMING_CALL);
361 }
wangqic8cf79e2017-10-17 09:21:00 -0700362 // Set the intent as a full screen intent as well if a call is incoming
Eric Erfanian10b34a52017-05-04 08:23:17 -0700363 configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */));
364 // Set the notification category and bump the priority for incoming calls
365 builder.setCategory(Notification.CATEGORY_CALL);
366 // This will be ignored on O+ and handled by the channel
Eric Erfanian10b34a52017-05-04 08:23:17 -0700367 builder.setPriority(Notification.PRIORITY_MAX);
linyuh183cb712017-12-27 17:02:37 -0800368 if (currentNotification != NOTIFICATION_INCOMING_CALL) {
Eric Erfanian10b34a52017-05-04 08:23:17 -0700369 LogUtil.i(
370 "StatusBarNotifier.buildAndSendNotification",
371 "Canceling old notification so this one can be noisy");
372 // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old
373 // notification (if there is one) so the fullScreenIntent or HUN will show
yueg01a964d2017-10-03 15:25:41 -0700374 TelecomAdapter.getInstance().stopForegroundNotification();
Eric Erfanian10b34a52017-05-04 08:23:17 -0700375 }
376 break;
377 case NOTIFICATION_INCOMING_CALL_QUIET:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700378 if (BuildCompat.isAtLeastO()) {
379 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
380 }
Eric Erfanian10b34a52017-05-04 08:23:17 -0700381 break;
382 case NOTIFICATION_IN_CALL:
Eric Erfanian2ca43182017-08-31 06:57:16 -0700383 if (BuildCompat.isAtLeastO()) {
384 publicBuilder.setColorized(true);
385 builder.setColorized(true);
386 builder.setChannelId(NotificationChannelId.ONGOING_CALL);
387 }
388 break;
389 default:
Eric Erfanian10b34a52017-05-04 08:23:17 -0700390 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800391 }
392
393 // Set the content
394 builder.setContentText(content);
395 builder.setSmallIcon(iconResId);
396 builder.setContentTitle(contentTitle);
397 builder.setLargeIcon(largeIcon);
yueg01a964d2017-10-03 15:25:41 -0700398 builder.setColor(InCallPresenter.getInstance().getThemeColorManager().getPrimaryColor());
Eric Erfanianccca3152017-02-22 16:32:36 -0800399
400 if (isVideoUpgradeRequest) {
401 builder.setUsesChronometer(false);
402 addDismissUpgradeRequestAction(builder);
403 addAcceptUpgradeRequestAction(builder);
404 } else {
yuegb26c1ae2017-09-18 16:59:16 -0700405 createIncomingCallNotification(call, callState, callAudioState, builder);
Eric Erfanianccca3152017-02-22 16:32:36 -0800406 }
407
408 addPersonReference(builder, contactInfo, call);
409
wangqicf61ca02017-08-31 15:32:55 -0700410 Trace.beginSection("fire notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800411 // Fire off the notification
412 Notification notification = builder.build();
413
linyuh183cb712017-12-27 17:02:37 -0800414 if (dialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800415 notification.flags |= Notification.FLAG_INSISTENT;
416 notification.sound = contactInfo.contactRingtoneUri;
417 AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder();
418 audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
419 audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE);
420 notification.audioAttributes = audioAttributes.build();
linyuh183cb712017-12-27 17:02:37 -0800421 if (dialerRingtoneManager.shouldVibrate(context.getContentResolver())) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800422 notification.vibrate = VIBRATE_PATTERN;
423 }
424 }
linyuh183cb712017-12-27 17:02:37 -0800425 if (dialerRingtoneManager.shouldPlayCallWaitingTone(callState)) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700426 LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone");
linyuh183cb712017-12-27 17:02:37 -0800427 dialerRingtoneManager.playCallWaitingTone();
Eric Erfanianccca3152017-02-22 16:32:36 -0800428 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800429
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700430 LogUtil.i(
431 "StatusBarNotifier.buildAndSendNotification",
432 "displaying notification for " + notificationType);
433
yueg01a964d2017-10-03 15:25:41 -0700434 // If a notification exists, this will only update it.
435 TelecomAdapter.getInstance().startForegroundNotification(NOTIFICATION_ID, notification);
436
wangqicf61ca02017-08-31 15:32:55 -0700437 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800438 call.getLatencyReport().onNotificationShown();
linyuh183cb712017-12-27 17:02:37 -0800439 currentNotification = notificationType;
wangqicf61ca02017-08-31 15:32:55 -0700440 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800441 }
442
443 private void createIncomingCallNotification(
yuegb26c1ae2017-09-18 16:59:16 -0700444 DialerCall call, int state, CallAudioState callAudioState, Notification.Builder builder) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800445 setNotificationWhen(call, state, builder);
446
447 // Add hang up option for any active calls (active | onhold), outgoing calls (dialing).
wangqibb94ca62018-04-27 14:34:04 -0700448 if (state == DialerCallState.ACTIVE
449 || state == DialerCallState.ONHOLD
450 || DialerCallState.isDialing(state)) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800451 addHangupAction(builder);
yuegb26c1ae2017-09-18 16:59:16 -0700452 addSpeakerAction(builder, callAudioState);
wangqibb94ca62018-04-27 14:34:04 -0700453 } else if (state == DialerCallState.INCOMING || state == DialerCallState.CALL_WAITING) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800454 addDismissAction(builder);
455 if (call.isVideoCall()) {
456 addVideoCallAction(builder);
457 } else {
458 addAnswerAction(builder);
erfanian94a7b3b2018-06-19 14:36:18 -0700459 addSpeakeasyAnswerAction(builder, call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800460 }
461 }
462 }
463
464 /**
465 * Sets the notification's when section as needed. For active calls, this is explicitly set as the
466 * duration of the call. For all other states, the notification will automatically show the time
467 * at which the notification was created.
468 */
469 private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) {
wangqibb94ca62018-04-27 14:34:04 -0700470 if (state == DialerCallState.ACTIVE) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800471 builder.setUsesChronometer(true);
472 builder.setWhen(call.getConnectTimeMillis());
473 } else {
474 builder.setUsesChronometer(false);
475 }
476 }
477
478 /**
479 * Checks the new notification data and compares it against any notification that we are already
480 * displaying. If the data is exactly the same, we return false so that we do not issue a new
481 * notification for the exact same data.
482 */
483 private boolean checkForChangeAndSaveData(
484 int icon,
485 String content,
486 Bitmap largeIcon,
487 String contentTitle,
488 int state,
roldenburgc14610d2017-10-31 12:51:06 -0700489 int videoState,
Eric Erfanianccca3152017-02-22 16:32:36 -0800490 int notificationType,
yuegb26c1ae2017-09-18 16:59:16 -0700491 Uri ringtone,
492 CallAudioState callAudioState) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800493
494 // The two are different:
495 // if new title is not null, it should be different from saved version OR
496 // if new title is null, the saved version should not be null
497 final boolean contentTitleChanged =
linyuh183cb712017-12-27 17:02:37 -0800498 (contentTitle != null && !contentTitle.equals(savedContentTitle))
499 || (contentTitle == null && savedContentTitle != null);
Eric Erfanianccca3152017-02-22 16:32:36 -0800500
roldenburgc14610d2017-10-31 12:51:06 -0700501 boolean largeIconChanged;
linyuh183cb712017-12-27 17:02:37 -0800502 if (savedLargeIcon == null) {
roldenburgc14610d2017-10-31 12:51:06 -0700503 largeIconChanged = largeIcon != null;
504 } else {
linyuh183cb712017-12-27 17:02:37 -0800505 largeIconChanged = largeIcon == null || !savedLargeIcon.sameAs(largeIcon);
roldenburgc14610d2017-10-31 12:51:06 -0700506 }
Eric Erfanian8369df02017-05-03 10:27:13 -0700507
Eric Erfanianccca3152017-02-22 16:32:36 -0800508 // any change means we are definitely updating
509 boolean retval =
linyuh183cb712017-12-27 17:02:37 -0800510 (savedIcon != icon)
511 || !Objects.equals(savedContent, content)
512 || (callState != state)
513 || (this.videoState != videoState)
Eric Erfanian8369df02017-05-03 10:27:13 -0700514 || largeIconChanged
Eric Erfanianccca3152017-02-22 16:32:36 -0800515 || contentTitleChanged
linyuh183cb712017-12-27 17:02:37 -0800516 || !Objects.equals(this.ringtone, ringtone)
yuegb26c1ae2017-09-18 16:59:16 -0700517 || !Objects.equals(savedCallAudioState, callAudioState);
Eric Erfanianccca3152017-02-22 16:32:36 -0800518
wangqi9982f0d2017-10-11 17:46:07 -0700519 LogUtil.d(
520 "StatusBarNotifier.checkForChangeAndSaveData",
roldenburgc14610d2017-10-31 12:51:06 -0700521 "data changed: icon: %b, content: %b, state: %b, videoState: %b, largeIcon: %b, title: %b,"
522 + "ringtone: %b, audioState: %b, type: %b",
linyuh183cb712017-12-27 17:02:37 -0800523 (savedIcon != icon),
524 !Objects.equals(savedContent, content),
525 (callState != state),
526 (this.videoState != videoState),
wangqi9982f0d2017-10-11 17:46:07 -0700527 largeIconChanged,
528 contentTitleChanged,
linyuh183cb712017-12-27 17:02:37 -0800529 !Objects.equals(this.ringtone, ringtone),
wangqi9982f0d2017-10-11 17:46:07 -0700530 !Objects.equals(savedCallAudioState, callAudioState),
linyuh183cb712017-12-27 17:02:37 -0800531 currentNotification != notificationType);
Eric Erfanianccca3152017-02-22 16:32:36 -0800532 // If we aren't showing a notification right now or the notification type is changing,
533 // definitely do an update.
linyuh183cb712017-12-27 17:02:37 -0800534 if (currentNotification != notificationType) {
535 if (currentNotification == NOTIFICATION_NONE) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700536 LogUtil.d(
537 "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time.");
Eric Erfanianccca3152017-02-22 16:32:36 -0800538 }
539 retval = true;
540 }
541
linyuh183cb712017-12-27 17:02:37 -0800542 savedIcon = icon;
543 savedContent = content;
544 callState = state;
545 this.videoState = videoState;
546 savedLargeIcon = largeIcon;
547 savedContentTitle = contentTitle;
548 this.ringtone = ringtone;
yuegb26c1ae2017-09-18 16:59:16 -0700549 savedCallAudioState = callAudioState;
Eric Erfanianccca3152017-02-22 16:32:36 -0800550
551 if (retval) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700552 LogUtil.d(
553 "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800554 }
555
556 return retval;
557 }
558
559 /** Returns the main string to use in the notification. */
560 @VisibleForTesting
561 @Nullable
562 String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700563 if (call.isConferenceCall()) {
564 return CallerInfoUtils.getConferenceString(
linyuh183cb712017-12-27 17:02:37 -0800565 context, call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanianccca3152017-02-22 16:32:36 -0800566 }
567
568 String preferredName =
twyen5578d922018-06-25 12:36:26 -0700569 ContactsComponent.get(context)
570 .contactDisplayPreferences()
571 .getDisplayName(contactInfo.namePrimary, contactInfo.nameAlternative);
Eric Erfanianccca3152017-02-22 16:32:36 -0800572 if (TextUtils.isEmpty(preferredName)) {
573 return TextUtils.isEmpty(contactInfo.number)
574 ? null
575 : BidiFormatter.getInstance()
576 .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
577 }
578 return preferredName;
579 }
580
581 private void addPersonReference(
582 Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) {
583 // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
584 // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
585 // NotificationManager using it.
586 if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
587 builder.addPerson(contactInfo.lookupUri.toString());
588 } else if (!TextUtils.isEmpty(call.getNumber())) {
589 builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString());
590 }
591 }
592
593 /** Gets a large icon from the contact info object to display in the notification. */
Eric Erfanian83b20212017-05-31 08:53:10 -0700594 private static Bitmap getLargeIconToDisplay(
595 Context context, ContactCacheEntry contactInfo, DialerCall call) {
wangqic8cf79e2017-10-17 09:21:00 -0700596 Trace.beginSection("StatusBarNotifier.getLargeIconToDisplay");
Eric Erfanian83b20212017-05-31 08:53:10 -0700597 Resources resources = context.getResources();
Eric Erfanianccca3152017-02-22 16:32:36 -0800598 Bitmap largeIcon = null;
Eric Erfanianccca3152017-02-22 16:32:36 -0800599 if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
600 largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
601 }
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700602 if (contactInfo.photo == null) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700603 int width = (int) resources.getDimension(android.R.dimen.notification_large_icon_width);
604 int height = (int) resources.getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700605 @ContactType
606 int contactType =
607 LetterTileDrawable.getContactTypeFromPrimitives(
wangqi9982f0d2017-10-11 17:46:07 -0700608 call.isVoiceMailNumber(),
Eric Erfanian2ca43182017-08-31 06:57:16 -0700609 call.isSpam(),
610 contactInfo.isBusiness,
611 call.getNumberPresentation(),
612 call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE));
Eric Erfanian83b20212017-05-31 08:53:10 -0700613 LetterTileDrawable lettertile = new LetterTileDrawable(resources);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700614
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700615 lettertile.setCanonicalDialerLetterTileDetails(
616 contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary,
617 contactInfo.lookupKey,
618 LetterTileDrawable.SHAPE_CIRCLE,
619 contactType);
620 largeIcon = lettertile.getBitmap(width, height);
621 }
622
Eric Erfanianccca3152017-02-22 16:32:36 -0800623 if (call.isSpam()) {
Eric Erfanian83b20212017-05-31 08:53:10 -0700624 Drawable drawable = resources.getDrawable(R.drawable.blocked_contact, context.getTheme());
Eric Erfanianccca3152017-02-22 16:32:36 -0800625 largeIcon = DrawableConverter.drawableToBitmap(drawable);
626 }
wangqic8cf79e2017-10-17 09:21:00 -0700627 Trace.endSection();
Eric Erfanianccca3152017-02-22 16:32:36 -0800628 return largeIcon;
629 }
630
631 private Bitmap getRoundedIcon(Bitmap bitmap) {
632 if (bitmap == null) {
633 return null;
634 }
635 final int height =
linyuh183cb712017-12-27 17:02:37 -0800636 (int) context.getResources().getDimension(android.R.dimen.notification_large_icon_height);
Eric Erfanianccca3152017-02-22 16:32:36 -0800637 final int width =
linyuh183cb712017-12-27 17:02:37 -0800638 (int) context.getResources().getDimension(android.R.dimen.notification_large_icon_width);
Eric Erfanianccca3152017-02-22 16:32:36 -0800639 return BitmapUtil.getRoundedBitmap(bitmap, width, height);
640 }
641
642 /**
643 * Returns the appropriate icon res Id to display based on the call for which we want to display
644 * information.
645 */
Eric Erfanian2827dd12017-10-26 09:37:50 -0700646 @VisibleForTesting
647 public int getIconToDisplay(DialerCall call) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800648 // Even if both lines are in use, we only show a single item in
649 // the expanded Notifications UI. It's labeled "Ongoing call"
650 // (or "On hold" if there's only one call, and it's on hold.)
651 // Also, we don't have room to display caller-id info from two
652 // different calls. So if both lines are in use, display info
653 // from the foreground call. And if there's a ringing call,
654 // display that regardless of the state of the other calls.
wangqibb94ca62018-04-27 14:34:04 -0700655 if (call.getState() == DialerCallState.ONHOLD) {
Eric Erfanian2827dd12017-10-26 09:37:50 -0700656 return R.drawable.quantum_ic_phone_paused_vd_theme_24;
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700657 } else if (call.getVideoTech().getSessionModificationState()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700658 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST
659 || call.isVideoCall()) {
linyuh8dc242d2018-03-28 13:09:47 -0700660 return R.drawable.quantum_ic_videocam_vd_white_24;
Eric Erfanian90508232017-03-24 09:31:16 -0700661 } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO)
linyuh183cb712017-12-27 17:02:37 -0800662 && MotorolaUtils.shouldShowHdIconInNotification(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700663 // Normally when a call is ongoing the status bar displays an icon of a phone. This is a
664 // helpful hint for users so they know how to get back to the call. For Sprint HD calls, we
665 // 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 -0700666 return R.drawable.ic_hd_call;
Eric Erfanian2827dd12017-10-26 09:37:50 -0700667 } else if (call.hasProperty(Details.PROPERTY_HAS_CDMA_VOICE_PRIVACY)) {
668 return R.drawable.quantum_ic_phone_locked_vd_theme_24;
Eric Erfanianccca3152017-02-22 16:32:36 -0800669 }
yueg48f93f42018-03-09 16:49:38 -0800670 // If ReturnToCall is enabled, use the static icon. The animated one will show in the bubble.
671 if (ReturnToCallController.isEnabled(context)) {
Eric Erfanian2ca43182017-08-31 06:57:16 -0700672 return R.drawable.quantum_ic_call_vd_theme_24;
673 } else {
674 return R.drawable.on_going_call;
675 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800676 }
677
678 /** Returns the message to use with the notification. */
twyend1d1d0c2017-10-05 17:34:43 -0700679 private CharSequence getContentString(DialerCall call, @UserType long userType) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800680 boolean isIncomingOrWaiting =
wangqibb94ca62018-04-27 14:34:04 -0700681 call.getState() == DialerCallState.INCOMING
682 || call.getState() == DialerCallState.CALL_WAITING;
Eric Erfanianccca3152017-02-22 16:32:36 -0800683
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000684 // Is the call placed through work connection service.
685 boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL)
686 || userType == ContactsUtils.USER_TYPE_WORK;
687
Eric Erfanianccca3152017-02-22 16:32:36 -0800688 if (isIncomingOrWaiting
689 && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) {
690
691 if (!TextUtils.isEmpty(call.getChildNumber())) {
linyuh183cb712017-12-27 17:02:37 -0800692 return context.getString(R.string.child_number, call.getChildNumber());
Eric Erfanianccca3152017-02-22 16:32:36 -0800693 } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) {
694 return call.getCallSubject();
695 }
696 }
697
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000698
699 String message = getOngoingCallNotificationMessage(isWorkCall);
700 String wifiBrand = getWifiBrand(isWorkCall);
701
702 // TODO(a bug): Potentially apply this template logic everywhere.
Eric Erfanianccca3152017-02-22 16:32:36 -0800703 if (call.hasProperty(Details.PROPERTY_WIFI)) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000704 message = context.getString(R.string.notification_ongoing_call_wifi_template, wifiBrand);
Eric Erfanianccca3152017-02-22 16:32:36 -0800705 }
706
707 if (isIncomingOrWaiting) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700708 if (call.isSpam()) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000709 message = context.getString(R.string.notification_incoming_spam_call);
Eric Erfanian2ca43182017-08-31 06:57:16 -0700710 } else if (shouldShowEnrichedCallNotification(call.getEnrichedCallSession())) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000711 message = context.getString(getECIncomingCallText(call.getEnrichedCallSession()));
Eric Erfaniand8046e52017-04-06 09:41:50 -0700712 } else if (call.hasProperty(Details.PROPERTY_WIFI)) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000713 message = context.getString(R.string.notification_incoming_call_wifi_template, wifiBrand);
wangqi9982f0d2017-10-11 17:46:07 -0700714 } else if (call.getAccountHandle() != null && hasMultiplePhoneAccounts(call)) {
twyend1d1d0c2017-10-05 17:34:43 -0700715 return getMultiSimIncomingText(call);
yueg45e45732017-10-09 14:35:06 -0700716 } else if (call.isVideoCall()) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000717 message = context.getString(R.string.notification_incoming_video_call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800718 } else {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000719 message = getIncomingCallNotificationMessage(isWorkCall);
Eric Erfanianccca3152017-02-22 16:32:36 -0800720 }
wangqibb94ca62018-04-27 14:34:04 -0700721 } else if (call.getState() == DialerCallState.ONHOLD) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000722 message = context.getString(R.string.notification_on_hold);
Qiong Liu706e4ae2018-06-26 15:56:44 +0800723 } else if (DialerCallState.isDialing(call.getState())) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000724 message = context.getString(R.string.notification_dialing);
calderwoodra1dc2cea2017-09-20 16:30:41 -0700725 } else if (call.isVideoCall()) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000726 message = context.getString(call.getVideoTech().isPaused()
calderwoodra1dc2cea2017-09-20 16:30:41 -0700727 ? R.string.notification_ongoing_paused_video_call
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000728 : R.string.notification_ongoing_video_call);
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700729 } else if (call.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -0700730 == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) {
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000731 message = context.getString(R.string.notification_requesting_video_call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800732 }
733
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000734 return message;
735 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700736
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000737 private String getOngoingCallNotificationMessage(boolean isWorkCall) {
738 if (isWorkCall) {
739 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
kholoud mohamed76f50972022-03-14 11:50:58 +0000740 return dpm.getResources().getString(NOTIFICATION_ONGOING_WORK_CALL_TITLE, () ->
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000741 context.getString(R.string.notification_ongoing_work_call));
742 } else {
743 return context.getString(R.string.notification_ongoing_call);
Eric Erfanianccca3152017-02-22 16:32:36 -0800744 }
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000745 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800746
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000747 private String getIncomingCallNotificationMessage(boolean isWorkCall) {
748 if (isWorkCall) {
749 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
kholoud mohamed76f50972022-03-14 11:50:58 +0000750 return dpm.getResources().getString(NOTIFICATION_INCOMING_WORK_CALL_TITLE, () ->
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000751 context.getString(R.string.notification_incoming_work_call));
752 } else {
753 return context.getString(R.string.notification_incoming_call);
754 }
755 }
756
757 private String getWifiBrand(boolean isWorkCall) {
758 if (isWorkCall) {
759 DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
kholoud mohamed76f50972022-03-14 11:50:58 +0000760 return dpm.getResources().getString(NOTIFICATION_WIFI_WORK_CALL_LABEL, () ->
kholoud mohamed7be24dd2022-02-01 14:55:27 +0000761 context.getString(R.string.notification_call_wifi_work_brand));
762 } else {
763 return context.getString(R.string.notification_call_wifi_brand);
764 }
Eric Erfanianccca3152017-02-22 16:32:36 -0800765 }
766
Eric Erfanian2ca43182017-08-31 06:57:16 -0700767 private boolean shouldShowEnrichedCallNotification(Session session) {
768 if (session == null) {
769 return false;
770 }
771 return session.getMultimediaData().hasData() || session.getMultimediaData().isImportant();
772 }
773
Eric Erfaniand8046e52017-04-06 09:41:50 -0700774 private int getECIncomingCallText(Session session) {
775 int resId;
776 MultimediaData data = session.getMultimediaData();
777 boolean hasImage = data.hasImageData();
778 boolean hasSubject = !TextUtils.isEmpty(data.getText());
779 boolean hasMap = data.getLocation() != null;
780 if (data.isImportant()) {
781 if (hasMap) {
782 if (hasImage) {
783 if (hasSubject) {
784 resId = R.string.important_notification_incoming_call_with_photo_message_location;
785 } else {
786 resId = R.string.important_notification_incoming_call_with_photo_location;
787 }
788 } else if (hasSubject) {
789 resId = R.string.important_notification_incoming_call_with_message_location;
790 } else {
791 resId = R.string.important_notification_incoming_call_with_location;
792 }
793 } else if (hasImage) {
794 if (hasSubject) {
795 resId = R.string.important_notification_incoming_call_with_photo_message;
796 } else {
797 resId = R.string.important_notification_incoming_call_with_photo;
798 }
Eric Erfanian2ca43182017-08-31 06:57:16 -0700799 } else if (hasSubject) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700800 resId = R.string.important_notification_incoming_call_with_message;
Eric Erfanian2ca43182017-08-31 06:57:16 -0700801 } else {
802 resId = R.string.important_notification_incoming_call;
Eric Erfaniand8046e52017-04-06 09:41:50 -0700803 }
linyuh183cb712017-12-27 17:02:37 -0800804 if (context.getString(resId).length() > 50) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700805 resId = R.string.important_notification_incoming_call_attachments;
806 }
807 } else {
808 if (hasMap) {
809 if (hasImage) {
810 if (hasSubject) {
811 resId = R.string.notification_incoming_call_with_photo_message_location;
812 } else {
813 resId = R.string.notification_incoming_call_with_photo_location;
814 }
815 } else if (hasSubject) {
816 resId = R.string.notification_incoming_call_with_message_location;
817 } else {
818 resId = R.string.notification_incoming_call_with_location;
819 }
820 } else if (hasImage) {
821 if (hasSubject) {
822 resId = R.string.notification_incoming_call_with_photo_message;
823 } else {
824 resId = R.string.notification_incoming_call_with_photo;
825 }
826 } else {
827 resId = R.string.notification_incoming_call_with_message;
828 }
829 }
linyuh183cb712017-12-27 17:02:37 -0800830 if (context.getString(resId).length() > 50) {
Eric Erfaniand8046e52017-04-06 09:41:50 -0700831 resId = R.string.notification_incoming_call_attachments;
832 }
833 return resId;
834 }
835
twyend1d1d0c2017-10-05 17:34:43 -0700836 private CharSequence getMultiSimIncomingText(DialerCall call) {
837 PhoneAccount phoneAccount =
linyuh183cb712017-12-27 17:02:37 -0800838 context.getSystemService(TelecomManager.class).getPhoneAccount(call.getAccountHandle());
twyene931c122018-03-19 14:47:28 -0700839 if (phoneAccount == null) {
840 return context.getString(R.string.notification_incoming_call);
841 }
twyend1d1d0c2017-10-05 17:34:43 -0700842 SpannableString string =
843 new SpannableString(
linyuh183cb712017-12-27 17:02:37 -0800844 context.getString(
twyend1d1d0c2017-10-05 17:34:43 -0700845 R.string.notification_incoming_call_mutli_sim, phoneAccount.getLabel()));
846 int accountStart = string.toString().lastIndexOf(phoneAccount.getLabel().toString());
847 int accountEnd = accountStart + phoneAccount.getLabel().length();
848
849 string.setSpan(
850 new ForegroundColorSpan(phoneAccount.getHighlightColor()),
851 accountStart,
852 accountEnd,
853 Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
854 return string;
855 }
856
Eric Erfanianccca3152017-02-22 16:32:36 -0800857 /** Gets the most relevant call to display in the notification. */
858 private DialerCall getCallToShow(CallList callList) {
859 if (callList == null) {
860 return null;
861 }
862 DialerCall call = callList.getIncomingCall();
863 if (call == null) {
864 call = callList.getOutgoingCall();
865 }
866 if (call == null) {
867 call = callList.getVideoUpgradeRequestCall();
868 }
869 if (call == null) {
870 call = callList.getActiveOrBackgroundCall();
871 }
872 return call;
873 }
874
875 private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) {
linyuh183cb712017-12-27 17:02:37 -0800876 Spannable spannable = new SpannableString(context.getText(stringRes));
Eric Erfanianccca3152017-02-22 16:32:36 -0800877 if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) {
878 // This will only work for cases where the Notification.Builder has a fullscreen intent set
879 // Notification.Builder that does not have a full screen intent will take the color of the
880 // app and the following leads to a no-op.
881 spannable.setSpan(
linyuh183cb712017-12-27 17:02:37 -0800882 new ForegroundColorSpan(context.getColor(colorRes)), 0, spannable.length(), 0);
Eric Erfanianccca3152017-02-22 16:32:36 -0800883 }
884 return spannable;
885 }
886
887 private void addAnswerAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700888 LogUtil.d(
889 "StatusBarNotifier.addAnswerAction",
890 "will show \"answer\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800891 PendingIntent answerVoicePendingIntent =
linyuh183cb712017-12-27 17:02:37 -0800892 createNotificationPendingIntent(context, ACTION_ANSWER_VOICE_INCOMING_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -0800893 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700894 new Notification.Action.Builder(
linyuh183cb712017-12-27 17:02:37 -0800895 Icon.createWithResource(context, R.drawable.quantum_ic_call_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700896 getActionText(
897 R.string.notification_action_answer, R.color.notification_action_accept),
898 answerVoicePendingIntent)
899 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800900 }
901
erfanian94a7b3b2018-06-19 14:36:18 -0700902 private void addSpeakeasyAnswerAction(Notification.Builder builder, DialerCall call) {
903 if (!call.isSpeakEasyEligible()) {
904 return;
905 }
906
erfanian1e4ef182018-06-11 11:05:51 -0700907 if (!ConfigProviderComponent.get(context)
908 .getConfigProvider()
909 .getBoolean("enable_speakeasy_notification_button", false)) {
910 return;
911 }
912
913 if (!SpeakEasyComponent.get(context).speakEasyCallManager().isAvailable(context)) {
914 return;
915 }
916
917 Optional<Integer> buttonText = SpeakEasyComponent.get(context).speakEasyTextResource();
918 if (!buttonText.isPresent()) {
919 return;
920 }
921
922 LogUtil.d("StatusBarNotifier.addSpeakeasyAnswerAction", "showing button");
923 PendingIntent answerVoicePendingIntent =
924 createNotificationPendingIntent(context, ACTION_ANSWER_SPEAKEASY_CALL);
925
926 Spannable spannable = new SpannableString(context.getText(buttonText.get()));
927 // TODO(erfanian): Migrate these color values to somewhere more permanent in subsequent
928 // implementation.
929 spannable.setSpan(
930 new ForegroundColorSpan(
931 context.getColor(R.color.DO_NOT_USE_OR_I_WILL_BREAK_YOU_text_span_tertiary_button)),
932 0,
933 spannable.length(),
934 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
935
936 builder.addAction(
937 new Notification.Action.Builder(
938 Icon.createWithResource(context, R.drawable.quantum_ic_call_white_24),
939 spannable,
940 answerVoicePendingIntent)
941 .build());
942 }
943
Eric Erfanianccca3152017-02-22 16:32:36 -0800944 private void addDismissAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700945 LogUtil.d(
946 "StatusBarNotifier.addDismissAction",
947 "will show \"decline\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800948 PendingIntent declinePendingIntent =
linyuh183cb712017-12-27 17:02:37 -0800949 createNotificationPendingIntent(context, ACTION_DECLINE_INCOMING_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -0800950 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700951 new Notification.Action.Builder(
linyuh183cb712017-12-27 17:02:37 -0800952 Icon.createWithResource(context, R.drawable.quantum_ic_close_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700953 getActionText(
954 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
955 declinePendingIntent)
956 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800957 }
958
959 private void addHangupAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700960 LogUtil.d(
961 "StatusBarNotifier.addHangupAction",
962 "will show \"hang-up\" action in the ongoing active call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -0800963 PendingIntent hangupPendingIntent =
linyuh183cb712017-12-27 17:02:37 -0800964 createNotificationPendingIntent(context, ACTION_HANG_UP_ONGOING_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -0800965 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700966 new Notification.Action.Builder(
linyuh183cb712017-12-27 17:02:37 -0800967 Icon.createWithResource(context, R.drawable.quantum_ic_call_end_white_24),
968 context.getText(R.string.notification_action_end_call),
Eric Erfaniand5e47f62017-03-15 14:41:07 -0700969 hangupPendingIntent)
970 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -0800971 }
972
yuegb26c1ae2017-09-18 16:59:16 -0700973 private void addSpeakerAction(Notification.Builder builder, CallAudioState callAudioState) {
974 if ((callAudioState.getSupportedRouteMask() & CallAudioState.ROUTE_BLUETOOTH)
975 == CallAudioState.ROUTE_BLUETOOTH) {
976 // Don't add speaker button if bluetooth is connected
977 return;
978 }
979 if (callAudioState.getRoute() == CallAudioState.ROUTE_SPEAKER) {
980 addSpeakerOffAction(builder);
981 } else if ((callAudioState.getRoute() & CallAudioState.ROUTE_WIRED_OR_EARPIECE) != 0) {
982 addSpeakerOnAction(builder);
983 }
984 }
985
986 private void addSpeakerOnAction(Notification.Builder builder) {
987 LogUtil.d(
988 "StatusBarNotifier.addSpeakerOnAction",
989 "will show \"Speaker on\" action in the ongoing active call Notification");
990 PendingIntent speakerOnPendingIntent =
linyuh183cb712017-12-27 17:02:37 -0800991 createNotificationPendingIntent(context, ACTION_TURN_ON_SPEAKER);
yuegb26c1ae2017-09-18 16:59:16 -0700992 builder.addAction(
993 new Notification.Action.Builder(
wangqi6ca8c722018-03-29 10:19:58 -0700994 Icon.createWithResource(context, R.drawable.quantum_ic_volume_up_vd_theme_24),
linyuh183cb712017-12-27 17:02:37 -0800995 context.getText(R.string.notification_action_speaker_on),
yuegb26c1ae2017-09-18 16:59:16 -0700996 speakerOnPendingIntent)
997 .build());
998 }
999
1000 private void addSpeakerOffAction(Notification.Builder builder) {
1001 LogUtil.d(
1002 "StatusBarNotifier.addSpeakerOffAction",
1003 "will show \"Speaker off\" action in the ongoing active call Notification");
1004 PendingIntent speakerOffPendingIntent =
linyuh183cb712017-12-27 17:02:37 -08001005 createNotificationPendingIntent(context, ACTION_TURN_OFF_SPEAKER);
yuegb26c1ae2017-09-18 16:59:16 -07001006 builder.addAction(
1007 new Notification.Action.Builder(
wangqi6ca8c722018-03-29 10:19:58 -07001008 Icon.createWithResource(context, R.drawable.quantum_ic_phone_in_talk_vd_theme_24),
linyuh183cb712017-12-27 17:02:37 -08001009 context.getText(R.string.notification_action_speaker_off),
yuegb26c1ae2017-09-18 16:59:16 -07001010 speakerOffPendingIntent)
1011 .build());
1012 }
1013
Eric Erfanianccca3152017-02-22 16:32:36 -08001014 private void addVideoCallAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001015 LogUtil.i(
1016 "StatusBarNotifier.addVideoCallAction",
1017 "will show \"video\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -08001018 PendingIntent answerVideoPendingIntent =
linyuh183cb712017-12-27 17:02:37 -08001019 createNotificationPendingIntent(context, ACTION_ANSWER_VIDEO_INCOMING_CALL);
Eric Erfanianccca3152017-02-22 16:32:36 -08001020 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001021 new Notification.Action.Builder(
linyuh8dc242d2018-03-28 13:09:47 -07001022 Icon.createWithResource(context, R.drawable.quantum_ic_videocam_vd_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001023 getActionText(
1024 R.string.notification_action_answer_video,
1025 R.color.notification_action_answer_video),
1026 answerVideoPendingIntent)
1027 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -08001028 }
1029
1030 private void addAcceptUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001031 LogUtil.i(
1032 "StatusBarNotifier.addAcceptUpgradeRequestAction",
1033 "will show \"accept upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -08001034 PendingIntent acceptVideoPendingIntent =
linyuh183cb712017-12-27 17:02:37 -08001035 createNotificationPendingIntent(context, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST);
Eric Erfanianccca3152017-02-22 16:32:36 -08001036 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001037 new Notification.Action.Builder(
linyuh8dc242d2018-03-28 13:09:47 -07001038 Icon.createWithResource(context, R.drawable.quantum_ic_videocam_vd_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001039 getActionText(
1040 R.string.notification_action_accept, R.color.notification_action_accept),
1041 acceptVideoPendingIntent)
1042 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -08001043 }
1044
1045 private void addDismissUpgradeRequestAction(Notification.Builder builder) {
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001046 LogUtil.i(
1047 "StatusBarNotifier.addDismissUpgradeRequestAction",
1048 "will show \"dismiss upgrade\" action in the incoming call Notification");
Eric Erfanianccca3152017-02-22 16:32:36 -08001049 PendingIntent declineVideoPendingIntent =
linyuh183cb712017-12-27 17:02:37 -08001050 createNotificationPendingIntent(context, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST);
Eric Erfanianccca3152017-02-22 16:32:36 -08001051 builder.addAction(
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001052 new Notification.Action.Builder(
linyuh8dc242d2018-03-28 13:09:47 -07001053 Icon.createWithResource(context, R.drawable.quantum_ic_videocam_vd_white_24),
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001054 getActionText(
1055 R.string.notification_action_dismiss, R.color.notification_action_dismiss),
1056 declineVideoPendingIntent)
1057 .build());
Eric Erfanianccca3152017-02-22 16:32:36 -08001058 }
1059
1060 /** Adds fullscreen intent to the builder. */
Eric Erfanian10b34a52017-05-04 08:23:17 -07001061 private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001062 // Ok, we actually want to launch the incoming call
1063 // UI at this point (in addition to simply posting a notification
1064 // to the status bar). Setting fullScreenIntent will cause
1065 // the InCallScreen to be launched immediately *unless* the
1066 // current foreground activity is marked as "immersive".
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001067 LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent);
Eric Erfanianccca3152017-02-22 16:32:36 -08001068 builder.setFullScreenIntent(intent, true);
Eric Erfanianccca3152017-02-22 16:32:36 -08001069 }
1070
1071 private Notification.Builder getNotificationBuilder() {
linyuh183cb712017-12-27 17:02:37 -08001072 final Notification.Builder builder = new Notification.Builder(context);
Eric Erfanianccca3152017-02-22 16:32:36 -08001073 builder.setOngoing(true);
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001074 builder.setOnlyAlertOnce(true);
Eric Erfanian10b34a52017-05-04 08:23:17 -07001075 // This will be ignored on O+ and handled by the channel
Eric Erfanian2ca43182017-08-31 06:57:16 -07001076 // noinspection deprecation
Eric Erfanian10b34a52017-05-04 08:23:17 -07001077 builder.setPriority(Notification.PRIORITY_HIGH);
Eric Erfanianccca3152017-02-22 16:32:36 -08001078
1079 return builder;
1080 }
1081
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001082 private PendingIntent createLaunchPendingIntent(boolean isFullScreen) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001083 Intent intent =
1084 InCallActivity.getIntent(
linyuh183cb712017-12-27 17:02:37 -08001085 context, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen);
Eric Erfanianccca3152017-02-22 16:32:36 -08001086
linyuhc3968e62017-11-20 17:40:50 -08001087 int requestCode = InCallActivity.PendingIntentRequestCodes.NON_FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001088 if (isFullScreen) {
1089 // Use a unique request code so that the pending intent isn't clobbered by the
1090 // non-full screen pending intent.
linyuhc3968e62017-11-20 17:40:50 -08001091 requestCode = InCallActivity.PendingIntentRequestCodes.FULL_SCREEN;
Eric Erfanianccca3152017-02-22 16:32:36 -08001092 }
1093
1094 // PendingIntent that can be used to launch the InCallActivity. The
1095 // system fires off this intent if the user pulls down the windowshade
1096 // and clicks the notification's expanded view. It's also used to
1097 // launch the InCallActivity immediately when when there's an incoming
1098 // call (see the "fullScreenIntent" field below).
linyuh183cb712017-12-27 17:02:37 -08001099 return PendingIntent.getActivity(context, requestCode, intent, 0);
Eric Erfanianccca3152017-02-22 16:32:36 -08001100 }
1101
1102 private void setStatusBarCallListener(StatusBarCallListener listener) {
linyuh183cb712017-12-27 17:02:37 -08001103 if (statusBarCallListener != null) {
1104 statusBarCallListener.cleanup();
Eric Erfanianccca3152017-02-22 16:32:36 -08001105 }
linyuh183cb712017-12-27 17:02:37 -08001106 statusBarCallListener = listener;
Eric Erfanianccca3152017-02-22 16:32:36 -08001107 }
1108
wangqi9982f0d2017-10-11 17:46:07 -07001109 private boolean hasMultiplePhoneAccounts(DialerCall call) {
1110 if (call.getCallCapableAccounts() == null) {
1111 return false;
1112 }
1113 return call.getCallCapableAccounts().size() > 1;
twyend1d1d0c2017-10-05 17:34:43 -07001114 }
1115
yuegb26c1ae2017-09-18 16:59:16 -07001116 @Override
wangqic8cf79e2017-10-17 09:21:00 -07001117 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1118 public void onContactInfoComplete(String callId, ContactCacheEntry entry) {
1119 DialerCall call = CallList.getInstance().getCallById(callId);
1120 if (call != null) {
1121 call.getLogState().contactLookupResult = entry.contactLookupResult;
1122 buildAndSendNotification(CallList.getInstance(), call, entry);
1123 }
1124 }
1125
1126 @Override
1127 @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
1128 public void onImageLoadComplete(String callId, ContactCacheEntry entry) {
1129 DialerCall call = CallList.getInstance().getCallById(callId);
1130 if (call != null) {
1131 buildAndSendNotification(CallList.getInstance(), call, entry);
1132 }
yuegb26c1ae2017-09-18 16:59:16 -07001133 }
1134
Eric Erfanianccca3152017-02-22 16:32:36 -08001135 private class StatusBarCallListener implements DialerCallListener {
1136
linyuh183cb712017-12-27 17:02:37 -08001137 private DialerCall dialerCall;
Eric Erfanianccca3152017-02-22 16:32:36 -08001138
1139 StatusBarCallListener(DialerCall dialerCall) {
linyuh183cb712017-12-27 17:02:37 -08001140 this.dialerCall = dialerCall;
1141 this.dialerCall.addListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -08001142 }
1143
1144 void cleanup() {
linyuh183cb712017-12-27 17:02:37 -08001145 dialerCall.removeListener(this);
Eric Erfanianccca3152017-02-22 16:32:36 -08001146 }
1147
1148 @Override
1149 public void onDialerCallDisconnect() {}
1150
1151 @Override
1152 public void onDialerCallUpdate() {
1153 if (CallList.getInstance().getIncomingCall() == null) {
linyuh183cb712017-12-27 17:02:37 -08001154 dialerRingtoneManager.stopCallWaitingTone();
Eric Erfanianccca3152017-02-22 16:32:36 -08001155 }
1156 }
1157
1158 @Override
1159 public void onDialerCallChildNumberChange() {}
1160
1161 @Override
1162 public void onDialerCallLastForwardedNumberChange() {}
1163
1164 @Override
1165 public void onDialerCallUpgradeToVideo() {}
1166
1167 @Override
1168 public void onWiFiToLteHandover() {}
1169
1170 @Override
1171 public void onHandoverToWifiFailure() {}
1172
Eric Erfanianc857f902017-05-15 14:05:33 -07001173 @Override
1174 public void onInternationalCallOnWifi() {}
1175
Eric Erfanian2ca43182017-08-31 06:57:16 -07001176 @Override
1177 public void onEnrichedCallSessionUpdate() {}
1178
Eric Erfanianccca3152017-02-22 16:32:36 -08001179 /**
1180 * Responds to changes in the session modification state for the call by dismissing the status
1181 * bar notification as required.
1182 */
1183 @Override
Eric Erfaniand5e47f62017-03-15 14:41:07 -07001184 public void onDialerCallSessionModificationStateChange() {
linyuh183cb712017-12-27 17:02:37 -08001185 if (dialerCall.getVideoTech().getSessionModificationState()
Eric Erfanian90508232017-03-24 09:31:16 -07001186 == SessionModificationState.NO_REQUEST) {
Eric Erfanianccca3152017-02-22 16:32:36 -08001187 cleanup();
wangqic8cf79e2017-10-17 09:21:00 -07001188 updateNotification();
Eric Erfanianccca3152017-02-22 16:32:36 -08001189 }
1190 }
1191 }
1192}