Skip to content

Commit 464002b

Browse files
sjuddglide-copybara-robot
authored andcommittedDec 20, 2022
Create glide okhttp integration using okhttp4 dependency
PiperOrigin-RevId: 496520960
1 parent 128d290 commit 464002b

File tree

9 files changed

+257
-0
lines changed

9 files changed

+257
-0
lines changed
 

‎gradle.properties

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ VERSION_NAME=4.15.0-SNAPSHOT
4949
## SDK versioning
5050
COMPILE_SDK_VERSION=33
5151
MIN_SDK_VERSION=14
52+
OK_HTTP_4_MIN_SDK_VERSION=21
5253
TARGET_SDK_VERSION=32
5354

5455
## AndroidX versions
@@ -105,6 +106,7 @@ MOCKITO_ANDROID_VERSION=2.24.0
105106
MOCKITO_VERSION=2.24.0
106107
MOCKWEBSERVER_VERSION=3.0.0-RC1
107108
OK_HTTP_VERSION=3.10.0
109+
OK_HTTP_4_VERSION=4.10.0
108110
PMD_VERSION=6.0.0
109111
ROBOLECTRIC_VERSION=4.8.1
110112
TRUTH_VERSION=1.1.3

‎integration/okhttp4/build.gradle

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apply plugin: 'com.android.library'
2+
3+
dependencies {
4+
implementation project(':library')
5+
annotationProcessor project(':annotation:compiler')
6+
7+
api "com.squareup.okhttp3:okhttp:${OK_HTTP_4_VERSION}"
8+
api "androidx.annotation:annotation:${ANDROID_X_ANNOTATION_VERSION}"
9+
}
10+
11+
android {
12+
compileSdk COMPILE_SDK_VERSION as int
13+
14+
defaultConfig {
15+
minSdk OK_HTTP_4_MIN_SDK_VERSION as int
16+
targetSdk TARGET_SDK_VERSION as int
17+
18+
versionName VERSION_NAME as String
19+
}
20+
21+
compileOptions {
22+
sourceCompatibility JavaVersion.VERSION_1_8
23+
targetCompatibility JavaVersion.VERSION_1_8
24+
}
25+
}
26+
27+
apply from: "${rootProject.projectDir}/scripts/upload.gradle"

‎integration/okhttp4/gradle.properties

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
POM_NAME=Glide OkHttp 4.x Integration
2+
POM_ARTIFACT_ID=okhttp4-integration
3+
POM_PACKAGING=aar
4+
POM_DESCRIPTION=An integration library to use OkHttp 4.x to fetch data over http/https in Glide

‎integration/okhttp4/lint.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<issue id="AllowBackup" severity="ignore"/>
4+
</issue>
5+
</lint>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<manifest
2+
package="com.bumptech.glide.integration.okhttp">
3+
4+
<application />
5+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.bumptech.glide.integration.okhttp3;
2+
3+
import android.content.Context;
4+
import androidx.annotation.NonNull;
5+
import com.bumptech.glide.Glide;
6+
import com.bumptech.glide.Registry;
7+
import com.bumptech.glide.annotation.GlideModule;
8+
import com.bumptech.glide.load.model.GlideUrl;
9+
import com.bumptech.glide.module.AppGlideModule;
10+
import com.bumptech.glide.module.LibraryGlideModule;
11+
import java.io.InputStream;
12+
13+
/**
14+
* Registers OkHttp related classes via Glide's annotation processor.
15+
*
16+
* <p>For Applications that depend on this library and include an {@link AppGlideModule} and Glide's
17+
* annotation processor, this class will be automatically included.
18+
*/
19+
@GlideModule
20+
public final class OkHttpLibraryGlideModule extends LibraryGlideModule {
21+
@Override
22+
public void registerComponents(
23+
@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
24+
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.bumptech.glide.integration.okhttp3;
2+
3+
import android.util.Log;
4+
import androidx.annotation.NonNull;
5+
import com.bumptech.glide.Priority;
6+
import com.bumptech.glide.load.DataSource;
7+
import com.bumptech.glide.load.HttpException;
8+
import com.bumptech.glide.load.data.DataFetcher;
9+
import com.bumptech.glide.load.model.GlideUrl;
10+
import com.bumptech.glide.util.ContentLengthInputStream;
11+
import com.bumptech.glide.util.Preconditions;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.util.Map;
15+
import okhttp3.Call;
16+
import okhttp3.Request;
17+
import okhttp3.Response;
18+
import okhttp3.ResponseBody;
19+
20+
/** Fetches an {@link InputStream} using the okhttp library. */
21+
public class OkHttpStreamFetcher implements DataFetcher<InputStream>, okhttp3.Callback {
22+
private static final String TAG = "OkHttpFetcher";
23+
private final Call.Factory client;
24+
private final GlideUrl url;
25+
private InputStream stream;
26+
private ResponseBody responseBody;
27+
private DataCallback<? super InputStream> callback;
28+
// call may be accessed on the main thread while the object is in use on other threads. All other
29+
// accesses to variables may occur on different threads, but only one at a time.
30+
private volatile Call call;
31+
32+
// Public API.
33+
@SuppressWarnings("WeakerAccess")
34+
public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) {
35+
this.client = client;
36+
this.url = url;
37+
}
38+
39+
@Override
40+
public void loadData(
41+
@NonNull Priority priority, @NonNull final DataCallback<? super InputStream> callback) {
42+
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
43+
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
44+
String key = headerEntry.getKey();
45+
requestBuilder.addHeader(key, headerEntry.getValue());
46+
}
47+
Request request = requestBuilder.build();
48+
this.callback = callback;
49+
50+
call = client.newCall(request);
51+
call.enqueue(this);
52+
}
53+
54+
@Override
55+
public void onFailure(@NonNull Call call, @NonNull IOException e) {
56+
if (Log.isLoggable(TAG, Log.DEBUG)) {
57+
Log.d(TAG, "OkHttp failed to obtain result", e);
58+
}
59+
60+
callback.onLoadFailed(e);
61+
}
62+
63+
@Override
64+
public void onResponse(@NonNull Call call, @NonNull Response response) {
65+
responseBody = response.body();
66+
if (response.isSuccessful()) {
67+
long contentLength = Preconditions.checkNotNull(responseBody).contentLength();
68+
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
69+
callback.onDataReady(stream);
70+
} else {
71+
callback.onLoadFailed(new HttpException(response.message(), response.code()));
72+
}
73+
}
74+
75+
@Override
76+
public void cleanup() {
77+
try {
78+
if (stream != null) {
79+
stream.close();
80+
}
81+
} catch (IOException e) {
82+
// Ignored
83+
}
84+
if (responseBody != null) {
85+
responseBody.close();
86+
}
87+
callback = null;
88+
}
89+
90+
@Override
91+
public void cancel() {
92+
Call local = call;
93+
if (local != null) {
94+
local.cancel();
95+
}
96+
}
97+
98+
@NonNull
99+
@Override
100+
public Class<InputStream> getDataClass() {
101+
return InputStream.class;
102+
}
103+
104+
@NonNull
105+
@Override
106+
public DataSource getDataSource() {
107+
return DataSource.REMOTE;
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.bumptech.glide.integration.okhttp3;
2+
3+
import androidx.annotation.NonNull;
4+
import com.bumptech.glide.load.Options;
5+
import com.bumptech.glide.load.model.GlideUrl;
6+
import com.bumptech.glide.load.model.ModelLoader;
7+
import com.bumptech.glide.load.model.ModelLoaderFactory;
8+
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
9+
import java.io.InputStream;
10+
import okhttp3.Call;
11+
import okhttp3.OkHttpClient;
12+
13+
/** A simple model loader for fetching media over http/https using OkHttp. */
14+
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> {
15+
16+
private final Call.Factory client;
17+
18+
// Public API.
19+
@SuppressWarnings("WeakerAccess")
20+
public OkHttpUrlLoader(@NonNull Call.Factory client) {
21+
this.client = client;
22+
}
23+
24+
@Override
25+
public boolean handles(@NonNull GlideUrl url) {
26+
return true;
27+
}
28+
29+
@Override
30+
public LoadData<InputStream> buildLoadData(
31+
@NonNull GlideUrl model, int width, int height, @NonNull Options options) {
32+
return new LoadData<>(model, new OkHttpStreamFetcher(client, model));
33+
}
34+
35+
/** The default factory for {@link OkHttpUrlLoader}s. */
36+
// Public API.
37+
@SuppressWarnings("WeakerAccess")
38+
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
39+
private static volatile Call.Factory internalClient;
40+
private final Call.Factory client;
41+
42+
private static Call.Factory getInternalClient() {
43+
if (internalClient == null) {
44+
synchronized (Factory.class) {
45+
if (internalClient == null) {
46+
internalClient = new OkHttpClient();
47+
}
48+
}
49+
}
50+
return internalClient;
51+
}
52+
53+
/** Constructor for a new Factory that runs requests using a static singleton client. */
54+
public Factory() {
55+
this(getInternalClient());
56+
}
57+
58+
/**
59+
* Constructor for a new Factory that runs requests using given client.
60+
*
61+
* @param client this is typically an instance of {@code OkHttpClient}.
62+
*/
63+
public Factory(@NonNull Call.Factory client) {
64+
this.client = client;
65+
}
66+
67+
@NonNull
68+
@Override
69+
public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
70+
return new OkHttpUrlLoader(client);
71+
}
72+
73+
@Override
74+
public void teardown() {
75+
// Do nothing, this instance doesn't own the client.
76+
}
77+
}
78+
}

‎settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include ':integration:gifencoder'
3030
include ':integration:ktx'
3131
include ':integration:okhttp'
3232
include ':integration:okhttp3'
33+
include ':integration:okhttp4'
3334
include ':integration:recyclerview'
3435
include ':integration:volley'
3536
include ':testutil'

0 commit comments

Comments
 (0)
Please sign in to comment.