1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-08 08:26:09 +00:00

Refactor SMSFetcher

This commit is contained in:
Loic Blot 2016-10-22 18:44:23 +02:00
parent 19e564213b
commit 792b6137fd
2 changed files with 133 additions and 167 deletions

View File

@ -52,55 +52,29 @@ public class SmsFetcher {
} }
if ((mbID != MailboxID.INBOX) && (mbID != MailboxID.SENT) && if ((mbID != MailboxID.INBOX) && (mbID != MailboxID.SENT) &&
(mbID != MailboxID.DRAFTS)) { (mbID != MailboxID.DRAFTS)) {
Log.e(SmsFetcher.TAG,"Unhandled MailboxID " + mbID.ordinal()); Log.e(SmsFetcher.TAG, "Unhandled MailboxID " + mbID.ordinal());
return; return;
} }
// We generate a ID list for this message box // We generate a ID list for this message box
String existingIDs = buildExistingMessagesString(mbID); String existingIDs = buildExistingMessagesString(mbID);
Cursor c = new SmsDataProvider(_context).queryNonExistingMessages(mbURI, existingIDs); Cursor c = new SmsDataProvider(_context).queryNonExistingMessages(mbURI, existingIDs);
// Reading mailbox // Reading mailbox
if ((c != null) && (c.getCount() > 0)) { if ((c != null) && (c.getCount() > 0)) {
c.moveToFirst(); c.moveToFirst();
do { do {
JSONObject entry = new JSONObject(); JSONObject entry = new JSONObject();
try { try {
String colName; for (int idx = 0; idx < c.getColumnCount(); idx++) {
for(int idx = 0; idx < c.getColumnCount(); idx++) { handleProviderColumn(c, idx, entry);
colName = c.getColumnName(idx);
// Id column is must be an integer
switch (colName) {
case "_id":
case "type":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, (c.getInt(idx) > 0) ? "true" : "false");
break;
default:
// Special case for date, we need to record last without searching
if ("date".equals(colName)) {
Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
break;
}
} }
// Mailbox ID is required by server // Mailbox ID is required by server
entry.put("mbox", mbID.ordinal()); entry.put("mbox", mbID.ordinal());
result.put(entry);
result.put(entry);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(SmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); Log.e(SmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e);
@ -111,9 +85,9 @@ public class SmsFetcher {
Log.i(SmsFetcher.TAG, c.getCount() + " messages read from " + mbURI); Log.i(SmsFetcher.TAG, c.getCount() + " messages read from " + mbURI);
} }
if (c != null) { if (c != null) {
c.close(); c.close();
} }
} }
// Used by Content Observer // Used by Content Observer
@ -126,41 +100,23 @@ public class SmsFetcher {
// Fetch Sent SMS Message from Built-in Content Provider // Fetch Sent SMS Message from Built-in Content Provider
Cursor c = (new SmsDataProvider(_context)).query(mbURI); Cursor c = (new SmsDataProvider(_context)).query(mbURI);
if (c == null) { if (c == null) {
return null; return null;
} }
c.moveToNext(); c.moveToNext();
// We create a list of strings to store results // We create a list of strings to store results
JSONArray results = new JSONArray(); JSONArray results = new JSONArray();
JSONObject entry = new JSONObject(); JSONObject entry = new JSONObject();
try { try {
Integer mboxId = -1; Integer mboxId = -1;
String colName; for (int idx = 0; idx < c.getColumnCount(); idx++) {
for(int idx = 0;idx < c.getColumnCount(); idx++) { Integer rid = handleProviderColumn(c, idx, entry);
colName = c.getColumnName(idx); if (rid != -1) {
mboxId = rid;
// Id column is must be an integer }
switch (colName) {
case "_id":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, (c.getInt(idx) > 0) ? "true" : "false");
break;
case "type":
mboxId = c.getInt(idx);
entry.put(colName, c.getInt(idx));
break;
default:
entry.put(colName, c.getString(idx));
break;
}
} }
/* /*
@ -188,7 +144,8 @@ public class SmsFetcher {
} }
// Used by ConnectivityChanged Event // Used by ConnectivityChanged Event
private void bufferMessagesSinceDate(JSONArray result, MailboxID mbID, Long sinceDate) { private void bufferMessagesSinceDate(JSONArray result, MailboxID mbID, Long sinceDate) {
Log.i(SmsFetcher.TAG, "bufferMessagesSinceDate for " + mbID.toString() + " sinceDate " + sinceDate.toString());
String mbURI = mapMailboxIDToURI(mbID); String mbURI = mapMailboxIDToURI(mbID);
if ((_context == null) || (mbURI == null)) { if ((_context == null) || (mbURI == null)) {
@ -196,12 +153,11 @@ public class SmsFetcher {
} }
Cursor c = new SmsDataProvider(_context).queryMessagesSinceDate(mbURI, sinceDate); Cursor c = new SmsDataProvider(_context).queryMessagesSinceDate(mbURI, sinceDate);
if (c != null) { if (c != null) {
Log.i(SmsFetcher.TAG, "Retrieved " + c.getCount() + " messages."); Log.i(SmsFetcher.TAG, "Retrieved " + c.getCount() + " messages.");
} } else {
else { Log.i(SmsFetcher.TAG, "No message retrieved.");
Log.i(SmsFetcher.TAG, "No message retrieved."); }
}
// Reading mailbox // Reading mailbox
if ((c != null) && (c.getCount() > 0)) { if ((c != null) && (c.getCount() > 0)) {
@ -210,38 +166,13 @@ public class SmsFetcher {
JSONObject entry = new JSONObject(); JSONObject entry = new JSONObject();
try { try {
String colName;
for (int idx = 0; idx < c.getColumnCount(); idx++) { for (int idx = 0; idx < c.getColumnCount(); idx++) {
colName = c.getColumnName(idx); handleProviderColumn(c, idx, entry);
switch (colName) {
// Id column is must be an integer
case "_id":
case "type":
entry.put(colName, c.getInt(idx));
break;
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, (c.getInt(idx) > 0) ? "true" : "false");
break;
default:
// Special case for date, we need to record last without searching
if ("date".equals(colName)) {
Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
}
entry.put(colName, c.getString(idx));
break;
}
} }
// Mailbox ID is required by server // Mailbox ID is required by server
entry.put("mbox", mbID.ordinal()); entry.put("mbox", mbID.ordinal());
result.put(entry);
result.put(entry);
} catch (JSONException e) { } catch (JSONException e) {
Log.e(SmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); Log.e(SmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e);
} }
@ -251,22 +182,55 @@ public class SmsFetcher {
Log.i(SmsFetcher.TAG, c.getCount() + " messages read from " + mbURI); Log.i(SmsFetcher.TAG, c.getCount() + " messages read from " + mbURI);
} }
if (c != null) { if (c != null) {
c.close(); c.close();
} }
}
private Integer handleProviderColumn(Cursor c, int idx, JSONObject entry) throws JSONException {
String colName = c.getColumnName(idx);
// Id column is must be an integer
switch (colName) {
case "_id":
entry.put(colName, c.getInt(idx));
break;
case "type":
entry.put(colName, c.getInt(idx));
return c.getInt(idx);
/* For debug purpose
case "length(address)":
Log.i(SmsFetcher.TAG, "Column name " + colName + " " + c.getString(idx));
break;*/
// Seen and read must be pseudo boolean
case "read":
case "seen":
entry.put(colName, (c.getInt(idx) > 0) ? "true" : "false");
break;
case "date":
// Special case for date, we need to record last without searching
Long tmpDate = c.getLong(idx);
if (tmpDate > _lastMsgDate) {
_lastMsgDate = tmpDate;
}
entry.put(colName, c.getString(idx));
break;
default:
entry.put(colName, c.getString(idx));
break;
}
return -1;
} }
private String mapMailboxIDToURI(MailboxID mbID) { private String mapMailboxIDToURI(MailboxID mbID) {
if (mbID == MailboxID.INBOX) { if (mbID == MailboxID.INBOX) {
return "content://sms/inbox"; return "content://sms/inbox";
} } else if (mbID == MailboxID.DRAFTS) {
else if (mbID == MailboxID.DRAFTS) {
return "content://sms/drafts"; return "content://sms/drafts";
} } else if (mbID == MailboxID.SENT) {
else if (mbID == MailboxID.SENT) {
return "content://sms/sent"; return "content://sms/sent";
} } else if (mbID == MailboxID.ALL) {
else if (mbID == MailboxID.ALL) {
return "content://sms"; return "content://sms";
} }
@ -283,25 +247,25 @@ public class SmsFetcher {
existingMessages = _existingSentMessages; existingMessages = _existingSentMessages;
} }
if (existingMessages == null) { if (existingMessages == null) {
return ""; return "";
} }
// Note: The default case isn't possible, we check the mailbox before // Note: The default case isn't possible, we check the mailbox before
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int len = existingMessages.length(); int len = existingMessages.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
try { try {
if (sb.length() > 0) { if (sb.length() > 0) {
sb.append(","); sb.append(",");
} }
sb.append(existingMessages.getInt(i)); sb.append(existingMessages.getInt(i));
} catch (JSONException ignored) { } catch (JSONException ignored) {
} }
} }
return sb.toString(); return sb.toString();
} }
void setExistingInboxMessages(JSONArray inboxMessages) { void setExistingInboxMessages(JSONArray inboxMessages) {

View File

@ -52,7 +52,15 @@ public class SmsDataProvider extends ContentProvider {
Log.i(SmsDataProvider.TAG, "queryNonExistingMessages !"); Log.i(SmsDataProvider.TAG, "queryNonExistingMessages !");
if (!existingIds.isEmpty()) { if (!existingIds.isEmpty()) {
return query(Uri.parse(mailBox), return query(Uri.parse(mailBox),
new String[] { "read", "date", "address", "seen", "body", "_id", "type", }, new String[] {
"read",
"date",
"address",
"seen",
"body",
"_id",
"type",
},
"_id NOT IN (" + existingIds + ")", null, null "_id NOT IN (" + existingIds + ")", null, null
); );
} }
@ -62,7 +70,16 @@ public class SmsDataProvider extends ContentProvider {
public Cursor queryMessagesSinceDate(String mailBox, Long sinceDate) { public Cursor queryMessagesSinceDate(String mailBox, Long sinceDate) {
return query(Uri.parse(mailBox), return query(Uri.parse(mailBox),
new String[] { "read", "date", "address", "seen", "body", "_id", "type", }, new String[] {
"read",
"date",
"address",
"seen",
"body",
"_id",
"type",
//"length(address)" // For debug purposes
},
"date > ?", new String[] { sinceDate.toString() }, null "date > ?", new String[] { sinceDate.toString() }, null
); );
} }
@ -83,26 +100,11 @@ public class SmsDataProvider extends ContentProvider {
// If minSize > 0 we should filter // If minSize > 0 we should filter
if (senderMinSize > 0) { if (senderMinSize > 0) {
if ((selection == null) || (selection.isEmpty())) { selection = ((selection == null) || (selection.isEmpty())) ?
selection = "length(address) > ?"; ("length(address) >= " + senderMinSize.toString()) :
selectionArgs = new String[] { senderMinSize.toString() }; ("length(address) >= " + senderMinSize.toString() + " AND " + selection);
}
else {
selection = "length(address) > ? AND " + selection;
int nSelectionArgLength = 1;
if (selectionArgs != null) {
nSelectionArgLength += selectionArgs.length;
}
String[] nSelectionArgs = new String[nSelectionArgLength];
nSelectionArgs[0] = senderMinSize.toString();
if (selectionArgs != null) {
System.arraycopy(selectionArgs, 0, nSelectionArgs, 1, selectionArgs.length);
}
selectionArgs = nSelectionArgs;
}
Log.i(SmsDataProvider.TAG, "query: Minimum message length set to " + selectionArgs[0] + Log.i(SmsDataProvider.TAG, "query: Minimum message length set to " + senderMinSize);
". There is " + (selectionArgs.length - 1) + " other arg.");
} }
if (bulkLimit > 0) { if (bulkLimit > 0) {