Fixes, improvements

pull/147/head
M66B 7 years ago
parent d4272427b5
commit 662b2b52a7

@ -58,7 +58,7 @@ public class ActivityMain extends AppCompatActivity implements FragmentManager.O
startActivity(new Intent(ActivityMain.this, ActivitySetup.class)); startActivity(new Intent(ActivityMain.this, ActivitySetup.class));
else { else {
startActivity(new Intent(ActivityMain.this, ActivityView.class)); startActivity(new Intent(ActivityMain.this, ActivityView.class));
ServiceSynchronize.init(ActivityMain.this); ServiceSynchronize.init(ActivityMain.this, false);
} }
finish(); finish();
} }

@ -98,6 +98,9 @@ import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION; import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
public class Helper { public class Helper {
static final int NOTIFICATION_SYNCHRONIZE = 1;
static final int NOTIFICATION_EXTERNAL = 2;
static final int JOB_DAILY = 1001; static final int JOB_DAILY = 1001;
static final int AUTH_TYPE_PASSWORD = 1; static final int AUTH_TYPE_PASSWORD = 1;

@ -33,7 +33,7 @@ public class ReceiverAutostart extends BroadcastReceiver {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) || if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) ||
Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) { Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) {
EntityLog.log(context, intent.getAction()); EntityLog.log(context, intent.getAction());
ServiceSynchronize.init(context); ServiceSynchronize.init(context, true);
Thread thread = new Thread(new Runnable() { Thread thread = new Thread(new Runnable() {
@Override @Override

@ -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;
}
} }

@ -141,8 +141,6 @@ public class ServiceSynchronize extends LifecycleService {
private ServiceManager serviceManager = new ServiceManager(); private ServiceManager serviceManager = new ServiceManager();
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
private static final int NOTIFICATION_SYNCHRONIZE = 1;
private static final int CONNECT_BACKOFF_START = 8; // seconds private static final int CONNECT_BACKOFF_START = 8; // seconds
private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes) private static final int CONNECT_BACKOFF_MAX = 64; // seconds (totally 2 minutes)
private static final int CONNECT_BACKOFF_AlARM = 15; // minutes private static final int CONNECT_BACKOFF_AlARM = 15; // minutes
@ -184,7 +182,7 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public void onChanged(@Nullable TupleAccountStats stats) { public void onChanged(@Nullable TupleAccountStats stats) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIFICATION_SYNCHRONIZE, getNotificationService(stats).build()); nm.notify(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(stats).build());
} }
}); });
@ -296,7 +294,7 @@ public class ServiceSynchronize extends LifecycleService {
stopForeground(true); stopForeground(true);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(NOTIFICATION_SYNCHRONIZE); nm.cancel(Helper.NOTIFICATION_SYNCHRONIZE);
super.onDestroy(); super.onDestroy();
} }
@ -307,7 +305,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i("Service command intent=" + intent + " action=" + action); Log.i("Service command intent=" + intent + " action=" + action);
Log.logExtras(intent); Log.logExtras(intent);
startForeground(NOTIFICATION_SYNCHRONIZE, getNotificationService(null).build()); startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null).build());
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
@ -334,7 +332,7 @@ public class ServiceSynchronize extends LifecycleService {
case "init": case "init":
// Network events will manage the service // Network events will manage the service
serviceManager.service_init(); serviceManager.service_init(intent.getBooleanExtra("schedule", false));
break; break;
case "schedule": case "schedule":
@ -3019,13 +3017,16 @@ public class ServiceSynchronize extends LifecycleService {
return prefs.getBoolean("enabled", true); return prefs.getBoolean("enabled", true);
} }
private void service_init() { private void service_init(boolean schedule) {
EntityLog.log(ServiceSynchronize.this, "Service init"); boolean enabled = isEnabled();
EntityLog.log(ServiceSynchronize.this,
"Service init schedule=" + schedule + " enabled=" + enabled);
next_schedule(); if (schedule) {
next_schedule();
JobDaily.schedule(ServiceSynchronize.this, enabled);
}
boolean enabled = isEnabled();
JobDaily.schedule(ServiceSynchronize.this, enabled);
if (!enabled) if (!enabled)
stopSelf(); stopSelf();
} }
@ -3350,10 +3351,11 @@ public class ServiceSynchronize extends LifecycleService {
} }
} }
public static void init(Context context) { public static void init(Context context, boolean schedule) {
ContextCompat.startForegroundService(context, ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class) new Intent(context, ServiceSynchronize.class)
.setAction("init")); .setAction("init")
.putExtra("schedule", schedule));
} }
public static void schedule(Context context) { public static void schedule(Context context) {

Loading…
Cancel
Save