2019-02-14 13:28:14 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2019-05-04 20:49:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2019-05-04 20:49:22 +00:00
|
|
|
*/
|
|
|
|
|
2019-02-15 07:51:14 +00:00
|
|
|
import android.app.Service;
|
2019-08-03 19:51:02 +00:00
|
|
|
import android.content.Context;
|
2019-02-14 13:28:14 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2019-02-15 07:51:14 +00:00
|
|
|
import android.os.IBinder;
|
2019-02-14 13:28:14 +00:00
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
2019-02-22 16:04:53 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2019-03-15 13:54:25 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-02-14 13:28:14 +00:00
|
|
|
|
2020-07-20 06:25:25 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2020-01-12 14:38:00 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2019-08-03 19:51:02 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
2019-02-15 07:51:14 +00:00
|
|
|
public class ServiceExternal extends Service {
|
2019-09-24 17:45:46 +00:00
|
|
|
private static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
|
2019-03-17 12:35:13 +00:00
|
|
|
private static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
|
|
|
|
private static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
|
2020-07-20 06:25:25 +00:00
|
|
|
private static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
|
2019-02-14 13:28:14 +00:00
|
|
|
|
2019-12-13 10:55:11 +00:00
|
|
|
// adb shell am startservice -a eu.faircode.email.POLL --es account Gmail
|
2019-08-03 19:51:02 +00:00
|
|
|
// adb shell am startservice -a eu.faircode.email.ENABLE --es account Gmail
|
|
|
|
// adb shell am startservice -a eu.faircode.email.DISABLE --es account Gmail
|
2020-07-20 06:25:25 +00:00
|
|
|
// adb shell am startservice -a eu.faircode.email.DISCONNECT
|
2019-08-03 19:51:02 +00:00
|
|
|
|
2019-10-10 11:26:44 +00:00
|
|
|
private static final ExecutorService executor =
|
|
|
|
Helper.getBackgroundExecutor(1, "external");
|
2019-02-14 13:28:14 +00:00
|
|
|
|
|
|
|
|
2019-07-30 06:46:38 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2019-09-24 17:45:46 +00:00
|
|
|
Log.i("Service external create");
|
2019-07-30 06:46:38 +00:00
|
|
|
super.onCreate();
|
|
|
|
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
|
|
|
|
}
|
|
|
|
|
2019-09-24 17:45:46 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
Log.i("Service external destroy");
|
|
|
|
stopForeground(true);
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:28:14 +00:00
|
|
|
@Override
|
2019-02-15 07:51:14 +00:00
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
try {
|
2019-09-24 17:45:46 +00:00
|
|
|
EntityLog.log(this, "Service external intent=" + intent);
|
2019-02-15 07:51:14 +00:00
|
|
|
Log.logExtras(intent);
|
|
|
|
|
2019-07-31 06:18:05 +00:00
|
|
|
super.onStartCommand(intent, flags, startId);
|
|
|
|
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
|
|
|
|
|
2019-02-15 07:51:14 +00:00
|
|
|
if (intent == null)
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
2020-07-20 06:25:25 +00:00
|
|
|
final String action = intent.getAction();
|
|
|
|
boolean pro = ActivityBilling.isPro(this);
|
|
|
|
EntityLog.log(this, action + " pro=" + pro);
|
|
|
|
|
|
|
|
if (!pro)
|
2019-02-15 07:51:14 +00:00
|
|
|
return START_NOT_STICKY;
|
|
|
|
|
2019-12-13 10:55:11 +00:00
|
|
|
final Context context = getApplicationContext();
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2020-07-20 06:25:25 +00:00
|
|
|
try {
|
|
|
|
switch (action) {
|
|
|
|
case ACTION_POLL:
|
|
|
|
poll(context, intent);
|
|
|
|
break;
|
|
|
|
case ACTION_ENABLE:
|
|
|
|
case ACTION_DISABLE:
|
|
|
|
set(context, intent);
|
|
|
|
break;
|
|
|
|
case ACTION_DISCONNECT_ME:
|
|
|
|
disconnect(context, intent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException(action);
|
2019-12-13 10:55:11 +00:00
|
|
|
}
|
2020-07-20 06:25:25 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
EntityLog.log(context, Log.formatThrowable(ex));
|
2019-12-13 10:55:11 +00:00
|
|
|
}
|
2019-02-15 07:51:14 +00:00
|
|
|
}
|
2019-12-13 10:55:11 +00:00
|
|
|
});
|
2019-02-15 07:51:14 +00:00
|
|
|
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
} finally {
|
2019-09-24 17:45:46 +00:00
|
|
|
stopSelf(startId);
|
2019-02-14 13:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-15 07:51:14 +00:00
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-22 16:04:53 +00:00
|
|
|
private NotificationCompat.Builder getNotification() {
|
2019-06-23 08:23:49 +00:00
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(this, "service")
|
|
|
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
|
|
|
.setContentTitle(getString(R.string.tile_synchronize))
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(false)
|
2020-02-10 19:06:55 +00:00
|
|
|
.setDefaults(0) // disable sound on pre Android 8
|
2019-06-23 08:23:49 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
2019-07-23 09:38:36 +00:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_SERVICE)
|
2019-10-16 13:18:59 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
|
|
|
.setLocalOnly(true);
|
2019-02-15 07:51:14 +00:00
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
2020-07-20 06:25:25 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
} 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);
|
|
|
|
}
|
2019-02-14 13:28:14 +00:00
|
|
|
}
|