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

Commit 53355e83 authored by David Luhmer's avatar David Luhmer
Browse files

use thread to connect to api / cleanup

parent 641ff94e
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ public class AidlNetworkRequest extends NetworkRequest {
    private IInputStreamService mService = null;
    private final AtomicBoolean mBound = new AtomicBoolean(false); // Flag indicating whether we have called bind on the service

    public AidlNetworkRequest(Context context, SingleSignOnAccount account, NextcloudAPI.ApiConnectedListener callback) {
    AidlNetworkRequest(Context context, SingleSignOnAccount account, NextcloudAPI.ApiConnectedListener callback) {
        super(context, account, callback);
    }

@@ -212,7 +212,7 @@ public class AidlNetworkRequest extends NetworkRequest {
    }


    public static <T> T deserializeObject(InputStream is) throws IOException, ClassNotFoundException {
    private static <T> T deserializeObject(InputStream is) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(is);
        T result = (T) ois.readObject();
        return result;
+9 −9
Original line number Diff line number Diff line
@@ -15,9 +15,9 @@ public abstract class NetworkRequest {
    private static final String TAG = NetworkRequest.class.getCanonicalName();

    private SingleSignOnAccount mAccount;
    Context mContext;
    NextcloudAPI.ApiConnectedListener mCallback;
    boolean mDestroyed = false; // Flag indicating if API is destroyed
    protected Context mContext;
    protected NextcloudAPI.ApiConnectedListener mCallback;
    protected boolean mDestroyed = false; // Flag indicating if API is destroyed


    protected NetworkRequest(Context context, SingleSignOnAccount account, NextcloudAPI.ApiConnectedListener callback) {
@@ -27,7 +27,7 @@ public abstract class NetworkRequest {
    }


    void connect() {
    protected void connect() {
        Log.v(TAG, "Nextcloud Single sign-on connect() called [" + Thread.currentThread().getName() + "]");
        if (mDestroyed) {
            throw new IllegalStateException("API already destroyed! You cannot reuse a stopped API instance");
@@ -36,7 +36,7 @@ public abstract class NetworkRequest {

    protected abstract InputStream performNetworkRequest(NextcloudRequest request, InputStream requestBodyInputStream) throws Exception;

    void connectApiWithBackoff() {
    protected void connectApiWithBackoff() {
        new ExponentialBackoff(1000, 10000, 2, 5, Looper.getMainLooper(), new Runnable() {
            @Override
            public void run() {
@@ -45,18 +45,18 @@ public abstract class NetworkRequest {
        }).start();
    }

    void stop() {
    protected void stop() {
        mCallback = null;
        mAccount = null;
        mDestroyed = true;
    }


    String getAccountName() {
    protected String getAccountName() {
        return mAccount.name;
    }

    String getAccountToken() {
    protected String getAccountToken() {
        return mAccount.token;
    }

+7 −1
Original line number Diff line number Diff line
@@ -69,7 +69,13 @@ public class NextcloudAPI {
    public NextcloudAPI(Gson gson, NetworkRequest networkRequest) {
        this.gson = gson;
        this.networkRequest = networkRequest;
        this.networkRequest.connect();

        new Thread() {
            @Override
            public void run() {
                NextcloudAPI.this.networkRequest.connectApiWithBackoff();
            }
        }.start();
    }

    public void stop() {