diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/SmsFetcher.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/SmsFetcher.java index 07fe314..129cf1b 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/SmsFetcher.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/SmsFetcher.java @@ -32,75 +32,49 @@ public class SmsFetcher { public SmsFetcher(Context ct) { _lastMsgDate = (long) 0; _context = ct; - + _existingInboxMessages = null; _existingSentMessages = null; _existingDraftsMessages = null; } - + void fetchAllMessages(JSONArray result) { bufferMailboxMessages(result, MailboxID.INBOX); bufferMailboxMessages(result, MailboxID.SENT); bufferMailboxMessages(result, MailboxID.DRAFTS); } - + private void bufferMailboxMessages(JSONArray result, MailboxID mbID) { String mbURI = mapMailboxIDToURI(mbID); - + if ((_context == null) || (mbURI == null)) { return; } - + 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,56 +85,38 @@ 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 public JSONArray getLastMessage(MailboxID mbID) { String mbURI = mapMailboxIDToURI(mbID); - + if ((_context == null) || (mbURI == null)) { return null; } - + // 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; + } } /* @@ -174,34 +130,34 @@ public class SmsFetcher { } catch (JSONException e) { Log.e(SmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); } - + c.close(); - + return results; } - + // Used by ConnectivityChanged Event public void bufferMessagesSinceDate(JSONArray result, Long sinceDate) { bufferMessagesSinceDate(result, MailboxID.INBOX, sinceDate); bufferMessagesSinceDate(result, MailboxID.SENT, sinceDate); bufferMessagesSinceDate(result, MailboxID.DRAFTS, sinceDate); } - + // 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)) { return; } - + 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,69 +166,77 @@ 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); } } while (c.moveToNext()); - + 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"; } - + return null; } - + private String buildExistingMessagesString(MailboxID _mbID) { JSONArray existingMessages = null; if (_mbID == MailboxID.INBOX) { @@ -282,28 +246,28 @@ public class SmsFetcher { } else if (_mbID == MailboxID.SENT) { 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) { _existingInboxMessages = inboxMessages; } @@ -315,17 +279,17 @@ public class SmsFetcher { void setExistingDraftsMessages(JSONArray draftMessages) { _existingDraftsMessages = draftMessages; } - + Long getLastMessageDate() { return _lastMsgDate; } - + private final Context _context; private JSONArray _existingInboxMessages; private JSONArray _existingSentMessages; private JSONArray _existingDraftsMessages; - + private Long _lastMsgDate; - + private static final String TAG = SmsFetcher.class.getSimpleName(); } diff --git a/src/main/java/fr/unix_experience/owncloud_sms/providers/SmsDataProvider.java b/src/main/java/fr/unix_experience/owncloud_sms/providers/SmsDataProvider.java index 9adc426..993c47b 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/providers/SmsDataProvider.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/providers/SmsDataProvider.java @@ -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) {