Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/TelephonyPlugin/TelephonyPlugin.java
Show First 20 Lines • Show All 62 Lines • ▼ Show 20 Line(s) | 59 | public class TelephonyPlugin extends Plugin { | |||
---|---|---|---|---|---|
63 | /** | 63 | /** | ||
64 | * Packet used for simple telephony events | 64 | * Packet used for simple telephony events | ||
65 | * | 65 | * | ||
66 | * It contains the key "event" which maps to a string indicating the type of event: | 66 | * It contains the key "event" which maps to a string indicating the type of event: | ||
67 | * - "ringing" - A phone call is incoming | 67 | * - "ringing" - A phone call is incoming | ||
68 | * - "missedCall" - An incoming call was not answered | 68 | * - "missedCall" - An incoming call was not answered | ||
69 | * - "sms" - An incoming SMS message | 69 | * - "sms" - An incoming SMS message | ||
70 | * - Note: As of this writing (15 May 2018) the SMS interface is being improved and this type of event | 70 | * - Note: As of this writing (15 May 2018) the SMS interface is being improved and this type of event | ||
71 | * is no longer the preferred way of retrieving SMS. Use PACKET_TYPE_TELEPHONY_MESSAGE instead. | 71 | * is no longer the preferred way of handling SMS. Use the packets defined by the SMS plugin instead. | ||
72 | * | 72 | * | ||
73 | * Depending on the event, other fields may be defined | 73 | * Depending on the event, other fields may be defined | ||
74 | */ | 74 | */ | ||
75 | public final static String PACKET_TYPE_TELEPHONY = "kdeconnect.telephony"; | 75 | public final static String PACKET_TYPE_TELEPHONY = "kdeconnect.telephony"; | ||
76 | | ||||
77 | /** | ||||
78 | * Old-style packet sent to request a simple telephony action | ||||
79 | * | ||||
80 | * The two possible events used the be to request a message be sent or request the device | ||||
81 | * silence its ringer | ||||
82 | * | ||||
83 | * In case an SMS was being requested, the body was like so: | ||||
84 | * { "sendSms": true, | ||||
85 | * "phoneNumber": "542904563213", | ||||
86 | * "messageBody": "Hi mom!" | ||||
87 | * } | ||||
88 | * | ||||
89 | * In case a ringer muted was requested, the body looked like so: | ||||
90 | * { "action": "mute" } | ||||
91 | * | ||||
92 | * As of 15 May 2018, the SMS interface is being improved. Use the packets defined by the | ||||
93 | * SMS plugin instead for SMS events | ||||
94 | * | ||||
95 | * Ringer mute requests are best handled by PACKET_TYPE_TELEPHONY_REQUEST_MUTE | ||||
96 | * | ||||
97 | * This packet type is retained for backwards-compatibility with old desktop applications, | ||||
98 | * but support should be dropped once those applications are no longer supported. New | ||||
99 | * applications should not use this packet type. | ||||
100 | */ | ||||
101 | @Deprecated | ||||
76 | public final static String PACKET_TYPE_TELEPHONY_REQUEST = "kdeconnect.telephony.request"; | 102 | public final static String PACKET_TYPE_TELEPHONY_REQUEST = "kdeconnect.telephony.request"; | ||
77 | 103 | | |||
104 | /** | ||||
105 | * Packet sent to indicate the user has requested the device mute its ringer | ||||
106 | * | ||||
107 | * The body should be empty | ||||
108 | */ | ||||
109 | public final static String PACKET_TYPE_TELEPHONY_REQUEST_MUTE = "kdeconnect.telephony.request_mute"; | ||||
110 | | ||||
78 | private static final String KEY_PREF_BLOCKED_NUMBERS = "telephony_blocked_numbers"; | 111 | private static final String KEY_PREF_BLOCKED_NUMBERS = "telephony_blocked_numbers"; | ||
79 | private int lastState = TelephonyManager.CALL_STATE_IDLE; | 112 | private int lastState = TelephonyManager.CALL_STATE_IDLE; | ||
80 | private NetworkPacket lastPacket = null; | 113 | private NetworkPacket lastPacket = null; | ||
81 | private boolean isMuted = false; | 114 | private boolean isMuted = false; | ||
82 | 115 | | |||
83 | private final BroadcastReceiver receiver = new BroadcastReceiver() { | 116 | private final BroadcastReceiver receiver = new BroadcastReceiver() { | ||
84 | @Override | 117 | @Override | ||
85 | public void onReceive(Context context, Intent intent) { | 118 | public void onReceive(Context context, Intent intent) { | ||
▲ Show 20 Lines • Show All 134 Lines • ▼ Show 20 Line(s) | 217 | case TelephonyManager.CALL_STATE_IDLE: | |||
220 | break; | 253 | break; | ||
221 | 254 | | |||
222 | } | 255 | } | ||
223 | 256 | | |||
224 | lastPacket = np; | 257 | lastPacket = np; | ||
225 | lastState = state; | 258 | lastState = state; | ||
226 | } | 259 | } | ||
227 | 260 | | |||
261 | private void muteRinger() { | ||||
262 | if (!isMuted) { | ||||
263 | AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | ||||
264 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||||
265 | am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0); | ||||
266 | } else { | ||||
267 | am.setStreamMute(AudioManager.STREAM_RING, true); | ||||
268 | } | ||||
269 | isMuted = true; | ||||
270 | } | ||||
271 | } | ||||
272 | | ||||
228 | @Override | 273 | @Override | ||
229 | public boolean onCreate() { | 274 | public boolean onCreate() { | ||
230 | IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); | 275 | IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED"); | ||
231 | filter.setPriority(500); | 276 | filter.setPriority(500); | ||
232 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); | 277 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); | ||
233 | context.registerReceiver(receiver, filter); | 278 | context.registerReceiver(receiver, filter); | ||
234 | permissionExplanation = R.string.telephony_permission_explanation; | 279 | permissionExplanation = R.string.telephony_permission_explanation; | ||
235 | optionalPermissionExplanation = R.string.telephony_optional_permission_explanation; | 280 | optionalPermissionExplanation = R.string.telephony_optional_permission_explanation; | ||
236 | return true; | 281 | return true; | ||
237 | } | 282 | } | ||
238 | 283 | | |||
239 | @Override | 284 | @Override | ||
240 | public void onDestroy() { | 285 | public void onDestroy() { | ||
241 | context.unregisterReceiver(receiver); | 286 | context.unregisterReceiver(receiver); | ||
242 | } | 287 | } | ||
243 | 288 | | |||
244 | @Override | 289 | @Override | ||
245 | public boolean onPacketReceived(NetworkPacket np) { | 290 | public boolean onPacketReceived(NetworkPacket np) { | ||
246 | 291 | | |||
292 | switch (np.getType()) { | ||||
293 | case PACKET_TYPE_TELEPHONY_REQUEST: | ||||
247 | if (np.getString("action").equals("mute")) { | 294 | if (np.getString("action").equals("mute")) { | ||
248 | if (!isMuted) { | 295 | muteRinger(); | ||
249 | AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | | |||
250 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | | |||
251 | am.setStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_MUTE, 0); | | |||
252 | } else { | | |||
253 | am.setStreamMute(AudioManager.STREAM_RING, true); | | |||
254 | } | | |||
255 | isMuted = true; | | |||
256 | } | 296 | } | ||
297 | break; | ||||
298 | case PACKET_TYPE_TELEPHONY_REQUEST_MUTE: | ||||
299 | muteRinger(); | ||||
300 | break; | ||||
257 | } | 301 | } | ||
258 | //Do nothing | | |||
259 | return true; | 302 | return true; | ||
260 | } | 303 | } | ||
261 | 304 | | |||
262 | private boolean isNumberBlocked(String number) { | 305 | private boolean isNumberBlocked(String number) { | ||
263 | SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | 306 | SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context); | ||
264 | String[] blockedNumbers = sharedPref.getString(KEY_PREF_BLOCKED_NUMBERS, "").split("\n"); | 307 | String[] blockedNumbers = sharedPref.getString(KEY_PREF_BLOCKED_NUMBERS, "").split("\n"); | ||
265 | 308 | | |||
266 | for (String s : blockedNumbers) { | 309 | for (String s : blockedNumbers) { | ||
267 | if (PhoneNumberUtils.compare(number, s)) | 310 | if (PhoneNumberUtils.compare(number, s)) | ||
268 | return true; | 311 | return true; | ||
269 | } | 312 | } | ||
270 | 313 | | |||
271 | return false; | 314 | return false; | ||
272 | } | 315 | } | ||
273 | 316 | | |||
274 | @Override | 317 | @Override | ||
275 | public String[] getSupportedPacketTypes() { | 318 | public String[] getSupportedPacketTypes() { | ||
276 | return new String[]{ | 319 | return new String[]{ | ||
277 | PACKET_TYPE_TELEPHONY_REQUEST | 320 | PACKET_TYPE_TELEPHONY_REQUEST, | ||
321 | PACKET_TYPE_TELEPHONY_REQUEST_MUTE, | ||||
278 | }; | 322 | }; | ||
279 | } | 323 | } | ||
280 | 324 | | |||
281 | @Override | 325 | @Override | ||
282 | public String[] getOutgoingPacketTypes() { | 326 | public String[] getOutgoingPacketTypes() { | ||
283 | return new String[]{ | 327 | return new String[]{ | ||
284 | PACKET_TYPE_TELEPHONY | 328 | PACKET_TYPE_TELEPHONY | ||
285 | }; | 329 | }; | ||
Show All 17 Lines |