Avoids going to sleep for at most 2000ms while retrieving file size via HTTP HEAD

This commit is contained in:
steckbrief 2018-05-05 13:41:11 +02:00
parent f5210768cc
commit 853f50e43f

View file

@ -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() {