-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
4,861 additions
and
476 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions
37
dart_native/android/src/main/java/com/dartnative/dart_native/CallbackInvocationHandler.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,37 @@ | ||
package com.dartnative.dart_native; | ||
|
||
import android.util.Log; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Created by huizzzhou on 2020/11/11. | ||
*/ | ||
public class CallbackInvocationHandler implements InvocationHandler { | ||
private static final String TAG = "CallbackHandler"; | ||
|
||
private static HashMap<Class<?>, String> sTypeConvert = new HashMap<Class<?>, String>() {{ | ||
put(int.class, "I"); | ||
put(float.class, "F"); | ||
put(double.class, "D"); | ||
put(String.class, "Ljava/lang/String;"); | ||
}}; | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
Log.d(TAG, "invoke method: " + method.getName()); | ||
Class<?>[] paramTypes = method.getParameterTypes(); | ||
String[] params = new String[paramTypes.length]; | ||
for (int i = 0; i < paramTypes.length; i++) { | ||
params[i] = sTypeConvert.get(paramTypes[i]); | ||
} | ||
String funName = method.getName(); | ||
long dartObjectAddr = CallbackManager.getInstance().getRegisterDartAddr(proxy); | ||
hookCallback(dartObjectAddr, funName, paramTypes.length, params, args); | ||
return proxy; | ||
} | ||
|
||
static native void hookCallback(long dartObjectAddr, String funName, int argCount, String[] argTypes, Object[] args); | ||
} |
57 changes: 57 additions & 0 deletions
57
dart_native/android/src/main/java/com/dartnative/dart_native/CallbackManager.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,57 @@ | ||
package com.dartnative.dart_native; | ||
|
||
import android.util.Log; | ||
|
||
import java.lang.reflect.Proxy; | ||
import java.util.HashMap; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* Created by huizzzhou on 2020/11/11. | ||
*/ | ||
public class CallbackManager { | ||
private static final String TAG = "CallbackManager"; | ||
private static CallbackManager sCallbackManager; | ||
|
||
private CallbackInvocationHandler mCallbackHandler = new CallbackInvocationHandler(); | ||
private HashMap<Integer, Long> mObjectMap = new HashMap<>(); | ||
|
||
static CallbackManager getInstance() { | ||
if (sCallbackManager == null) { | ||
synchronized (CallbackManager.class) { | ||
if (sCallbackManager == null) { | ||
sCallbackManager = new CallbackManager(); | ||
} | ||
} | ||
} | ||
return sCallbackManager; | ||
} | ||
|
||
@Nullable | ||
public static Object registerCallback(long dartAddr, String clsName) { | ||
try { | ||
Class<?> clz = Class.forName(clsName.replace("/", ".")); | ||
Object proxyObject = Proxy.newProxyInstance( | ||
clz.getClassLoader(), | ||
new Class[] { clz }, | ||
getInstance().mCallbackHandler); | ||
|
||
getInstance().mObjectMap.put(System.identityHashCode(proxyObject), dartAddr); | ||
return proxyObject; | ||
} catch (Exception e) { | ||
Log.e(TAG, e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
long getRegisterDartAddr(Object proxyObject) { | ||
if (proxyObject == null) { | ||
return 0L; | ||
} | ||
|
||
Long dartAddress = getInstance().mObjectMap.get(System.identityHashCode(proxyObject)); | ||
return dartAddress == null ? 0L : dartAddress; | ||
} | ||
|
||
} |
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
Oops, something went wrong.