-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCallKit.java
More file actions
315 lines (249 loc) · 11.1 KB
/
CallKit.java
File metadata and controls
315 lines (249 loc) · 11.1 KB
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
package hu.taracque.ionic.plugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.os.Build;
import android.os.Vibrator;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.WindowManager;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.UUID;
public class CallKit extends CordovaPlugin {
public static final String TAG = "CallKit";
public static PowerManager powerManager;
public static PowerManager.WakeLock wakeLock;
private static Ringtone ringtone;
private static Vibrator vibrator;
private static String callName;
@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock((PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE), TAG);
Log.v(TAG, "Init CallKit");
}
@Override
public synchronized boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action == null) {
return false;
}
if (action.equals("register")) {
try {
this.register(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
else if (action.equals("reportIncomingCall")) {
try {
this.reportIncomingCall(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
else if (action.equals("startCall")) {
try {
this.startCall(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
else if (action.equals("callConnected")) {
try {
this.callConnected(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
else if (action.equals("endCall")) {
try {
this.endCall(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
else if (action.equals("finishRing")) {
try {
this.finishRing(args, callbackContext);
}
catch (Exception exception) {
callbackContext.error("CallKit uncaught exception: " + exception.getMessage());
}
return true;
}
return false;
}
private synchronized void register(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
/* initialize the ringtone */
Context ctx = cordova.getActivity().getBaseContext();
Uri ringtoneUri;
int ringtoneID = ctx.getResources().getIdentifier("ringtone","raw", ctx.getPackageName());
if (ringtoneID != 0) {
ringtoneUri = Uri.parse("android.resource://" + ctx.getPackageName() + "/" + ringtoneID);
} else {
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
ringtone = RingtoneManager.getRingtone(ctx, ringtoneUri);
if (Build.VERSION.SDK_INT >= 21) {
AudioAttributes aa = new AudioAttributes.Builder()
.setFlags(AudioAttributes.USAGE_NOTIFICATION_RINGTONE | AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST)
.build();
ringtone.setAudioAttributes(aa);
} else {
ringtone.setStreamType(RingtoneManager.TYPE_RINGTONE);
}
ringtone.stop();
callbackContext.success();
}
private synchronized void reportIncomingCall(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
callName = args.getString(0);
boolean hasVideo = args.getBoolean(1);
final String uuid = UUID.randomUUID().toString();
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String packageName = cordova.getActivity().getApplicationContext().getPackageName();
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(new ComponentName(packageName, packageName + ".MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
cordova.getActivity().getApplicationContext().startActivity(intent);
} catch (Exception e) {
Log.v(TAG, "CallKit error: " + e.getMessage());
}
cordova.getActivity().getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
);
}
});
if(wakeLock.isHeld()) {
wakeLock.release();
}
wakeLock.acquire();
try {
boolean vibrate = false;
Uri ringtoneUri;
AudioManager audioManager = (AudioManager) cordova.getActivity().getApplication().getSystemService(Context.AUDIO_SERVICE);
Context ctx = cordova.getActivity().getBaseContext();
if(ringtone.isPlaying()) {
ringtone.stop();
}
ringtone.play();
if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE){
vibrate = true;
} else if (1 == Settings.System.getInt(ctx.getContentResolver(), "vibrate_when_ringing", 0)) //vibrate on
vibrate = true;
vibrator = (Vibrator) ctx.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrate) {
vibrator.vibrate(new long[] {0, 1000, 1000}, 0);
}
} catch (Exception e) {
Log.v(TAG, "CallKit error: " + e.getMessage());
}
callbackContext.success(uuid);
}
private synchronized void callConnected(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String uuid = args.getString(0);
/* do nothing... */
callbackContext.success();
}
private synchronized void startCall(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
callName = args.getString(0);
boolean isVideo = args.getBoolean(1);
final String uuid = UUID.randomUUID().toString();
/* do nothing... */
callbackContext.success(uuid);
}
private void notifyUser(String uuid, String contentTitle) {
String appName;
ApplicationInfo app = null;
Context context = cordova.getActivity().getApplicationContext();
PackageManager packageManager = cordova.getActivity().getPackageManager();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try {
app = packageManager.getApplicationInfo(cordova.getActivity().getPackageName(), 0);
appName = (String)packageManager.getApplicationLabel(app);
} catch (PackageManager.NameNotFoundException e) {
appName = "Incoming";
e.printStackTrace();
}
boolean useDefaultTitle = contentTitle == null || contentTitle.isEmpty();
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setContentTitle(useDefaultTitle ? (appName + " call missed") : contentTitle)
.setContentText(callName)
.setSound(defaultSoundUri);
int resID = context.getResources().getIdentifier("callkit_missed_call", "drawable", cordova.getActivity().getPackageName());
if (resID != 0) {
notificationBuilder.setSmallIcon(resID);
} else {
notificationBuilder.setSmallIcon(app.icon);
}
notificationBuilder.setLargeIcon( BitmapFactory.decodeResource( context.getResources(), app.icon ) );
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, CallKitReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(uuid.hashCode(), notificationBuilder.build());
}
private synchronized void finishRing(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String uuid = args.getString(0);
if(ringtone.isPlaying()) {
ringtone.stop();
}
vibrator.cancel();
callbackContext.success();
}
private synchronized void endCall(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
String uuid = args.getString(0);
boolean notify = args.getBoolean(1);
String contentTitle = args.getString(2);
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
cordova.getActivity().getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
if(wakeLock.isHeld()) {
wakeLock.release();
}
finishRing(args,callbackContext);
if (notify) {
this.notifyUser(uuid, contentTitle);
}
callbackContext.success();
}
}