mirror of https://github.com/M66B/FairEmail.git
parent
d4272427b5
commit
662b2b52a7
@ -1,50 +1,82 @@
|
|||||||
package eu.faircode.email;
|
package eu.faircode.email;
|
||||||
|
|
||||||
import android.app.IntentService;
|
import android.app.Notification;
|
||||||
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.IBinder;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
public class ServiceExternal extends IntentService {
|
public class ServiceExternal extends Service {
|
||||||
private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE";
|
private final static String ACTION_ENABLE = "eu.faircode.email.ENABLE";
|
||||||
private final static String ACTION_DISABLE = "eu.faircode.email.DISABLE";
|
private final static String ACTION_DISABLE = "eu.faircode.email.DISABLE";
|
||||||
|
|
||||||
// adb shell am startservice -a eu.faircode.email.ENABLE
|
// 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.DISABLE
|
||||||
|
|
||||||
public ServiceExternal() {
|
|
||||||
super(ServiceExternal.class.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServiceExternal(String name) {
|
|
||||||
super(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(@Nullable Intent intent) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if (intent == null)
|
try {
|
||||||
return;
|
startForeground(Helper.NOTIFICATION_EXTERNAL, getNotification().build());
|
||||||
|
|
||||||
if (!Helper.isPro(this))
|
Log.i("Received intent=" + intent);
|
||||||
return;
|
Log.logExtras(intent);
|
||||||
|
|
||||||
Boolean enabled = null;
|
if (intent == null)
|
||||||
if (ACTION_ENABLE.equals(intent.getAction()))
|
return START_NOT_STICKY;
|
||||||
enabled = true;
|
|
||||||
else if (ACTION_DISABLE.equals(intent.getAction()))
|
if (!Helper.isPro(this))
|
||||||
enabled = false;
|
return START_NOT_STICKY;
|
||||||
|
|
||||||
if (enabled != null) {
|
Boolean enabled = null;
|
||||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
if (ACTION_ENABLE.equals(intent.getAction()))
|
||||||
prefs.edit().putBoolean("schedule", false).apply();
|
enabled = true;
|
||||||
|
else if (ACTION_DISABLE.equals(intent.getAction()))
|
||||||
boolean previous = prefs.getBoolean("enabled", true);
|
enabled = false;
|
||||||
if (!enabled.equals(previous)) {
|
|
||||||
prefs.edit().putBoolean("enabled", enabled).apply();
|
if (enabled != null) {
|
||||||
ServiceSynchronize.reload(this, "external");
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return START_NOT_STICKY;
|
||||||
|
} finally {
|
||||||
|
stopForeground(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public IBinder onBind(Intent intent) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Notification.Builder getNotification() {
|
||||||
|
Notification.Builder builder;
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||||
|
builder = new Notification.Builder(this, "service");
|
||||||
|
else
|
||||||
|
builder = new Notification.Builder(this);
|
||||||
|
|
||||||
|
builder
|
||||||
|
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
|
||||||
|
.setContentTitle(getString(R.string.tile_synchronize))
|
||||||
|
.setAutoCancel(false)
|
||||||
|
.setShowWhen(false)
|
||||||
|
.setPriority(Notification.PRIORITY_MIN)
|
||||||
|
.setCategory(Notification.CATEGORY_STATUS)
|
||||||
|
.setVisibility(Notification.VISIBILITY_SECRET);
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue