|
|
@ -29,6 +29,10 @@ import androidx.annotation.Nullable;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
@ -37,10 +41,12 @@ public class ServiceExternal extends Service {
|
|
|
|
private static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
|
|
|
|
private static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
|
|
|
|
private static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
|
|
|
|
private static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
|
|
|
|
private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
|
|
|
|
private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
|
|
|
|
|
|
|
|
private static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
|
|
|
|
|
|
|
|
|
|
|
|
// adb shell am startservice -a eu.faircode.email.POLL --es account Gmail
|
|
|
|
// adb shell am startservice -a eu.faircode.email.POLL --es account Gmail
|
|
|
|
// adb shell am startservice -a eu.faircode.email.ENABLE --es account 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
|
|
|
|
// adb shell am startservice -a eu.faircode.email.DISABLE --es account Gmail
|
|
|
|
|
|
|
|
// adb shell am startservice -a eu.faircode.email.DISCONNECT
|
|
|
|
|
|
|
|
|
|
|
|
private static final ExecutorService executor =
|
|
|
|
private static final ExecutorService executor =
|
|
|
|
Helper.getBackgroundExecutor(1, "external");
|
|
|
|
Helper.getBackgroundExecutor(1, "external");
|
|
|
@ -72,52 +78,35 @@ public class ServiceExternal extends Service {
|
|
|
|
if (intent == null)
|
|
|
|
if (intent == null)
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
|
|
|
|
|
|
|
if (!ActivityBilling.isPro(this))
|
|
|
|
final String action = intent.getAction();
|
|
|
|
|
|
|
|
boolean pro = ActivityBilling.isPro(this);
|
|
|
|
|
|
|
|
EntityLog.log(this, action + " pro=" + pro);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!pro)
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
|
|
|
|
|
|
|
final Context context = getApplicationContext();
|
|
|
|
final Context context = getApplicationContext();
|
|
|
|
final String accountName = intent.getStringExtra("account");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final Boolean enabled;
|
|
|
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
if (ACTION_ENABLE.equals(action))
|
|
|
|
|
|
|
|
enabled = true;
|
|
|
|
|
|
|
|
else if (ACTION_DISABLE.equals(action))
|
|
|
|
|
|
|
|
enabled = false;
|
|
|
|
|
|
|
|
else // poll
|
|
|
|
|
|
|
|
enabled = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
if (enabled == null) {
|
|
|
|
case ACTION_POLL:
|
|
|
|
List<EntityAccount> accounts = db.account().getSynchronizingAccounts();
|
|
|
|
poll(context, intent);
|
|
|
|
for (EntityAccount account : accounts)
|
|
|
|
break;
|
|
|
|
if (accountName == null || accountName.equals(account.name)) {
|
|
|
|
case ACTION_ENABLE:
|
|
|
|
List<EntityFolder> folders = db.folder().getSynchronizingFolders(account.id);
|
|
|
|
case ACTION_DISABLE:
|
|
|
|
if (folders.size() > 0)
|
|
|
|
set(context, intent);
|
|
|
|
Collections.sort(folders, folders.get(0).getComparator(context));
|
|
|
|
break;
|
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
case ACTION_DISCONNECT_ME:
|
|
|
|
EntityOperation.sync(context, folder.id, false);
|
|
|
|
disconnect(context, intent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
ServiceSynchronize.eval(context, "external poll account=" + accountName);
|
|
|
|
default:
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException(action);
|
|
|
|
if (accountName == null) {
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
prefs.edit().putBoolean("enabled", enabled).apply();
|
|
|
|
|
|
|
|
ServiceSynchronize.eval(context, "external enabled=" + enabled);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
EntityAccount account = db.account().getAccount(accountName);
|
|
|
|
|
|
|
|
if (account == null) {
|
|
|
|
|
|
|
|
EntityLog.log(context, "Account not found name=" + accountName);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.account().setAccountSynchronize(account.id, enabled);
|
|
|
|
|
|
|
|
ServiceSynchronize.eval(context, "external account=" + accountName + " enabled=" + enabled);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
EntityLog.log(context, Log.formatThrowable(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -149,4 +138,53 @@ public class ServiceExternal extends Service {
|
|
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void poll(Context context, Intent intent) {
|
|
|
|
|
|
|
|
String accountName = intent.getStringExtra("account");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
List<EntityAccount> accounts;
|
|
|
|
|
|
|
|
if (accountName == null)
|
|
|
|
|
|
|
|
accounts = db.account().getSynchronizingAccounts();
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
EntityAccount account = db.account().getAccount(accountName);
|
|
|
|
|
|
|
|
if (account == null)
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Account not found name=" + accountName);
|
|
|
|
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
|
|
|
|
accounts.add(account);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (EntityAccount account : accounts) {
|
|
|
|
|
|
|
|
List<EntityFolder> folders = db.folder().getSynchronizingFolders(account.id);
|
|
|
|
|
|
|
|
if (folders.size() > 0)
|
|
|
|
|
|
|
|
Collections.sort(folders, folders.get(0).getComparator(context));
|
|
|
|
|
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
|
|
|
|
EntityOperation.sync(context, folder.id, false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServiceSynchronize.eval(context, "external poll account=" + accountName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void set(Context context, Intent intent) {
|
|
|
|
|
|
|
|
String accountName = intent.getStringExtra("account");
|
|
|
|
|
|
|
|
boolean enabled = ACTION_ENABLE.equals(intent.getAction());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
if (accountName == null) {
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
|
|
|
prefs.edit().putBoolean("enabled", enabled).apply();
|
|
|
|
|
|
|
|
ServiceSynchronize.eval(context, "external enabled=" + enabled);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
EntityAccount account = db.account().getAccount(accountName);
|
|
|
|
|
|
|
|
if (account == null)
|
|
|
|
|
|
|
|
throw new IllegalArgumentException("Account not found name=" + accountName);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.account().setAccountSynchronize(account.id, enabled);
|
|
|
|
|
|
|
|
ServiceSynchronize.eval(context, "external account=" + accountName + " enabled=" + enabled);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void disconnect(Context context, Intent intent) throws IOException, JSONException {
|
|
|
|
|
|
|
|
DisconnectBlacklist.download(context);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|