Skip to content

Commit

Permalink
[TIMOB-2953] Hyperloop fails to cast Ti Intent to native Android Intent
Browse files Browse the repository at this point in the history
try to special case IntentProxy/Intent and ActivityProxy/Activity for method calls/constructors so that we can unwrap the titanium proxies to the native classes, or can accept titanium or native versions for any method expecting titanium or native versions.
  • Loading branch information
sgtcoolguy committed Sep 28, 2016
1 parent 89e7950 commit 4830a63
Showing 1 changed file with 40 additions and 6 deletions.
46 changes: 40 additions & 6 deletions android/src/hyperloop/HyperloopUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.titanium.proxy.ActivityProxy;
import org.appcelerator.titanium.proxy.IntentProxy;
import org.appcelerator.titanium.proxy.TiViewProxy;

import android.util.Log;

abstract class HyperloopUtil {
// TODO This is a hack. We should move all this stuff into the BaseProxy or
// something...
Expand Down Expand Up @@ -112,14 +115,14 @@ private static boolean isKnownType(Object item) {
|| item instanceof float[] || item instanceof short[]
|| item instanceof long[] || item instanceof boolean[];
}

/**
* Validate whether or not the current device is a simulator.
*
* @return
*/
public static boolean isEmulator() {
return Build.FINGERPRINT.startsWith("generic")
return Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
Expand Down Expand Up @@ -171,6 +174,10 @@ static Object unwrap(Object object) {
ActivityProxy ap = (ActivityProxy) object;
return ap.getActivity();
}
if (object instanceof IntentProxy) {
IntentProxy ip = (IntentProxy) object;
return ip.getIntent();
}
// Convert Ti.UI.View subclasses
if (object instanceof TiViewProxy) {
TiViewProxy tvp = (TiViewProxy) object;
Expand Down Expand Up @@ -302,7 +309,14 @@ static Object convertTo(Object newValue, Class<?> target) {
return converted;
}
}
// Not a primitive or array... So, just hope it's the right type?

// Special case proxy conversions
if (IntentProxy.class.equals(target) && (newValue instanceof Intent)) {
return new IntentProxy((Intent) newValue);
} else if (ActivityProxy.class.equals(target) && (newValue instanceof Activity)) {
return new ActivityProxy((Activity) newValue);
}
// Not a primitive or array, or special proxy conversion... So, just hope it's the right type?
return newValue;
}

Expand Down Expand Up @@ -678,14 +692,27 @@ else if (boolean.class.equals(target) && Boolean.class.equals(argument)) {
}

// Non-primitives
if (!target.isAssignableFrom(argument)) {
if (!isAssignable(target, argument, arg)) {
return Match.NO_MATCH;
}

// How far are the two types in the type hierarchy?
return 100 * hops(argument, target, 0);
}

private static boolean isAssignable(Class<?> target, Class<?> fromType, Object object) {
if (target.isAssignableFrom(fromType)) {
return true;
}
// FIXME Handle converting com.android.view.View -> org.appcelerator.titanium.proxy.TiViewProxy
if (ActivityProxy.class.equals(target)) {
return (object instanceof Activity);
} else if (IntentProxy.class.equals(target)) {
return (object instanceof Intent);
}
return false;
}

/**
* Try to use recursion to determine how many types away in the type
* hierarchy the target type is.
Expand All @@ -710,6 +737,13 @@ private static int hops(Class<?> src, Class<?> target, int hops) {
return hops;
}

// return 100 hops when converting between Activity <-> ActivityProxy, Intent <-> IntentProxy
if (ActivityProxy.class.equals(target) && Activity.class.isAssignableFrom(src)) {
return 100;
} else if (IntentProxy.class.equals(target) && Intent.class.isAssignableFrom(src)) {
return 100;
}

// Take the least hops of traversing the parent type...
int result = hops(src.getSuperclass(), target, hops + 1);

Expand Down

0 comments on commit 4830a63

Please sign in to comment.