Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 075451e9 authored by David Luhmer's avatar David Luhmer
Browse files

add support to stop/destroy api if not needed anymore (as shown in the readme example)

parent e8c64a0e
Loading
Loading
Loading
Loading
+25 −8
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ public class NextcloudAPI {
    }

    public NextcloudAPI(Context context, SingleSignOnAccount account, Gson gson, ApiConnectedListener callback) {
        this.context = context; // memory leaks..??
        this.mContext = context;
        this.mAccount = account;
        this.gson = gson;
        this.mCallback = callback;
@@ -80,9 +80,11 @@ public class NextcloudAPI {
    private Gson gson;
    private IInputStreamService mService = null;
    private boolean mBound = false; // Flag indicating whether we have called bind on the service
    private boolean mDestroyed = false; // Flag indicating if API is destroyed
    private SingleSignOnAccount mAccount;
    private ApiConnectedListener mCallback;
    private Context context;
    private Context mContext;


    private String getAccountName() {
        return mAccount.name;
@@ -102,15 +104,19 @@ public class NextcloudAPI {
    }

    private void connect() {
        if(mDestroyed) {
            throw new IllegalStateException("API already destroyed! You cannot reuse a stopped API instance");
        }

        // Disconnect if connected
        if(mBound) {
            stop(context);
            stop();
        }

        try {
            Intent intentService = new Intent();
            intentService.setComponent(new ComponentName("com.nextcloud.client", "com.owncloud.android.services.AccountManagerService"));
            if (!context.bindService(intentService, mConnection, Context.BIND_AUTO_CREATE)) {
            if (!mContext.bindService(intentService, mConnection, Context.BIND_AUTO_CREATE)) {
                Log.d(TAG, "Binding to AccountManagerService returned false");
                throw new IllegalStateException("Binding to AccountManagerService returned false");
            }
@@ -121,11 +127,21 @@ public class NextcloudAPI {
    }


    public void stop(Context context) {
    public void stop() {
        gson = null;
        mDestroyed = true;
        mAccount = null;
        mCallback = null;

        // Unbind from the service
        if (mBound) {
            context.unbindService(mConnection);
            if(mContext != null) {
                mContext.unbindService(mConnection);
            } else {
                Log.e(TAG, "Context was null, cannot unbind nextcloud single sign-on service connection!");
            }
            mBound = false;
            mContext = null;
        }
    }

@@ -149,9 +165,10 @@ public class NextcloudAPI {
            mService = null;
            mBound = false;


            if(!mDestroyed) {
                connectApiWithBackoff();
            }
        }
    };