From 3cb8ffd0cdb9164a9d87bbef79a5e3ff32fd3a6a Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 3 Aug 2019 21:51:02 +0200 Subject: [PATCH] Added external enable/disable account --- FAQ.md | 15 +++++++ .../java/eu/faircode/email/DaoAccount.java | 3 ++ .../eu/faircode/email/ServiceExternal.java | 44 ++++++++++++++----- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/FAQ.md b/FAQ.md index bea123343e..b27f2d09d0 100644 --- a/FAQ.md +++ b/FAQ.md @@ -1505,6 +1505,13 @@ You can also automate turning synchronization on and off by sending these comman Sending these commands will automatically turn scheduling off. +It is also possible to just enable/disable one account, for example the account with the name *Gmail*: + +``` +(adb shell) am startservice -a eu.faircode.email.ENABLE --es account Gmail +(adb shell) am startservice -a eu.faircode.email.DISABLE --es account Gmail +``` + You can automatically send commands with for example [Tasker](https://tasker.joaoapps.com/userguide/en/intents.html): ``` @@ -1514,6 +1521,14 @@ Action: eu.faircode.email.ENABLE Target: Service ``` +To enable/disable an account with the name *Gmail*: + +``` +Extras: account:Gmail +``` + +Account names are case sensitive. + Automation can be used for more advanced schedules, like for example multiple synchronization periods per day or different synchronization periods for different days. diff --git a/app/src/main/java/eu/faircode/email/DaoAccount.java b/app/src/main/java/eu/faircode/email/DaoAccount.java index d58fae3ac8..7921be8b73 100644 --- a/app/src/main/java/eu/faircode/email/DaoAccount.java +++ b/app/src/main/java/eu/faircode/email/DaoAccount.java @@ -71,6 +71,9 @@ public interface DaoAccount { @Query("SELECT * FROM account WHERE id = :id") EntityAccount getAccount(long id); + @Query("SELECT * FROM account WHERE name = :name") + EntityAccount getAccount(String name); + @Query("SELECT * FROM account WHERE `primary`") EntityAccount getPrimaryAccount(); diff --git a/app/src/main/java/eu/faircode/email/ServiceExternal.java b/app/src/main/java/eu/faircode/email/ServiceExternal.java index 75f592bb1a..60ccaef50e 100644 --- a/app/src/main/java/eu/faircode/email/ServiceExternal.java +++ b/app/src/main/java/eu/faircode/email/ServiceExternal.java @@ -20,6 +20,7 @@ package eu.faircode.email; */ import android.app.Service; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.IBinder; @@ -28,12 +29,17 @@ import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.preference.PreferenceManager; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + public class ServiceExternal extends Service { private static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE"; private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE"; - // adb shell am startservice -a eu.faircode.email.ENABLE - // adb shell am startservice -a eu.faircode.email.DISABLE + // adb shell am startservice -a eu.faircode.email.ENABLE --es account Gmail + // adb shell am startservice -a eu.faircode.email.DISABLE --es account Gmail + + private static ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); @Override @@ -57,20 +63,38 @@ public class ServiceExternal extends Service { if (!Helper.isPro(this)) return START_NOT_STICKY; - Boolean enabled = null; + final Boolean enabled; if (ACTION_ENABLE.equals(intent.getAction())) enabled = true; else if (ACTION_DISABLE.equals(intent.getAction())) enabled = false; + else + enabled = null; if (enabled != null) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - prefs.edit().putBoolean("schedule", false).apply(); - - boolean previous = prefs.getBoolean("enabled", true); - if (!enabled.equals(previous)) { - prefs.edit().putBoolean("enabled", enabled).apply(); - ServiceSynchronize.reload(this, "external"); + final String accountName = intent.getStringExtra("account"); + if (accountName == null) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + prefs.edit().putBoolean("schedule", false).apply(); + + boolean previous = prefs.getBoolean("enabled", true); + if (!enabled.equals(previous)) { + prefs.edit().putBoolean("enabled", enabled).apply(); + ServiceSynchronize.reload(this, "external"); + } + } else { + final Context context = getApplicationContext(); + executor.submit(new Runnable() { + @Override + public void run() { + DB db = DB.getInstance(context); + EntityAccount account = db.account().getAccount(accountName); + if (account != null) { + db.account().setAccountSynchronize(account.id, enabled); + ServiceSynchronize.reload(context, "account enabled=" + enabled); + } + } + }); } }