Added external enable/disable account

This commit is contained in:
M66B 2019-08-03 21:51:02 +02:00
parent 1053ba3ac1
commit 3cb8ffd0cd
3 changed files with 51 additions and 9 deletions

15
FAQ.md
View File

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

View File

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

View File

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