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

Unverified Commit 6ab7ada4 authored by Andy Scherzinger's avatar Andy Scherzinger Committed by GitHub
Browse files

Merge pull request #546 from nextcloud/autoclosable

feat: Implement `java.lang.AutoClosable`
parents 451b78c0 f2d3e123
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -219,7 +219,7 @@ public class MyActivity extends AppCompatActivity {
        super.onStop();
        super.onStop();
        // Close Service Connection to Nextcloud Files App and
        // Close Service Connection to Nextcloud Files App and
        // disconnect API from Context (prevent Memory Leak)
        // disconnect API from Context (prevent Memory Leak)
        mNextcloudAPI.stop();
        mNextcloudAPI.close();
    }
    }


    private void downloadFile() {
    private void downloadFile() {
+11 −2
Original line number Original line Diff line number Diff line
@@ -128,13 +128,22 @@ public class AidlNetworkRequest extends NetworkRequest {
        connectApiWithBackoff();
        connectApiWithBackoff();
    }
    }


    public void stop() {
    @Override
        super.stop();
    public void close() {
        super.close();


        unbindService();
        unbindService();
        mContext = null;
        mContext = null;
    }
    }


    /**
     * @deprecated Use {@link #close()}
     */
    @Deprecated(forRemoval = true)
    public void stop() {
        close();
    }

    private void unbindService() {
    private void unbindService() {
        // Unbind from the service
        // Unbind from the service
        if (mBound.get()) {
        if (mBound.get()) {
+12 −3
Original line number Original line Diff line number Diff line
@@ -12,7 +12,7 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;


import java.io.InputStream;
import java.io.InputStream;


public abstract class NetworkRequest {
public abstract class NetworkRequest implements AutoCloseable {


    private static final String TAG = NetworkRequest.class.getCanonicalName();
    private static final String TAG = NetworkRequest.class.getCanonicalName();


@@ -43,16 +43,25 @@ public abstract class NetworkRequest {
            connect(mAccount.type);
            connect(mAccount.type);
        }, () -> {
        }, () -> {
            Log.e(TAG, "Unable to recover API");
            Log.e(TAG, "Unable to recover API");
            stop();
            close();
        }).start();
        }).start();
    }
    }


    protected void stop() {
    @Override
    public void close() {
        mCallback = null;
        mCallback = null;
        mAccount = null;
        mAccount = null;
        mDestroyed = true;
        mDestroyed = true;
    }
    }


    /**
     * @deprecated Use {@link #close()}
     */
    @Deprecated(forRemoval = true)
    protected void stop() {
        close();
    }

    protected String getAccountName() {
    protected String getAccountName() {
        return mAccount.name;
        return mAccount.name;
    }
    }
+12 −3
Original line number Original line Diff line number Diff line
@@ -44,7 +44,7 @@ import java.lang.reflect.Type;
import io.reactivex.Observable;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.NonNull;


public class NextcloudAPI {
public class NextcloudAPI implements AutoCloseable {


    private static final String TAG = NextcloudAPI.class.getCanonicalName();
    private static final String TAG = NextcloudAPI.class.getCanonicalName();


@@ -101,10 +101,19 @@ public class NextcloudAPI {
     *
     *
     * <p>A good place <em>depending on your actual implementation</em> might be {@link Activity#onStop()}.</p>
     * <p>A good place <em>depending on your actual implementation</em> might be {@link Activity#onStop()}.</p>
     */
     */
    @Override
    @SuppressWarnings("JavadocReference")
    @SuppressWarnings("JavadocReference")
    public void stop() {
    public void close() {
        gson = null;
        gson = null;
        networkRequest.stop();
        networkRequest.close();
    }

    /**
     * @deprecated Use {@link #close()}
     */
    @Deprecated(forRemoval = true)
    public void stop() {
        close();
    }
    }


    public <T> Observable<ParsedResponse<T>> performRequestObservableV2(final Type type, final NextcloudRequest request) {
    public <T> Observable<ParsedResponse<T>> performRequestObservableV2(final Type type, final NextcloudRequest request) {
+11 −3
Original line number Original line Diff line number Diff line
@@ -27,7 +27,7 @@ import java.security.InvalidParameterException;




/** The implementation of exponential backoff with jitter applied. */
/** The implementation of exponential backoff with jitter applied. */
public class ExponentialBackoff {
public class ExponentialBackoff implements AutoCloseable {


    private static final String TAG = ExponentialBackoff.class.getCanonicalName();
    private static final String TAG = ExponentialBackoff.class.getCanonicalName();


@@ -105,10 +105,18 @@ public class ExponentialBackoff {
    }
    }


    /** Stops the backoff, all pending messages will be removed from the message queue. */
    /** Stops the backoff, all pending messages will be removed from the message queue. */
    public void stop() {
    @Override
    public void close() {
        mRetryCounter = 0;
        mRetryCounter = 0;
        mHandlerAdapter.removeCallbacks(mRunnable);
        mHandlerAdapter.removeCallbacks(mRunnable);
    }


    /**
     * @deprecated Use {@link #close()}.
     */
    @Deprecated(forRemoval = true)
    public void stop() {
        close();
    }
    }


    /** Should call when the retry action has failed and we want to retry after a longer delay. */
    /** Should call when the retry action has failed and we want to retry after a longer delay. */
@@ -116,7 +124,7 @@ public class ExponentialBackoff {
        Log.d(TAG, "[notifyFailed] Error: [" + ex.getMessage() + "]");
        Log.d(TAG, "[notifyFailed] Error: [" + ex.getMessage() + "]");
        if(mRetryCounter > mMaxRetries) {
        if(mRetryCounter > mMaxRetries) {
            Log.d(TAG, "[notifyFailed] Retries exceeded, ending now");
            Log.d(TAG, "[notifyFailed] Retries exceeded, ending now");
            stop();
            close();
            mFailedCallback.run();
            mFailedCallback.run();
        } else {
        } else {
            mRetryCounter++;
            mRetryCounter++;
Loading