mirror of
https://github.com/nerzhul/ownCloud-SMS-App.git
synced 2025-06-08 16:36:10 +00:00
Refactor SMSFetcher
This commit is contained in:
parent
19e564213b
commit
792b6137fd
@ -52,55 +52,29 @@ public class SmsFetcher {
|
||||
}
|
||||
|
||||
if ((mbID != MailboxID.INBOX) && (mbID != MailboxID.SENT) &&
|
||||
(mbID != MailboxID.DRAFTS)) {
|
||||
Log.e(SmsFetcher.TAG,"Unhandled MailboxID " + mbID.ordinal());
|
||||
(mbID != MailboxID.DRAFTS)) {
|
||||
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
|
||||
// Reading mailbox
|
||||
if ((c != null) && (c.getCount() > 0)) {
|
||||
c.moveToFirst();
|
||||
do {
|
||||
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);
|
||||
result.put(entry);
|
||||
|
||||
} catch (JSONException 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);
|
||||
}
|
||||
|
||||
if (c != null) {
|
||||
c.close();
|
||||
}
|
||||
if (c != null) {
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Used by Content Observer
|
||||
@ -126,41 +100,23 @@ public class SmsFetcher {
|
||||
|
||||
// Fetch Sent SMS Message from Built-in Content Provider
|
||||
Cursor c = (new SmsDataProvider(_context)).query(mbURI);
|
||||
if (c == null) {
|
||||
return null;
|
||||
}
|
||||
if (c == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
c.moveToNext();
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@ -188,7 +144,8 @@ public class SmsFetcher {
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
if ((_context == null) || (mbURI == null)) {
|
||||
@ -196,12 +153,11 @@ public class SmsFetcher {
|
||||
}
|
||||
|
||||
Cursor c = new SmsDataProvider(_context).queryMessagesSinceDate(mbURI, sinceDate);
|
||||
if (c != null) {
|
||||
Log.i(SmsFetcher.TAG, "Retrieved " + c.getCount() + " messages.");
|
||||
}
|
||||
else {
|
||||
Log.i(SmsFetcher.TAG, "No message retrieved.");
|
||||
}
|
||||
if (c != null) {
|
||||
Log.i(SmsFetcher.TAG, "Retrieved " + c.getCount() + " messages.");
|
||||
} else {
|
||||
Log.i(SmsFetcher.TAG, "No message retrieved.");
|
||||
}
|
||||
|
||||
// Reading mailbox
|
||||
if ((c != null) && (c.getCount() > 0)) {
|
||||
@ -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);
|
||||
|
||||
result.put(entry);
|
||||
} catch (JSONException 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);
|
||||
}
|
||||
|
||||
if (c != null) {
|
||||
c.close();
|
||||
}
|
||||
if (c != null) {
|
||||
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) {
|
||||
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";
|
||||
}
|
||||
|
||||
@ -283,25 +247,25 @@ public class SmsFetcher {
|
||||
existingMessages = _existingSentMessages;
|
||||
}
|
||||
|
||||
if (existingMessages == null) {
|
||||
return "";
|
||||
}
|
||||
if (existingMessages == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Note: The default case isn't possible, we check the mailbox before
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int len = existingMessages.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
try {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(existingMessages.getInt(i));
|
||||
} catch (JSONException ignored) {
|
||||
int len = existingMessages.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
try {
|
||||
if (sb.length() > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(existingMessages.getInt(i));
|
||||
} catch (JSONException ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
void setExistingInboxMessages(JSONArray inboxMessages) {
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user