Unbind KVV service

This commit is contained in:
Caesar2011
2019-02-10 20:25:44 +01:00
parent 6048714563
commit b4e11e3d10
2 changed files with 33 additions and 15 deletions

View File

@@ -14,12 +14,14 @@ import java.util.HashMap;
import de.sebse.fuplanner.services.kvv.types.LoginTokenKVV;
import de.sebse.fuplanner.tools.CustomAccountManager;
import de.sebse.fuplanner.tools.logging.Logger;
public class KVV extends Service {
private final HashMap<String, Object> addons = new HashMap<>();
private final HashMap<String, KVVListener> mListeners = new HashMap<>();
private final KVVListener mListener = new KVVListener() {
CustomAccountManager accountManager = null;
@Override
public CustomAccountManager getAccountManager() {
if (accountManager == null)
@@ -51,6 +53,12 @@ public class KVV extends Service {
listener.onModuleListChange();
}
};
public KVV() {
Logger log = new Logger(this);
log.d("constructor");
}
// Binder given to clients
private final IBinder mBinder = new LocalBinder();

View File

@@ -41,21 +41,26 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
private NewAsyncQueue mQueue = new NewAsyncQueue();
boolean mBound = false;
boolean mWaitForBound = false;
private boolean mBound = false;
private boolean mWaitForBound = false;
private boolean mWaitForUnbound = false;
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
KVV.LocalBinder binder = (KVV.LocalBinder) service;
mKVV = binder.getService();
if (mWaitForBound) mQueue.next();
mBound = true;
if (mWaitForBound) mQueue.next();
mWaitForBound = false;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
mKVV = null;
if (mWaitForUnbound) mQueue.next();
mWaitForUnbound = false;
}
};
@@ -63,7 +68,7 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
/**
* Set up the sync adapter
*/
public KVVSyncAdapter(Context context, boolean autoInitialize) {
KVVSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
init(context);
}
@@ -82,17 +87,6 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
private void init(Context context) {
// Bind to LocalService
Intent intent = new Intent(getContext(), KVV.class);
getContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
if (!mBound) {
mWaitForBound = true;
mQueue.add(() -> {});
}
mQueue.add(() -> {
mKVV.account().restoreOnlineLogin(bool -> {
mQueue.next();
});
});
}
/*
@@ -107,6 +101,17 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
String authority,
ContentProviderClient provider,
SyncResult syncResult) {
Intent intent = new Intent(getContext(), KVV.class);
getContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
if (!mBound) {
mWaitForBound = true;
mQueue.add(() -> {});
}
mQueue.add(() -> {
mKVV.account().restoreOnlineLogin(bool -> {
mQueue.next();
});
});
mQueue.add(() -> {
if (!mKVV.account().isLoggedIn()) {
log.w("Not logged in!");
@@ -151,6 +156,11 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
mQueue.next();
}, true);
});
getContext().unbindService(mConnection);
if (mBound) {
mWaitForUnbound = true;
mQueue.add(() -> {});
}
}
private <T> void sendNotifications(Iterable<T> oldList, Iterable<T> newList, String title, StringInterface<T> titleInterface, StringInterface<T> idInterface, String moduleId, int modulePart, @StringRes int updateRes, @StringRes int addRes, @StringRes int removeRes) {