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