summaryrefslogtreecommitdiffstats
path: root/java/com/android/incallui/ExternalCallNotifier.java
blob: f01a29458b15c9d6ed7ad50f9d23c43bf039de7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.incallui;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.os.BuildCompat;
import android.telecom.Call;
import android.telecom.PhoneAccount;
import android.telecom.VideoProfile;
import android.text.BidiFormatter;
import android.text.TextDirectionHeuristics;
import android.text.TextUtils;
import android.util.ArrayMap;
import com.android.contacts.common.ContactsUtils;
import com.android.contacts.common.compat.CallCompat;
import com.android.contacts.common.preference.ContactsPreferences;
import com.android.contacts.common.util.BitmapUtil;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.dialer.notification.NotificationChannelId;
import com.android.incallui.call.DialerCall;
import com.android.incallui.call.DialerCallDelegate;
import com.android.incallui.call.ExternalCallList;
import com.android.incallui.latencyreport.LatencyReport;
import com.android.incallui.util.TelecomCallUtil;
import java.util.Map;

/**
 * Handles the display of notifications for "external calls".
 *
 * <p>External calls are a representation of a call which is in progress on the user's other device
 * (e.g. another phone, or a watch).
 */
public class ExternalCallNotifier implements ExternalCallList.ExternalCallListener {

  /** Tag used with the notification manager to uniquely identify external call notifications. */
  private static final String NOTIFICATION_TAG = "EXTERNAL_CALL";

  private static final int NOTIFICATION_SUMMARY_ID = -1;

  private final Context mContext;
  private final ContactInfoCache mContactInfoCache;
  private Map<Call, NotificationInfo> mNotifications = new ArrayMap<>();
  private int mNextUniqueNotificationId;
  private ContactsPreferences mContactsPreferences;
  private boolean mShowingSummary;

  /** Initializes a new instance of the external call notifier. */
  public ExternalCallNotifier(
      @NonNull Context context, @NonNull ContactInfoCache contactInfoCache) {
    mContext = context;
    mContactsPreferences = ContactsPreferencesFactory.newContactsPreferences(mContext);
    mContactInfoCache = contactInfoCache;
  }

  /**
   * Handles the addition of a new external call by showing a new notification. Triggered by {@link
   * CallList#onCallAdded(android.telecom.Call)}.
   */
  @Override
  public void onExternalCallAdded(android.telecom.Call call) {
    Log.i(this, "onExternalCallAdded " + call);
    if (mNotifications.containsKey(call)) {
      throw new IllegalArgumentException();
    }
    NotificationInfo info = new NotificationInfo(call, mNextUniqueNotificationId++);
    mNotifications.put(call, info);

    showNotifcation(info);
  }

  /**
   * Handles the removal of an external call by hiding its associated notification. Triggered by
   * {@link CallList#onCallRemoved(android.telecom.Call)}.
   */
  @Override
  public void onExternalCallRemoved(android.telecom.Call call) {
    Log.i(this, "onExternalCallRemoved " + call);

    dismissNotification(call);
  }

  /** Handles updates to an external call. */
  @Override
  public void onExternalCallUpdated(Call call) {
    if (!mNotifications.containsKey(call)) {
      throw new IllegalArgumentException();
    }
    postNotification(mNotifications.get(call));
  }

  @Override
  public void onExternalCallPulled(Call call) {
    // no-op; if an external call is pulled, it will be removed via onExternalCallRemoved.
  }

  /**
   * Initiates a call pull given a notification ID.
   *
   * @param notificationId The notification ID associated with the external call which is to be
   *     pulled.
   */
  @TargetApi(VERSION_CODES.N_MR1)
  public void pullExternalCall(int notificationId) {
    for (NotificationInfo info : mNotifications.values()) {
      if (info.getNotificationId() == notificationId
          && CallCompat.canPullExternalCall(info.getCall())) {
        info.getCall().pullExternalCall();
        return;
      }
    }
  }

  /**
   * Shows a notification for a new external call. Performs a contact cache lookup to find any
   * associated photo and information for the call.
   */
  private void showNotifcation(final NotificationInfo info) {
    // We make a call to the contact info cache to query for supplemental data to what the
    // call provides.  This includes the contact name and photo.
    // This callback will always get called immediately and synchronously with whatever data
    // it has available, and may make a subsequent call later (same thread) if it had to
    // call into the contacts provider for more data.
    DialerCall dialerCall =
        new DialerCall(
            mContext,
            new DialerCallDelegateStub(),
            info.getCall(),
            new LatencyReport(),
            false /* registerCallback */);

    mContactInfoCache.findInfo(
        dialerCall,
        false /* isIncoming */,
        new ContactInfoCache.ContactInfoCacheCallback() {
          @Override
          public void onContactInfoComplete(
              String callId, ContactInfoCache.ContactCacheEntry entry) {

            // Ensure notification still exists as the external call could have been
            // removed during async contact info lookup.
            if (mNotifications.containsKey(info.getCall())) {
              saveContactInfo(info, entry);
            }
          }

          @Override
          public void onImageLoadComplete(String callId, ContactInfoCache.ContactCacheEntry entry) {

            // Ensure notification still exists as the external call could have been
            // removed during async contact info lookup.
            if (mNotifications.containsKey(info.getCall())) {
              savePhoto(info, entry);
            }
          }
        });
  }

  /** Dismisses a notification for an external call. */
  private void dismissNotification(Call call) {
    if (!mNotifications.containsKey(call)) {
      throw new IllegalArgumentException();
    }

    NotificationManager notificationManager =
        (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_TAG, mNotifications.get(call).getNotificationId());

    mNotifications.remove(call);

    if (mShowingSummary && mNotifications.size() <= 1) {
      // Where a summary notification is showing and there is now not enough notifications to
      // necessitate a summary, cancel the summary.
      notificationManager.cancel(NOTIFICATION_TAG, NOTIFICATION_SUMMARY_ID);
      mShowingSummary = false;

      // If there is still a single call requiring a notification, re-post the notification as a
      // standalone notification without a summary notification.
      if (mNotifications.size() == 1) {
        postNotification(mNotifications.values().iterator().next());
      }
    }
  }

  /**
   * Attempts to build a large icon to use for the notification based on the contact info and post
   * the updated notification to the notification manager.
   */
  private void savePhoto(NotificationInfo info, ContactInfoCache.ContactCacheEntry entry) {
    Bitmap largeIcon = getLargeIconToDisplay(mContext, entry, info.getCall());
    if (largeIcon != null) {
      largeIcon = getRoundedIcon(mContext, largeIcon);
    }
    info.setLargeIcon(largeIcon);
    postNotification(info);
  }

  /**
   * Builds and stores the contact information the notification will display and posts the updated
   * notification to the notification manager.
   */
  private void saveContactInfo(NotificationInfo info, ContactInfoCache.ContactCacheEntry entry) {
    info.setContentTitle(getContentTitle(mContext, mContactsPreferences, entry, info.getCall()));
    info.setPersonReference(getPersonReference(entry, info.getCall()));
    postNotification(info);
  }

  /** Rebuild an existing or show a new notification given {@link NotificationInfo}. */
  private void postNotification(NotificationInfo info) {
    Notification.Builder builder = new Notification.Builder(mContext);
    // Set notification as ongoing since calls are long-running versus a point-in-time notice.
    builder.setOngoing(true);
    // Make the notification prioritized over the other normal notifications.
    builder.setPriority(Notification.PRIORITY_HIGH);
    builder.setGroup(NOTIFICATION_TAG);

    boolean isVideoCall = VideoProfile.isVideo(info.getCall().getDetails().getVideoState());
    // Set the content ("Ongoing call on another device")
    builder.setContentText(
        mContext.getString(
            isVideoCall
                ? R.string.notification_external_video_call
                : R.string.notification_external_call));
    builder.setSmallIcon(R.drawable.quantum_ic_call_white_24);
    builder.setContentTitle(info.getContentTitle());
    builder.setLargeIcon(info.getLargeIcon());
    builder.setColor(mContext.getResources().getColor(R.color.dialer_theme_color));
    builder.addPerson(info.getPersonReference());
    if (BuildCompat.isAtLeastO()) {
      builder.setChannelId(NotificationChannelId.DEFAULT);
    }

    // Where the external call supports being transferred to the local device, add an action
    // to the notification to initiate the call pull process.
    if (CallCompat.canPullExternalCall(info.getCall())) {

      Intent intent =
          new Intent(
              NotificationBroadcastReceiver.ACTION_PULL_EXTERNAL_CALL,
              null,
              mContext,
              NotificationBroadcastReceiver.class);
      intent.putExtra(
          NotificationBroadcastReceiver.EXTRA_NOTIFICATION_ID, info.getNotificationId());
      builder.addAction(
          new Notification.Action.Builder(
                  R.drawable.quantum_ic_call_white_24,
                  mContext.getString(
                      isVideoCall
                          ? R.string.notification_take_video_call
                          : R.string.notification_take_call),
                  PendingIntent.getBroadcast(mContext, info.getNotificationId(), intent, 0))
              .build());
    }

    /**
     * This builder is used for the notification shown when the device is locked and the user has
     * set their notification settings to 'hide sensitive content' {@see
     * Notification.Builder#setPublicVersion}.
     */
    Notification.Builder publicBuilder = new Notification.Builder(mContext);
    publicBuilder.setSmallIcon(R.drawable.quantum_ic_call_white_24);
    publicBuilder.setColor(mContext.getResources().getColor(R.color.dialer_theme_color));
    if (BuildCompat.isAtLeastO()) {
      publicBuilder.setChannelId(NotificationChannelId.DEFAULT);
    }

    builder.setPublicVersion(publicBuilder.build());
    Notification notification = builder.build();

    NotificationManager notificationManager =
        (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_TAG, info.getNotificationId(), notification);

    if (!mShowingSummary && mNotifications.size() > 1) {
      // If the number of notifications shown is > 1, and we're not already showing a group summary,
      // build one now.  This will ensure the like notifications are grouped together.

      Notification.Builder summary = new Notification.Builder(mContext);
      // Set notification as ongoing since calls are long-running versus a point-in-time notice.
      summary.setOngoing(true);
      // Make the notification prioritized over the other normal notifications.
      summary.setPriority(Notification.PRIORITY_HIGH);
      summary.setGroup(NOTIFICATION_TAG);
      summary.setGroupSummary(true);
      summary.setSmallIcon(R.drawable.quantum_ic_call_white_24);
      if (BuildCompat.isAtLeastO()) {
        summary.setChannelId(NotificationChannelId.DEFAULT);
      }
      notificationManager.notify(NOTIFICATION_TAG, NOTIFICATION_SUMMARY_ID, summary.build());
      mShowingSummary = true;
    }
  }

  /**
   * Finds a large icon to display in a notification for a call. For conference calls, a conference
   * call icon is used, otherwise if contact info is specified, the user's contact photo or avatar
   * is used.
   *
   * @param context The context.
   * @param contactInfo The contact cache info.
   * @param call The call.
   * @return The large icon to use for the notification.
   */
  private @Nullable Bitmap getLargeIconToDisplay(
      Context context, ContactInfoCache.ContactCacheEntry contactInfo, android.telecom.Call call) {

    Bitmap largeIcon = null;
    if (call.getDetails().hasProperty(android.telecom.Call.Details.PROPERTY_CONFERENCE)
        && !call.getDetails()
            .hasProperty(android.telecom.Call.Details.PROPERTY_GENERIC_CONFERENCE)) {

      largeIcon =
          BitmapFactory.decodeResource(
              context.getResources(), R.drawable.quantum_ic_group_vd_theme_24);
    }
    if (contactInfo.photo != null && (contactInfo.photo instanceof BitmapDrawable)) {
      largeIcon = ((BitmapDrawable) contactInfo.photo).getBitmap();
    }
    return largeIcon;
  }

  /**
   * Given a bitmap, returns a rounded version of the icon suitable for display in a notification.
   *
   * @param context The context.
   * @param bitmap The bitmap to round.
   * @return The rounded bitmap.
   */
  private @Nullable Bitmap getRoundedIcon(Context context, @Nullable Bitmap bitmap) {
    if (bitmap == null) {
      return null;
    }
    final int height =
        (int) context.getResources().getDimension(android.R.dimen.notification_large_icon_height);
    final int width =
        (int) context.getResources().getDimension(android.R.dimen.notification_large_icon_width);
    return BitmapUtil.getRoundedBitmap(bitmap, width, height);
  }

  /**
   * Builds a notification content title for a call. If the call is a conference call, it is
   * identified as such. Otherwise an attempt is made to show an associated contact name or phone
   * number.
   *
   * @param context The context.
   * @param contactsPreferences Contacts preferences, used to determine the preferred formatting for
   *     contact names.
   * @param contactInfo The contact info which was looked up in the contact cache.
   * @param call The call to generate a title for.
   * @return The content title.
   */
  private @Nullable String getContentTitle(
      Context context,
      @Nullable ContactsPreferences contactsPreferences,
      ContactInfoCache.ContactCacheEntry contactInfo,
      android.telecom.Call call) {

    if (call.getDetails().hasProperty(android.telecom.Call.Details.PROPERTY_CONFERENCE)) {
      return CallerInfoUtils.getConferenceString(
          context,
          call.getDetails().hasProperty(android.telecom.Call.Details.PROPERTY_GENERIC_CONFERENCE));
    }

    String preferredName =
        ContactDisplayUtils.getPreferredDisplayName(
            contactInfo.namePrimary, contactInfo.nameAlternative, contactsPreferences);
    if (TextUtils.isEmpty(preferredName)) {
      return TextUtils.isEmpty(contactInfo.number)
          ? null
          : BidiFormatter.getInstance()
              .unicodeWrap(contactInfo.number, TextDirectionHeuristics.LTR);
    }
    return preferredName;
  }

  /**
   * Gets a "person reference" for a notification, used by the system to determine whether the
   * notification should be allowed past notification interruption filters.
   *
   * @param contactInfo The contact info from cache.
   * @param call The call.
   * @return the person reference.
   */
  private String getPersonReference(ContactInfoCache.ContactCacheEntry contactInfo, Call call) {

    String number = TelecomCallUtil.getNumber(call);
    // Query {@link Contacts#CONTENT_LOOKUP_URI} directly with work lookup key is not allowed.
    // So, do not pass {@link Contacts#CONTENT_LOOKUP_URI} to NotificationManager to avoid
    // NotificationManager using it.
    if (contactInfo.lookupUri != null && contactInfo.userType != ContactsUtils.USER_TYPE_WORK) {
      return contactInfo.lookupUri.toString();
    } else if (!TextUtils.isEmpty(number)) {
      return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null).toString();
    }
    return "";
  }

  private static class DialerCallDelegateStub implements DialerCallDelegate {

    @Override
    public DialerCall getDialerCallFromTelecomCall(Call telecomCall) {
      return null;
    }
  }

  /** Represents a call and associated cached notification data. */
  private static class NotificationInfo {

    @NonNull private final Call mCall;
    private final int mNotificationId;
    @Nullable private String mContentTitle;
    @Nullable private Bitmap mLargeIcon;
    @Nullable private String mPersonReference;

    public NotificationInfo(@NonNull Call call, int notificationId) {
      mCall = call;
      mNotificationId = notificationId;
    }

    public Call getCall() {
      return mCall;
    }

    public int getNotificationId() {
      return mNotificationId;
    }

    public @Nullable String getContentTitle() {
      return mContentTitle;
    }

    public void setContentTitle(@Nullable String contentTitle) {
      mContentTitle = contentTitle;
    }

    public @Nullable Bitmap getLargeIcon() {
      return mLargeIcon;
    }

    public void setLargeIcon(@Nullable Bitmap largeIcon) {
      mLargeIcon = largeIcon;
    }

    public @Nullable String getPersonReference() {
      return mPersonReference;
    }

    public void setPersonReference(@Nullable String personReference) {
      mPersonReference = personReference;
    }
  }
}