aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsteckbrief <steckbrief@chefmail.de>2018-05-05 13:41:11 +0200
committersteckbrief <steckbrief@chefmail.de>2018-05-05 13:41:11 +0200
commit853f50e43f68599774469dd12240f35800144991 (patch)
treed564971557ad4b1e90b02f348348ece95ef9570d
parentf5210768ccf920d4c270206d8e9742968408dc48 (diff)
Avoids going to sleep for at most 2000ms while retrieving file size via HTTP HEAD
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/http/HttpClient.java20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/http/HttpClient.java b/src/main/java/de/thedevstack/conversationsplus/http/HttpClient.java
index e62be056..cde0675c 100644
--- a/src/main/java/de/thedevstack/conversationsplus/http/HttpClient.java
+++ b/src/main/java/de/thedevstack/conversationsplus/http/HttpClient.java
@@ -1,5 +1,6 @@
package de.thedevstack.conversationsplus.http;
+import android.os.PowerManager;
import android.support.annotation.NonNull;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
@@ -36,6 +37,7 @@ import okio.Source;
*
*/
public final class HttpClient implements Http {
+ private static final long RETRIEVE_HEAD_WAKELOCK_TIMEOUT = 2000;
private static HttpClient INSTANCE;
private static final String LOGTAG = "http-client";
@@ -54,7 +56,7 @@ public final class HttpClient implements Http {
private static OkHttpClient.Builder getBuilder(boolean interactive) {
OkHttpClient.Builder builder = INSTANCE.client.newBuilder();
- INSTANCE.initTrustManager(builder, interactive);
+ initTrustManager(builder, interactive);
return builder;
}
@@ -85,7 +87,21 @@ public final class HttpClient implements Http {
//.addHeader(HEADER_NAME_ACCEPT_ENCODING, "")
.head()
.build();
- client.newCall(request).enqueue(callback);
+ Call call = client.newCall(request);
+ PowerManager.WakeLock wakeLock = ConversationsPlusApplication.createPartialWakeLock("http-retrieve-head-" + call.hashCode());
+ try {
+ wakeLock.acquire(RETRIEVE_HEAD_WAKELOCK_TIMEOUT);
+ Response response = call.execute();
+ if (response.isSuccessful()) {
+ callback.onResponse(call, response);
+ }
+ } catch (IOException e) {
+ callback.onFailure(call, e);
+ } finally {
+ if (wakeLock.isHeld()) {
+ wakeLock.release();
+ }
+ }
}
private HttpClient() {