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 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 DialerExecutorComponent.get(context) 57 .dialerExecutorFactory() 58 .createNonUiTaskBuilder(MissedCallNotifier.getInstance(context)) 59 .onSuccess( 60 output -> { 61 LogUtil.i( 62 "MissedCallNotificationReceiver.onReceive", 63 "update missed call notifications successful"); 64 updateBadgeCount(context, count); 65 pendingResult.finish(); 66 }) 67 .onFailure( 68 throwable -> { 69 LogUtil.i( 70 "MissedCallNotificationReceiver.onReceive", 71 "update missed call notifications failed"); 72 pendingResult.finish(); 73 }) 74 .build() 75 .executeParallel(new Pair<>(count, phoneNumber)); 76 } 77 updateBadgeCount(Context context, int count)78 private static void updateBadgeCount(Context context, int count) { 79 boolean success = ShortcutBadger.applyCount(context, count); 80 LogUtil.i( 81 "MissedCallNotificationReceiver.updateBadgeCount", 82 "update badge count: %d success: %b", 83 count, 84 success); 85 } 86 } 87