Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.incallui; |
| 18 | |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 19 | import static android.telecom.Call.Details.PROPERTY_HIGH_DEF_AUDIO; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 20 | import static com.android.contacts.common.compat.CallCompat.Details.PROPERTY_ENTERPRISE_CALL; |
| 21 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST; |
| 22 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VIDEO_INCOMING_CALL; |
| 23 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_ANSWER_VOICE_INCOMING_CALL; |
| 24 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_INCOMING_CALL; |
| 25 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_DECLINE_VIDEO_UPGRADE_REQUEST; |
| 26 | import static com.android.incallui.NotificationBroadcastReceiver.ACTION_HANG_UP_ONGOING_CALL; |
| 27 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 28 | import android.Manifest; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 29 | import android.app.ActivityManager; |
| 30 | import android.app.Notification; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 31 | import android.app.Notification.Builder; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 32 | import android.app.NotificationManager; |
| 33 | import android.app.PendingIntent; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.BitmapFactory; |
| 38 | import android.graphics.drawable.BitmapDrawable; |
| 39 | import android.graphics.drawable.Drawable; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 40 | import android.graphics.drawable.Icon; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 41 | import android.media.AudioAttributes; |
| 42 | import android.net.Uri; |
| 43 | import android.os.Build.VERSION; |
| 44 | import android.os.Build.VERSION_CODES; |
| 45 | import android.support.annotation.ColorRes; |
| 46 | import android.support.annotation.NonNull; |
| 47 | import android.support.annotation.Nullable; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 48 | import android.support.annotation.RequiresPermission; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | import android.support.annotation.StringRes; |
| 50 | import android.support.annotation.VisibleForTesting; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 51 | import android.support.v4.os.BuildCompat; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 52 | import android.telecom.Call.Details; |
| 53 | import android.telecom.PhoneAccount; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 54 | import android.telecom.PhoneAccountHandle; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 55 | import android.telecom.TelecomManager; |
| 56 | import android.text.BidiFormatter; |
| 57 | import android.text.Spannable; |
| 58 | import android.text.SpannableString; |
| 59 | import android.text.TextDirectionHeuristics; |
| 60 | import android.text.TextUtils; |
| 61 | import android.text.style.ForegroundColorSpan; |
| 62 | import com.android.contacts.common.ContactsUtils; |
| 63 | import com.android.contacts.common.ContactsUtils.UserType; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 64 | import com.android.contacts.common.lettertiles.LetterTileDrawable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | import com.android.contacts.common.preference.ContactsPreferences; |
| 66 | import com.android.contacts.common.util.BitmapUtil; |
| 67 | import com.android.contacts.common.util.ContactDisplayUtils; |
| 68 | import com.android.dialer.common.LogUtil; |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 69 | import com.android.dialer.enrichedcall.EnrichedCallComponent; |
| 70 | import com.android.dialer.enrichedcall.EnrichedCallManager; |
| 71 | import com.android.dialer.enrichedcall.Session; |
| 72 | import com.android.dialer.multimedia.MultimediaData; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 73 | import com.android.dialer.notification.NotificationChannelManager; |
| 74 | import com.android.dialer.notification.NotificationChannelManager.Channel; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 75 | import com.android.dialer.oem.MotorolaUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 76 | import com.android.dialer.util.DrawableConverter; |
| 77 | import com.android.incallui.ContactInfoCache.ContactCacheEntry; |
| 78 | import com.android.incallui.ContactInfoCache.ContactInfoCacheCallback; |
| 79 | import com.android.incallui.InCallPresenter.InCallState; |
| 80 | import com.android.incallui.async.PausableExecutorImpl; |
| 81 | import com.android.incallui.call.CallList; |
| 82 | import com.android.incallui.call.DialerCall; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 83 | import com.android.incallui.call.DialerCallListener; |
| 84 | import com.android.incallui.ringtone.DialerRingtoneManager; |
| 85 | import com.android.incallui.ringtone.InCallTonePlayer; |
| 86 | import com.android.incallui.ringtone.ToneGeneratorFactory; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 87 | import com.android.incallui.videotech.utils.SessionModificationState; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 88 | import java.util.List; |
| 89 | import java.util.Locale; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 90 | import java.util.Objects; |
| 91 | |
| 92 | /** This class adds Notifications to the status bar for the in-call experience. */ |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 93 | public class StatusBarNotifier |
| 94 | implements InCallPresenter.InCallStateListener, EnrichedCallManager.StateChangedListener { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 95 | |
| 96 | // Notification types |
| 97 | // Indicates that no notification is currently showing. |
| 98 | private static final int NOTIFICATION_NONE = 0; |
| 99 | // Notification for an active call. This is non-interruptive, but cannot be dismissed. |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 100 | private static final int NOTIFICATION_IN_CALL = 1; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 101 | // Notification for incoming calls. This is interruptive and will show up as a HUN. |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 102 | private static final int NOTIFICATION_INCOMING_CALL = 2; |
| 103 | // Notification for incoming calls in the case where there is already an active call. |
| 104 | // This is non-interruptive, but otherwise behaves the same as NOTIFICATION_INCOMING_CALL |
| 105 | private static final int NOTIFICATION_INCOMING_CALL_QUIET = 3; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 106 | |
| 107 | private static final int PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN = 0; |
| 108 | private static final int PENDING_INTENT_REQUEST_CODE_FULL_SCREEN = 1; |
| 109 | |
| 110 | private static final long[] VIBRATE_PATTERN = new long[] {0, 1000, 1000}; |
| 111 | |
| 112 | private final Context mContext; |
| 113 | private final ContactInfoCache mContactInfoCache; |
| 114 | private final NotificationManager mNotificationManager; |
| 115 | private final DialerRingtoneManager mDialerRingtoneManager; |
| 116 | @Nullable private ContactsPreferences mContactsPreferences; |
| 117 | private int mCurrentNotification = NOTIFICATION_NONE; |
| 118 | private int mCallState = DialerCall.State.INVALID; |
| 119 | private int mSavedIcon = 0; |
| 120 | private String mSavedContent = null; |
| 121 | private Bitmap mSavedLargeIcon; |
| 122 | private String mSavedContentTitle; |
| 123 | private Uri mRingtone; |
| 124 | private StatusBarCallListener mStatusBarCallListener; |
| 125 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 126 | public StatusBarNotifier(@NonNull Context context, @NonNull ContactInfoCache contactInfoCache) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 127 | Objects.requireNonNull(context); |
| 128 | mContext = context; |
| 129 | mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext); |
| 130 | mContactInfoCache = contactInfoCache; |
| 131 | mNotificationManager = context.getSystemService(NotificationManager.class); |
| 132 | mDialerRingtoneManager = |
| 133 | new DialerRingtoneManager( |
| 134 | new InCallTonePlayer(new ToneGeneratorFactory(), new PausableExecutorImpl()), |
| 135 | CallList.getInstance()); |
| 136 | mCurrentNotification = NOTIFICATION_NONE; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Should only be called from a irrecoverable state where it is necessary to dismiss all |
| 141 | * notifications. |
| 142 | */ |
| 143 | static void clearAllCallNotifications(Context backupContext) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 144 | LogUtil.i( |
| 145 | "StatusBarNotifier.clearAllCallNotifications", |
| 146 | "something terrible happened, clear all InCall notifications"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 147 | |
| 148 | NotificationManager notificationManager = |
| 149 | backupContext.getSystemService(NotificationManager.class); |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 150 | notificationManager.cancel(R.id.notification_ongoing_call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private static int getWorkStringFromPersonalString(int resId) { |
| 154 | if (resId == R.string.notification_ongoing_call) { |
| 155 | return R.string.notification_ongoing_work_call; |
| 156 | } else if (resId == R.string.notification_ongoing_call_wifi) { |
| 157 | return R.string.notification_ongoing_work_call_wifi; |
| 158 | } else if (resId == R.string.notification_incoming_call_wifi) { |
| 159 | return R.string.notification_incoming_work_call_wifi; |
| 160 | } else if (resId == R.string.notification_incoming_call) { |
| 161 | return R.string.notification_incoming_work_call; |
| 162 | } else { |
| 163 | return resId; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns PendingIntent for answering a phone call. This will typically be used from Notification |
| 169 | * context. |
| 170 | */ |
| 171 | private static PendingIntent createNotificationPendingIntent(Context context, String action) { |
| 172 | final Intent intent = new Intent(action, null, context, NotificationBroadcastReceiver.class); |
| 173 | return PendingIntent.getBroadcast(context, 0, intent, 0); |
| 174 | } |
| 175 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 176 | private static void setColorized(@NonNull Builder builder) { |
| 177 | if (BuildCompat.isAtLeastO()) { |
| 178 | builder.setColorized(true); |
| 179 | } |
| 180 | } |
| 181 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 182 | /** Creates notifications according to the state we receive from {@link InCallPresenter}. */ |
| 183 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 184 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 185 | public void onStateChange(InCallState oldState, InCallState newState, CallList callList) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 186 | LogUtil.d("StatusBarNotifier.onStateChange", "%s->%s", oldState, newState); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 187 | updateNotification(callList); |
| 188 | } |
| 189 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public void onEnrichedCallStateChanged() { |
| 192 | LogUtil.enterBlock("StatusBarNotifier.onEnrichedCallStateChanged"); |
| 193 | updateNotification(CallList.getInstance()); |
| 194 | } |
| 195 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Updates the phone app's status bar notification *and* launches the incoming call UI in response |
| 198 | * to a new incoming call. |
| 199 | * |
| 200 | * <p>If an incoming call is ringing (or call-waiting), the notification will also include a |
| 201 | * "fullScreenIntent" that will cause the InCallScreen to be launched, unless the current |
| 202 | * foreground activity is marked as "immersive". |
| 203 | * |
| 204 | * <p>(This is the mechanism that actually brings up the incoming call UI when we receive a "new |
| 205 | * ringing connection" event from the telephony layer.) |
| 206 | * |
| 207 | * <p>Also note that this method is safe to call even if the phone isn't actually ringing (or, |
| 208 | * more likely, if an incoming call *was* ringing briefly but then disconnected). In that case, |
| 209 | * we'll simply update or cancel the in-call notification based on the current phone state. |
| 210 | * |
| 211 | * @see #updateInCallNotification(CallList) |
| 212 | */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 213 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 214 | public void updateNotification(CallList callList) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 215 | updateInCallNotification(callList); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Take down the in-call notification. |
| 220 | * |
| 221 | * @see #updateInCallNotification(CallList) |
| 222 | */ |
| 223 | private void cancelNotification() { |
| 224 | if (mStatusBarCallListener != null) { |
| 225 | setStatusBarCallListener(null); |
| 226 | } |
| 227 | if (mCurrentNotification != NOTIFICATION_NONE) { |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 228 | LogUtil.i("StatusBarNotifier.cancelNotification", "cancel"); |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 229 | mNotificationManager.cancel(R.id.notification_ongoing_call); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 230 | } |
| 231 | mCurrentNotification = NOTIFICATION_NONE; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Helper method for updateInCallNotification() and updateNotification(): Update the phone app's |
| 236 | * status bar notification based on the current telephony state, or cancels the notification if |
| 237 | * the phone is totally idle. |
| 238 | */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 239 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 240 | private void updateInCallNotification(CallList callList) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 241 | LogUtil.d("StatusBarNotifier.updateInCallNotification", ""); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 242 | |
| 243 | final DialerCall call = getCallToShow(callList); |
| 244 | |
| 245 | if (call != null) { |
| 246 | showNotification(callList, call); |
| 247 | } else { |
| 248 | cancelNotification(); |
| 249 | } |
| 250 | } |
| 251 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 252 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 253 | private void showNotification(final CallList callList, final DialerCall call) { |
| 254 | final boolean isIncoming = |
| 255 | (call.getState() == DialerCall.State.INCOMING |
| 256 | || call.getState() == DialerCall.State.CALL_WAITING); |
| 257 | setStatusBarCallListener(new StatusBarCallListener(call)); |
| 258 | |
| 259 | // we make a call to the contact info cache to query for supplemental data to what the |
| 260 | // call provides. This includes the contact name and photo. |
| 261 | // This callback will always get called immediately and synchronously with whatever data |
| 262 | // it has available, and may make a subsequent call later (same thread) if it had to |
| 263 | // call into the contacts provider for more data. |
| 264 | mContactInfoCache.findInfo( |
| 265 | call, |
| 266 | isIncoming, |
| 267 | new ContactInfoCacheCallback() { |
| 268 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 269 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 270 | public void onContactInfoComplete(String callId, ContactCacheEntry entry) { |
| 271 | DialerCall call = callList.getCallById(callId); |
| 272 | if (call != null) { |
| 273 | call.getLogState().contactLookupResult = entry.contactLookupResult; |
| 274 | buildAndSendNotification(callList, call, entry); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 279 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 280 | public void onImageLoadComplete(String callId, ContactCacheEntry entry) { |
| 281 | DialerCall call = callList.getCallById(callId); |
| 282 | if (call != null) { |
| 283 | buildAndSendNotification(callList, call, entry); |
| 284 | } |
| 285 | } |
| 286 | }); |
| 287 | } |
| 288 | |
| 289 | /** Sets up the main Ui for the notification */ |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 290 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 291 | private void buildAndSendNotification( |
| 292 | CallList callList, DialerCall originalCall, ContactCacheEntry contactInfo) { |
| 293 | // This can get called to update an existing notification after contact information has come |
| 294 | // back. However, it can happen much later. Before we continue, we need to make sure that |
| 295 | // the call being passed in is still the one we want to show in the notification. |
| 296 | final DialerCall call = getCallToShow(callList); |
| 297 | if (call == null || !call.getId().equals(originalCall.getId())) { |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | final int callState = call.getState(); |
| 302 | |
| 303 | // Check if data has changed; if nothing is different, don't issue another notification. |
| 304 | final int iconResId = getIconToDisplay(call); |
| 305 | Bitmap largeIcon = getLargeIconToDisplay(contactInfo, call); |
| 306 | final String content = getContentString(call, contactInfo.userType); |
| 307 | final String contentTitle = getContentTitle(contactInfo, call); |
| 308 | |
| 309 | final boolean isVideoUpgradeRequest = |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 310 | call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 311 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 312 | final int notificationType; |
| 313 | if (callState == DialerCall.State.INCOMING |
| 314 | || callState == DialerCall.State.CALL_WAITING |
| 315 | || isVideoUpgradeRequest) { |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 316 | boolean alreadyActive = |
| 317 | callList.getActiveOrBackgroundCall() != null |
| 318 | && InCallPresenter.getInstance().isShowingInCallUi(); |
| 319 | notificationType = |
| 320 | alreadyActive ? NOTIFICATION_INCOMING_CALL_QUIET : NOTIFICATION_INCOMING_CALL; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 321 | } else { |
| 322 | notificationType = NOTIFICATION_IN_CALL; |
| 323 | } |
| 324 | |
| 325 | if (!checkForChangeAndSaveData( |
| 326 | iconResId, |
| 327 | content, |
| 328 | largeIcon, |
| 329 | contentTitle, |
| 330 | callState, |
| 331 | notificationType, |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 332 | contactInfo.contactRingtoneUri)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (largeIcon != null) { |
| 337 | largeIcon = getRoundedIcon(largeIcon); |
| 338 | } |
| 339 | |
| 340 | // This builder is used for the notification shown when the device is locked and the user |
| 341 | // has set their notification settings to 'hide sensitive content' |
| 342 | // {@see Notification.Builder#setPublicVersion}. |
| 343 | Notification.Builder publicBuilder = new Notification.Builder(mContext); |
| 344 | publicBuilder |
| 345 | .setSmallIcon(iconResId) |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 346 | .setColor(mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())) |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 347 | // Hide work call state for the lock screen notification |
| 348 | .setContentTitle(getContentString(call, ContactsUtils.USER_TYPE_CURRENT)); |
| 349 | setNotificationWhen(call, callState, publicBuilder); |
| 350 | |
| 351 | // Builder for the notification shown when the device is unlocked or the user has set their |
| 352 | // notification settings to 'show all notification content'. |
| 353 | final Notification.Builder builder = getNotificationBuilder(); |
| 354 | builder.setPublicVersion(publicBuilder.build()); |
| 355 | |
| 356 | // Set up the main intent to send the user to the in-call screen |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 357 | builder.setContentIntent(createLaunchPendingIntent(false /* isFullScreen */)); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 358 | |
| 359 | // Set the intent as a full screen intent as well if a call is incoming |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 360 | PhoneAccountHandle accountHandle = call.getAccountHandle(); |
| 361 | if (accountHandle == null) { |
| 362 | accountHandle = getAnyPhoneAccount(); |
| 363 | } |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 364 | |
| 365 | LogUtil.i("StatusBarNotifier.buildAndSendNotification", "notificationType=" + notificationType); |
| 366 | switch (notificationType) { |
| 367 | case NOTIFICATION_INCOMING_CALL: |
| 368 | NotificationChannelManager.applyChannel( |
| 369 | builder, mContext, Channel.INCOMING_CALL, accountHandle); |
| 370 | configureFullScreenIntent(builder, createLaunchPendingIntent(true /* isFullScreen */)); |
| 371 | // Set the notification category and bump the priority for incoming calls |
| 372 | builder.setCategory(Notification.CATEGORY_CALL); |
| 373 | // This will be ignored on O+ and handled by the channel |
| 374 | //noinspection deprecation |
| 375 | builder.setPriority(Notification.PRIORITY_MAX); |
| 376 | if (mCurrentNotification != NOTIFICATION_INCOMING_CALL) { |
| 377 | LogUtil.i( |
| 378 | "StatusBarNotifier.buildAndSendNotification", |
| 379 | "Canceling old notification so this one can be noisy"); |
| 380 | // Moving from a non-interuptive notification (or none) to a noisy one. Cancel the old |
| 381 | // notification (if there is one) so the fullScreenIntent or HUN will show |
| 382 | mNotificationManager.cancel(R.id.notification_ongoing_call); |
| 383 | } |
| 384 | break; |
| 385 | case NOTIFICATION_INCOMING_CALL_QUIET: |
| 386 | NotificationChannelManager.applyChannel( |
| 387 | builder, mContext, Channel.ONGOING_CALL, accountHandle); |
| 388 | break; |
| 389 | case NOTIFICATION_IN_CALL: |
| 390 | setColorized(publicBuilder); |
| 391 | setColorized(builder); |
| 392 | NotificationChannelManager.applyChannel( |
| 393 | builder, mContext, Channel.ONGOING_CALL, accountHandle); |
| 394 | break; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Set the content |
| 398 | builder.setContentText(content); |
| 399 | builder.setSmallIcon(iconResId); |
| 400 | builder.setContentTitle(contentTitle); |
| 401 | builder.setLargeIcon(largeIcon); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 402 | builder.setColor( |
| 403 | mContext.getResources().getColor(R.color.dialer_theme_color, mContext.getTheme())); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 404 | |
| 405 | if (isVideoUpgradeRequest) { |
| 406 | builder.setUsesChronometer(false); |
| 407 | addDismissUpgradeRequestAction(builder); |
| 408 | addAcceptUpgradeRequestAction(builder); |
| 409 | } else { |
| 410 | createIncomingCallNotification(call, callState, builder); |
| 411 | } |
| 412 | |
| 413 | addPersonReference(builder, contactInfo, call); |
| 414 | |
| 415 | // Fire off the notification |
| 416 | Notification notification = builder.build(); |
| 417 | |
| 418 | if (mDialerRingtoneManager.shouldPlayRingtone(callState, contactInfo.contactRingtoneUri)) { |
| 419 | notification.flags |= Notification.FLAG_INSISTENT; |
| 420 | notification.sound = contactInfo.contactRingtoneUri; |
| 421 | AudioAttributes.Builder audioAttributes = new AudioAttributes.Builder(); |
| 422 | audioAttributes.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC); |
| 423 | audioAttributes.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE); |
| 424 | notification.audioAttributes = audioAttributes.build(); |
| 425 | if (mDialerRingtoneManager.shouldVibrate(mContext.getContentResolver())) { |
| 426 | notification.vibrate = VIBRATE_PATTERN; |
| 427 | } |
| 428 | } |
| 429 | if (mDialerRingtoneManager.shouldPlayCallWaitingTone(callState)) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 430 | LogUtil.v("StatusBarNotifier.buildAndSendNotification", "playing call waiting tone"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 431 | mDialerRingtoneManager.playCallWaitingTone(); |
| 432 | } |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 433 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 434 | LogUtil.i( |
| 435 | "StatusBarNotifier.buildAndSendNotification", |
| 436 | "displaying notification for " + notificationType); |
| 437 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 438 | try { |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 439 | mNotificationManager.notify(R.id.notification_ongoing_call, notification); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 440 | } catch (RuntimeException e) { |
| 441 | // TODO(b/34744003): Move the memory stats into silent feedback PSD. |
| 442 | ActivityManager activityManager = mContext.getSystemService(ActivityManager.class); |
| 443 | ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); |
| 444 | activityManager.getMemoryInfo(memoryInfo); |
| 445 | throw new RuntimeException( |
| 446 | String.format( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 447 | Locale.US, |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 448 | "Error displaying notification with photo type: %d (low memory? %b, availMem: %d)", |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 449 | contactInfo.photoType, |
| 450 | memoryInfo.lowMemory, |
| 451 | memoryInfo.availMem), |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 452 | e); |
| 453 | } |
| 454 | call.getLatencyReport().onNotificationShown(); |
| 455 | mCurrentNotification = notificationType; |
| 456 | } |
| 457 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 458 | @Nullable |
| 459 | @RequiresPermission(Manifest.permission.READ_PHONE_STATE) |
| 460 | private PhoneAccountHandle getAnyPhoneAccount() { |
| 461 | PhoneAccountHandle accountHandle; |
| 462 | TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class); |
| 463 | accountHandle = telecomManager.getDefaultOutgoingPhoneAccount(PhoneAccount.SCHEME_TEL); |
| 464 | if (accountHandle == null) { |
| 465 | List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts(); |
| 466 | if (!accountHandles.isEmpty()) { |
| 467 | accountHandle = accountHandles.get(0); |
| 468 | } |
| 469 | } |
| 470 | return accountHandle; |
| 471 | } |
| 472 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 473 | private void createIncomingCallNotification( |
| 474 | DialerCall call, int state, Notification.Builder builder) { |
| 475 | setNotificationWhen(call, state, builder); |
| 476 | |
| 477 | // Add hang up option for any active calls (active | onhold), outgoing calls (dialing). |
| 478 | if (state == DialerCall.State.ACTIVE |
| 479 | || state == DialerCall.State.ONHOLD |
| 480 | || DialerCall.State.isDialing(state)) { |
| 481 | addHangupAction(builder); |
| 482 | } else if (state == DialerCall.State.INCOMING || state == DialerCall.State.CALL_WAITING) { |
| 483 | addDismissAction(builder); |
| 484 | if (call.isVideoCall()) { |
| 485 | addVideoCallAction(builder); |
| 486 | } else { |
| 487 | addAnswerAction(builder); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Sets the notification's when section as needed. For active calls, this is explicitly set as the |
| 494 | * duration of the call. For all other states, the notification will automatically show the time |
| 495 | * at which the notification was created. |
| 496 | */ |
| 497 | private void setNotificationWhen(DialerCall call, int state, Notification.Builder builder) { |
| 498 | if (state == DialerCall.State.ACTIVE) { |
| 499 | builder.setUsesChronometer(true); |
| 500 | builder.setWhen(call.getConnectTimeMillis()); |
| 501 | } else { |
| 502 | builder.setUsesChronometer(false); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Checks the new notification data and compares it against any notification that we are already |
| 508 | * displaying. If the data is exactly the same, we return false so that we do not issue a new |
| 509 | * notification for the exact same data. |
| 510 | */ |
| 511 | private boolean checkForChangeAndSaveData( |
| 512 | int icon, |
| 513 | String content, |
| 514 | Bitmap largeIcon, |
| 515 | String contentTitle, |
| 516 | int state, |
| 517 | int notificationType, |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 518 | Uri ringtone) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 519 | |
| 520 | // The two are different: |
| 521 | // if new title is not null, it should be different from saved version OR |
| 522 | // if new title is null, the saved version should not be null |
| 523 | final boolean contentTitleChanged = |
| 524 | (contentTitle != null && !contentTitle.equals(mSavedContentTitle)) |
| 525 | || (contentTitle == null && mSavedContentTitle != null); |
| 526 | |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 527 | boolean largeIconChanged = |
| 528 | mSavedLargeIcon == null ? largeIcon != null : !mSavedLargeIcon.sameAs(largeIcon); |
| 529 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 530 | // any change means we are definitely updating |
| 531 | boolean retval = |
| 532 | (mSavedIcon != icon) |
| 533 | || !Objects.equals(mSavedContent, content) |
| 534 | || (mCallState != state) |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 535 | || largeIconChanged |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 536 | || contentTitleChanged |
Eric Erfanian | 8369df0 | 2017-05-03 10:27:13 -0700 | [diff] [blame] | 537 | || !Objects.equals(mRingtone, ringtone); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 538 | |
| 539 | // If we aren't showing a notification right now or the notification type is changing, |
| 540 | // definitely do an update. |
| 541 | if (mCurrentNotification != notificationType) { |
| 542 | if (mCurrentNotification == NOTIFICATION_NONE) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 543 | LogUtil.d( |
| 544 | "StatusBarNotifier.checkForChangeAndSaveData", "showing notification for first time."); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 545 | } |
| 546 | retval = true; |
| 547 | } |
| 548 | |
| 549 | mSavedIcon = icon; |
| 550 | mSavedContent = content; |
| 551 | mCallState = state; |
| 552 | mSavedLargeIcon = largeIcon; |
| 553 | mSavedContentTitle = contentTitle; |
| 554 | mRingtone = ringtone; |
| 555 | |
| 556 | if (retval) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 557 | LogUtil.d( |
| 558 | "StatusBarNotifier.checkForChangeAndSaveData", "data changed. Showing notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | return retval; |
| 562 | } |
| 563 | |
| 564 | /** Returns the main string to use in the notification. */ |
| 565 | @VisibleForTesting |
| 566 | @Nullable |
| 567 | String getContentTitle(ContactCacheEntry contactInfo, DialerCall call) { |
| 568 | if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) { |
| 569 | return mContext.getResources().getString(R.string.conference_call_name); |
| 570 | } |
| 571 | |
| 572 | String preferredName = |
| 573 | ContactDisplayUtils.getPreferredDisplayName( |
| 574 | contactInfo.namePrimary, contactInfo.nameAlternative, mContactsPreferences); |
| 575 | if (TextUtils.isEmpty(preferredName)) { |
| 576 | return TextUtils.isEmpty(contactInfo.number) |
| 577 | ? null |
| 578 | : BidiFormatter.getInstance() |
| 579 | .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR); |
| 580 | } |
| 581 | return preferredName; |
| 582 | } |
| 583 | |
| 584 | private void addPersonReference( |
| 585 | Notification.Builder builder, ContactCacheEntry contactInfo, DialerCall call) { |
| 586 | // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed. |
| 587 | // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid |
| 588 | // NotificationManager using it. |
| 589 | if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) { |
| 590 | builder.addPerson(contactInfo.lookupUri.toString()); |
| 591 | } else if (!TextUtils.isEmpty(call.getNumber())) { |
| 592 | builder.addPerson(Uri.fromParts(PhoneAccount.SCHEME_TEL, call.getNumber(), null).toString()); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** Gets a large icon from the contact info object to display in the notification. */ |
| 597 | private Bitmap getLargeIconToDisplay(ContactCacheEntry contactInfo, DialerCall call) { |
| 598 | Bitmap largeIcon = null; |
| 599 | if (call.isConferenceCall() && !call.hasProperty(Details.PROPERTY_GENERIC_CONFERENCE)) { |
| 600 | largeIcon = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.img_conference); |
| 601 | } |
| 602 | if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) { |
| 603 | largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap(); |
| 604 | } |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 605 | if (contactInfo.photo == null) { |
| 606 | int width = |
| 607 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width); |
| 608 | int height = |
| 609 | (int) |
| 610 | mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height); |
| 611 | int contactType = LetterTileDrawable.TYPE_DEFAULT; |
| 612 | LetterTileDrawable lettertile = new LetterTileDrawable(mContext.getResources()); |
| 613 | |
| 614 | // TODO: Deduplicate across Dialer. b/36195917 |
| 615 | if (CallerInfoUtils.isVoiceMailNumber(mContext, call)) { |
| 616 | contactType = LetterTileDrawable.TYPE_VOICEMAIL; |
| 617 | } else if (contactInfo.isBusiness) { |
| 618 | contactType = LetterTileDrawable.TYPE_BUSINESS; |
| 619 | } else if (call.getNumberPresentation() == TelecomManager.PRESENTATION_RESTRICTED) { |
| 620 | contactType = LetterTileDrawable.TYPE_GENERIC_AVATAR; |
| 621 | } |
| 622 | lettertile.setCanonicalDialerLetterTileDetails( |
| 623 | contactInfo.namePrimary == null ? contactInfo.number : contactInfo.namePrimary, |
| 624 | contactInfo.lookupKey, |
| 625 | LetterTileDrawable.SHAPE_CIRCLE, |
| 626 | contactType); |
| 627 | largeIcon = lettertile.getBitmap(width, height); |
| 628 | } |
| 629 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 630 | if (call.isSpam()) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 631 | Drawable drawable = |
| 632 | mContext.getResources().getDrawable(R.drawable.blocked_contact, mContext.getTheme()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 633 | largeIcon = DrawableConverter.drawableToBitmap(drawable); |
| 634 | } |
| 635 | return largeIcon; |
| 636 | } |
| 637 | |
| 638 | private Bitmap getRoundedIcon(Bitmap bitmap) { |
| 639 | if (bitmap == null) { |
| 640 | return null; |
| 641 | } |
| 642 | final int height = |
| 643 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_height); |
| 644 | final int width = |
| 645 | (int) mContext.getResources().getDimension(android.R.dimen.notification_large_icon_width); |
| 646 | return BitmapUtil.getRoundedBitmap(bitmap, width, height); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * Returns the appropriate icon res Id to display based on the call for which we want to display |
| 651 | * information. |
| 652 | */ |
| 653 | private int getIconToDisplay(DialerCall call) { |
| 654 | // Even if both lines are in use, we only show a single item in |
| 655 | // the expanded Notifications UI. It's labeled "Ongoing call" |
| 656 | // (or "On hold" if there's only one call, and it's on hold.) |
| 657 | // Also, we don't have room to display caller-id info from two |
| 658 | // different calls. So if both lines are in use, display info |
| 659 | // from the foreground call. And if there's a ringing call, |
| 660 | // display that regardless of the state of the other calls. |
| 661 | if (call.getState() == DialerCall.State.ONHOLD) { |
| 662 | return R.drawable.ic_phone_paused_white_24dp; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 663 | } else if (call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 664 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 665 | return R.drawable.quantum_ic_videocam_white_24; |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 666 | } else if (call.hasProperty(PROPERTY_HIGH_DEF_AUDIO) |
| 667 | && MotorolaUtils.shouldShowHdIconInNotification(mContext)) { |
| 668 | // Normally when a call is ongoing the status bar displays an icon of a phone with animated |
| 669 | // lines. This is a helpful hint for users so they know how to get back to the call. |
| 670 | // For Sprint HD calls, we replace this icon with an icon of a phone with a HD badge. |
| 671 | // This is a carrier requirement. |
| 672 | return R.drawable.ic_hd_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 673 | } |
| 674 | return R.anim.on_going_call; |
| 675 | } |
| 676 | |
| 677 | /** Returns the message to use with the notification. */ |
| 678 | private String getContentString(DialerCall call, @UserType long userType) { |
| 679 | boolean isIncomingOrWaiting = |
| 680 | call.getState() == DialerCall.State.INCOMING |
| 681 | || call.getState() == DialerCall.State.CALL_WAITING; |
| 682 | |
| 683 | if (isIncomingOrWaiting |
| 684 | && call.getNumberPresentation() == TelecomManager.PRESENTATION_ALLOWED) { |
| 685 | |
| 686 | if (!TextUtils.isEmpty(call.getChildNumber())) { |
| 687 | return mContext.getString(R.string.child_number, call.getChildNumber()); |
| 688 | } else if (!TextUtils.isEmpty(call.getCallSubject()) && call.isCallSubjectSupported()) { |
| 689 | return call.getCallSubject(); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | int resId = R.string.notification_ongoing_call; |
| 694 | if (call.hasProperty(Details.PROPERTY_WIFI)) { |
| 695 | resId = R.string.notification_ongoing_call_wifi; |
| 696 | } |
| 697 | |
| 698 | if (isIncomingOrWaiting) { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 699 | EnrichedCallManager manager = EnrichedCallComponent.get(mContext).getEnrichedCallManager(); |
| 700 | Session session = null; |
| 701 | if (call.getNumber() != null) { |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 702 | session = |
| 703 | manager.getSession( |
| 704 | call.getUniqueCallId(), |
| 705 | call.getNumber(), |
| 706 | manager.createIncomingCallComposerFilter()); |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (call.isSpam()) { |
| 710 | resId = R.string.notification_incoming_spam_call; |
| 711 | } else if (session != null) { |
| 712 | resId = getECIncomingCallText(session); |
| 713 | } else if (call.hasProperty(Details.PROPERTY_WIFI)) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 714 | resId = R.string.notification_incoming_call_wifi; |
| 715 | } else { |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 716 | resId = R.string.notification_incoming_call; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 717 | } |
| 718 | } else if (call.getState() == DialerCall.State.ONHOLD) { |
| 719 | resId = R.string.notification_on_hold; |
| 720 | } else if (DialerCall.State.isDialing(call.getState())) { |
| 721 | resId = R.string.notification_dialing; |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 722 | } else if (call.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 723 | == SessionModificationState.RECEIVED_UPGRADE_TO_VIDEO_REQUEST) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 724 | resId = R.string.notification_requesting_video_call; |
| 725 | } |
| 726 | |
| 727 | // Is the call placed through work connection service. |
| 728 | boolean isWorkCall = call.hasProperty(PROPERTY_ENTERPRISE_CALL); |
| 729 | if (userType == ContactsUtils.USER_TYPE_WORK || isWorkCall) { |
| 730 | resId = getWorkStringFromPersonalString(resId); |
| 731 | } |
| 732 | |
| 733 | return mContext.getString(resId); |
| 734 | } |
| 735 | |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 736 | private int getECIncomingCallText(Session session) { |
| 737 | int resId; |
| 738 | MultimediaData data = session.getMultimediaData(); |
| 739 | boolean hasImage = data.hasImageData(); |
| 740 | boolean hasSubject = !TextUtils.isEmpty(data.getText()); |
| 741 | boolean hasMap = data.getLocation() != null; |
| 742 | if (data.isImportant()) { |
| 743 | if (hasMap) { |
| 744 | if (hasImage) { |
| 745 | if (hasSubject) { |
| 746 | resId = R.string.important_notification_incoming_call_with_photo_message_location; |
| 747 | } else { |
| 748 | resId = R.string.important_notification_incoming_call_with_photo_location; |
| 749 | } |
| 750 | } else if (hasSubject) { |
| 751 | resId = R.string.important_notification_incoming_call_with_message_location; |
| 752 | } else { |
| 753 | resId = R.string.important_notification_incoming_call_with_location; |
| 754 | } |
| 755 | } else if (hasImage) { |
| 756 | if (hasSubject) { |
| 757 | resId = R.string.important_notification_incoming_call_with_photo_message; |
| 758 | } else { |
| 759 | resId = R.string.important_notification_incoming_call_with_photo; |
| 760 | } |
| 761 | } else { |
| 762 | resId = R.string.important_notification_incoming_call_with_message; |
| 763 | } |
| 764 | if (mContext.getString(resId).length() > 50) { |
| 765 | resId = R.string.important_notification_incoming_call_attachments; |
| 766 | } |
| 767 | } else { |
| 768 | if (hasMap) { |
| 769 | if (hasImage) { |
| 770 | if (hasSubject) { |
| 771 | resId = R.string.notification_incoming_call_with_photo_message_location; |
| 772 | } else { |
| 773 | resId = R.string.notification_incoming_call_with_photo_location; |
| 774 | } |
| 775 | } else if (hasSubject) { |
| 776 | resId = R.string.notification_incoming_call_with_message_location; |
| 777 | } else { |
| 778 | resId = R.string.notification_incoming_call_with_location; |
| 779 | } |
| 780 | } else if (hasImage) { |
| 781 | if (hasSubject) { |
| 782 | resId = R.string.notification_incoming_call_with_photo_message; |
| 783 | } else { |
| 784 | resId = R.string.notification_incoming_call_with_photo; |
| 785 | } |
| 786 | } else { |
| 787 | resId = R.string.notification_incoming_call_with_message; |
| 788 | } |
| 789 | } |
| 790 | if (mContext.getString(resId).length() > 50) { |
| 791 | resId = R.string.notification_incoming_call_attachments; |
| 792 | } |
| 793 | return resId; |
| 794 | } |
| 795 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 796 | /** Gets the most relevant call to display in the notification. */ |
| 797 | private DialerCall getCallToShow(CallList callList) { |
| 798 | if (callList == null) { |
| 799 | return null; |
| 800 | } |
| 801 | DialerCall call = callList.getIncomingCall(); |
| 802 | if (call == null) { |
| 803 | call = callList.getOutgoingCall(); |
| 804 | } |
| 805 | if (call == null) { |
| 806 | call = callList.getVideoUpgradeRequestCall(); |
| 807 | } |
| 808 | if (call == null) { |
| 809 | call = callList.getActiveOrBackgroundCall(); |
| 810 | } |
| 811 | return call; |
| 812 | } |
| 813 | |
| 814 | private Spannable getActionText(@StringRes int stringRes, @ColorRes int colorRes) { |
| 815 | Spannable spannable = new SpannableString(mContext.getText(stringRes)); |
| 816 | if (VERSION.SDK_INT >= VERSION_CODES.N_MR1) { |
| 817 | // This will only work for cases where the Notification.Builder has a fullscreen intent set |
| 818 | // Notification.Builder that does not have a full screen intent will take the color of the |
| 819 | // app and the following leads to a no-op. |
| 820 | spannable.setSpan( |
| 821 | new ForegroundColorSpan(mContext.getColor(colorRes)), 0, spannable.length(), 0); |
| 822 | } |
| 823 | return spannable; |
| 824 | } |
| 825 | |
| 826 | private void addAnswerAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 827 | LogUtil.d( |
| 828 | "StatusBarNotifier.addAnswerAction", |
| 829 | "will show \"answer\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 830 | PendingIntent answerVoicePendingIntent = |
| 831 | createNotificationPendingIntent(mContext, ACTION_ANSWER_VOICE_INCOMING_CALL); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 832 | // We put animation resources in "anim" folder instead of "drawable", which causes Android |
| 833 | // Studio to complain. |
| 834 | // TODO: Move "anim" resources to "drawable" as recommended in AnimationDrawable doc? |
| 835 | //noinspection ResourceType |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 836 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 837 | new Notification.Action.Builder( |
| 838 | Icon.createWithResource(mContext, R.anim.on_going_call), |
| 839 | getActionText( |
| 840 | R.string.notification_action_answer, R.color.notification_action_accept), |
| 841 | answerVoicePendingIntent) |
| 842 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | private void addDismissAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 846 | LogUtil.d( |
| 847 | "StatusBarNotifier.addDismissAction", |
| 848 | "will show \"decline\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 849 | PendingIntent declinePendingIntent = |
| 850 | createNotificationPendingIntent(mContext, ACTION_DECLINE_INCOMING_CALL); |
| 851 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 852 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 853 | Icon.createWithResource(mContext, R.drawable.quantum_ic_close_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 854 | getActionText( |
| 855 | R.string.notification_action_dismiss, R.color.notification_action_dismiss), |
| 856 | declinePendingIntent) |
| 857 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | private void addHangupAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 861 | LogUtil.d( |
| 862 | "StatusBarNotifier.addHangupAction", |
| 863 | "will show \"hang-up\" action in the ongoing active call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 864 | PendingIntent hangupPendingIntent = |
| 865 | createNotificationPendingIntent(mContext, ACTION_HANG_UP_ONGOING_CALL); |
| 866 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 867 | new Notification.Action.Builder( |
| 868 | Icon.createWithResource(mContext, R.drawable.ic_call_end_white_24dp), |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 869 | mContext.getText(R.string.notification_action_end_call), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 870 | hangupPendingIntent) |
| 871 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | private void addVideoCallAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 875 | LogUtil.i( |
| 876 | "StatusBarNotifier.addVideoCallAction", |
| 877 | "will show \"video\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 878 | PendingIntent answerVideoPendingIntent = |
| 879 | createNotificationPendingIntent(mContext, ACTION_ANSWER_VIDEO_INCOMING_CALL); |
| 880 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 881 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 882 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 883 | getActionText( |
| 884 | R.string.notification_action_answer_video, |
| 885 | R.color.notification_action_answer_video), |
| 886 | answerVideoPendingIntent) |
| 887 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | private void addAcceptUpgradeRequestAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 891 | LogUtil.i( |
| 892 | "StatusBarNotifier.addAcceptUpgradeRequestAction", |
| 893 | "will show \"accept upgrade\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 894 | PendingIntent acceptVideoPendingIntent = |
| 895 | createNotificationPendingIntent(mContext, ACTION_ACCEPT_VIDEO_UPGRADE_REQUEST); |
| 896 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 897 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 898 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 899 | getActionText( |
| 900 | R.string.notification_action_accept, R.color.notification_action_accept), |
| 901 | acceptVideoPendingIntent) |
| 902 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | private void addDismissUpgradeRequestAction(Notification.Builder builder) { |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 906 | LogUtil.i( |
| 907 | "StatusBarNotifier.addDismissUpgradeRequestAction", |
| 908 | "will show \"dismiss upgrade\" action in the incoming call Notification"); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 909 | PendingIntent declineVideoPendingIntent = |
| 910 | createNotificationPendingIntent(mContext, ACTION_DECLINE_VIDEO_UPGRADE_REQUEST); |
| 911 | builder.addAction( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 912 | new Notification.Action.Builder( |
Eric Erfanian | d8046e5 | 2017-04-06 09:41:50 -0700 | [diff] [blame] | 913 | Icon.createWithResource(mContext, R.drawable.quantum_ic_videocam_white_24), |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 914 | getActionText( |
| 915 | R.string.notification_action_dismiss, R.color.notification_action_dismiss), |
| 916 | declineVideoPendingIntent) |
| 917 | .build()); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /** Adds fullscreen intent to the builder. */ |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 921 | private void configureFullScreenIntent(Notification.Builder builder, PendingIntent intent) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 922 | // Ok, we actually want to launch the incoming call |
| 923 | // UI at this point (in addition to simply posting a notification |
| 924 | // to the status bar). Setting fullScreenIntent will cause |
| 925 | // the InCallScreen to be launched immediately *unless* the |
| 926 | // current foreground activity is marked as "immersive". |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 927 | LogUtil.d("StatusBarNotifier.configureFullScreenIntent", "setting fullScreenIntent: " + intent); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 928 | builder.setFullScreenIntent(intent, true); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | private Notification.Builder getNotificationBuilder() { |
| 932 | final Notification.Builder builder = new Notification.Builder(mContext); |
| 933 | builder.setOngoing(true); |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 934 | builder.setOnlyAlertOnce(true); |
Eric Erfanian | 10b34a5 | 2017-05-04 08:23:17 -0700 | [diff] [blame^] | 935 | // This will be ignored on O+ and handled by the channel |
| 936 | //noinspection deprecation |
| 937 | builder.setPriority(Notification.PRIORITY_HIGH); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 938 | |
| 939 | return builder; |
| 940 | } |
| 941 | |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 942 | private PendingIntent createLaunchPendingIntent(boolean isFullScreen) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 943 | Intent intent = |
| 944 | InCallActivity.getIntent( |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 945 | mContext, false /* showDialpad */, false /* newOutgoingCall */, isFullScreen); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 946 | |
| 947 | int requestCode = PENDING_INTENT_REQUEST_CODE_NON_FULL_SCREEN; |
| 948 | if (isFullScreen) { |
| 949 | // Use a unique request code so that the pending intent isn't clobbered by the |
| 950 | // non-full screen pending intent. |
| 951 | requestCode = PENDING_INTENT_REQUEST_CODE_FULL_SCREEN; |
| 952 | } |
| 953 | |
| 954 | // PendingIntent that can be used to launch the InCallActivity. The |
| 955 | // system fires off this intent if the user pulls down the windowshade |
| 956 | // and clicks the notification's expanded view. It's also used to |
| 957 | // launch the InCallActivity immediately when when there's an incoming |
| 958 | // call (see the "fullScreenIntent" field below). |
| 959 | return PendingIntent.getActivity(mContext, requestCode, intent, 0); |
| 960 | } |
| 961 | |
| 962 | private void setStatusBarCallListener(StatusBarCallListener listener) { |
| 963 | if (mStatusBarCallListener != null) { |
| 964 | mStatusBarCallListener.cleanup(); |
| 965 | } |
| 966 | mStatusBarCallListener = listener; |
| 967 | } |
| 968 | |
| 969 | private class StatusBarCallListener implements DialerCallListener { |
| 970 | |
| 971 | private DialerCall mDialerCall; |
| 972 | |
| 973 | StatusBarCallListener(DialerCall dialerCall) { |
| 974 | mDialerCall = dialerCall; |
| 975 | mDialerCall.addListener(this); |
| 976 | } |
| 977 | |
| 978 | void cleanup() { |
| 979 | mDialerCall.removeListener(this); |
| 980 | } |
| 981 | |
| 982 | @Override |
| 983 | public void onDialerCallDisconnect() {} |
| 984 | |
| 985 | @Override |
| 986 | public void onDialerCallUpdate() { |
| 987 | if (CallList.getInstance().getIncomingCall() == null) { |
| 988 | mDialerRingtoneManager.stopCallWaitingTone(); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | @Override |
| 993 | public void onDialerCallChildNumberChange() {} |
| 994 | |
| 995 | @Override |
| 996 | public void onDialerCallLastForwardedNumberChange() {} |
| 997 | |
| 998 | @Override |
| 999 | public void onDialerCallUpgradeToVideo() {} |
| 1000 | |
| 1001 | @Override |
| 1002 | public void onWiFiToLteHandover() {} |
| 1003 | |
| 1004 | @Override |
| 1005 | public void onHandoverToWifiFailure() {} |
| 1006 | |
| 1007 | /** |
| 1008 | * Responds to changes in the session modification state for the call by dismissing the status |
| 1009 | * bar notification as required. |
| 1010 | */ |
| 1011 | @Override |
Eric Erfanian | d5e47f6 | 2017-03-15 14:41:07 -0700 | [diff] [blame] | 1012 | public void onDialerCallSessionModificationStateChange() { |
| 1013 | if (mDialerCall.getVideoTech().getSessionModificationState() |
Eric Erfanian | 9050823 | 2017-03-24 09:31:16 -0700 | [diff] [blame] | 1014 | == SessionModificationState.NO_REQUEST) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1015 | cleanup(); |
| 1016 | updateNotification(CallList.getInstance()); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | } |