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