From 853f50e43f68599774469dd12240f35800144991 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Sat, 5 May 2018 13:41:11 +0200 Subject: Avoids going to sleep for at most 2000ms while retrieving file size via HTTP HEAD --- .../conversationsplus/http/HttpClient.java | 20 ++++++++++++++++++-- 1 file 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() { -- cgit v1.2.3