Enable/disable via sync service

This commit is contained in:
M66B 2021-03-29 08:37:45 +02:00
parent 386475a4d9
commit 9ec584f08e
3 changed files with 22 additions and 7 deletions

View File

@ -38,10 +38,10 @@ import java.util.List;
import java.util.concurrent.ExecutorService;
public class ServiceExternal extends Service {
static final String ACTION_POLL = BuildConfig.APPLICATION_ID + ".POLL";
static final String ACTION_ENABLE = BuildConfig.APPLICATION_ID + ".ENABLE";
static final String ACTION_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
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_DISABLE = BuildConfig.APPLICATION_ID + ".DISABLE";
private static final String ACTION_DISCONNECT_ME = BuildConfig.APPLICATION_ID + ".DISCONNECT.ME";
static final int PI_WIDGET_ENABLE = 1;

View File

@ -152,6 +152,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
static final int PI_ALARM = 1;
static final int PI_BACKOFF = 2;
static final int PI_KEEPALIVE = 3;
static final int PI_ENABLE = 4;
@Override
public void onCreate() {
@ -792,6 +793,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
if (action != null)
try {
switch (action.split(":")[0]) {
case "enable":
onEnable(intent);
break;
case "eval":
onEval(intent);
break;
@ -827,6 +832,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
return START_STICKY;
}
private void onEnable(Intent intent) {
boolean enabled = intent.getBooleanExtra("enabled", true);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("enabled", enabled).apply();
onEval(intent);
}
private void onEval(Intent intent) {
Bundle command = new Bundle();
command.putString("name", "eval");

View File

@ -39,9 +39,12 @@ public class WidgetSync extends AppWidgetProvider {
boolean enabled = prefs.getBoolean("enabled", true);
try {
Intent intent = new Intent(enabled ? ServiceExternal.ACTION_DISABLE : ServiceExternal.ACTION_ENABLE);
PendingIntent pi = PendingIntentCompat.getService(
context, ServiceExternal.PI_WIDGET_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ServiceSynchronize.class)
.setAction("enable")
.putExtra("enabled", !enabled);
PendingIntent pi = PendingIntentCompat.getForegroundService(
context, ServiceSynchronize.PI_ENABLE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
for (int appWidgetId : appWidgetIds) {
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);