2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 13:17:43 +00:00

[SMS App] Fix sending simple SMS

## Summary

In https://invent.kde.org/network/kdeconnect-kde/-/merge_requests/316 we made some changes which are not backwards-compatible with the Android app. Since we have not yet released the Android app since those changes, we have hopefully not broken anybody but me.

However, this might be related to a recent bug: https://bugs.kde.org/show_bug.cgi?id=425638

Also updates some of the header comments for the recent changes

## Test Plan

### Before:
Sending SMS would silently fail

### After:
Sending SMS throws an IllegalArgumentException

With https://invent.kde.org/network/kdeconnect-kde/-/merge_requests/320, SMS can be sent normally
This commit is contained in:
Simon Redman 2020-10-09 00:36:03 +00:00
parent 4193f09267
commit aa8bd4ad59

View File

@ -124,14 +124,25 @@ public class SMSPlugin extends Plugin {
* but be sure the Android side remains compatible with old desktop apps! * but be sure the Android side remains compatible with old desktop apps!
* <p> * <p>
* The body should look like so: * The body should look like so:
* { "sendSms": true, * {
* "phoneNumber": "542904563213" // For older desktop versions of SMS app this packet carries phoneNumber field * "version": 2, // The version of the packet being sent. Compare to SMS_REQUEST_PACKET_VERSION before attempting to handle.
* "addresses": <List of Addresses> // For newer desktop versions of SMS app it contains addresses field instead of phoneNumber field * "sendSms": true, // (Depreciated, ignored) Old versions of the desktop app used to mix phone calls, SMS, etc. in the same packet type and used this field to differentiate.
* "messageBody": "Hi mom!", * "phoneNumber": "542904563213", // (Depreciated) Retained for backwards-compatibility. Old versions of the desktop app send a single phoneNumber. Use the Addresses field instead.
* "sub_id": "3859358340534" * "addresses": <List of Addresses>, // The one or many targets of this message
* "messageBody": "Hi mom!", // Plain-text string to be sent as the body of the message (Optional if sending an attachment)
* "attachments": <List of Attached files>,
* "sub_id": 3859358340534 // Some magic number which tells Android which SIM card to use (Optional, if omitted, sends with the default SIM card)
* }
*
* An AttachmentContainer object looks like:
* {
* "fileName": <String> // Name of the file
* "base64EncodedFile": <String> // Base64 encoded file
* "mimeType": <String> // File type (eg: image/jpg, video/mp4 etc.)
* } * }
*/ */
private final static String PACKET_TYPE_SMS_REQUEST = "kdeconnect.sms.request"; private final static String PACKET_TYPE_SMS_REQUEST = "kdeconnect.sms.request";
private final static int SMS_REQUEST_PACKET_VERSION = 2; // We *handle* packets of this version or lower. Update this number only if future packets break backwards-compatibility.
/** /**
* Packet sent to request the most-recent message in each conversations on the device * Packet sent to request the most-recent message in each conversations on the device
@ -387,6 +398,7 @@ public class SMSPlugin extends Plugin {
@Override @Override
public boolean onPacketReceived(NetworkPacket np) { public boolean onPacketReceived(NetworkPacket np) {
long subID;
switch (np.getType()) { switch (np.getType()) {
case PACKET_TYPE_SMS_REQUEST_CONVERSATIONS: case PACKET_TYPE_SMS_REQUEST_CONVERSATIONS:
@ -394,27 +406,25 @@ public class SMSPlugin extends Plugin {
case PACKET_TYPE_SMS_REQUEST_CONVERSATION: case PACKET_TYPE_SMS_REQUEST_CONVERSATION:
return this.handleRequestSingleConversation(np); return this.handleRequestSingleConversation(np);
case PACKET_TYPE_SMS_REQUEST: case PACKET_TYPE_SMS_REQUEST:
if (np.getBoolean("sendSms")) { String textMessage = np.getString("messageBody");
String textMessage = np.getString("messageBody"); subID = np.getLong("subID", -1);
long subID = np.getLong("subID", -1);
List<SMSHelper.Address> addressList = SMSHelper.jsonArrayToAddressList(np.getJSONArray("addresses")); List<SMSHelper.Address> addressList = SMSHelper.jsonArrayToAddressList(np.getJSONArray("addresses"));
if (addressList == null) { if (addressList == null) {
// If the List of Address is null, then the SMS_REQUEST packet is // If the List of Address is null, then the SMS_REQUEST packet is
// most probably from the older version of the desktop app. // most probably from the older version of the desktop app.
addressList = new ArrayList<>(); addressList = new ArrayList<>();
addressList.add(new SMSHelper.Address(np.getString("phoneNumber"))); addressList.add(new SMSHelper.Address(np.getString("phoneNumber")));
}
SmsMmsUtils.sendMessage(context, textMessage, addressList, (int) subID);
} }
SmsMmsUtils.sendMessage(context, textMessage, addressList, (int) subID);
break; break;
case TelephonyPlugin.PACKET_TYPE_TELEPHONY_REQUEST: case TelephonyPlugin.PACKET_TYPE_TELEPHONY_REQUEST:
if (np.getBoolean("sendSms")) { if (np.getBoolean("sendSms")) {
String phoneNo = np.getString("phoneNumber"); String phoneNo = np.getString("phoneNumber");
String sms = np.getString("messageBody"); String sms = np.getString("messageBody");
long subID = np.getLong("subID", -1); subID = np.getLong("subID", -1);
try { try {
SmsManager smsManager = subID == -1? SmsManager.getDefault() : SmsManager smsManager = subID == -1? SmsManager.getDefault() :