-
Notifications
You must be signed in to change notification settings - Fork 60
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
MethodNotFoundException fixed #6
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,15 @@ | |
import android.graphics.PorterDuff; | ||
import android.graphics.PorterDuffColorFilter; | ||
import android.graphics.drawable.Drawable; | ||
import android.os.Build; | ||
import android.support.v4.graphics.drawable.DrawableCompat; | ||
import android.util.Log; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static android.graphics.PorterDuff.Mode; | ||
import static android.graphics.PorterDuff.Mode.SRC_IN; | ||
import static android.os.Build.VERSION.SDK_INT; | ||
|
@@ -41,20 +48,25 @@ public class Utils { | |
static final Mode DEFAULT_TINT_MODE = SRC_IN; | ||
static final boolean LOLLIPOP_PLUS = SDK_INT >= LOLLIPOP; | ||
|
||
public static <T> T tryInvoke(Object target, String methodName, Object... args) { | ||
final int argsCount = args == null ? 0 : args.length; | ||
Class<?>[] argTypes = new Class<?>[argsCount]; | ||
for (int i = 0; i < argsCount; i++) { | ||
argTypes[i] = args[i].getClass(); | ||
} | ||
|
||
return tryInvoke(target, methodName, argTypes, args); | ||
} | ||
static Map<String, Method> sCachedMethods = new HashMap<>(); | ||
static Class[][] sCachedArgsTypes = { | ||
{int.class}, {PorterDuff.Mode.class}, {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better use three different constants, so it is more readable when using it: static final Class[] INT_ARG = {int.class};
static final Class[] MODE_ARG = {Mode.class};
static final Class[] NO_ARG = {}; |
||
}; | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <T> T tryInvoke(Object target, String methodName, Class<?>[] argTypes, | ||
Object... args) { | ||
|
||
try { | ||
return (T) target.getClass().getDeclaredMethod(methodName, argTypes).invoke(target, args); | ||
Method method = sCachedMethods.get(methodName); | ||
if(method != null){ | ||
return (T) method.invoke(target, args); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two spaces please |
||
} | ||
|
||
method = target.getClass().getDeclaredMethod(methodName, argTypes); | ||
sCachedMethods.put(methodName, method); | ||
|
||
return (T) method.invoke(target, args); | ||
} catch (Exception pokemon) { | ||
Log.e(LOGTAG, "Unable to invoke " + methodName + " on " + target, pokemon); | ||
} | ||
|
@@ -63,7 +75,7 @@ public static <T> T tryInvoke(Object target, String methodName, Class<?>[] argTy | |
} | ||
|
||
static int getLayoutDirection(Drawable drawable) { | ||
final Integer layoutDirection = tryInvoke(drawable, "getLayoutDirection", (Object[])null); | ||
final Integer layoutDirection = tryInvoke(drawable, "getLayoutDirection", sCachedArgsTypes[2]); | ||
return layoutDirection == null ? LTR : layoutDirection.intValue(); | ||
} | ||
|
||
|
@@ -80,8 +92,14 @@ static Mode parseTintMode(int value, Mode defaultMode) { | |
case 9: return Mode.SRC_ATOP; | ||
case 14: return Mode.MULTIPLY; | ||
case 15: return Mode.SCREEN; | ||
case 16: return Mode.ADD; | ||
default: return defaultMode; | ||
|
||
case 16: | ||
if(Build.VERSION.SDK_INT > 11) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use a constant like |
||
return Mode.ADD; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto: two spaces please |
||
} | ||
|
||
default: | ||
return defaultMode; | ||
} | ||
} | ||
|
||
|
@@ -96,12 +114,13 @@ static PorterDuffColorFilter updateTintFilter(Drawable drawable, PorterDuffColor | |
} | ||
|
||
final int color = tint.getColorForState(drawable.getState(), Color.TRANSPARENT); | ||
|
||
if (tintFilter == null || !LOLLIPOP_PLUS) { // TODO worth caching them? | ||
return new PorterDuffColorFilter(color, tintMode); | ||
} | ||
|
||
tryInvoke(tintFilter, "setColor", color); | ||
tryInvoke(tintFilter, "setMode", tintMode); | ||
tryInvoke(tintFilter, "setColor", sCachedArgsTypes[0], color); | ||
tryInvoke(tintFilter, "setMode", sCachedArgsTypes[1],tintMode); | ||
return tintFilter; | ||
} | ||
|
||
|
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.
Please use SimpleArrayMap