summaryrefslogtreecommitdiff
path: root/src/com/android/car/dialer/notification/InCallNotificationController.java
blob: 022ccbdf98a38d5c1355ddc8ae9a17e01ad720db (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
/*
 * 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.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.telecom.Call;
import android.text.TextUtils;

import androidx.annotation.StringRes;

import com.android.car.dialer.R;
import com.android.car.dialer.log.L;
import com.android.car.telephony.common.CallDetail;
import com.android.car.telephony.common.TelecomUtils;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.hilt.android.qualifiers.ApplicationContext;

/** Controller that manages the heads up notification for incoming calls. */
@Singleton
public final class InCallNotificationController {
    private static final String TAG = "CD.InCallNotificationController";
    private static final String CHANNEL_ID = "com.android.car.dialer.incoming";
    // A random number that is used for notification id.
    private static final int NOTIFICATION_ID = 20181105;

    private boolean mShowFullscreenIncallUi;

    private final Context mContext;
    private final NotificationManager mNotificationManager;
    private final Notification.Builder mNotificationBuilder;
    private final Set<String> mActiveInCallNotifications;
    private CompletableFuture<Void> mNotificationFuture;

    @Inject
    public InCallNotificationController(@ApplicationContext Context context) {
        mContext = context;

        mShowFullscreenIncallUi = mContext.getResources().getBoolean(
                R.bool.config_show_hun_fullscreen_incall_ui);
        mNotificationManager =
                (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        CharSequence name = mContext.getString(R.string.in_call_notification_channel_name);
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name,
                NotificationManager.IMPORTANCE_HIGH);
        mNotificationManager.createNotificationChannel(notificationChannel);

        mNotificationBuilder = new Notification.Builder(mContext, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_phone)
                .setColor(mContext.getColor(R.color.notification_app_icon_color))
                .setCategory(Notification.CATEGORY_CALL)
                .setOngoing(true)
                .setAutoCancel(false);

        mActiveInCallNotifications = new HashSet<>();
    }


    /** Show a new incoming call notification or update the existing incoming call notification. */
    public void showInCallNotification(Call call) {
        L.d(TAG, "showInCallNotification");

        if (mNotificationFuture != null) {
            mNotificationFuture.cancel(true);
        }

        CallDetail callDetail = CallDetail.fromTelecomCallDetail(call.getDetails());
        String callNumber = callDetail.getNumber();
        mActiveInCallNotifications.add(callNumber);

        if (mShowFullscreenIncallUi) {
            mNotificationBuilder.setFullScreenIntent(
                    getFullscreenIntent(call), /* highPriority= */true);
        }
        mNotificationBuilder
                .setLargeIcon((Icon) null)
                .setContentTitle(TelecomUtils.getBidiWrappedNumber(callNumber))
                .setContentText(mContext.getString(R.string.notification_incoming_call))
                .setActions(
                        getAction(call, R.string.answer_call,
                                NotificationService.ACTION_ANSWER_CALL),
                        getAction(call, R.string.decline_call,
                                NotificationService.ACTION_DECLINE_CALL));
        mNotificationManager.notify(
                callNumber,
                NOTIFICATION_ID,
                mNotificationBuilder.build());

        mNotificationFuture = NotificationUtils.getDisplayNameAndRoundedAvatar(mContext, callNumber)
                .thenAcceptAsync((pair) -> {
                    // Check that the notification hasn't already been dismissed
                    if (mActiveInCallNotifications.contains(callNumber)) {
                        mNotificationBuilder
                                .setLargeIcon(pair.second)
                                .setContentTitle(TelecomUtils.getBidiWrappedNumber(pair.first));

                        mNotificationManager.notify(
                                callNumber,
                                NOTIFICATION_ID,
                                mNotificationBuilder.build());
                    }
                }, mContext.getMainExecutor());
    }

    /** Cancel the incoming call notification for the given call. */
    public void cancelInCallNotification(Call call) {
        L.d(TAG, "cancelInCallNotification");
        if (call.getDetails() != null) {
            String callNumber = CallDetail.fromTelecomCallDetail(call.getDetails()).getNumber();
            cancelInCallNotification(callNumber);
        }
    }

    /**
     * Cancel the incoming call notification for the given call id. Any action that dismisses the
     * notification needs to call this explicitly.
     */
    void cancelInCallNotification(String callId) {
        if (TextUtils.isEmpty(callId)) {
            return;
        }
        mActiveInCallNotifications.remove(callId);
        mNotificationManager.cancel(callId, NOTIFICATION_ID);
    }

    private PendingIntent getFullscreenIntent(Call call) {
        Intent intent = getIntent(NotificationService.ACTION_SHOW_FULLSCREEN_UI, call);
        return PendingIntent.getService(mContext, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
    }

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

    private Intent getIntent(String action, Call call) {
        Intent intent = new Intent(action, null, mContext, NotificationService.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(NotificationService.EXTRA_PHONE_NUMBER,
                CallDetail.fromTelecomCallDetail(call.getDetails()).getNumber());
        return intent;
    }
}