From 618945c5bf9bea279c01025d6455c5e246e98a11 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 8 Jan 2017 16:38:49 +0900 Subject: [PATCH] Use volley for API requests (fixes #827) --- build.gradle | 1 + .../http/{RestTask.java => ApiRequest.java} | 116 +++++++++++------- .../syncthingandroid/http/GetRequest.java | 36 ++++++ .../syncthingandroid/http/GetTask.java | 54 -------- .../http/PollWebGuiAvailableTask.java | 49 ++++---- .../http/PostConfigRequest.java | 22 ++++ .../syncthingandroid/http/PostConfigTask.java | 43 ------- .../http/PostScanRequest.java | 22 ++++ .../syncthingandroid/http/PostScanTask.java | 40 ------ .../syncthingandroid/service/RestApi.java | 58 +++++---- .../service/SyncthingService.java | 4 +- 11 files changed, 202 insertions(+), 243 deletions(-) rename src/main/java/com/nutomic/syncthingandroid/http/{RestTask.java => ApiRequest.java} (58%) create mode 100644 src/main/java/com/nutomic/syncthingandroid/http/GetRequest.java delete mode 100644 src/main/java/com/nutomic/syncthingandroid/http/GetTask.java create mode 100644 src/main/java/com/nutomic/syncthingandroid/http/PostConfigRequest.java delete mode 100644 src/main/java/com/nutomic/syncthingandroid/http/PostConfigTask.java create mode 100644 src/main/java/com/nutomic/syncthingandroid/http/PostScanRequest.java delete mode 100644 src/main/java/com/nutomic/syncthingandroid/http/PostScanTask.java diff --git a/build.gradle b/build.gradle index e5976765..a13a023b 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ dependencies { compile 'org.mindrot:jbcrypt:0.3m' compile 'com.google.guava:guava:20.0' compile 'com.annimon:stream:1.1.4' + compile 'com.android.volley:volley:1.0.0' androidTestCompile 'com.android.support.test:rules:0.5' } diff --git a/src/main/java/com/nutomic/syncthingandroid/http/RestTask.java b/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java similarity index 58% rename from src/main/java/com/nutomic/syncthingandroid/http/RestTask.java rename to src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java index 3d257d1b..6285e972 100644 --- a/src/main/java/com/nutomic/syncthingandroid/http/RestTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/http/ApiRequest.java @@ -2,13 +2,19 @@ package com.nutomic.syncthingandroid.http; import android.annotation.SuppressLint; +import android.content.Context; import android.net.Uri; -import android.os.AsyncTask; +import android.support.annotation.Nullable; import android.util.Log; -import android.util.Pair; -import com.google.common.base.Charsets; -import com.google.common.io.ByteSource; +import com.android.volley.AuthFailureError; +import com.android.volley.RequestQueue; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.HurlStack; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -27,16 +33,17 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.Map; +import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; -public abstract class RestTask extends - AsyncTask> { +public abstract class ApiRequest { - private static final String TAG = "RestTask"; + private static final String TAG = "ApiRequest"; /** * The name of the HTTP header used for the syncthing API key. @@ -47,74 +54,91 @@ public abstract class RestTask extends public void onSuccess(String result); } + public interface OnErrorListener { + public void onError(VolleyError error); + } + + private static RequestQueue sVolleyQueue; + + private RequestQueue getVolleyQueue() { + if (sVolleyQueue == null) { + Context context = mContext.getApplicationContext(); + sVolleyQueue = Volley.newRequestQueue(context, new NetworkStack()); + } + return sVolleyQueue; + } + + private final Context mContext; private final URL mUrl; protected final String mPath; private final String mHttpsCertPath; private final String mApiKey; - private final OnSuccessListener mListener; - public RestTask(URL url, String path, String httpsCertPath, String apiKey, - OnSuccessListener listener) { + public ApiRequest(Context context, URL url, String path, String httpsCertPath, String apiKey) { + mContext = context; mUrl = url; mPath = path; mHttpsCertPath = httpsCertPath; mApiKey = apiKey; - mListener = listener; } - protected HttpsURLConnection openConnection(Map params) throws IOException { + protected Uri buildUri(Map params) { Uri.Builder uriBuilder = Uri.parse(mUrl.toString()) .buildUpon() .path(mPath); for (Map.Entry entry : params.entrySet()) { uriBuilder.appendQueryParameter(entry.getKey(), entry.getValue()); } - URL url = new URL(uriBuilder.build().toString()); - Log.v(TAG, "Calling Rest API at " + url); - - HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.setRequestProperty(HEADER_API_KEY, mApiKey); - connection.setHostnameVerifier((h, s) -> true); - connection.setSSLSocketFactory(getSslSocketFactory()); - return connection; + return uriBuilder.build(); } /** * Opens the connection, then returns success status and response string. */ - protected Pair connect(HttpsURLConnection connection) throws IOException { - connection.connect(); - Pair result; - if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { - int responseCode = connection.getResponseCode(); - String responseMessage = connection.getResponseMessage(); - Log.i(TAG, "Request to " + connection.getURL() + " failed, code: " + responseCode + - ", message: " + responseMessage); - result = new Pair<>(false, streamToString(connection.getErrorStream())); - } - else { - result = new Pair<>(true, streamToString(connection.getInputStream())); - } - connection.disconnect(); - return result; - } + protected void connect(int requestMethod, Uri uri, @Nullable String requestBody, + @Nullable OnSuccessListener listener, @Nullable OnErrorListener errorListener) { + StringRequest request = new StringRequest(requestMethod, uri.toString(), reply -> { + if (listener != null) + listener.onSuccess(reply); + }, error -> { + if (errorListener != null) + errorListener.onError(error); - private String streamToString(InputStream is) throws IOException { - ByteSource byteSource = new ByteSource() { + Log.w(TAG, "Request to " + uri + " failed: " + error.getMessage()); + }) { @Override - public InputStream openStream() throws IOException { - return is; + public Map getHeaders() throws AuthFailureError { + return ImmutableMap.of(HEADER_API_KEY, mApiKey); + } + + @Override + public byte[] getBody() throws AuthFailureError { + return Optional.fromNullable(requestBody).transform(String::getBytes).orNull(); } }; - return byteSource.asCharSource(Charsets.UTF_8).read(); + getVolleyQueue().add(request); } + /** + * Extends {@link HurlStack}, uses {@link #getSslSocketFactory()} and disables hostname + * verification. + */ + private class NetworkStack extends HurlStack { - protected void onPostExecute(Pair result) { - if (mListener == null || !result.first) - return; - - mListener.onSuccess(result.second); + public NetworkStack() { + super(null, getSslSocketFactory()); + } + @Override + protected HttpURLConnection createConnection(URL url) throws IOException { + HttpsURLConnection connection = (HttpsURLConnection) super.createConnection(url); + connection.setHostnameVerifier(new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + return connection; + } } private SSLSocketFactory getSslSocketFactory() { diff --git a/src/main/java/com/nutomic/syncthingandroid/http/GetRequest.java b/src/main/java/com/nutomic/syncthingandroid/http/GetRequest.java new file mode 100644 index 00000000..a8a55d3c --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/http/GetRequest.java @@ -0,0 +1,36 @@ +package com.nutomic.syncthingandroid.http; + +import android.content.Context; +import android.net.Uri; +import android.support.annotation.Nullable; + +import com.android.volley.Request; +import com.google.common.base.Optional; + +import java.net.URL; +import java.util.Collections; +import java.util.Map; + +/** + * Performs a GET request to the Syncthing API + */ +public class GetRequest extends ApiRequest { + + public static final String URI_CONFIG = "/rest/system/config"; + public static final String URI_VERSION = "/rest/system/version"; + public static final String URI_SYSTEM = "/rest/system/status"; + public static final String URI_CONNECTIONS = "/rest/system/connections"; + public static final String URI_MODEL = "/rest/db/status"; + public static final String URI_DEVICEID = "/rest/svc/deviceid"; + public static final String URI_REPORT = "/rest/svc/report"; + public static final String URI_EVENTS = "/rest/events"; + + public GetRequest(Context context, URL url, String path, String httpsCertPath, String apiKey, + @Nullable Map params, OnSuccessListener listener) { + super(context, url, path, httpsCertPath, apiKey); + Map safeParams = Optional.fromNullable(params).or(Collections.emptyMap()); + Uri uri = buildUri(safeParams); + connect(Request.Method.GET, uri, null, listener, null); + } + +} diff --git a/src/main/java/com/nutomic/syncthingandroid/http/GetTask.java b/src/main/java/com/nutomic/syncthingandroid/http/GetTask.java deleted file mode 100644 index 8a809d5f..00000000 --- a/src/main/java/com/nutomic/syncthingandroid/http/GetTask.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.nutomic.syncthingandroid.http; - -import android.support.annotation.Nullable; -import android.util.Log; -import android.util.Pair; - -import com.google.common.base.Optional; - -import java.io.IOException; -import java.net.URL; -import java.util.Collections; -import java.util.Map; - -import javax.net.ssl.HttpsURLConnection; - -/** - * Performs a GET request to the Syncthing API - * Performs a GET request with no parameters to the URL in uri[0] with the path in uri[1] and - * returns the result as a String. - */ -public class GetTask extends RestTask { - - private static final String TAG = "GetTask"; - - public static final String URI_CONFIG = "/rest/system/config"; - public static final String URI_VERSION = "/rest/system/version"; - public static final String URI_SYSTEM = "/rest/system/status"; - public static final String URI_CONNECTIONS = "/rest/system/connections"; - public static final String URI_MODEL = "/rest/db/status"; - public static final String URI_DEVICEID = "/rest/svc/deviceid"; - public static final String URI_REPORT = "/rest/svc/report"; - public static final String URI_EVENTS = "/rest/events"; - - private final Map mParams; - - public GetTask(URL url, String path, String httpsCertPath, String apiKey, - @Nullable Map params, OnSuccessListener listener) { - super(url, path, httpsCertPath, apiKey, listener); - mParams = Optional.fromNullable(params).or(Collections.emptyMap()); - } - - @Override - protected Pair doInBackground(Void... aVoid) { - try { - HttpsURLConnection connection = openConnection(mParams); - Log.v(TAG, "Calling Rest API at " + connection.getURL()); - return connect(connection); - } catch (IOException e) { - Log.w(TAG, "Failed to call rest api", e); - return new Pair<>(false, null); - } - } - -} diff --git a/src/main/java/com/nutomic/syncthingandroid/http/PollWebGuiAvailableTask.java b/src/main/java/com/nutomic/syncthingandroid/http/PollWebGuiAvailableTask.java index 149cb9ec..cf019609 100644 --- a/src/main/java/com/nutomic/syncthingandroid/http/PollWebGuiAvailableTask.java +++ b/src/main/java/com/nutomic/syncthingandroid/http/PollWebGuiAvailableTask.java @@ -1,21 +1,20 @@ package com.nutomic.syncthingandroid.http; -import android.util.Log; -import android.util.Pair; +import android.content.Context; +import android.net.Uri; +import android.os.Handler; -import com.google.common.collect.Maps; +import com.android.volley.Request; +import com.android.volley.VolleyError; -import java.io.IOException; import java.net.URL; import java.util.Collections; -import javax.net.ssl.HttpsURLConnection; - /** - * Polls to load the web interface, until we receive http status 200. + * Polls to load the web interface, until it is available. */ -public class PollWebGuiAvailableTask extends RestTask { +public class PollWebGuiAvailableTask extends ApiRequest { private static final String TAG = "PollWebGuiAvailableTask"; @@ -25,29 +24,23 @@ public class PollWebGuiAvailableTask extends RestTask { */ private static final long WEB_GUI_POLL_INTERVAL = 100; - public PollWebGuiAvailableTask(URL url, String httpsCertPath, String apiKey, + private final OnSuccessListener mListener; + private final Handler mHandler = new Handler(); + + public PollWebGuiAvailableTask(Context context, URL url, String httpsCertPath, String apiKey, OnSuccessListener listener) { - super(url, "", httpsCertPath, apiKey, listener); + super(context, url, "", httpsCertPath, apiKey); + mListener = listener; + performRequest(); } - @Override - protected Pair doInBackground(Void... aVoid) { - int status = 0; - do { - try { - HttpsURLConnection connection = openConnection(Collections.emptyMap()); - connection.connect(); - status = connection.getResponseCode(); - } catch (IOException e) { - // We catch this in every call, as long as the service is not online, so we ignore and continue. - try { - Thread.sleep(WEB_GUI_POLL_INTERVAL); - } catch (InterruptedException e2) { - Log.w(TAG, "Failed to sleep", e2); - } - } - } while (status != HttpsURLConnection.HTTP_OK); - return new Pair<>(true, null); + private void performRequest() { + Uri uri = buildUri(Collections.emptyMap()); + connect(Request.Method.GET, uri, null, mListener, this::onError); + } + + private void onError(VolleyError error) { + mHandler.postDelayed(this::performRequest, WEB_GUI_POLL_INTERVAL); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/http/PostConfigRequest.java b/src/main/java/com/nutomic/syncthingandroid/http/PostConfigRequest.java new file mode 100644 index 00000000..d8f36474 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/http/PostConfigRequest.java @@ -0,0 +1,22 @@ +package com.nutomic.syncthingandroid.http; + +import android.content.Context; +import android.net.Uri; + +import com.android.volley.Request; + +import java.net.URL; +import java.util.Collections; + +public class PostConfigRequest extends ApiRequest { + + private static final String URI_CONFIG = "/rest/system/config"; + + public PostConfigRequest(Context context, URL url, String httpsCertPath, String apiKey, String config, + OnSuccessListener listener) { + super(context, url, URI_CONFIG, httpsCertPath, apiKey); + Uri uri = buildUri(Collections.emptyMap()); + connect(Request.Method.POST, uri, config, listener, null); + } + +} diff --git a/src/main/java/com/nutomic/syncthingandroid/http/PostConfigTask.java b/src/main/java/com/nutomic/syncthingandroid/http/PostConfigTask.java deleted file mode 100644 index a24a2767..00000000 --- a/src/main/java/com/nutomic/syncthingandroid/http/PostConfigTask.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.nutomic.syncthingandroid.http; - -import android.util.Log; -import android.util.Pair; - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URL; -import java.util.Collections; - -import javax.net.ssl.HttpsURLConnection; - -public class PostConfigTask extends RestTask { - - private static final String TAG = "PostConfigTask"; - - private static final String URI_CONFIG = "/rest/system/config"; - - private final String mConfig; - - public PostConfigTask(URL url, String httpsCertPath, String apiKey, String config, - OnSuccessListener listener) { - super(url, URI_CONFIG, httpsCertPath, apiKey, listener); - mConfig = config; - } - - @Override - protected Pair doInBackground(Void... params) { - try { - HttpsURLConnection connection = openConnection(Collections.emptyMap()); - connection.setRequestMethod("POST"); - Log.v(TAG, "Calling Rest API at " + connection.getURL()); - - OutputStream os = connection.getOutputStream(); - os.write(mConfig.getBytes("UTF-8")); - return connect(connection); - } catch (IOException e) { - Log.w(TAG, "Failed to call rest api", e); - return new Pair<>(false, null); - } - } - -} diff --git a/src/main/java/com/nutomic/syncthingandroid/http/PostScanRequest.java b/src/main/java/com/nutomic/syncthingandroid/http/PostScanRequest.java new file mode 100644 index 00000000..266b9487 --- /dev/null +++ b/src/main/java/com/nutomic/syncthingandroid/http/PostScanRequest.java @@ -0,0 +1,22 @@ +package com.nutomic.syncthingandroid.http; + +import android.content.Context; +import android.net.Uri; + +import com.android.volley.Request; +import com.google.common.collect.ImmutableMap; + +import java.net.URL; + +public class PostScanRequest extends ApiRequest { + + private static final String URI_SCAN = "/rest/db/scan"; + + public PostScanRequest(Context context, URL url, String httpsCertPath, String apiKey, + String folder, String sub) { + super(context, url, URI_SCAN, httpsCertPath, apiKey); + Uri uri = buildUri(ImmutableMap.of("folder", folder, "sub", sub)); + connect(Request.Method.POST, uri, null, null, null); + } + +} diff --git a/src/main/java/com/nutomic/syncthingandroid/http/PostScanTask.java b/src/main/java/com/nutomic/syncthingandroid/http/PostScanTask.java deleted file mode 100644 index 08661a97..00000000 --- a/src/main/java/com/nutomic/syncthingandroid/http/PostScanTask.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.nutomic.syncthingandroid.http; - -import android.util.Log; -import android.util.Pair; - -import com.google.common.collect.ImmutableMap; - -import java.io.IOException; -import java.net.URL; - -import javax.net.ssl.HttpsURLConnection; - -public class PostScanTask extends RestTask { - - private static final String TAG = "PostScanTask"; - - private static final String URI_SCAN = "/rest/db/scan"; - - private final String mFolder; - private final String mSub; - - public PostScanTask(URL url, String httpsCertPath, String apiKey, String folder, String sub) { - super(url, URI_SCAN, httpsCertPath, apiKey, null); - mFolder = folder; - mSub = sub; - } - - @Override - protected Pair doInBackground(Void... params) { - try { - HttpsURLConnection connection = openConnection(ImmutableMap.of("folder", mFolder, "sub", mSub)); - connection.setRequestMethod("POST"); - return connect(connection); - } catch (IOException e) { - Log.w(TAG, "Failed to call rest api", e); - return new Pair<>(false, null); - } - } - -} diff --git a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index f9af0fa1..77836022 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -18,9 +18,9 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.nutomic.syncthingandroid.BuildConfig; import com.nutomic.syncthingandroid.activities.RestartActivity; -import com.nutomic.syncthingandroid.http.GetTask; -import com.nutomic.syncthingandroid.http.PostConfigTask; -import com.nutomic.syncthingandroid.http.PostScanTask; +import com.nutomic.syncthingandroid.http.GetRequest; +import com.nutomic.syncthingandroid.http.PostConfigRequest; +import com.nutomic.syncthingandroid.http.PostScanRequest; import com.nutomic.syncthingandroid.model.Config; import com.nutomic.syncthingandroid.model.Connections; import com.nutomic.syncthingandroid.model.Device; @@ -71,13 +71,13 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, private boolean mRestartPostponed = false; /** - * Stores the result of the last successful request to {@link GetTask#URI_CONNECTIONS}, + * Stores the result of the last successful request to {@link GetRequest#URI_CONNECTIONS}, * or an empty Map. */ private Optional mPreviousConnections = Optional.absent(); /** - * Stores the timestamp of the last successful request to {@link GetTask#URI_CONNECTIONS}. + * Stores the timestamp of the last successful request to {@link GetRequest#URI_CONNECTIONS}. */ private long mPreviousConnectionTime = 0; @@ -121,16 +121,16 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, @Override public void onWebGuiAvailable() { mAvailableCount.set(0); - new GetTask(mUrl, GetTask.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> { JsonObject json = new JsonParser().parse(result).getAsJsonObject(); mVersion = json.get("version").getAsString(); Log.i(TAG, "Syncthing version is " + mVersion); tryIsAvailable(); - }).execute(); - new GetTask(mUrl, GetTask.URI_CONFIG, mHttpsCertPath, mApiKey, null, result -> { + }); + new GetRequest(mContext, mUrl, GetRequest.URI_CONFIG, mHttpsCertPath, mApiKey, null, result -> { mConfig = new Gson().fromJson(result, Config.class); tryIsAvailable(); - }).execute(); + }); getSystemInfo(info -> { mLocalDeviceId = info.myID; tryIsAvailable(); @@ -168,19 +168,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * Sends current config to Syncthing. */ private void sendConfig() { - new PostConfigTask(mUrl, mHttpsCertPath, mApiKey, new Gson().toJson(mConfig), null) - .execute(); + new PostConfigRequest(mContext, mUrl, mHttpsCertPath, mApiKey, new Gson().toJson(mConfig), null); } /** * Sends current config and restarts Syncthing. */ public void restart() { - new PostConfigTask(mUrl, mHttpsCertPath, mApiKey, new Gson().toJson(mConfig), result -> { + new PostConfigRequest(mContext, mUrl, mHttpsCertPath, mApiKey, new Gson().toJson(mConfig), result -> { Intent intent = new Intent(mContext, SyncthingService.class) .setAction(SyncthingService.ACTION_RESTART); mContext.startService(intent); - }).execute(); + }); } /** @@ -312,9 +311,9 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * Requests and parses information about current system status and resource usage. */ public void getSystemInfo(OnResultListener1 listener) { - new GetTask(mUrl, GetTask.URI_SYSTEM, mHttpsCertPath, mApiKey, null, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_SYSTEM, mHttpsCertPath, mApiKey, null, result -> { listener.onResult(new Gson().fromJson(result, SystemInfo.class)); - }).execute(); + }); } public boolean isConfigLoaded() { @@ -325,17 +324,17 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * Requests and parses system version information. */ public void getSystemVersion(OnResultListener1 listener) { - new GetTask(mUrl, GetTask.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mHttpsCertPath, mApiKey, null, result -> { SystemVersion systemVersion = new Gson().fromJson(result, SystemVersion.class); listener.onResult(systemVersion); - }).execute(); + }); } /** * Returns connection info for the local device and all connected devices. */ public void getConnections(final OnResultListener1 listener) { - new GetTask(mUrl, GetTask.URI_CONNECTIONS, mHttpsCertPath, mApiKey, null, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_CONNECTIONS, mHttpsCertPath, mApiKey, null, result -> { Long now = System.currentTimeMillis(); Long msElapsed = now - mPreviousConnectionTime; if (msElapsed < SyncthingService.GUI_UPDATE_INTERVAL) { @@ -359,7 +358,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, connections.total.setTransferRate(prev, msElapsed); mPreviousConnections = Optional.of(connections); listener.onResult(deepCopy(connections, Connections.class)); - }).execute(); + }); } /** @@ -398,12 +397,12 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, * Returns status information about the folder with the given id. */ public void getModel(final String folderId, final OnResultListener2 listener) { - new GetTask(mUrl, GetTask.URI_MODEL, mHttpsCertPath, mApiKey, + new GetRequest(mContext, mUrl, GetRequest.URI_MODEL, mHttpsCertPath, mApiKey, ImmutableMap.of("folder", folderId), result -> { Model m = new Gson().fromJson(result, Model.class); mCachedModelInfo.put(folderId, m); listener.onResult(folderId, m); - }).execute(); + }); } /** @@ -431,7 +430,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public final void getEvents(final long sinceId, final long limit, final OnReceiveEventListener listener) { Map params = ImmutableMap.of("since", String.valueOf(sinceId), "limit", String.valueOf(limit)); - new GetTask(mUrl, GetTask.URI_EVENTS, mHttpsCertPath, mApiKey, params, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_EVENTS, mHttpsCertPath, mApiKey, params, result -> { JsonArray jsonEvents = new JsonParser().parse(result).getAsJsonArray(); long lastId = 0; @@ -446,7 +445,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } listener.onDone(lastId); - }).execute(); + }); } /** @@ -454,8 +453,8 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, */ public void normalizeDeviceId(String id, OnResultListener1 listener, OnResultListener1 errorListener) { - new GetTask(mUrl, GetTask.URI_DEVICEID, mHttpsCertPath, mApiKey, ImmutableMap.of("id", id), - result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_DEVICEID, mHttpsCertPath, mApiKey, + ImmutableMap.of("id", id), result -> { JsonObject json = new JsonParser().parse(result).getAsJsonObject(); JsonElement normalizedId = json.get("id"); JsonElement error = json.get("error"); @@ -463,7 +462,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, listener.onResult(normalizedId.getAsString()); if (error != null) errorListener.onResult(error.getAsString()); - }).execute(); + }); } /** @@ -471,19 +470,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, */ @Override public void onFolderFileChange(String folderId, String relativePath) { - new PostScanTask(mUrl, mHttpsCertPath, mApiKey, folderId, relativePath) - .execute(); + new PostScanRequest(mContext, mUrl, mHttpsCertPath, mApiKey, folderId, relativePath); } /** * Returns prettyfied usage report. */ public void getUsageReport(final OnResultListener1 listener) { - new GetTask(mUrl, GetTask.URI_REPORT, mHttpsCertPath, mApiKey, null, result -> { + new GetRequest(mContext, mUrl, GetRequest.URI_REPORT, mHttpsCertPath, mApiKey, null, result -> { JsonElement json = new JsonParser().parse(result); Gson gson = new GsonBuilder().setPrettyPrinting().create(); listener.onResult(gson.toJson(json)); - }).execute(); + }); } public void setRestartPostponed() { diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index ede2c639..79f6c359 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -491,7 +491,7 @@ public class SyncthingService extends Service implements * for SyncthingService.onDestroy for details. */ private void pollWebGui() { - new PollWebGuiAvailableTask(getWebGuiUrl(), getFilesDir() + "/" + HTTPS_CERT_FILE, + new PollWebGuiAvailableTask(this, getWebGuiUrl(), getFilesDir() + "/" + HTTPS_CERT_FILE, mConfig.getApiKey(), result -> { synchronized (stateLock) { if (mStopScheduled) { @@ -510,7 +510,7 @@ public class SyncthingService extends Service implements listener.onWebGuiAvailable(); } mOnWebGuiAvailableListeners.clear(); - }).execute(); + }); } /**