summaryrefslogtreecommitdiffstats
path: root/src/com/android/car/dialer/notification/MissedCallNotificationController.java
blob: 5b0fd8648f6608054b52c5dd11caf995477a946d (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
/*
 * Copyright (C) 2019 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.car.dialer.notification;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.lifecycle.Observer;

import com.android.car.dialer.Constants;
import com.android.car.dialer.R;
import com.android.car.dialer.livedata.UnreadMissedCallLiveData;
import com.android.car.dialer.log.L;
import com.android.car.dialer.ui.TelecomActivity;
import com.android.car.dialer.ui.TelecomPageTab;
import com.android.car.telephony.common.PhoneCallLog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/** Controller that manages the missed call notifications. */
public final class MissedCallNotificationController {
    private static final String TAG = "CD.MissedCallNotification";
    private static final String CHANNEL_ID = "com.android.car.dialer.missedcall";
    // A random number that is used for notification id.
    private static final int NOTIFICATION_ID = 20190520;

    private static MissedCallNotificationController sMissedCallNotificationController;

    /**
     * Initialized a globally accessible {@link MissedCallNotificationController} which can be
     * retrieved by {@link #get}. If this function is called a second time before calling {@link
     * #tearDown()}, an {@link IllegalStateException} will be thrown.
     *
     * @param applicationContext Application context.
     */
    public static void init(Context applicationContext) {
        if (sMissedCallNotificationController == null) {
            sMissedCallNotificationController = new MissedCallNotificationController(
                    applicationContext);
        } else {
            throw new IllegalStateException(
                    "MissedCallNotificationController has been initialized.");
        }
    }

    /**
     * Gets the global {@link MissedCallNotificationController} instance. Make sure {@link
     * #init(Context)} is called before calling this method.
     */
    public static MissedCallNotificationController get() {
        if (sMissedCallNotificationController == null) {
            throw new IllegalStateException(
                    "Call MissedCallNotificationController.init(Context) before calling this "
                            + "function");
        }
        return sMissedCallNotificationController;
    }

    /** Tear down the global missed call notification controller. */
    public void tearDown() {
        mUnreadMissedCallLiveData.removeObserver(mUnreadMissedCallObserver);
        sMissedCallNotificationController = null;
    }

    private final Context mContext;
    private final NotificationManager mNotificationManager;
    private final UnreadMissedCallLiveData mUnreadMissedCallLiveData;
    private final Observer<List<PhoneCallLog>> mUnreadMissedCallObserver;
    private final List<PhoneCallLog> mCurrentPhoneCallLogList;
    private final Map<String, CompletableFuture<Void>> mUpdateFutures;

    @TargetApi(26)
    private MissedCallNotificationController(Context context) {
        mContext = context;
        mNotificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        CharSequence name = mContext.getString(R.string.missed_call_notification_channel_name);
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name,
                NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(notificationChannel);

        mCurrentPhoneCallLogList = new ArrayList<>();
        mUnreadMissedCallLiveData = UnreadMissedCallLiveData.newInstance(context);
        mUnreadMissedCallObserver = this::updateNotifications;
        mUnreadMissedCallLiveData.observeForever(mUnreadMissedCallObserver);

        mUpdateFutures = new HashMap<>();
    }

    /**
     * The phone call log list might be null when switching users if permission gets denied and
     * throws exception.
     */
    private void updateNotifications(@Nullable List<PhoneCallLog> phoneCallLogs) {
        List<PhoneCallLog> updatedPhoneCallLogs =
                phoneCallLogs == null ? Collections.emptyList() : phoneCallLogs;
        for (PhoneCallLog phoneCallLog : updatedPhoneCallLogs) {
            showMissedCallNotification(phoneCallLog);
            if (mCurrentPhoneCallLogList.contains(phoneCallLog)) {
                mCurrentPhoneCallLogList.remove(phoneCallLog);
            }
        }

        for (PhoneCallLog phoneCallLog : mCurrentPhoneCallLogList) {
            cancelMissedCallNotification(phoneCallLog);
        }
        mCurrentPhoneCallLogList.clear();
        mCurrentPhoneCallLogList.addAll(updatedPhoneCallLogs);
    }

    private void showMissedCallNotification(PhoneCallLog callLog) {
        L.d(TAG, "show missed call notification %s", callLog);
        String phoneNumber = callLog.getPhoneNumberString();
        String tag = getTag(callLog);
        CompletableFuture<Void> updateFuture = mUpdateFutures.get(tag);
        if (updateFuture != null) {
            updateFuture.cancel(true);
        }
        updateFuture = NotificationUtils.getDisplayNameAndRoundedAvatar(
                mContext, phoneNumber)
                .thenAcceptAsync((pair) -> {
                    int callLogSize = callLog.getAllCallRecords().size();
                    Notification.Builder builder = new Notification.Builder(mContext, CHANNEL_ID)
                            .setSmallIcon(R.drawable.ic_phone)
                            .setLargeIcon(pair.second)
                            .setContentTitle(mContext.getResources().getQuantityString(
                                    R.plurals.notification_missed_call, callLogSize, callLogSize))
                            .setContentText(pair.first)
                            .setContentIntent(getContentPendingIntent())
                            .setDeleteIntent(getDeleteIntent())
                            .setOnlyAlertOnce(true)
                            .setShowWhen(true)
                            .setWhen(callLog.getLastCallEndTimestamp())
                            .setAutoCancel(false);

                    if (!TextUtils.isEmpty(phoneNumber)) {
                        builder.addAction(getAction(phoneNumber, R.string.call_back,
                                NotificationService.ACTION_CALL_BACK_MISSED));
                        // TODO: add action button to send message
                    }

                    mNotificationManager.notify(
                            tag,
                            NOTIFICATION_ID,
                            builder.build());
                }, mContext.getMainExecutor());
        mUpdateFutures.put(tag, updateFuture);
    }

    private void cancelMissedCallNotification(PhoneCallLog phoneCallLog) {
        L.d(TAG, "cancel missed call notification %s", phoneCallLog);
        String tag = getTag(phoneCallLog);
        mNotificationManager.cancel(tag, NOTIFICATION_ID);
        mUpdateFutures.remove(tag);
    }

    private PendingIntent getContentPendingIntent() {
        Intent intent = new Intent(mContext, TelecomActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Constants.Intents.ACTION_SHOW_PAGE);
        intent.putExtra(Constants.Intents.EXTRA_SHOW_PAGE, TelecomPageTab.Page.CALL_HISTORY);
        intent.putExtra(Constants.Intents.EXTRA_ACTION_READ_MISSED, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    private PendingIntent getDeleteIntent() {
        Intent intent = new Intent(NotificationService.ACTION_READ_ALL_MISSED, null, mContext,
                NotificationReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                mContext,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        return pendingIntent;
    }

    private Notification.Action getAction(String phoneNumberString, @StringRes int actionText,
            String intentAction) {
        CharSequence text = mContext.getString(actionText);
        PendingIntent intent = PendingIntent.getBroadcast(
                mContext,
                0,
                getIntent(intentAction, phoneNumberString),
                PendingIntent.FLAG_UPDATE_CURRENT);
        return new Notification.Action.Builder(null, text, intent).build();
    }

    private Intent getIntent(String action, String phoneNumberString) {
        Intent intent = new Intent(action, null, mContext, NotificationReceiver.class);
        intent.putExtra(NotificationService.EXTRA_CALL_ID, phoneNumberString);
        return intent;
    }

    private String getTag(@NonNull PhoneCallLog phoneCallLog) {
        return String.valueOf(phoneCallLog.hashCode());
    }
}