mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
Fix lint errors in SMSHelper.java
## Summary Clean up a bunch of lint errors in SMSHelper.java. Some of these are null issues which in theory might prevent a crash. ## Test Plan No functional changes are expected. Fetching SMS still works for me.
This commit is contained in:
@@ -55,6 +55,7 @@ import java.util.HashSet;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
@@ -109,7 +110,6 @@ public class SMSHelper {
|
|||||||
return Uri.parse("content://mms-sms/conversations?simple=true");
|
return Uri.parse("content://mms-sms/conversations?simple=true");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.FROYO)
|
|
||||||
private static Uri getCompleteConversationsUri() {
|
private static Uri getCompleteConversationsUri() {
|
||||||
// This glorious - but completely undocumented - content URI gives us all messages, both MMS and SMS,
|
// This glorious - but completely undocumented - content URI gives us all messages, both MMS and SMS,
|
||||||
// in all conversations
|
// in all conversations
|
||||||
@@ -490,7 +490,7 @@ public class SMSHelper {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
threadTimestampPair.add(new Pair(threadID, messageDate));
|
threadTimestampPair.add(new Pair<>(threadID, messageDate));
|
||||||
}
|
}
|
||||||
|
|
||||||
threadIds = threadTimestampPair.stream()
|
threadIds = threadTimestampPair.stream()
|
||||||
@@ -555,13 +555,30 @@ public class SMSHelper {
|
|||||||
event = addEventFlag(event, Message.EVENT_TEXT_MESSAGE);
|
event = addEventFlag(event, Message.EVENT_TEXT_MESSAGE);
|
||||||
|
|
||||||
@NonNull List<Address> address = Collections.singletonList(new Address(messageInfo.get(Telephony.Sms.ADDRESS)));
|
@NonNull List<Address> address = Collections.singletonList(new Address(messageInfo.get(Telephony.Sms.ADDRESS)));
|
||||||
@NonNull String body = messageInfo.get(Message.BODY);
|
@Nullable String maybeBody = messageInfo.getOrDefault(Message.BODY, "");
|
||||||
long date = Long.parseLong(messageInfo.get(Message.DATE));
|
@NonNull String body = maybeBody != null ? maybeBody : "";
|
||||||
int type = Integer.parseInt(messageInfo.get(Message.TYPE));
|
long date = NumberUtils.toLong(messageInfo.getOrDefault(Message.DATE, null));
|
||||||
int read = Integer.parseInt(messageInfo.get(Message.READ));
|
int type = NumberUtils.toInt(messageInfo.getOrDefault(Message.TYPE, null));
|
||||||
@NonNull ThreadID threadID = new ThreadID(Long.parseLong(messageInfo.get(Message.THREAD_ID)));
|
int read = NumberUtils.toInt(messageInfo.getOrDefault(Message.READ, null));
|
||||||
long uID = Long.parseLong(messageInfo.get(Message.U_ID));
|
@NonNull ThreadID threadID = new ThreadID(NumberUtils.toLong(messageInfo.getOrDefault(Message.THREAD_ID, null), ThreadID.invalidThreadId.threadID));
|
||||||
int subscriptionID = NumberUtils.toInt(messageInfo.get(Message.SUBSCRIPTION_ID));
|
long uID = NumberUtils.toLong(messageInfo.getOrDefault(Message.U_ID, null));
|
||||||
|
int subscriptionID = NumberUtils.toInt(messageInfo.getOrDefault(Message.SUBSCRIPTION_ID, null));
|
||||||
|
|
||||||
|
// Examine all the required SMS columns and emit a log if something seems amiss
|
||||||
|
boolean anyNulls = Arrays.stream(new String[] {
|
||||||
|
Telephony.Sms.ADDRESS,
|
||||||
|
Message.BODY,
|
||||||
|
Message.DATE,
|
||||||
|
Message.TYPE,
|
||||||
|
Message.READ,
|
||||||
|
Message.THREAD_ID,
|
||||||
|
Message.U_ID })
|
||||||
|
.map(key -> messageInfo.getOrDefault(key, null))
|
||||||
|
.anyMatch(Objects::isNull);
|
||||||
|
if (anyNulls)
|
||||||
|
{
|
||||||
|
Log.e("parseSMS", "Some fields were invalid. This indicates either a corrupted SMS database or an unsupported device.");
|
||||||
|
}
|
||||||
|
|
||||||
return new Message(
|
return new Message(
|
||||||
address,
|
address,
|
||||||
@@ -591,9 +608,9 @@ public class SMSHelper {
|
|||||||
@NonNull String body = "";
|
@NonNull String body = "";
|
||||||
long date;
|
long date;
|
||||||
int type;
|
int type;
|
||||||
int read = Integer.parseInt(messageInfo.get(Message.READ));
|
int read = NumberUtils.toInt(messageInfo.get(Message.READ));
|
||||||
@NonNull ThreadID threadID = new ThreadID(Long.parseLong(messageInfo.get(Message.THREAD_ID)));
|
@NonNull ThreadID threadID = new ThreadID(NumberUtils.toLong(messageInfo.getOrDefault(Message.THREAD_ID, null), ThreadID.invalidThreadId.threadID));
|
||||||
long uID = Long.parseLong(messageInfo.get(Message.U_ID));
|
long uID = NumberUtils.toLong(messageInfo.get(Message.U_ID));
|
||||||
int subscriptionID = NumberUtils.toInt(messageInfo.get(Message.SUBSCRIPTION_ID));
|
int subscriptionID = NumberUtils.toInt(messageInfo.get(Message.SUBSCRIPTION_ID));
|
||||||
List<Attachment> attachments = new ArrayList<>();
|
List<Attachment> attachments = new ArrayList<>();
|
||||||
|
|
||||||
@@ -671,7 +688,7 @@ public class SMSHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Determine whether the message was in- our out- bound
|
// Determine whether the message was in- our out- bound
|
||||||
long messageBox = Long.parseLong(messageInfo.get(Telephony.Mms.MESSAGE_BOX));
|
long messageBox = NumberUtils.toLong(messageInfo.get(Telephony.Mms.MESSAGE_BOX));
|
||||||
if (messageBox == Telephony.Mms.MESSAGE_BOX_INBOX) {
|
if (messageBox == Telephony.Mms.MESSAGE_BOX_INBOX) {
|
||||||
type = Telephony.Sms.MESSAGE_TYPE_INBOX;
|
type = Telephony.Sms.MESSAGE_TYPE_INBOX;
|
||||||
} else if (messageBox == Telephony.Mms.MESSAGE_BOX_SENT) {
|
} else if (messageBox == Telephony.Mms.MESSAGE_BOX_SENT) {
|
||||||
@@ -681,7 +698,7 @@ public class SMSHelper {
|
|||||||
// are the same as Sms.MESSAGE_TYPE_* of the same type. So by default let's just use
|
// are the same as Sms.MESSAGE_TYPE_* of the same type. So by default let's just use
|
||||||
// the value we've got.
|
// the value we've got.
|
||||||
// This includes things like drafts, which are a far-distant plan to support
|
// This includes things like drafts, which are a far-distant plan to support
|
||||||
type = Integer.parseInt(messageInfo.get(Telephony.Mms.MESSAGE_BOX));
|
type = NumberUtils.toInt(messageInfo.get(Telephony.Mms.MESSAGE_BOX));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get address(es) of the message
|
// Get address(es) of the message
|
||||||
@@ -699,11 +716,11 @@ public class SMSHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (to != null) {
|
if (to != null) {
|
||||||
for (Address address : to) {
|
for (Address toAddress : to) {
|
||||||
boolean isLocalPhoneNumber = userPhoneNumbers.stream().anyMatch(localPhoneNumber -> localPhoneNumber.isMatchingPhoneNumber(address.address));
|
boolean isLocalPhoneNumber = userPhoneNumbers.stream().anyMatch(localPhoneNumber -> localPhoneNumber.isMatchingPhoneNumber(toAddress.address));
|
||||||
|
|
||||||
if (!isLocalPhoneNumber && !from.toString().equals("insert-address-token")) {
|
if (!isLocalPhoneNumber && !toAddress.toString().equals("insert-address-token")) {
|
||||||
addresses.add(address);
|
addresses.add(toAddress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -719,7 +736,7 @@ public class SMSHelper {
|
|||||||
|
|
||||||
// Canonicalize the date field
|
// Canonicalize the date field
|
||||||
// SMS uses epoch milliseconds, MMS uses epoch seconds. Standardize on milliseconds.
|
// SMS uses epoch milliseconds, MMS uses epoch seconds. Standardize on milliseconds.
|
||||||
long rawDate = Long.parseLong(messageInfo.get(Message.DATE));
|
long rawDate = NumberUtils.toLong(messageInfo.get(Message.DATE));
|
||||||
date = rawDate * 1000;
|
date = rawDate * 1000;
|
||||||
|
|
||||||
return new Message(
|
return new Message(
|
||||||
@@ -803,6 +820,12 @@ public class SMSHelper {
|
|||||||
final long threadID;
|
final long threadID;
|
||||||
static final String lookupColumn = Telephony.Sms.THREAD_ID;
|
static final String lookupColumn = Telephony.Sms.THREAD_ID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define a value against which we can compare others, which should never be returned from
|
||||||
|
* a valid thread.
|
||||||
|
*/
|
||||||
|
public static final ThreadID invalidThreadId = new ThreadID(-1);
|
||||||
|
|
||||||
public ThreadID(long threadID) {
|
public ThreadID(long threadID) {
|
||||||
this.threadID = threadID;
|
this.threadID = threadID;
|
||||||
}
|
}
|
||||||
@@ -922,6 +945,7 @@ public class SMSHelper {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return address;
|
return address;
|
||||||
@@ -1125,6 +1149,7 @@ public class SMSHelper {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return body;
|
return body;
|
||||||
|
Reference in New Issue
Block a user