1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-08 00:16: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

@ -59,7 +59,6 @@ public class SmsFetcher {
// 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
@ -69,37 +68,12 @@ 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);
// 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) {
@ -134,32 +108,14 @@ public class SmsFetcher {
// 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++) {
colName = c.getColumnName(idx); Integer rid = handleProviderColumn(c, idx, entry);
if (rid != -1) {
// Id column is must be an integer mboxId = rid;
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;
} }
} }
@ -189,6 +145,7 @@ 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)) {
@ -198,8 +155,7 @@ 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.");
} }
@ -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);
} }
@ -256,17 +187,50 @@ public class SmsFetcher {
} }
} }
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";
} }

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) {