1
0
mirror of https://github.com/nerzhul/ownCloud-SMS-App.git synced 2025-06-07 07:56:14 +00:00

Don't restore already existing messages

This commit is contained in:
Loic Blot 2016-12-08 21:43:32 +01:00
parent 9bb0a7d2f2
commit 819fa4543d
2 changed files with 38 additions and 12 deletions

View File

@ -68,7 +68,7 @@ public interface ASyncSMSRecovery {
while (obj.getLong("last_id") != start) {
JSONObject messages = obj.getJSONObject("messages");
Iterator<?> keys = messages.keys();
while(keys.hasNext()) {
while (keys.hasNext()) {
String key = (String)keys.next();
if (messages.get(key) instanceof JSONObject) {
JSONObject msg = messages.getJSONObject(key);
@ -76,25 +76,43 @@ public interface ASyncSMSRecovery {
int mbid = msg.getInt("mailbox");
// Ignore invalid mailbox
if (mbid > MailboxID.ALL.getId()) {
Log.e(ASyncSMSRecovery.TAG, "Invalid mailbox found: " + msg.getString("mailbox"));
continue;
}
String address = msg.getString("address");
String address;
String body;
int type;
try {
address = msg.getString("address");
body = msg.getString("msg");
type = msg.getInt("type");
}
catch (JSONException e) {
Log.e(ASyncSMSRecovery.TAG, "Invalid SMS data found: " + e.getMessage());
continue;
}
MailboxID mailbox_id = MailboxID.fromInt(mbid);
// Ignore already existing messages
if (smsDataProvider.messageExists(address, body, key, mailbox_id)) {
publishProgress(nb);
continue;
}
ContentValues values = new ContentValues();
values.put(Telephony.Sms.ADDRESS, address);
values.put(Telephony.Sms.BODY, msg.getString("msg"));
values.put(Telephony.Sms.BODY, body);
values.put(Telephony.Sms.DATE, key);
values.put(Telephony.Sms.TYPE, msg.getInt("type"));
values.put(Telephony.Sms.TYPE, type);
values.put(Telephony.Sms.SEEN, 1);
values.put(Telephony.Sms.READ, 1);
MailboxID mailbox_id = MailboxID.fromInt(mbid);
// @TODO verify message exists before inserting it
_context.getContentResolver().insert(Uri.parse(mailbox_id.getURI()), values);
nb++;
if ((nb % 100) == 0) {
if ((nb % 5) == 0) {
publishProgress(nb);
}
}

View File

@ -25,6 +25,7 @@ import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;
import fr.unix_experience.owncloud_sms.enums.MailboxID;
import fr.unix_experience.owncloud_sms.prefs.OCSMSSharedPrefs;
public class SmsDataProvider extends ContentProvider {
@ -84,6 +85,19 @@ public class SmsDataProvider extends ContentProvider {
);
}
public boolean messageExists(String address, String body, String date, MailboxID mb) {
Cursor c = query(Uri.parse(mb.getURI()), new String[] { "_id" },
"address = ? AND body = ? AND date = ?",
new String[] { address, body, date }, null);
if (c == null) {
return false;
}
boolean exists = c.getCount() > 0;
c.close();
return exists;
}
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
@ -103,20 +117,14 @@ public class SmsDataProvider extends ContentProvider {
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 " + senderMinSize);
}
if (bulkLimit > 0) {
if (sortOrder == null)
sortOrder = "date DESC";
sortOrder += " LIMIT " + bulkLimit.toString();
Log.i(SmsDataProvider.TAG, "query: Bulk limit set to " + bulkLimit.toString());
}
Log.i(SmsDataProvider.TAG, "query: selection set to " + selection);
Cursor cursor = _context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
if (cursor == null) {
return null;