From b4e11e3d10410ec922fba74c4176d5bea3bf26c1 Mon Sep 17 00:00:00 2001 From: Caesar2011 Date: Sun, 10 Feb 2019 20:25:44 +0100 Subject: [PATCH] Unbind KVV service --- .../de/sebse/fuplanner/services/kvv/KVV.java | 8 ++++ .../services/kvv/sync/KVVSyncAdapter.java | 40 ++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java index c2c87b2..aae95ac 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/KVV.java @@ -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 addons = new HashMap<>(); private final HashMap 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(); diff --git a/app/src/main/java/de/sebse/fuplanner/services/kvv/sync/KVVSyncAdapter.java b/app/src/main/java/de/sebse/fuplanner/services/kvv/sync/KVVSyncAdapter.java index 29c2376..d52c6e5 100644 --- a/app/src/main/java/de/sebse/fuplanner/services/kvv/sync/KVVSyncAdapter.java +++ b/app/src/main/java/de/sebse/fuplanner/services/kvv/sync/KVVSyncAdapter.java @@ -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 void sendNotifications(Iterable oldList, Iterable newList, String title, StringInterface titleInterface, StringInterface idInterface, String moduleId, int modulePart, @StringRes int updateRes, @StringRes int addRes, @StringRes int removeRes) {