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