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

@ -53,13 +53,12 @@ public class SmsFetcher {
if ((mbID != MailboxID.INBOX) && (mbID != MailboxID.SENT) &&
(mbID != MailboxID.DRAFTS)) {
Log.e(SmsFetcher.TAG,"Unhandled MailboxID " + mbID.ordinal());
Log.e(SmsFetcher.TAG, "Unhandled MailboxID " + mbID.ordinal());
return;
}
// We generate a ID list for this message box
String existingIDs = buildExistingMessagesString(mbID);
Cursor c = new SmsDataProvider(_context).queryNonExistingMessages(mbURI, existingIDs);
// Reading mailbox
@ -69,37 +68,12 @@ public class SmsFetcher {
JSONObject entry = new JSONObject();
try {
String colName;
for(int idx = 0; idx < c.getColumnCount(); idx++) {
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;
}
for (int idx = 0; idx < c.getColumnCount(); idx++) {
handleProviderColumn(c, idx, entry);
}
// Mailbox ID is required by server
entry.put("mbox", mbID.ordinal());
result.put(entry);
} catch (JSONException e) {
@ -134,32 +108,14 @@ public class SmsFetcher {
// We create a list of strings to store results
JSONArray results = new JSONArray();
JSONObject entry = new JSONObject();
try {
Integer mboxId = -1;
String colName;
for(int idx = 0;idx < c.getColumnCount(); idx++) {
colName = c.getColumnName(idx);
// 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;
for (int idx = 0; idx < c.getColumnCount(); idx++) {
Integer rid = handleProviderColumn(c, idx, entry);
if (rid != -1) {
mboxId = rid;
}
}
@ -189,6 +145,7 @@ public class SmsFetcher {
// Used by ConnectivityChanged Event
private void bufferMessagesSinceDate(JSONArray result, MailboxID mbID, Long sinceDate) {
Log.i(SmsFetcher.TAG, "bufferMessagesSinceDate for " + mbID.toString() + " sinceDate " + sinceDate.toString());
String mbURI = mapMailboxIDToURI(mbID);
if ((_context == null) || (mbURI == null)) {
@ -198,8 +155,7 @@ public class SmsFetcher {
Cursor c = new SmsDataProvider(_context).queryMessagesSinceDate(mbURI, sinceDate);
if (c != null) {
Log.i(SmsFetcher.TAG, "Retrieved " + c.getCount() + " messages.");
}
else {
} else {
Log.i(SmsFetcher.TAG, "No message retrieved.");
}
@ -210,38 +166,13 @@ public class SmsFetcher {
JSONObject entry = new JSONObject();
try {
String colName;
for (int idx = 0; idx < c.getColumnCount(); idx++) {
colName = c.getColumnName(idx);
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;
}
handleProviderColumn(c, idx, entry);
}
// Mailbox ID is required by server
entry.put("mbox", mbID.ordinal());
result.put(entry);
} catch (JSONException 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) {
if (mbID == MailboxID.INBOX) {
return "content://sms/inbox";
}
else if (mbID == MailboxID.DRAFTS) {
} else if (mbID == MailboxID.DRAFTS) {
return "content://sms/drafts";
}
else if (mbID == MailboxID.SENT) {
} else if (mbID == MailboxID.SENT) {
return "content://sms/sent";
}
else if (mbID == MailboxID.ALL) {
} else if (mbID == MailboxID.ALL) {
return "content://sms";
}

View File

@ -52,7 +52,15 @@ public class SmsDataProvider extends ContentProvider {
Log.i(SmsDataProvider.TAG, "queryNonExistingMessages !");
if (!existingIds.isEmpty()) {
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
);
}
@ -62,7 +70,16 @@ public class SmsDataProvider extends ContentProvider {
public Cursor queryMessagesSinceDate(String mailBox, Long sinceDate) {
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
);
}
@ -83,26 +100,11 @@ public class SmsDataProvider extends ContentProvider {
// If minSize > 0 we should filter
if (senderMinSize > 0) {
if ((selection == null) || (selection.isEmpty())) {
selection = "length(address) > ?";
selectionArgs = new String[] { senderMinSize.toString() };
}
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;
}
selection = ((selection == null) || (selection.isEmpty())) ?
("length(address) >= " + senderMinSize.toString()) :
("length(address) >= " + senderMinSize.toString() + " AND " + selection);
Log.i(SmsDataProvider.TAG, "query: Minimum message length set to " + selectionArgs[0] +
". There is " + (selectionArgs.length - 1) + " other arg.");
Log.i(SmsDataProvider.TAG, "query: Minimum message length set to " + senderMinSize);
}
if (bulkLimit > 0) {