Synchroneous service stop

pull/50/head
M66B 6 years ago
parent 2f2dabe919
commit 1e1022a147

@ -422,6 +422,8 @@ public class FragmentAccount extends FragmentEx {
name = host + "/" + user; name = host + "/" + user;
try { try {
ServiceSynchronize.stopSynchroneous(getContext(), "save account");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
try { try {
db.beginTransaction(); db.beginTransaction();
@ -496,7 +498,7 @@ public class FragmentAccount extends FragmentEx {
return null; return null;
} finally { } finally {
ServiceSynchronize.restart(getContext(), "account"); ServiceSynchronize.start(getContext());
} }
} }
@ -538,12 +540,15 @@ public class FragmentAccount extends FragmentEx {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
// To prevent foreign key constraints from triggering try {
long id = args.getLong("id"); ServiceSynchronize.stopSynchroneous(getContext(), "delete account");
ServiceSynchronize.stop(context, "delete account");
DB.getInstance(context).account().deleteAccount(id); long id = args.getLong("id");
ServiceSynchronize.start(context); DB.getInstance(context).account().deleteAccount(id);
return null; return null;
} finally {
ServiceSynchronize.start(getContext());
}
} }
@Override @Override

@ -77,6 +77,8 @@ public class FragmentFolder extends FragmentEx {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
try { try {
ServiceSynchronize.stopSynchroneous(getContext(), "save folder");
long id = args.getLong("id"); long id = args.getLong("id");
boolean synchronize = args.getBoolean("synchronize"); boolean synchronize = args.getBoolean("synchronize");
String after = args.getString("after"); String after = args.getString("after");
@ -99,7 +101,7 @@ public class FragmentFolder extends FragmentEx {
return null; return null;
} finally { } finally {
ServiceSynchronize.restart(getContext(), "folder"); ServiceSynchronize.start(getContext());
} }
} }

@ -253,6 +253,8 @@ public class FragmentIdentity extends FragmentEx {
} }
try { try {
ServiceSynchronize.stopSynchroneous(getContext(), "save identity");
DB db = DB.getInstance(getContext()); DB db = DB.getInstance(getContext());
try { try {
db.beginTransaction(); db.beginTransaction();
@ -288,7 +290,7 @@ public class FragmentIdentity extends FragmentEx {
return null; return null;
} finally { } finally {
ServiceSynchronize.restart(getContext(), "identity"); ServiceSynchronize.start(getContext());
} }
} }
@ -329,9 +331,15 @@ public class FragmentIdentity extends FragmentEx {
new SimpleTask<Void>() { new SimpleTask<Void>() {
@Override @Override
protected Void onLoad(Context context, Bundle args) { protected Void onLoad(Context context, Bundle args) {
long id = args.getLong("id"); try {
DB.getInstance(context).identity().deleteIdentity(id); ServiceSynchronize.stopSynchroneous(getContext(), "delete identity");
return null;
long id = args.getLong("id");
DB.getInstance(context).identity().deleteIdentity(id);
return null;
} finally {
ServiceSynchronize.start(getContext());
}
} }
@Override @Override

@ -24,17 +24,21 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Network; import android.net.Network;
import android.net.NetworkCapabilities; import android.net.NetworkCapabilities;
import android.net.NetworkRequest; import android.net.NetworkRequest;
import android.net.Uri; import android.net.Uri;
import android.os.Binder;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils; import android.text.TextUtils;
@ -177,7 +181,7 @@ public class ServiceSynchronize extends LifecycleService {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(Helper.TAG, "Service start"); Log.i(Helper.TAG, "Service start intent=" + intent);
super.onStartCommand(intent, flags, startId); super.onStartCommand(intent, flags, startId);
if (intent != null && "unseen".equals(intent.getAction())) { if (intent != null && "unseen".equals(intent.getAction())) {
@ -1502,15 +1506,73 @@ public class ServiceSynchronize extends LifecycleService {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class)); ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class));
} }
public static void stop(Context context, String reason) { private IBinder binder = new LocalBinder();
Log.i(Helper.TAG, "Stop because of '" + reason + "'");
context.stopService(new Intent(context, ServiceSynchronize.class)); private class LocalBinder extends Binder {
ServiceSynchronize getService() {
return ServiceSynchronize.this;
}
} }
public static void restart(Context context, String reason) { @Override
Log.i(Helper.TAG, "Restart because of '" + reason + "'"); public IBinder onBind(Intent intent) {
context.stopService(new Intent(context, ServiceSynchronize.class)); return binder;
start(context); }
public void quit() {
Log.i(Helper.TAG, "Service quit");
stopSelf();
}
public static void stopSynchroneous(Context context, String reason) {
Log.i(Helper.TAG, "Stop because of '" + reason + "'");
final Object lock = new Object();
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
Log.i(Helper.TAG, "Service connected");
((LocalBinder) binder).getService().quit();
synchronized (lock) {
lock.notify();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(Helper.TAG, "Service disconnected");
synchronized (lock) {
lock.notify();
}
}
@Override
public void onBindingDied(ComponentName name) {
Log.i(Helper.TAG, "Service died");
synchronized (lock) {
lock.notify();
}
}
};
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");
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
}
context.getApplicationContext().unbindService(connection);
}
Log.i(Helper.TAG, "Service stopped");
} }
private class ServiceState { private class ServiceState {

Loading…
Cancel
Save