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

View File

@@ -41,21 +41,26 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
private NewAsyncQueue mQueue = new NewAsyncQueue(); private NewAsyncQueue mQueue = new NewAsyncQueue();
boolean mBound = false; private boolean mBound = false;
boolean mWaitForBound = false; private boolean mWaitForBound = false;
private boolean mWaitForUnbound = false;
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance // We've bound to LocalService, cast the IBinder and get LocalService instance
KVV.LocalBinder binder = (KVV.LocalBinder) service; KVV.LocalBinder binder = (KVV.LocalBinder) service;
mKVV = binder.getService(); mKVV = binder.getService();
if (mWaitForBound) mQueue.next();
mBound = true; mBound = true;
if (mWaitForBound) mQueue.next();
mWaitForBound = false;
} }
@Override @Override
public void onServiceDisconnected(ComponentName arg0) { public void onServiceDisconnected(ComponentName arg0) {
mBound = false; mBound = false;
mKVV = null;
if (mWaitForUnbound) mQueue.next();
mWaitForUnbound = false;
} }
}; };
@@ -63,7 +68,7 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
/** /**
* Set up the sync adapter * Set up the sync adapter
*/ */
public KVVSyncAdapter(Context context, boolean autoInitialize) { KVVSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize); super(context, autoInitialize);
init(context); init(context);
} }
@@ -82,17 +87,6 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
private void init(Context context) { private void init(Context context) {
// Bind to LocalService // 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, String authority,
ContentProviderClient provider, ContentProviderClient provider,
SyncResult syncResult) { 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(() -> { mQueue.add(() -> {
if (!mKVV.account().isLoggedIn()) { if (!mKVV.account().isLoggedIn()) {
log.w("Not logged in!"); log.w("Not logged in!");
@@ -151,6 +156,11 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
mQueue.next(); mQueue.next();
}, true); }, 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) { 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) {