forked from bumptech/glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request bumptech#856 from swankjesse/jwilson_0102_okhttp_3
Upgrade to OkHttp 3.
- Loading branch information
Showing
18 changed files
with
304 additions
and
23 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ [email protected] | |
SUPPORT_V4_VERSION=22.2.0 | ||
SUPPORT_V7_VERSION=22.2.0 | ||
VOLLEY_VERSION=1.0.16 | ||
OK_HTTP_VERSION=2.3.0 | ||
OK_HTTP_VERSION=3.0.0-RC1 | ||
# TODO: use this in library/build.gradle. | ||
ANDROID_GRADLE_VERSION=1.2.3 | ||
|
||
|
@@ -30,7 +30,7 @@ COVERALLS_GRADLE_VERSION=2.4.0 | |
JUNIT_VERSION=4.12 | ||
MOCKITO_VERSION=1.10.19 | ||
ROBOLECTRIC_VERSION=3.0-rc3 | ||
MOCKWEBSERVER_VERSION=2.3.0 | ||
MOCKWEBSERVER_VERSION=3.0.0-RC1 | ||
TRUTH_VERSION=0.26 | ||
|
||
FINDBUGS_VERSION=3.0.0 | ||
|
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
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
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,31 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile project(':glide') | ||
|
||
compile "com.squareup.okhttp3:okhttp:${OK_HTTP_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 | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_7 | ||
targetCompatibility JavaVersion.VERSION_1_7 | ||
} | ||
} | ||
|
||
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=2.0.0-SNAPSHOT | ||
VERSION_MAJOR=2 | ||
VERSION_MINOR=0 | ||
VERSION_PATCH=0 | ||
VERSION_CODE=8 | ||
|
||
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.GlideBuilder; | ||
import com.bumptech.glide.Registry; | ||
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> 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, Registry registry) { | ||
registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory()); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...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,95 @@ | ||
package com.bumptech.glide.integration.okhttp3; | ||
|
||
import android.util.Log; | ||
|
||
import com.bumptech.glide.Priority; | ||
import com.bumptech.glide.load.DataSource; | ||
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 okhttp library. | ||
*/ | ||
public class OkHttpStreamFetcher implements DataFetcher<InputStream> { | ||
private static final String TAG = "OkHttpFetcher"; | ||
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 void loadData(Priority priority, final DataCallback<? super InputStream> callback) { | ||
Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl()); | ||
boolean isUserAgentSet = false; | ||
for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { | ||
String key = headerEntry.getKey(); | ||
requestBuilder.addHeader(key, headerEntry.getValue()); | ||
} | ||
Request request = requestBuilder.build(); | ||
|
||
client.newCall(request).enqueue(new okhttp3.Callback() { | ||
@Override | ||
public void onFailure(Request request, IOException e) { | ||
if (Log.isLoggable(TAG, Log.DEBUG)) { | ||
Log.d(TAG, "OkHttp failed to obtain result", e); | ||
} | ||
callback.onLoadFailed(e); | ||
} | ||
|
||
@Override | ||
public void onResponse(Response response) throws IOException { | ||
if (response.isSuccessful()) { | ||
long contentLength = response.body().contentLength(); | ||
responseBody = response.body(); | ||
stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); | ||
} else if (Log.isLoggable(TAG, Log.DEBUG)) { | ||
Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message()); | ||
} | ||
callback.onDataReady(stream); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void cleanup() { | ||
try { | ||
if (stream != null) { | ||
stream.close(); | ||
} | ||
} catch (IOException e) { | ||
// Ignored | ||
} | ||
if (responseBody != null) { | ||
responseBody.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
// TODO: call cancel on the client when this method is called on a background thread. See #257 | ||
} | ||
|
||
@Override | ||
public Class<InputStream> getDataClass() { | ||
return InputStream.class; | ||
} | ||
|
||
@Override | ||
public DataSource getDataSource() { | ||
return DataSource.REMOTE; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...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,82 @@ | ||
package com.bumptech.glide.integration.okhttp3; | ||
|
||
import android.content.Context; | ||
|
||
import com.bumptech.glide.load.Options; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.load.model.ModelLoader; | ||
import com.bumptech.glide.load.model.ModelLoaderFactory; | ||
import com.bumptech.glide.load.model.MultiModelLoaderFactory; | ||
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 boolean handles(GlideUrl url) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height, | ||
Options options) { | ||
return new LoadData<>(model, 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; | ||
|
||
private static Call.Factory getInternalClient() { | ||
if (internalClient == null) { | ||
synchronized (Factory.class) { | ||
if (internalClient == null) { | ||
internalClient = new OkHttpClient(); | ||
} | ||
} | ||
} | ||
return internalClient; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
@Override | ||
public ModelLoader<GlideUrl, InputStream> build(Context context, | ||
MultiModelLoaderFactory multiFactory) { | ||
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
Oops, something went wrong.