diff options
Diffstat (limited to 'src/main/java/de')
3 files changed, 56 insertions, 8 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index bef367b0a..243db470a 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -28,11 +28,14 @@ import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -1052,16 +1055,55 @@ public class DatabaseBackend extends SQLiteOpenHelper { Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms"); } - public void expireOldMessages(long timestamp) { + public long countExpireOldMessages(long timestamp) { long start = SystemClock.elapsedRealtime(); final String[] args = {String.valueOf(timestamp)}; SQLiteDatabase db = this.getReadableDatabase(); db.beginTransaction(); - db.delete("messages_index", "uuid in (select uuid from messages where timeSent<?)", args); - int num = db.delete(Message.TABLENAME, "timeSent<?", args); + long num = DatabaseUtils.queryNumEntries(db, Message.TABLENAME, "timeSent<?", args); db.setTransactionSuccessful(); db.endTransaction(); + Log.d(Config.LOGTAG, "found " + num + " expired messages in " + (SystemClock.elapsedRealtime() - start) + "ms"); + return num; + } + + public long getOldestMessages() { + Cursor cursor = null; + try { + SQLiteDatabase db = this.getReadableDatabase(); + db.beginTransaction(); + cursor = db.rawQuery("select timeSent from " + Message.TABLENAME + " ORDER BY " + Message.TIME_SENT + " ASC limit 1", null); + db.setTransactionSuccessful(); + db.endTransaction(); + if (cursor.getCount() == 0) { + return 0; + } else { + cursor.moveToFirst(); + return cursor.getLong(0); + } + } catch (Exception e) { + return 0; + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + + public int expireOldMessages(long timestamp) { + long start = SystemClock.elapsedRealtime(); + int num = 0; + if (countExpireOldMessages(timestamp) >= 1) { + final String[] args = {String.valueOf(timestamp)}; + SQLiteDatabase db = this.getReadableDatabase(); + db.beginTransaction(); + db.delete("messages_index", "uuid in (select uuid from messages where timeSent<?)", args); + num = db.delete(Message.TABLENAME, "timeSent<?", args); + db.setTransactionSuccessful(); + db.endTransaction(); + } Log.d(Config.LOGTAG, "deleted " + num + " expired messages in " + (SystemClock.elapsedRealtime() - start) + "ms"); + return num; } public MamReference getLastMessageReceived(Account account) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index b57d9846f..c0c6c91d9 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -79,6 +79,7 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; @@ -4871,16 +4872,21 @@ public class XmppConnectionService extends Service { private void expireOldMessages(long timestamp, boolean stepped) { if (stepped) { - final int year = 365; + final long expiredMessagesCount = databaseBackend.countExpireOldMessages(timestamp); + final long days = TimeUnit.MILLISECONDS.toDays(Calendar.getInstance().getTimeInMillis() - databaseBackend.getOldestMessages()); final long day = (long) 24 * 60 * 60 * 1000; int count = 0; - while (count <= year) { + int messagesCount = 0; + while (count <= days) { try { - databaseBackend.expireOldMessages(timestamp - ((year - count) * day)); + messagesCount += databaseBackend.expireOldMessages(timestamp - ((days - count) * day)); } catch (Exception e) { e.printStackTrace(); } count++; + if (expiredMessagesCount == messagesCount) { + break; + } } } else { databaseBackend.expireOldMessages(timestamp); diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java index 4cab09d99..28ea68d4e 100644 --- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java @@ -280,7 +280,7 @@ public class XmppConnection implements Runnable { try { startXmpp(localSocket); } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": thread was interrupted before beginning stream"); return; } catch (Exception e) { throw new IOException(e.getMessage()); @@ -372,7 +372,7 @@ public class XmppConnection implements Runnable { throw e; } } catch (InterruptedException e) { - Log.d(Config.LOGTAG,account.getJid().asBareJid()+": thread was interrupted before beginning stream"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": thread was interrupted before beginning stream"); return; } catch (final Throwable e) { Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")"); |