diff options
author | Christian Schneppe <christian@pix-art.de> | 2016-11-08 21:46:21 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2016-11-08 21:46:21 +0100 |
commit | 0cf9f5eb0f0293c6b3be9147fda3712e4edef89f (patch) | |
tree | 3e3fd35510596904c7e999aca6eafb853f8ad4cf /src | |
parent | 5b9494db6ee6b587b49b46e3861f823b584e8020 (diff) |
add frequent restart detection
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java | 27 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/services/XmppConnectionService.java | 4 |
2 files changed, 29 insertions, 2 deletions
diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index 63f6bb783..693def6eb 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -53,7 +53,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { private static DatabaseBackend instance = null; public static final String DATABASE_NAME = "history"; - public static final int DATABASE_VERSION = 29; + public static final int DATABASE_VERSION = 30; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -138,7 +138,9 @@ public class DatabaseBackend extends SQLiteOpenHelper { + ") ON CONFLICT IGNORE" + ");"; - private DatabaseBackend(Context context) { + private static String CREATE_START_TIMES_TABLE = "create table start_times (timestamp NUMBER);"; + + private DatabaseBackend(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @@ -193,6 +195,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT); db.execSQL(CREATE_IDENTITIES_STATEMENT); db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT); + db.execSQL(CREATE_START_TIMES_TABLE); } @Override @@ -337,6 +340,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { if (oldVersion < 29 && newVersion >= 29) { db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT"); } + + if (oldVersion < 30 && newVersion >= 30) { + db.execSQL(CREATE_START_TIMES_TABLE); + } } private void canonicalizeJids(SQLiteDatabase db) { @@ -1220,4 +1227,20 @@ public class DatabaseBackend extends SQLiteOpenHelper { SQLiteAxolotlStore.ACCOUNT + " = ?", deleteArgs); } + + public boolean startTimeCountExceedsThreshold() { + SQLiteDatabase db = this.getWritableDatabase(); + long cleanBeforeTimestamp = System.currentTimeMillis() - Config.FREQUENT_RESTARTS_DETECTION_WINDOW; + db.execSQL("delete from start_times where timestamp < "+cleanBeforeTimestamp); + ContentValues values = new ContentValues(); + values.put("timestamp",System.currentTimeMillis()); + db.insert("start_times",null,values); + String[] columns = new String[]{"count(timestamp)"}; + Cursor cursor = db.query("start_times",columns,null,null,null,null,null); + if (cursor.moveToFirst()) { + return cursor.getInt(0) >= Config.FREQUENT_RESTARTS_THRESHOLD; + } else { + return false; + } + } } diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 9a9eca3b7..f0764b630 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -1005,6 +1005,10 @@ public class XmppConnectionService extends Service { this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); this.accounts = databaseBackend.getAccounts(); + if (databaseBackend.startTimeCountExceedsThreshold()) { + Log.d(Config.LOGTAG,"number of restarts exceeds threshold."); + } + restoreFromDatabase(); getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contactObserver); |