-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
TelephonyPlugin.java
321 lines (269 loc) · 10.9 KB
/
TelephonyPlugin.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
/*
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Plugins.TelephonyPlugin;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import org.apache.commons.lang3.ArrayUtils;
import org.kde.kdeconnect.Helpers.ContactsHelper;
import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect.Plugins.PluginFactory;
import org.kde.kdeconnect_tp.R;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import androidx.core.content.ContextCompat;
@PluginFactory.LoadablePlugin
public class TelephonyPlugin extends Plugin {
/**
* Packet used for simple call events
* <p>
* It contains the key "event" which maps to a string indicating the type of event:
* - "ringing" - A phone call is incoming
* - "missedCall" - An incoming call was not answered
* - "sms" - An incoming SMS message
* - Note: As of this writing (15 May 2018) the SMS interface is being improved and this type of event
* is no longer the preferred way of handling SMS. Use the packets defined by the SMS plugin instead.
* <p>
* Depending on the event, other fields may be defined
*/
public final static String PACKET_TYPE_TELEPHONY = "kdeconnect.telephony";
/**
* Old-style packet sent to request a simple telephony action
* <p>
* The events handled were:
* - to request the device to mute its ringer
* - to request an SMS to be sent.
* <p>
* In case an SMS was being requested, the body was like so:
* { "sendSms": true,
* "phoneNumber": "542904563213",
* "messageBody": "Hi mom!"
* }
* <p>
* In case a ringer muted was requested, the body looked like so:
* { "action": "mute" }
* <p>
* Ringer mute requests are best handled by PACKET_TYPE_TELEPHONY_REQUEST_MUTE
* <p>
* This packet type is retained for backwards-compatibility with old desktop applications,
* but support should be dropped once those applications are no longer supported. New
* applications should not use this packet type.
*/
@Deprecated
public final static String PACKET_TYPE_TELEPHONY_REQUEST = "kdeconnect.telephony.request";
/**
* Packet sent to indicate the user has requested the device mute its ringer
* <p>
* The body should be empty
*/
private final static String PACKET_TYPE_TELEPHONY_REQUEST_MUTE = "kdeconnect.telephony.request_mute";
private static final String KEY_PREF_BLOCKED_NUMBERS = "telephony_blocked_numbers";
private int lastState = TelephonyManager.CALL_STATE_IDLE;
private NetworkPacket lastPacket = null;
private boolean isMuted = false;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Log.e("TelephonyPlugin","Telephony event: " + action);
if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
int intState = TelephonyManager.CALL_STATE_IDLE;
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
intState = TelephonyManager.CALL_STATE_RINGING;
else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
intState = TelephonyManager.CALL_STATE_OFFHOOK;
// We will get a second broadcast with the phone number https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED
if (!intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER))
return;
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (intState != lastState) {
lastState = intState;
callBroadcastReceived(intState, number);
}
}
}
};
@Override
public String getDisplayName() {
return context.getResources().getString(R.string.pref_plugin_telephony);
}
@Override
public String getDescription() {
return context.getResources().getString(R.string.pref_plugin_telephony_desc);
}
private void callBroadcastReceived(int state, String phoneNumber) {
if (isNumberBlocked(phoneNumber))
return;
NetworkPacket np = new NetworkPacket(PACKET_TYPE_TELEPHONY);
int permissionCheck = ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_CONTACTS);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
Map<String, String> contactInfo = ContactsHelper.phoneNumberLookup(context, phoneNumber);
if (contactInfo.containsKey("name")) {
np.set("contactName", contactInfo.get("name"));
}
if (contactInfo.containsKey("photoID")) {
String photoUri = contactInfo.get("photoID");
if (photoUri != null) {
try {
String base64photo = ContactsHelper.photoId64Encoded(context, photoUri);
if (!TextUtils.isEmpty(base64photo)) {
np.set("phoneThumbnail", base64photo);
}
} catch (Exception e) {
Log.e("TelephonyPlugin", "Failed to get contact photo");
}
}
}
} else {
np.set("contactName", phoneNumber);
}
if (phoneNumber != null) {
np.set("phoneNumber", phoneNumber);
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
unmuteRinger();
np.set("event", "ringing");
device.sendPacket(np);
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //Ongoing call
np.set("event", "talking");
device.sendPacket(np);
break;
case TelephonyManager.CALL_STATE_IDLE:
if (lastPacket != null) {
//Resend a cancel of the last event (can either be "ringing" or "talking")
lastPacket.set("isCancel", "true");
device.sendPacket(lastPacket);
if (isMuted) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
unmuteRinger();
}
}, 500);
}
//Emit a missed call notification if needed
if ("ringing".equals(lastPacket.getString("event", null))) {
np.set("event", "missedCall");
np.set("phoneNumber", lastPacket.getString("phoneNumber", null));
np.set("contactName", lastPacket.getString("contactName", null));
device.sendPacket(np);
}
}
break;
}
lastPacket = np;
}
private void unmuteRinger() {
if (isMuted) {
AudioManager am = ContextCompat.getSystemService(context, AudioManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_UNMUTE, 0);
} else {
am.setStreamMute(AudioManager.STREAM_RING, false);
}
isMuted = false;
}
}
private void muteRinger() {
if (!isMuted) {
AudioManager am = ContextCompat.getSystemService(context, AudioManager.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0);
} else {
am.setStreamMute(AudioManager.STREAM_RING, true);
}
isMuted = true;
}
}
@Override
public boolean onCreate() {
IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
filter.setPriority(500);
context.registerReceiver(receiver, filter);
permissionExplanation = R.string.telephony_permission_explanation;
optionalPermissionExplanation = R.string.telephony_optional_permission_explanation;
return true;
}
@Override
public void onDestroy() {
context.unregisterReceiver(receiver);
}
@Override
public boolean onPacketReceived(NetworkPacket np) {
switch (np.getType()) {
case PACKET_TYPE_TELEPHONY_REQUEST:
if (np.getString("action").equals("mute")) {
muteRinger();
}
break;
case PACKET_TYPE_TELEPHONY_REQUEST_MUTE:
muteRinger();
break;
}
return true;
}
private boolean isNumberBlocked(String number) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
String[] blockedNumbers = sharedPref.getString(KEY_PREF_BLOCKED_NUMBERS, "").split("\n");
for (String s : blockedNumbers) {
if (PhoneNumberUtils.compare(number, s))
return true;
}
return false;
}
@Override
public String[] getSupportedPacketTypes() {
return new String[]{
PACKET_TYPE_TELEPHONY_REQUEST,
PACKET_TYPE_TELEPHONY_REQUEST_MUTE,
};
}
@Override
public String[] getOutgoingPacketTypes() {
return new String[]{
PACKET_TYPE_TELEPHONY
};
}
@Override
public String[] getRequiredPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return new String[]{
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_CALL_LOG,
};
} else {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
}
@Override
public String[] getOptionalPermissions() {
return new String[]{
Manifest.permission.READ_CONTACTS,
};
}
@Override
public boolean hasSettings() {
return true;
}
}