From 50d436fd819a667eadc30a5697d41ba48b855700 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 1 Oct 2017 18:44:28 +0200 Subject: [PATCH] EventReceiver: use setting to store enabled accounts --- .../persistance/DatabaseBackend.java | 19 ------------------- .../conversations/services/EventReceiver.java | 15 ++++++++++++++- .../services/XmppConnectionService.java | 6 ++++++ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 9d5eda22b..34d62608d 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -760,25 +760,6 @@ public class DatabaseBackend extends SQLiteOpenHelper { return rows == 1; } - public boolean hasEnabledAccounts() { - SQLiteDatabase db = this.getReadableDatabase(); - Cursor cursor = db.rawQuery("select count(" + Account.UUID + ") from " - + Account.TABLENAME + " where not options & (1 <<1)", null); - try { - cursor.moveToFirst(); - int count = cursor.getInt(0); - return (count > 0); - } catch (SQLiteCantOpenDatabaseException e) { - return true; // better safe than sorry - } catch (RuntimeException e) { - return true; // better safe than sorry - } finally { - if (cursor != null) { - cursor.close(); - } - } - } - @Override public SQLiteDatabase getWritableDatabase() { SQLiteDatabase db = super.getWritableDatabase(); diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index 10fff73dc..0806ee9d7 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -3,10 +3,17 @@ package eu.siacs.conversations.services; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; +import eu.siacs.conversations.Config; import eu.siacs.conversations.persistance.DatabaseBackend; public class EventReceiver extends BroadcastReceiver { + + public static final String SETTING_ENABLED_ACCOUNTS = "enabled_accounts"; + @Override public void onReceive(Context context, Intent intent) { Intent mIntentForService = new Intent(context, XmppConnectionService.class); @@ -16,9 +23,15 @@ public class EventReceiver extends BroadcastReceiver { mIntentForService.setAction("other"); } final String action = intent.getAction(); - if (action.equals("ui") || DatabaseBackend.getInstance(context).hasEnabledAccounts()) { + if (action.equals("ui") || hasEnabledAccounts(context)) { context.startService(mIntentForService); + } else { + Log.d(Config.LOGTAG,"EventReceiver ignored action "+mIntentForService.getAction()); } } + public boolean hasEnabledAccounts(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(SETTING_ENABLED_ACCOUNTS,true); + } + } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9ae69f435..69ac9070a 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1748,9 +1748,14 @@ public class XmppConnectionService extends Service { this.accounts.add(account); this.reconnectAccountInBackground(account); updateAccountUi(); + syncEnabledAccountSetting(); toggleForegroundService(); } + private void syncEnabledAccountSetting() { + getPreferences().edit().putBoolean(EventReceiver.SETTING_ENABLED_ACCOUNTS,hasEnabledAccounts()).apply(); + } + public void createAccountFromKey(final String alias, final OnAccountCreated callback) { new Thread(new Runnable() { @Override @@ -1823,6 +1828,7 @@ public class XmppConnectionService extends Service { updateAccountUi(); getNotificationService().updateErrorNotification(); toggleForegroundService(); + syncEnabledAccountSetting(); return true; } else { return false;