EventReceiver: use setting to store enabled accounts

This commit is contained in:
Daniel Gultsch 2017-10-01 18:44:28 +02:00
parent a288ad8d6b
commit 50d436fd81
3 changed files with 20 additions and 20 deletions

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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;