Skip to content
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 1 commit into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SUPPORT_V7_VERSION=19.1.0
# TODO: Upgrade to latest version of Volley when we compile with Java 7.
VOLLEY_VERSION=1.0.8
OK_HTTP_VERSION=2.2.0
OK_HTTP3_VERSION=3.0.0-RC1
ANDROID_GRADLE_VERSION=1.0.0

ROBOLECTRIC_GRADLE_VERSION=0.14.0
Expand Down
26 changes: 26 additions & 0 deletions integration/okhttp3/build.gradle
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')
Copy link
Collaborator

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.


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"
15 changes: 15 additions & 0 deletions integration/okhttp3/gradle.properties
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
8 changes: 8 additions & 0 deletions integration/okhttp3/lint.xml
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>
9 changes: 9 additions & 0 deletions integration/okhttp3/src/main/AndroidManifest.xml
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>
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());
}
}
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
}
}
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.
}
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include ':samples:svg'
include ':integration'
include ':integration:volley'
include ':integration:okhttp'
include ':integration:okhttp3'
include ':testutil'

rootProject.name = 'glide-parent'