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