Simplified reload

pull/50/head
M66B 6 years ago
parent 3b52e9ddda
commit 4a5407e48f

@ -421,9 +421,6 @@ public class FragmentAccount extends FragmentEx {
if (TextUtils.isEmpty(name))
name = host + "/" + user;
try {
ServiceSynchronize.stopSynchronous(getContext(), "save account");
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
@ -500,10 +497,9 @@ public class FragmentAccount extends FragmentEx {
db.endTransaction();
}
ServiceSynchronize.reload(getContext(), "save account");
return null;
} finally {
ServiceSynchronize.start(getContext());
}
}
@Override
@ -544,15 +540,10 @@ public class FragmentAccount extends FragmentEx {
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
try {
ServiceSynchronize.stopSynchronous(getContext(), "delete account");
long id = args.getLong("id");
DB.getInstance(context).account().deleteAccount(id);
ServiceSynchronize.reload(getContext(), "delete account");
return null;
} finally {
ServiceSynchronize.start(getContext());
}
}
@Override

@ -76,9 +76,6 @@ public class FragmentFolder extends FragmentEx {
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
try {
ServiceSynchronize.stopSynchronous(getContext(), "save folder");
long id = args.getLong("id");
boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after");
@ -101,10 +98,9 @@ public class FragmentFolder extends FragmentEx {
db.endTransaction();
}
ServiceSynchronize.reload(getContext(), "save folder");
return null;
} finally {
ServiceSynchronize.start(getContext());
}
}
@Override

@ -253,9 +253,6 @@ public class FragmentIdentity extends FragmentEx {
}
}
try {
ServiceSynchronize.stopSynchronous(getContext(), "save identity");
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
@ -292,10 +289,9 @@ public class FragmentIdentity extends FragmentEx {
db.endTransaction();
}
ServiceSynchronize.reload(getContext(), "save identity");
return null;
} finally {
ServiceSynchronize.start(getContext());
}
}
@Override
@ -335,15 +331,10 @@ public class FragmentIdentity extends FragmentEx {
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
try {
ServiceSynchronize.stopSynchronous(getContext(), "delete identity");
long id = args.getLong("id");
DB.getInstance(context).identity().deleteIdentity(id);
ServiceSynchronize.reload(getContext(), "delete identity");
return null;
} finally {
ServiceSynchronize.start(getContext());
}
}
@Override

@ -23,11 +23,9 @@ import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.Network;
@ -35,10 +33,8 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils;
@ -186,10 +182,13 @@ public class ServiceSynchronize extends LifecycleService {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(Helper.TAG, "Service start intent=" + intent);
Log.i(Helper.TAG, "Service command intent=" + intent);
super.onStartCommand(intent, flags, startId);
if (intent != null && "unseen".equals(intent.getAction())) {
if (intent != null)
if ("reload".equals(intent.getAction()))
serviceManager.restart();
else if ("unseen".equals(intent.getAction())) {
Bundle args = new Bundle();
args.putLong("time", new Date().getTime());
@ -218,6 +217,7 @@ public class ServiceSynchronize extends LifecycleService {
Log.i(Helper.TAG, "Updated seen until");
}
}.load(ServiceSynchronize.this, args);
}
return START_STICKY;
@ -1490,6 +1490,23 @@ public class ServiceSynchronize extends LifecycleService {
}
}
private void restart() {
lifecycle.submit(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Stopping service");
stop(true);
}
});
lifecycle.submit(new Runnable() {
@Override
public void run() {
Log.i(Helper.TAG, "Starting service");
start();
}
});
}
private BroadcastReceiver outboxReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@ -1541,66 +1558,15 @@ public class ServiceSynchronize extends LifecycleService {
}
}
private IBinder binder = new LocalBinder();
private class LocalBinder extends Binder {
ServiceSynchronize getService() {
return ServiceSynchronize.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public void quit() {
Log.i(Helper.TAG, "Service quit");
serviceManager.stop(false);
Log.i(Helper.TAG, "Service quited");
stopSelf();
}
public static void start(Context context) {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class));
}
public static void stopSynchronous(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'");
final Semaphore semaphore = new Semaphore(0, true);
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(Helper.TAG, "Service connected");
((LocalBinder) binder).getService().quit();
semaphore.release();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(Helper.TAG, "Service disconnected");
semaphore.release();
}
@Override
public void onBindingDied(ComponentName name) {
Log.i(Helper.TAG, "Service died");
semaphore.release();
}
};
Intent intent = new Intent(context, ServiceSynchronize.class);
boolean exists = context.getApplicationContext().bindService(intent, connection, Context.BIND_AUTO_CREATE);
Log.i(Helper.TAG, "Service exists=" + exists);
if (exists) {
Log.i(Helper.TAG, "Service stopping");
acquire(semaphore, "service");
context.getApplicationContext().unbindService(connection);
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class));
}
Log.i(Helper.TAG, "Service stopped");
public static void reload(Context context, String reason) {
Log.i(Helper.TAG, "Reload because of '" + reason + "'");
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSynchronize.class).setAction("reload"));
}
private class ServiceState {

Loading…
Cancel
Save