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

[TIMOB-2953] (1_2_X) Hyperloop fails to cast Ti Intent to native Android Intent #80

Merged
merged 1 commit into from
Sep 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 47 additions & 6 deletions android/src/hyperloop/HyperloopUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
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.KrollDict;
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 +116,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 +175,16 @@ static Object unwrap(Object object) {
ActivityProxy ap = (ActivityProxy) object;
return ap.getActivity();
}
if (object instanceof IntentProxy) {
IntentProxy ip = (IntentProxy) object;
Intent i = ip.getIntent();
// Because our SDK is lame, if you supply not creation dictionary, the wrapped Intent object may be null!
if (i != null) {
return i;
}
ip.handleCreationDict(new KrollDict());
return ip.getIntent();
}
// Convert Ti.UI.View subclasses
if (object instanceof TiViewProxy) {
TiViewProxy tvp = (TiViewProxy) object;
Expand Down Expand Up @@ -302,7 +316,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 +699,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 +744,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