1/*
2 * Copyright (C) 2016 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
17package com.android.dialer.app.calllog;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.support.v4.util.Pair;
23import com.android.dialer.common.LogUtil;
24import com.android.dialer.common.concurrent.DialerExecutors;
25import me.leolin.shortcutbadger.ShortcutBadger;
26
27/**
28 * Receives broadcasts that should trigger a refresh of the missed call notification. This includes
29 * both an explicit broadcast from Telecom and a reboot.
30 */
31public class MissedCallNotificationReceiver extends BroadcastReceiver {
32
33  //TODO: Use compat class for these methods.
34  public static final String ACTION_SHOW_MISSED_CALLS_NOTIFICATION =
35      "android.telecom.action.SHOW_MISSED_CALLS_NOTIFICATION";
36
37  public static final String EXTRA_NOTIFICATION_COUNT = "android.telecom.extra.NOTIFICATION_COUNT";
38
39  public static final String EXTRA_NOTIFICATION_PHONE_NUMBER =
40      "android.telecom.extra.NOTIFICATION_PHONE_NUMBER";
41
42  @Override
43  public void onReceive(Context context, Intent intent) {
44    String action = intent.getAction();
45    if (!ACTION_SHOW_MISSED_CALLS_NOTIFICATION.equals(action)) {
46      return;
47    }
48
49    int count =
50        intent.getIntExtra(
51            EXTRA_NOTIFICATION_COUNT, CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT);
52    String phoneNumber = intent.getStringExtra(EXTRA_NOTIFICATION_PHONE_NUMBER);
53
54    PendingResult pendingResult = goAsync();
55
56    DialerExecutors.createNonUiTaskBuilder(MissedCallNotifier.getIstance(context))
57        .onSuccess(
58            output -> {
59              LogUtil.i(
60                  "MissedCallNotificationReceiver.onReceive",
61                  "update missed call notifications successful");
62              updateBadgeCount(context, count);
63              pendingResult.finish();
64            })
65        .onFailure(
66            throwable -> {
67              LogUtil.i(
68                  "MissedCallNotificationReceiver.onReceive",
69                  "update missed call notifications failed");
70              pendingResult.finish();
71            })
72        .build()
73        .executeParallel(new Pair<>(count, phoneNumber));
74  }
75
76  private static void updateBadgeCount(Context context, int count) {
77    boolean success = ShortcutBadger.applyCount(context, count);
78    LogUtil.i(
79        "MissedCallNotificationReceiver.updateBadgeCount",
80        "update badge count: %d success: %b",
81        count,
82        success);
83  }
84}
85