/
CallNotificationManager.java
357 lines (314 loc) · 17.4 KB
/
CallNotificationManager.java
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.hoxfon.react.RNTwilioVoice;
import android.app.ActivityManager;
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.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import androidx.core.app.NotificationCompat;
import android.util.Log;
import android.view.WindowManager;
import com.facebook.react.bridge.ReactApplicationContext;
import com.twilio.voice.CallInvite;
import com.twilio.voice.CancelledCallInvite;
import java.util.List;
import static android.content.Context.ACTIVITY_SERVICE;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.TAG;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_ANSWER_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_REJECT_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_HANGUP_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_INCOMING_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_MISSED_CALL;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_INVITE;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_CALL_NOTIFICATION_ID;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.NOTIFICATION_TYPE;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.CALL_SID_KEY;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.INCOMING_NOTIFICATION_PREFIX;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.MISSED_CALLS_GROUP;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.MISSED_CALLS_NOTIFICATION_ID;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.HANGUP_NOTIFICATION_ID;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.PREFERENCE_KEY;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.ACTION_CLEAR_MISSED_CALLS_COUNT;
import static com.hoxfon.react.RNTwilioVoice.TwilioVoiceModule.CLEAR_MISSED_CALLS_NOTIFICATION_ID;
public class CallNotificationManager {
private static final String VOICE_CHANNEL = "default";
private NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
public CallNotificationManager() {}
public int getApplicationImportance(ReactApplicationContext context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
if (activityManager == null) {
return 0;
}
List<ActivityManager.RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
if (processInfos == null) {
return 0;
}
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.processName.equals(context.getApplicationInfo().packageName)) {
return processInfo.importance;
}
}
return 0;
}
public Class getMainActivityClass(ReactApplicationContext context) {
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
public Intent getLaunchIntent(ReactApplicationContext context,
int notificationId,
CallInvite callInvite,
Boolean shouldStartNewTask,
int appImportance
) {
Intent launchIntent = new Intent(context, getMainActivityClass(context));
int launchFlag = Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP;
if (shouldStartNewTask || appImportance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
launchFlag = Intent.FLAG_ACTIVITY_NEW_TASK;
}
launchIntent.setAction(ACTION_INCOMING_CALL)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, notificationId)
.addFlags(
launchFlag +
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED +
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD +
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON +
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
);
if (callInvite != null) {
launchIntent.putExtra(INCOMING_CALL_INVITE, callInvite);
}
return launchIntent;
}
public void createIncomingCallNotification(ReactApplicationContext context,
CallInvite callInvite,
int notificationId,
Intent launchIntent)
{
if (BuildConfig.DEBUG) {
Log.d(TAG, "createIncomingCallNotification intent "+launchIntent.getFlags());
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
/*
* Pass the notification id and call sid to use as an identifier to cancel the
* notification later
*/
Bundle extras = new Bundle();
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, notificationId);
extras.putString(CALL_SID_KEY, callInvite.getCallSid());
extras.putString(NOTIFICATION_TYPE, ACTION_INCOMING_CALL);
/*
* Create the notification shown in the notification drawer
*/
initCallNotificationsChannel(notificationManager);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(context, VOICE_CHANNEL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setSmallIcon(R.drawable.ic_call_white_24dp)
.setContentTitle("Incoming call")
.setContentText(callInvite.getFrom() + " is calling")
.setOngoing(true)
.setAutoCancel(true)
.setExtras(extras)
.setFullScreenIntent(pendingIntent, true);
// build notification large icon
Resources res = context.getResources();
int largeIconResId = res.getIdentifier("ic_launcher", "mipmap", context.getPackageName());
Bitmap largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (largeIconResId != 0) {
notificationBuilder.setLargeIcon(largeIconBitmap);
}
}
// Reject action
Intent rejectIntent = new Intent(ACTION_REJECT_CALL)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, notificationId)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingRejectIntent = PendingIntent.getBroadcast(context, 1, rejectIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(0, "DISMISS", pendingRejectIntent);
// Answer action
Intent answerIntent = new Intent(ACTION_ANSWER_CALL);
answerIntent
.putExtra(INCOMING_CALL_NOTIFICATION_ID, notificationId)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingAnswerIntent = PendingIntent.getBroadcast(context, 0, answerIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_call_white_24dp, "ANSWER", pendingAnswerIntent);
notificationManager.notify(notificationId, notificationBuilder.build());
TwilioVoiceModule.callNotificationMap.put(INCOMING_NOTIFICATION_PREFIX+callInvite.getCallSid(), notificationId);
}
public void initCallNotificationsChannel(NotificationManager notificationManager) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationChannel channel = new NotificationChannel(VOICE_CHANNEL,
"Primary Voice Channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.setLightColor(Color.GREEN);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
}
public void createMissedCallNotification(ReactApplicationContext context, CallInvite callInvite) {
SharedPreferences sharedPref = context.getSharedPreferences(PREFERENCE_KEY, Context.MODE_PRIVATE);
SharedPreferences.Editor sharedPrefEditor = sharedPref.edit();
/*
* Create a PendingIntent to specify the action when the notification is
* selected in the notification drawer
*/
Intent intent = new Intent(context, getMainActivityClass(context));
intent.setAction(ACTION_MISSED_CALL)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent clearMissedCallsCountIntent = new Intent(ACTION_CLEAR_MISSED_CALLS_COUNT)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, CLEAR_MISSED_CALLS_NOTIFICATION_ID);
PendingIntent clearMissedCallsCountPendingIntent = PendingIntent.getBroadcast(context, 0, clearMissedCallsCountIntent, 0);
/*
* Pass the notification id and call sid to use as an identifier to open the notification
*/
Bundle extras = new Bundle();
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, MISSED_CALLS_NOTIFICATION_ID);
extras.putString(CALL_SID_KEY, callInvite.getCallSid());
extras.putString(NOTIFICATION_TYPE, ACTION_MISSED_CALL);
/*
* Create the notification shown in the notification drawer
*/
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, VOICE_CHANNEL)
.setGroup(MISSED_CALLS_GROUP)
.setGroupSummary(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_call_missed_white_24dp)
.setContentTitle("Missed call")
.setContentText(callInvite.getFrom() + " called")
.setAutoCancel(true)
.setShowWhen(true)
.setExtras(extras)
.setDeleteIntent(clearMissedCallsCountPendingIntent)
.setContentIntent(pendingIntent);
int missedCalls = sharedPref.getInt(MISSED_CALLS_GROUP, 0);
missedCalls++;
if (missedCalls == 1) {
inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle("Missed call");
} else {
inboxStyle.setBigContentTitle(String.valueOf(missedCalls) + " missed calls");
}
inboxStyle.addLine("from: " +callInvite.getFrom());
sharedPrefEditor.putInt(MISSED_CALLS_GROUP, missedCalls);
sharedPrefEditor.commit();
notification.setStyle(inboxStyle);
// build notification large icon
Resources res = context.getResources();
int largeIconResId = res.getIdentifier("ic_launcher", "mipmap", context.getPackageName());
Bitmap largeIconBitmap = BitmapFactory.decodeResource(res, largeIconResId);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && largeIconResId != 0) {
notification.setLargeIcon(largeIconBitmap);
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MISSED_CALLS_NOTIFICATION_ID, notification.build());
}
public void createHangupLocalNotification(ReactApplicationContext context, String callSid, String caller) {
PendingIntent pendingHangupIntent = PendingIntent.getBroadcast(
context,
0,
new Intent(ACTION_HANGUP_CALL).putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID),
PendingIntent.FLAG_UPDATE_CURRENT
);
Intent launchIntent = new Intent(context, getMainActivityClass(context));
launchIntent.setAction(ACTION_INCOMING_CALL)
.putExtra(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/*
* Pass the notification id and call sid to use as an identifier to cancel the
* notification later
*/
Bundle extras = new Bundle();
extras.putInt(INCOMING_CALL_NOTIFICATION_ID, HANGUP_NOTIFICATION_ID);
extras.putString(CALL_SID_KEY, callSid);
extras.putString(NOTIFICATION_TYPE, ACTION_HANGUP_CALL);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, VOICE_CHANNEL)
.setContentTitle("Call in progress")
.setContentText(caller)
.setSmallIcon(R.drawable.ic_call_white_24dp)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_CALL)
.setOngoing(true)
.setUsesChronometer(true)
.setExtras(extras)
.setContentIntent(activityPendingIntent);
notification.addAction(0, "HANG UP", pendingHangupIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create notifications channel (required for API > 25)
initCallNotificationsChannel(notificationManager);
notificationManager.notify(HANGUP_NOTIFICATION_ID, notification.build());
}
public void removeIncomingCallNotification(ReactApplicationContext context,
CancelledCallInvite callInvite,
int notificationId) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "removeIncomingCallNotification");
}
if (context == null) {
Log.e(TAG, "Context is null");
return;
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (callInvite != null) {
/*
* If the incoming call message was cancelled then remove the notification by matching
* it with the call sid from the list of notifications in the notification drawer.
*/
StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();
for (StatusBarNotification statusBarNotification : activeNotifications) {
Notification notification = statusBarNotification.getNotification();
String notificationType = notification.extras.getString(NOTIFICATION_TYPE);
if (callInvite.getCallSid().equals(notification.extras.getString(CALL_SID_KEY)) &&
notificationType != null && notificationType.equals(ACTION_INCOMING_CALL)) {
notificationManager.cancel(notification.extras.getInt(INCOMING_CALL_NOTIFICATION_ID));
}
}
} else if (notificationId != 0) {
notificationManager.cancel(notificationId);
}
} else {
if (notificationId != 0) {
notificationManager.cancel(notificationId);
} else if (callInvite != null) {
String notificationKey = INCOMING_NOTIFICATION_PREFIX+callInvite.getCallSid();
if (TwilioVoiceModule.callNotificationMap.containsKey(notificationKey)) {
notificationId = TwilioVoiceModule.callNotificationMap.get(notificationKey);
notificationManager.cancel(notificationId);
TwilioVoiceModule.callNotificationMap.remove(notificationKey);
}
}
}
}
public void removeHangupNotification(ReactApplicationContext context) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(HANGUP_NOTIFICATION_ID);
}
}