-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration library for use with Guava's ListenableFuture class.
PiperOrigin-RevId: 266481039
- Loading branch information
1 parent
9bb2c19
commit 9fb1036
Showing
8 changed files
with
131 additions
and
5 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
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ POM_DEVELOPER_NAME=Sam Judd | |
POM_DEVELOPER_EMAIL=[email protected] | ||
ANDROID_SUPPORT_VERSION=27.1.1 | ||
ANDROID_X_VERSION=1.0.0 | ||
ANDROID_X_FUTURES_VERSION=1.0.0-rc01 | ||
ANDROIDX_TEST_VERSION=1.1.0 | ||
VOLLEY_VERSION=1.1.0 | ||
OK_HTTP_VERSION=3.9.1 | ||
|
@@ -33,7 +34,7 @@ TRUTH_VERSION=0.45 | |
JSR_305_VERSION=3.0.2 | ||
AUTO_SERVICE_VERSION=1.0-rc3 | ||
JAVAPOET_VERSION=1.9.0 | ||
GUAVA_VERSION=27.0.1-android | ||
GUAVA_VERSION=28.1-android | ||
|
||
PMD_VERSION=6.0.0 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apply plugin: 'com.android.library' | ||
|
||
dependencies { | ||
implementation project(':library') | ||
implementation "com.google.guava:guava:${GUAVA_VERSION}" | ||
implementation "androidx.concurrent:concurrent-futures:${ANDROID_X_FUTURES_VERSION}" | ||
|
||
testImplementation "com.google.truth:truth:${TRUTH_VERSION}" | ||
testImplementation "junit:junit:${JUNIT_VERSION}" | ||
testImplementation "org.robolectric:robolectric:${ROBOLECTRIC_VERSION}" | ||
testImplementation "androidx.legacy:legacy-support-v4:${ANDROID_X_VERSION}" | ||
} | ||
|
||
android { | ||
compileSdkVersion COMPILE_SDK_VERSION as int | ||
|
||
defaultConfig { | ||
minSdkVersion MIN_SDK_VERSION as int | ||
targetSdkVersion TARGET_SDK_VERSION 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,4 @@ | ||
POM_NAME=Glide Concurrent Integration | ||
POM_ARTIFACT_ID=concurrent-integration | ||
POM_PACKAGING=aar | ||
POM_DESCRIPTION=An integration library for using Glide with ListenableFutures |
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,6 @@ | ||
<manifest | ||
package="com.bumptech.glide.integration.concurrent"> | ||
|
||
<application> | ||
</application> | ||
</manifest> |
82 changes: 82 additions & 0 deletions
82
...tion/concurrent/src/main/java/com/bumptech/glide/integration/concurrent/GlideFutures.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.concurrent; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.concurrent.futures.CallbackToFutureAdapter; | ||
import androidx.concurrent.futures.CallbackToFutureAdapter.Completer; | ||
import androidx.concurrent.futures.CallbackToFutureAdapter.Resolver; | ||
import com.bumptech.glide.RequestBuilder; | ||
import com.bumptech.glide.load.DataSource; | ||
import com.bumptech.glide.load.engine.GlideException; | ||
import com.bumptech.glide.request.FutureTarget; | ||
import com.bumptech.glide.request.RequestListener; | ||
import com.bumptech.glide.request.target.Target; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import com.google.common.util.concurrent.MoreExecutors; | ||
|
||
/** Utilities for getting ListenableFutures out of Glide. */ | ||
public final class GlideFutures { | ||
|
||
/** | ||
* Convert a pending load request into a ListenableFuture. | ||
* | ||
* <p>Sample code: | ||
* | ||
* <pre>{@code | ||
* ListenableFuture<File> image = | ||
* GlideFutures.submit(requestManager.asFile().load(url)); | ||
* }</pre> | ||
* | ||
* @param requestBuilder A request builder for the resource to load. It must be tied to an | ||
* application Glide instance, and must not have a listener set. | ||
*/ | ||
public static <T> ListenableFuture<T> submit(final RequestBuilder<T> requestBuilder) { | ||
return CallbackToFutureAdapter.getFuture( | ||
new Resolver<T>() { | ||
@Override | ||
public Object attachCompleter(@NonNull Completer<T> completer) { | ||
GlideLoadingListener<T> listener = new GlideLoadingListener<>(completer); | ||
final FutureTarget<T> futureTarget = requestBuilder.listener(listener).submit(); | ||
completer.addCancellationListener( | ||
new Runnable() { | ||
@Override | ||
public void run() { | ||
futureTarget.cancel(/*mayInterruptIfRunning=*/ true); | ||
} | ||
}, | ||
MoreExecutors.directExecutor()); | ||
return listener; | ||
} | ||
}); | ||
} | ||
|
||
/** Listener to convert Glide load results into ListenableFutures. */ | ||
private static final class GlideLoadingListener<T> implements RequestListener<T> { | ||
|
||
private final Completer<T> completer; | ||
|
||
GlideLoadingListener(Completer<T> completer) { | ||
this.completer = completer; | ||
} | ||
|
||
@Override | ||
public boolean onLoadFailed( | ||
@Nullable GlideException e, Object model, Target<T> target, boolean isFirst) { | ||
completer.setException(e != null ? e : new RuntimeException("Unknown error")); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onResourceReady( | ||
T resource, Object model, Target<T> target, DataSource dataSource, boolean isFirst) { | ||
try { | ||
completer.set(resource); | ||
} catch (Throwable t) { | ||
completer.setException(t); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
private GlideFutures() {} | ||
} |
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