2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-02 23:25:10 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
Simon Redman
5becd46f15 Initialize mostRecentTimestamp in SMSPlugin constructor 2023-05-15 16:51:56 -06:00
Aditya Mehra
796fa20aec don't wait for future get as its blocking 2023-05-10 09:10:30 +00:00
Aditya Mehra
93883f02ed Use threadhelper for handling request for all conversations in sms plugin 2023-05-10 09:10:30 +00:00
2 changed files with 26 additions and 20 deletions

View File

@@ -10,4 +10,7 @@ object ThreadHelper {
@JvmStatic
fun execute(command: Runnable) = executor.execute(command)
}
@JvmStatic
fun <T> executeCallable(callable: Callable<T>): Future<T> = executor.submit(callable)
}

View File

@@ -30,6 +30,7 @@ import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import androidx.annotation.WorkerThread;
import androidx.core.content.ContextCompat;
import com.klinker.android.logger.Log;
@@ -40,6 +41,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.kde.kdeconnect.Helpers.ContactsHelper;
import org.kde.kdeconnect.Helpers.SMSHelper;
import org.kde.kdeconnect.Helpers.ThreadHelper;
import org.kde.kdeconnect.NetworkPacket;
import org.kde.kdeconnect.Plugins.Plugin;
import org.kde.kdeconnect.Plugins.PluginFactory;
@@ -53,6 +55,9 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -371,6 +376,17 @@ public class SMSPlugin extends Plugin {
// To see debug messages for Klinker library, uncomment the below line
//Log.setDebug(true);
// Initialize mostRecentTimestamp
List<SMSHelper.Message> newestMessage = SMSHelper.getMessagesInRange(context, null, Long.MAX_VALUE, 1l, true);
// There should only be one, but in case for some reason there are more, take the latest
mostRecentTimestampLock.lock();
for (SMSHelper.Message message : newestMessage) {
if (message.date > mostRecentTimestamp) {
mostRecentTimestamp = message.date;
}
}
mostRecentTimestampLock.unlock();
return true;
}
@@ -390,7 +406,10 @@ public class SMSPlugin extends Plugin {
switch (np.getType()) {
case PACKET_TYPE_SMS_REQUEST_CONVERSATIONS:
return this.handleRequestAllConversations(np);
Callable<Boolean> callable = () -> this.handleRequestAllConversations(np);
ThreadHelper.executeCallable(callable);
return true;
case PACKET_TYPE_SMS_REQUEST_CONVERSATION:
return this.handleRequestSingleConversation(np);
case PACKET_TYPE_SMS_REQUEST:
@@ -484,24 +503,19 @@ public class SMSPlugin extends Plugin {
* <p>
* Send one packet of type PACKET_TYPE_SMS_MESSAGE with the first message in all conversations
*/
@WorkerThread
private boolean handleRequestAllConversations(NetworkPacket packet) {
Iterable<SMSHelper.Message> conversations = SMSHelper.getConversations(this.context);
// Prepare the mostRecentTimestamp counter based on these messages, since they are the most
// recent in every conversation
mostRecentTimestampLock.lock();
for (SMSHelper.Message message : conversations) {
if (message.date > mostRecentTimestamp) {
mostRecentTimestamp = message.date;
}
NetworkPacket partialReply = constructBulkMessagePacket(Collections.singleton(message));
device.sendPacket(partialReply);
}
mostRecentTimestampLock.unlock();
return true;
}
@WorkerThread
private boolean handleRequestSingleConversation(NetworkPacket packet) {
SMSHelper.ThreadID threadID = new SMSHelper.ThreadID(packet.getLong("threadID"));
@@ -519,17 +533,6 @@ public class SMSPlugin extends Plugin {
conversation = SMSHelper.getMessagesInRange(this.context, threadID, rangeStartTimestamp, numberToGet, true);
}
// Sometimes when desktop app is kept open while android app is restarted for any reason
// mostRecentTimeStamp must be updated in that scenario too if a user request for a
// single conversation and not the entire conversation list
mostRecentTimestampLock.lock();
for (SMSHelper.Message message : conversation) {
if (message.date > mostRecentTimestamp) {
mostRecentTimestamp = message.date;
}
}
mostRecentTimestampLock.unlock();
NetworkPacket reply = constructBulkMessagePacket(conversation);
device.sendPacket(reply);