FairEmail/app/src/main/java/eu/faircode/email/ServiceExternal.java

78 lines
2.5 KiB
Java
Raw Normal View History

2019-02-14 13:28:14 +00:00
package eu.faircode.email;
2019-02-15 07:51:14 +00:00
import android.app.Service;
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;
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
2019-02-15 07:51:14 +00:00
public class ServiceExternal extends Service {
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";
2019-02-14 13:28:14 +00:00
// adb shell am startservice -a eu.faircode.email.ENABLE
// adb shell am startservice -a eu.faircode.email.DISABLE
@Override
2019-02-15 07:51:14 +00:00
public int onStartCommand(Intent intent, int flags, int startId) {
try {
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
Log.i("Received intent=" + intent);
Log.logExtras(intent);
if (intent == null)
return START_NOT_STICKY;
if (!Helper.isPro(this))
return START_NOT_STICKY;
Boolean enabled = null;
if (ACTION_ENABLE.equals(intent.getAction()))
enabled = true;
else if (ACTION_DISABLE.equals(intent.getAction()))
enabled = false;
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");
}
2019-02-14 13:28:14 +00:00
}
2019-02-15 07:51:14 +00:00
return START_NOT_STICKY;
} finally {
stopForeground(true);
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;
}
private NotificationCompat.Builder getNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "service");
2019-02-15 07:51:14 +00:00
builder
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
.setContentTitle(getString(R.string.tile_synchronize))
.setAutoCancel(false)
.setShowWhen(false)
2019-03-07 12:29:03 +00:00
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
2019-02-15 07:51:14 +00:00
return builder;
}
2019-02-14 13:28:14 +00:00
}