-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Porting okhttp3 integration to Glide 3 #887
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile project(':library') | ||
|
||
compile "com.squareup.okhttp3:okhttp:${OK_HTTP3_VERSION}" | ||
} | ||
|
||
android { | ||
compileSdkVersion COMPILE_SDK_VERSION as int | ||
buildToolsVersion BUILD_TOOLS_VERSION as String | ||
|
||
defaultConfig { | ||
minSdkVersion MIN_SDK_VERSION as int | ||
targetSdkVersion TARGET_SDK_VERSION as int | ||
|
||
versionCode VERSION_CODE as int | ||
versionName VERSION_NAME as String | ||
} | ||
} | ||
|
||
apply from: "$rootProject.projectDir/scripts/upload.gradle" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
POM_NAME=Glide OkHttp 3.x Integration | ||
POM_ARTIFACT_ID=okhttp3-integration | ||
POM_PACKAGING=aar | ||
|
||
VERSION_NAME=1.4.0-SNAPSHOT | ||
VERSION_MAJOR=1 | ||
VERSION_MINOR=4 | ||
VERSION_PATCH=0 | ||
VERSION_CODE=10 | ||
|
||
POM_DESCRIPTION=An integration library to use OkHttp 3.x to fetch data over http/https in Glide | ||
|
||
# Prefix and postfix for source and javadoc jars. | ||
JAR_PREFIX=glide- | ||
JAR_POSTFIX=-integration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<lint> | ||
<issue id="AllowBackup" severity="ignore"/> | ||
<!-- See https://github.com/square/okio/issues/58 --> | ||
<issue id="InvalidPackage" severity="ignore"> | ||
<ignore regexp="okio-1.0.0.jar"/> | ||
</issue> | ||
</lint> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.bumptech.glide.integration.okhttp"> | ||
|
||
<application> | ||
<meta-data | ||
android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule" | ||
android:value="GlideModule"/> | ||
</application> | ||
</manifest> |
31 changes: 31 additions & 0 deletions
31
...ation/okhttp3/src/main/java/com/bumptech/glide/integration/okhttp3/OkHttpGlideModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.bumptech.glide.integration.okhttp3; | ||
|
||
import android.content.Context; | ||
|
||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.GlideBuilder; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.module.GlideModule; | ||
|
||
import java.io.InputStream; | ||
|
||
/** | ||
* A {@link com.bumptech.glide.module.GlideModule} implementation to replace Glide's default | ||
* {@link java.net.HttpURLConnection} based {@link com.bumptech.glide.load.model.ModelLoader} | ||
* with an OkHttp based {@link com.bumptech.glide.load.model.ModelLoader}. | ||
* <p/> | ||
* <p> If you're using gradle, you can include this module simply by depending on the aar, the | ||
* module will be merged in by manifest merger. For other build systems or for more more | ||
* information, see {@link com.bumptech.glide.module.GlideModule}. </p> | ||
*/ | ||
public class OkHttpGlideModule implements GlideModule { | ||
@Override | ||
public void applyOptions(Context context, GlideBuilder builder) { | ||
// Do nothing. | ||
} | ||
|
||
@Override | ||
public void registerComponents(Context context, Glide glide) { | ||
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory()); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...ion/okhttp3/src/main/java/com/bumptech/glide/integration/okhttp3/OkHttpStreamFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.bumptech.glide.integration.okhttp3; | ||
|
||
import com.bumptech.glide.Priority; | ||
import com.bumptech.glide.load.data.DataFetcher; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.util.ContentLengthInputStream; | ||
import okhttp3.Call; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
/** | ||
* Fetches an {@link InputStream} using the okhttp3library. | ||
*/ | ||
public class OkHttpStreamFetcher implements DataFetcher<InputStream> { | ||
private final Call.Factory client; | ||
private final GlideUrl url; | ||
private InputStream stream; | ||
private ResponseBody responseBody; | ||
|
||
public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) { | ||
this.client = client; | ||
this.url = url; | ||
} | ||
|
||
@Override | ||
public InputStream loadData(Priority priority) throws Exception { | ||
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl()); | ||
|
||
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { | ||
String key = headerEntry.getKey(); | ||
requestBuilder.addHeader(key, headerEntry.getValue()); | ||
} | ||
Request request = requestBuilder.build(); | ||
|
||
Response response = client.newCall(request).execute(); | ||
responseBody = response.body(); | ||
if (!response.isSuccessful()) { | ||
throw new IOException("Request failed with code: " + response.code()); | ||
} | ||
|
||
long contentLength = responseBody.contentLength(); | ||
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); | ||
return stream; | ||
} | ||
|
||
@Override | ||
public void cleanup() { | ||
try { | ||
if (stream != null) { | ||
stream.close(); | ||
} | ||
} catch (IOException e) { | ||
// Ignored | ||
} | ||
if (responseBody != null) { | ||
responseBody.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return url.getCacheKey(); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
// TODO: call cancel on the client when this method is called on a background thread. See #257 | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...gration/okhttp3/src/main/java/com/bumptech/glide/integration/okhttp3/OkHttpUrlLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.bumptech.glide.integration.okhttp3; | ||
|
||
import android.content.Context; | ||
|
||
import com.bumptech.glide.load.data.DataFetcher; | ||
import com.bumptech.glide.load.model.GenericLoaderFactory; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.load.model.ModelLoader; | ||
import com.bumptech.glide.load.model.ModelLoaderFactory; | ||
import okhttp3.Call; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.io.InputStream; | ||
|
||
/** | ||
* A simple model loader for fetching media over http/https using OkHttp. | ||
*/ | ||
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> { | ||
|
||
private final Call.Factory client; | ||
|
||
public OkHttpUrlLoader(Call.Factory client) { | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) { | ||
return new OkHttpStreamFetcher(client, model); | ||
} | ||
|
||
/** | ||
* The default factory for {@link OkHttpUrlLoader}s. | ||
*/ | ||
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> { | ||
private static volatile Call.Factory internalClient; | ||
private Call.Factory client; | ||
|
||
/** | ||
* Constructor for a new Factory that runs requests using a static singleton client. | ||
*/ | ||
public Factory() { | ||
this(getInternalClient()); | ||
} | ||
|
||
/** | ||
* Constructor for a new Factory that runs requests using given client. | ||
* | ||
* @param client this is typically an instance of {@code OkHttpClient}. | ||
*/ | ||
public Factory(Call.Factory client) { | ||
this.client = client; | ||
} | ||
|
||
private static Call.Factory getInternalClient() { | ||
if (internalClient == null) { | ||
synchronized (Factory.class) { | ||
if (internalClient == null) { | ||
internalClient = new OkHttpClient(); | ||
} | ||
} | ||
} | ||
return internalClient; | ||
} | ||
|
||
@Override | ||
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) { | ||
return new OkHttpUrlLoader(client); | ||
} | ||
|
||
@Override | ||
public void teardown() { | ||
// Do nothing, this instance doesn't own the client. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we missed this, see #941
Library module in v3 is not published, everything there depends on
:glide
as far as I remember.