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 
17 package com.android.dialer.app.calllog;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.support.v4.util.Pair;
23 import com.android.dialer.common.LogUtil;
24 import com.android.dialer.common.concurrent.DialerExecutorComponent;
25 import 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  */
31 public 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
onReceive(Context context, Intent intent)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     LogUtil.enterBlock("MissedCallNotificationReceiver.onReceive");
50 
51     int count =
52         intent.getIntExtra(
53             EXTRA_NOTIFICATION_COUNT, CallLogNotificationsService.UNKNOWN_MISSED_CALL_COUNT);
54     String phoneNumber = intent.getStringExtra(EXTRA_NOTIFICATION_PHONE_NUMBER);
55 
56     PendingResult pendingResult = goAsync();
57 
58     DialerExecutorComponent.get(context)
59         .dialerExecutorFactory()
60         .createNonUiTaskBuilder(MissedCallNotifier.getInstance(context))
61         .onSuccess(
62             output -> {
63               LogUtil.i(
64                   "MissedCallNotificationReceiver.onReceive",
65                   "update missed call notifications successful");
66               updateBadgeCount(context, count);
67               pendingResult.finish();
68             })
69         .onFailure(
70             throwable -> {
71               LogUtil.i(
72                   "MissedCallNotificationReceiver.onReceive",
73                   "update missed call notifications failed");
74               pendingResult.finish();
75             })
76         .build()
77         .executeParallel(new Pair<>(count, phoneNumber));
78   }
79 
updateBadgeCount(Context context, int count)80   private static void updateBadgeCount(Context context, int count) {
81     boolean success = ShortcutBadger.applyCount(context, count);
82     LogUtil.i(
83         "MissedCallNotificationReceiver.updateBadgeCount",
84         "update badge count: %d success: %b",
85         count,
86         success);
87   }
88 }
89