Skip to content

Commit

Permalink
Use correct async constructor reflection (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
elihart authored Aug 29, 2018
1 parent 9435d73 commit 5145c36
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.16.4 (Aug 29, 2018)
- **New** `EpoxyAsyncUtil` and `AsyncEpoxyController` make it easier to use Epoxy's async behavior out of the box
- **New** Epoxy's background diffing posts messages back to the main thread asynchronously so they are not blocked by waiting for vsync

# 2.16.3 (Aug 24, 2018)
- **New** Add `AsyncEpoxyController` for easy access to async support. Change background diffing to post asynchronously to the main thread (https://github.com/airbnb/epoxy/pull/509)

Expand Down
36 changes: 10 additions & 26 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/EpoxyAsyncUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import android.os.Build;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable;

import java.lang.reflect.Constructor;

/**
* Various helpers for running Epoxy operations off the main thread.
Expand Down Expand Up @@ -60,18 +58,18 @@ public static Handler createHandler(Looper looper, boolean async) {
return new Handler(looper);
}

// Standard way of exposing async handler on older api's from the support library
// https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/core
// /src/main/java/androidx/core/os/HandlerCompat.java#51
if (Build.VERSION.SDK_INT >= 28) {
return Handler.createAsync(looper);
}

if (Build.VERSION.SDK_INT >= 17) {
Constructor<Handler> handlerConstructor = asyncHandlerConstructor();
if (handlerConstructor != null) {
try {
return handlerConstructor.newInstance(looper, null, true);
} catch (Throwable e) {
// Fallback
}
if (Build.VERSION.SDK_INT >= 16) {
try {
//noinspection JavaReflectionMemberAccess
return Handler.class.getDeclaredConstructor(Looper.class, Callback.class, boolean.class)
.newInstance(looper, null, true);
} catch (Throwable ignored) {
}
}

Expand All @@ -86,18 +84,4 @@ public static Looper buildBackgroundLooper(String threadName) {
handlerThread.start();
return handlerThread.getLooper();
}

@Nullable
private static Constructor<Handler> asyncHandlerConstructor() {
try {
//noinspection JavaReflectionMemberAccess
return Handler.class.getConstructor(
Looper.class,
Handler.Callback.class,
Boolean.class
);
} catch (Throwable e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.airbnb.epoxy;

import android.os.Looper;

import static com.airbnb.epoxy.EpoxyAsyncUtil.createHandler;
import static com.airbnb.epoxy.EpoxyAsyncUtil.AYSNC_MAIN_THREAD_HANDLER;
import static com.airbnb.epoxy.EpoxyAsyncUtil.MAIN_THREAD_HANDLER;

class MainThreadExecutor extends HandlerExecutor {
static final MainThreadExecutor INSTANCE = new MainThreadExecutor(false);
static final MainThreadExecutor ASYNC_INSTANCE = new MainThreadExecutor(true);

MainThreadExecutor(boolean async) {
super(createHandler(Looper.getMainLooper(), async));
super(async ? AYSNC_MAIN_THREAD_HANDLER : MAIN_THREAD_HANDLER);
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=2.16.3
VERSION_NAME=2.16.4
GROUP=com.airbnb.android
POM_DESCRIPTION=Epoxy is a system for composing complex screens with a ReyclerView in Android.
POM_URL=https://github.com/airbnb/epoxy
Expand Down

0 comments on commit 5145c36

Please sign in to comment.