aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2018-01-21 21:20:59 +0100
committerChristian Schneppe <christian@pix-art.de>2018-01-21 21:29:00 +0100
commit062e51bdccde6f72d9c7cacc8408ec3b2827fa8d (patch)
tree872147770eee51865fca09852add112cddee87e8 /src/main/java
parentea8ca07e11770399eb9b4cfc3649d6fe574da1c3 (diff)
permanently cache last resolver result
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/de/pixart/messenger/Config.java3
-rw-r--r--src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java78
-rw-r--r--src/main/java/de/pixart/messenger/services/XmppConnectionService.java5
-rw-r--r--src/main/java/de/pixart/messenger/utils/Resolver.java66
-rw-r--r--src/main/java/de/pixart/messenger/xmpp/XmppConnection.java14
5 files changed, 119 insertions, 47 deletions
diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java
index 625b395ff..849e9d486 100644
--- a/src/main/java/de/pixart/messenger/Config.java
+++ b/src/main/java/de/pixart/messenger/Config.java
@@ -117,9 +117,6 @@ public final class Config {
public static final long MAM_MAX_CATCHUP = MILLISECONDS_IN_DAY * 5;
public static final int MAM_MAX_MESSAGES = 750;
- public static final long FREQUENT_RESTARTS_DETECTION_WINDOW = 8 * 60 * 60 * 1000; // 8 hours
- public static final long FREQUENT_RESTARTS_THRESHOLD = 8;
-
public static final ChatState DEFAULT_CHATSTATE = ChatState.ACTIVE;
public static final int TYPING_TIMEOUT = 5;
diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java
index 09a018d3d..cc334addb 100644
--- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java
+++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java
@@ -49,6 +49,7 @@ import de.pixart.messenger.entities.Roster;
import de.pixart.messenger.entities.ServiceDiscoveryResult;
import de.pixart.messenger.services.ShortcutService;
import de.pixart.messenger.utils.CryptoHelper;
+import de.pixart.messenger.utils.Resolver;
import de.pixart.messenger.xmpp.jid.InvalidJidException;
import de.pixart.messenger.xmpp.jid.Jid;
import de.pixart.messenger.xmpp.mam.MamReference;
@@ -58,7 +59,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null;
public static final String DATABASE_NAME = "history";
- public static final int DATABASE_VERSION = 39; // = Conversations DATABASE_VERSION + 1
+ public static final int DATABASE_VERSION = 40; // = Conversations DATABASE_VERSION + 1
private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@@ -145,8 +146,17 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ ") ON CONFLICT IGNORE"
+ ");";
- private static String START_TIMES_TABLE = "start_times";
- private static String CREATE_START_TIMES_TABLE = "create table " + START_TIMES_TABLE + " (timestamp NUMBER);";
+ private static String RESOLVER_RESULTS_TABLENAME = "resolver_results";
+ private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
+ + Resolver.Result.DOMAIN + " TEXT,"
+ + Resolver.Result.HOSTNAME + " TEXT,"
+ + Resolver.Result.IP + " BLOB,"
+ + Resolver.Result.PRIORITY + " NUMBER,"
+ + Resolver.Result.DIRECT_TLS + " NUMBER,"
+ + Resolver.Result.AUTHENTICATED + " NUMBER,"
+ + Resolver.Result.PORT + " NUMBER,"
+ + "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
+ + ");";
private static String CREATE_MESSAGE_TIME_INDEX = "create INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
private static String CREATE_MESSAGE_CONVERSATION_INDEX = "create INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
@@ -209,7 +219,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);
+ db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
}
@Override
@@ -366,9 +376,6 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
}
- if (oldVersion < 30 && newVersion >= 30) {
- db.execSQL(CREATE_START_TIMES_TABLE);
- }
if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
@@ -475,6 +482,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
if (oldVersion < 39 && newVersion >= 39) {
db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
}
+
+ if (oldVersion < 40 && newVersion >= 40) {
+ db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
+ }
}
private static ContentValues createFingerprintStatusContentValues(FingerprintStatus.Trust trust, boolean active) {
@@ -615,6 +626,28 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return result;
}
+ public void saveResolverResult(String domain, Resolver.Result result) {
+ SQLiteDatabase db = this.getWritableDatabase();
+ ContentValues contentValues = result.toContentValues();
+ contentValues.put(Resolver.Result.DOMAIN, domain);
+ db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
+ }
+
+ public Resolver.Result findResolverResult(String domain) {
+ SQLiteDatabase db = this.getReadableDatabase();
+ String where = Resolver.Result.DOMAIN + "=?";
+ String[] whereArgs = {domain};
+ Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
+ Resolver.Result result = null;
+ if (cursor != null) {
+ if (cursor.moveToFirst()) {
+ result = Resolver.Result.fromCursor(cursor);
+ }
+ cursor.close();
+ }
+ return result;
+ }
+
public void insertPresenceTemplate(PresenceTemplate template) {
SQLiteDatabase db = this.getWritableDatabase();
db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
@@ -1442,37 +1475,6 @@ public class DatabaseBackend extends SQLiteOpenHelper {
deleteArgs);
}
- public boolean startTimeCountExceedsThreshold() {
- SQLiteDatabase db = this.getWritableDatabase();
- long cleanBeforeTimestamp = System.currentTimeMillis() - Config.FREQUENT_RESTARTS_DETECTION_WINDOW;
- db.execSQL("delete from " + START_TIMES_TABLE + " where timestamp < " + cleanBeforeTimestamp);
- ContentValues values = new ContentValues();
- values.put("timestamp", System.currentTimeMillis());
- db.insert(START_TIMES_TABLE, null, values);
- String[] columns = new String[]{"count(timestamp)"};
- Cursor cursor = db.query(START_TIMES_TABLE, columns, null, null, null, null, null);
- int count;
- if (cursor.moveToFirst()) {
- count = cursor.getInt(0);
- } else {
- count = 0;
- }
- cursor.close();
- Log.d(Config.LOGTAG, "start time counter reached " + count);
- return count >= Config.FREQUENT_RESTARTS_THRESHOLD;
- }
-
- public void clearStartTimeCounter(boolean justOne) {
- SQLiteDatabase db = this.getWritableDatabase();
- if (justOne) {
- db.execSQL("delete from "+START_TIMES_TABLE+" where timestamp in (select timestamp from "+START_TIMES_TABLE+" order by timestamp desc limit 1)");
- Log.d(Config.LOGTAG,"do not count start up after being swiped away");
- } else {
- Log.d(Config.LOGTAG,"resetting start time counter");
- db.execSQL("delete from " + START_TIMES_TABLE);
- }
- }
-
public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
SQLiteDatabase db = this.getReadableDatabase();
final String SQL = "select " + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + " from " + Conversation.TABLENAME + " join " + Message.TABLENAME + " on conversations.uuid=messages.conversationUuid where messages.status!=0 and carbon==0 and conversations.mode=0 and messages.timeSent>=? group by conversations.uuid order by count(body) desc limit 4;";
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
index 6dbd07209..eee8a30ac 100644
--- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
+++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java
@@ -1085,10 +1085,6 @@ public class XmppConnectionService extends Service {
Log.d(Config.LOGTAG, "restoring accounts...");
this.accounts = databaseBackend.getAccounts();
- if (databaseBackend.startTimeCountExceedsThreshold()) {
- Log.d(Config.LOGTAG, "number of restarts exceeds threshold.");
- }
-
final SharedPreferences.Editor editor = getPreferences().edit();
if (this.accounts.size() == 0 && Arrays.asList("Sony", "Sony Ericsson").contains(Build.MANUFACTURER)) {
editor.putBoolean(SettingsActivity.SHOW_FOREGROUND_SERVICE, true);
@@ -1199,7 +1195,6 @@ public class XmppConnectionService extends Service {
private void logoutAndSave(boolean stop) {
int activeAccounts = 0;
- databaseBackend.clearStartTimeCounter(true); // regular swipes don't count towards restart counter
for (final Account account : accounts) {
if (account.getStatus() != Account.State.DISABLED) {
activeAccounts++;
diff --git a/src/main/java/de/pixart/messenger/utils/Resolver.java b/src/main/java/de/pixart/messenger/utils/Resolver.java
index ddff9cb59..e4a612f5f 100644
--- a/src/main/java/de/pixart/messenger/utils/Resolver.java
+++ b/src/main/java/de/pixart/messenger/utils/Resolver.java
@@ -1,11 +1,14 @@
package de.pixart.messenger.utils;
+import android.content.ContentValues;
+import android.database.Cursor;
import android.support.annotation.NonNull;
import android.util.Log;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -40,6 +43,7 @@ public class Resolver {
private static XmppConnectionService SERVICE = null;
+
public static void init(XmppConnectionService service) {
Resolver.SERVICE = service;
DNSClient.removeDNSServerLookupMechanism(AndroidUsingExec.INSTANCE);
@@ -50,7 +54,6 @@ public class Resolver {
public static List<Result> resolve(String domain) throws NetworkIsUnreachableException {
List<Result> results = new ArrayList<>();
HashSet<String> messages = new HashSet<>();
-
try {
results.addAll(resolveSrv(domain, true));
} catch (MultipleIoException e) {
@@ -173,6 +176,41 @@ public class Resolver {
}
public static class Result implements Comparable<Result> {
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ Result result = (Result) o;
+
+ if (port != result.port) return false;
+ if (directTls != result.directTls) return false;
+ if (authenticated != result.authenticated) return false;
+ if (priority != result.priority) return false;
+ if (ip != null ? !ip.equals(result.ip) : result.ip != null) return false;
+ return hostname != null ? hostname.equals(result.hostname) : result.hostname == null;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = ip != null ? ip.hashCode() : 0;
+ result = 31 * result + (hostname != null ? hostname.hashCode() : 0);
+ result = 31 * result + port;
+ result = 31 * result + (directTls ? 1 : 0);
+ result = 31 * result + (authenticated ? 1 : 0);
+ result = 31 * result + priority;
+ return result;
+ }
+
+ public static final String DOMAIN = "domain";
+
+ public static final String IP = "ip";
+ public static final String HOSTNAME = "hostname";
+ public static final String PORT = "port";
+ public static final String PRIORITY = "priority";
+ public static final String DIRECT_TLS = "directTls";
+ public static final String AUTHENTICATED = "authenticated";
+
private InetAddress ip;
private DNSName hostname;
private int port = 5222;
@@ -255,6 +293,32 @@ public class Resolver {
public static Result createDefault(DNSName hostname) {
return createDefault(hostname, null);
}
+
+ public static Result fromCursor(Cursor cursor) {
+ final Result result = new Result();
+ try {
+ result.ip = InetAddress.getByAddress(cursor.getBlob(cursor.getColumnIndex(IP)));
+ } catch (UnknownHostException e) {
+ result.ip = null;
+ }
+ result.hostname = DNSName.from(cursor.getString(cursor.getColumnIndex(HOSTNAME)));
+ result.port = cursor.getInt(cursor.getColumnIndex(PORT));
+ result.priority = cursor.getInt(cursor.getColumnIndex(PRIORITY));
+ result.authenticated = cursor.getInt(cursor.getColumnIndex(AUTHENTICATED)) > 0;
+ result.directTls = cursor.getInt(cursor.getColumnIndex(DIRECT_TLS)) > 0;
+ return result;
+ }
+
+ public ContentValues toContentValues() {
+ final ContentValues contentValues = new ContentValues();
+ contentValues.put(IP, ip.getAddress());
+ contentValues.put(HOSTNAME, hostname.toString());
+ contentValues.put(PORT, port);
+ contentValues.put(PRIORITY, priority);
+ contentValues.put(DIRECT_TLS, directTls ? 1 : 0);
+ contentValues.put(AUTHENTICATED, authenticated ? 1 : 0);
+ return contentValues;
+ }
}
public static class NetworkIsUnreachableException extends Exception {
diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
index 58329609a..c6b913677 100644
--- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
+++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
@@ -380,7 +380,18 @@ public class XmppConnection implements Runnable {
throw new IOException(e.getMessage());
}
} else {
+ final String domain = account.getJid().getDomainpart();
List<Resolver.Result> results = Resolver.resolve(account.getJid().getDomainpart());
+ Resolver.Result storedBackupResult;
+ if (!Thread.currentThread().isInterrupted()) {
+ storedBackupResult = mXmppConnectionService.databaseBackend.findResolverResult(domain);
+ if (storedBackupResult != null && !results.contains(storedBackupResult)) {
+ results.add(storedBackupResult);
+ Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": loaded backup resolver result from db: " + storedBackupResult);
+ }
+ } else {
+ storedBackupResult = null;
+ }
for (Iterator<Resolver.Result> iterator = results.iterator(); iterator.hasNext(); ) {
final Resolver.Result result = iterator.next();
if (Thread.currentThread().isInterrupted()) {
@@ -430,6 +441,9 @@ public class XmppConnection implements Runnable {
}
if (startXmpp(localSocket)) {
+ if (!result.equals(storedBackupResult)) {
+ mXmppConnectionService.databaseBackend.saveResolverResult(domain, result);
+ }
break; // successfully connected to server that speaks xmpp
} else {
localSocket.close();