mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-10 22:47:58 +08:00
Merging cxxbridge and bridge packages
Reviewed By: javache Differential Revision: D5027875 fbshipit-source-id: 47e081069d4219bdb29f63ce8a78c1f31a590da7
This commit is contained in:
committed by
Facebook Github Bot
parent
31a0b8788f
commit
8b53a2b29b
@@ -9,7 +9,6 @@ DEPS = [
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
react_native_target("java/com/facebook/react/common:common"),
|
||||
react_native_target("java/com/facebook/react/cxxbridge:bridge"),
|
||||
react_native_target("java/com/facebook/react/devsupport:devsupport"),
|
||||
react_native_target("java/com/facebook/react/devsupport:interfaces"),
|
||||
react_native_target("java/com/facebook/react/jstasks:jstasks"),
|
||||
|
||||
@@ -10,14 +10,14 @@ import java.util.Map;
|
||||
import com.facebook.common.logging.FLog;
|
||||
import com.facebook.react.bridge.BaseJavaModule;
|
||||
import com.facebook.react.bridge.ModuleSpec;
|
||||
import com.facebook.react.bridge.ModuleHolder;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.NativeModuleRegistry;
|
||||
import com.facebook.react.bridge.OnBatchCompleteListener;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactMarker;
|
||||
import com.facebook.react.bridge.ReactMarkerConstants;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.cxxbridge.ModuleHolder;
|
||||
import com.facebook.react.cxxbridge.NativeModuleRegistry;
|
||||
import com.facebook.react.module.model.ReactModuleInfo;
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,16 +39,16 @@ import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReactMarker;
|
||||
import com.facebook.react.bridge.ReactMarkerConstants;
|
||||
import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec;
|
||||
import com.facebook.react.bridge.CatalystInstanceImpl;
|
||||
import com.facebook.react.bridge.JSBundleLoader;
|
||||
import com.facebook.react.bridge.JSCJavaScriptExecutor;
|
||||
import com.facebook.react.bridge.JavaScriptExecutor;
|
||||
import com.facebook.react.bridge.NativeModuleRegistry;
|
||||
import com.facebook.react.bridge.ProxyJavaScriptExecutor;
|
||||
import com.facebook.react.bridge.UiThreadUtil;
|
||||
import com.facebook.react.common.LifecycleState;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.common.annotations.VisibleForTesting;
|
||||
import com.facebook.react.cxxbridge.CatalystInstanceImpl;
|
||||
import com.facebook.react.cxxbridge.JSBundleLoader;
|
||||
import com.facebook.react.cxxbridge.JSCJavaScriptExecutor;
|
||||
import com.facebook.react.cxxbridge.JavaScriptExecutor;
|
||||
import com.facebook.react.cxxbridge.NativeModuleRegistry;
|
||||
import com.facebook.react.cxxbridge.ProxyJavaScriptExecutor;
|
||||
import com.facebook.react.cxxbridge.UiThreadUtil;
|
||||
import com.facebook.react.devsupport.DevSupportManagerFactory;
|
||||
import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
|
||||
import com.facebook.react.devsupport.RedBoxHandler;
|
||||
|
||||
@@ -13,8 +13,8 @@ import android.app.Application;
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
|
||||
import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener;
|
||||
import com.facebook.react.bridge.JSBundleLoader;
|
||||
import com.facebook.react.common.LifecycleState;
|
||||
import com.facebook.react.cxxbridge.JSBundleLoader;
|
||||
import com.facebook.react.devsupport.interfaces.DevSupportManager;
|
||||
import com.facebook.react.devsupport.RedBoxHandler;
|
||||
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
|
||||
|
||||
@@ -6,14 +6,155 @@
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
import com.facebook.react.bridge.ReadableType;
|
||||
import com.facebook.react.bridge.WritableNativeArray;
|
||||
import com.facebook.react.bridge.WritableNativeMap;
|
||||
|
||||
public class Arguments {
|
||||
private static Object makeNativeObject(Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
} else if (object instanceof Float ||
|
||||
object instanceof Long ||
|
||||
object instanceof Byte ||
|
||||
object instanceof Short) {
|
||||
return new Double(((Number) object).doubleValue());
|
||||
} else if (object.getClass().isArray()) {
|
||||
return makeNativeArray(object);
|
||||
} else if (object instanceof List) {
|
||||
return makeNativeArray((List) object);
|
||||
} else if (object instanceof Map) {
|
||||
return makeNativeMap((Map<String, Object>) object);
|
||||
} else if (object instanceof Bundle) {
|
||||
return makeNativeMap((Bundle) object);
|
||||
} else {
|
||||
// Boolean, Integer, Double, String, WritableNativeArray, WritableNativeMap
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a List into a NativeArray. The data types supported
|
||||
* are boolean, int, float, double, and String. List, Map, and Bundle
|
||||
* objects, as well as arrays, containing values of the above types and/or
|
||||
* null, or any recursive arrangement of these, are also supported. The best
|
||||
* way to think of this is a way to generate a Java representation of a json
|
||||
* list, from Java types which have a natural representation in json.
|
||||
*/
|
||||
public static WritableNativeArray makeNativeArray(List objects) {
|
||||
WritableNativeArray nativeArray = new WritableNativeArray();
|
||||
if (objects == null) {
|
||||
return nativeArray;
|
||||
}
|
||||
for (Object elem : objects) {
|
||||
elem = makeNativeObject(elem);
|
||||
if (elem == null) {
|
||||
nativeArray.pushNull();
|
||||
} else if (elem instanceof Boolean) {
|
||||
nativeArray.pushBoolean((Boolean) elem);
|
||||
} else if (elem instanceof Integer) {
|
||||
nativeArray.pushInt((Integer) elem);
|
||||
} else if (elem instanceof Double) {
|
||||
nativeArray.pushDouble((Double) elem);
|
||||
} else if (elem instanceof String) {
|
||||
nativeArray.pushString((String) elem);
|
||||
} else if (elem instanceof WritableNativeArray) {
|
||||
nativeArray.pushArray((WritableNativeArray) elem);
|
||||
} else if (elem instanceof WritableNativeMap) {
|
||||
nativeArray.pushMap((WritableNativeMap) elem);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not convert " + elem.getClass());
|
||||
}
|
||||
}
|
||||
return nativeArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* This overload is like the above, but uses reflection to operate on any
|
||||
* primitive or object type.
|
||||
*/
|
||||
public static <T> WritableNativeArray makeNativeArray(final Object objects) {
|
||||
if (objects == null) {
|
||||
return new WritableNativeArray();
|
||||
}
|
||||
// No explicit check for objects's type here. If it's not an array, the
|
||||
// Array methods will throw IllegalArgumentException.
|
||||
return makeNativeArray(new AbstractList() {
|
||||
public int size() {
|
||||
return Array.getLength(objects);
|
||||
}
|
||||
|
||||
public Object get(int index) {
|
||||
return Array.get(objects, index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void addEntry(WritableNativeMap nativeMap, String key, Object value) {
|
||||
value = makeNativeObject(value);
|
||||
if (value == null) {
|
||||
nativeMap.putNull(key);
|
||||
} else if (value instanceof Boolean) {
|
||||
nativeMap.putBoolean(key, (Boolean) value);
|
||||
} else if (value instanceof Integer) {
|
||||
nativeMap.putInt(key, (Integer) value);
|
||||
} else if (value instanceof Number) {
|
||||
nativeMap.putDouble(key, ((Number) value).doubleValue());
|
||||
} else if (value instanceof String) {
|
||||
nativeMap.putString(key, (String) value);
|
||||
} else if (value instanceof WritableNativeArray) {
|
||||
nativeMap.putArray(key, (WritableNativeArray) value);
|
||||
} else if (value instanceof WritableNativeMap) {
|
||||
nativeMap.putMap(key, (WritableNativeMap) value);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not convert " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a Map into a NativeMap. Value types are supported as
|
||||
* with makeNativeArray. The best way to think of this is a way to generate
|
||||
* a Java representation of a json object, from Java types which have a
|
||||
* natural representation in json.
|
||||
*/
|
||||
public static WritableNativeMap makeNativeMap(Map<String, Object> objects) {
|
||||
WritableNativeMap nativeMap = new WritableNativeMap();
|
||||
if (objects == null) {
|
||||
return nativeMap;
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : objects.entrySet()) {
|
||||
addEntry(nativeMap, entry.getKey(), entry.getValue());
|
||||
}
|
||||
return nativeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the above, but takes a Bundle instead of a Map.
|
||||
*/
|
||||
public static WritableNativeMap makeNativeMap(Bundle bundle) {
|
||||
WritableNativeMap nativeMap = new WritableNativeMap();
|
||||
if (bundle == null) {
|
||||
return nativeMap;
|
||||
}
|
||||
for (String key : bundle.keySet()) {
|
||||
addEntry(nativeMap, key, bundle.get(key));
|
||||
}
|
||||
return nativeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method should be used when you need to stub out creating NativeArrays in unit tests.
|
||||
@@ -64,35 +205,34 @@ public class Arguments {
|
||||
* Convert an array to a {@link WritableArray}.
|
||||
*
|
||||
* @param array the array to convert. Supported types are: {@code String[]}, {@code Bundle[]},
|
||||
* {@code int[]}, {@code float[]}, {@code double[]}, {@code boolean[]}.
|
||||
*
|
||||
* {@code int[]}, {@code float[]}, {@code double[]}, {@code boolean[]}.
|
||||
* @return the converted {@link WritableArray}
|
||||
* @throws IllegalArgumentException if the passed object is none of the above types
|
||||
*/
|
||||
public static WritableArray fromArray(Object array) {
|
||||
WritableArray catalystArray = createArray();
|
||||
if (array instanceof String[]) {
|
||||
for (String v: (String[]) array) {
|
||||
for (String v : (String[]) array) {
|
||||
catalystArray.pushString(v);
|
||||
}
|
||||
} else if (array instanceof Bundle[]) {
|
||||
for (Bundle v: (Bundle[]) array) {
|
||||
for (Bundle v : (Bundle[]) array) {
|
||||
catalystArray.pushMap(fromBundle(v));
|
||||
}
|
||||
} else if (array instanceof int[]) {
|
||||
for (int v: (int[]) array) {
|
||||
for (int v : (int[]) array) {
|
||||
catalystArray.pushInt(v);
|
||||
}
|
||||
} else if (array instanceof float[]) {
|
||||
for (float v: (float[]) array) {
|
||||
for (float v : (float[]) array) {
|
||||
catalystArray.pushDouble(v);
|
||||
}
|
||||
} else if (array instanceof double[]) {
|
||||
for (double v: (double[]) array) {
|
||||
for (double v : (double[]) array) {
|
||||
catalystArray.pushDouble(v);
|
||||
}
|
||||
} else if (array instanceof boolean[]) {
|
||||
for (boolean v: (boolean[]) array) {
|
||||
for (boolean v : (boolean[]) array) {
|
||||
catalystArray.pushBoolean(v);
|
||||
}
|
||||
} else {
|
||||
@@ -104,11 +244,11 @@ public class Arguments {
|
||||
/**
|
||||
* Convert a {@link Bundle} to a {@link WritableMap}. Supported key types in the bundle
|
||||
* are:
|
||||
*
|
||||
* <p>
|
||||
* <ul>
|
||||
* <li>primitive types: int, float, double, boolean</li>
|
||||
* <li>arrays supported by {@link #fromArray(Object)}</li>
|
||||
* <li>{@link Bundle} objects that are recursively converted to maps</li>
|
||||
* <li>primitive types: int, float, double, boolean</li>
|
||||
* <li>arrays supported by {@link #fromArray(Object)}</li>
|
||||
* <li>{@link Bundle} objects that are recursively converted to maps</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param bundle the {@link Bundle} to convert
|
||||
@@ -117,7 +257,7 @@ public class Arguments {
|
||||
*/
|
||||
public static WritableMap fromBundle(Bundle bundle) {
|
||||
WritableMap map = createMap();
|
||||
for (String key: bundle.keySet()) {
|
||||
for (String key : bundle.keySet()) {
|
||||
Object value = bundle.get(key);
|
||||
if (value == null) {
|
||||
map.putNull(key);
|
||||
@@ -144,6 +284,7 @@ public class Arguments {
|
||||
|
||||
/**
|
||||
* Convert a {@link WritableMap} to a {@link Bundle}.
|
||||
*
|
||||
* @param readableMap the {@link WritableMap} to convert.
|
||||
* @return the converted {@link Bundle}.
|
||||
*/
|
||||
|
||||
@@ -33,5 +33,6 @@ android_library(
|
||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/common:common"),
|
||||
react_native_target("java/com/facebook/react/module/model:model"),
|
||||
],
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
@@ -10,15 +10,15 @@ import com.facebook.react.bridge.NativeArray;
|
||||
import static com.facebook.react.bridge.Arguments.*;
|
||||
|
||||
/**
|
||||
* Callback impl that calls directly into the cxxbridge. Created from C++.
|
||||
* Callback impl that calls directly into the cxx bridge. Created from C++.
|
||||
*/
|
||||
@DoNotStrip
|
||||
public class CallbackImpl implements Callback {
|
||||
public class CxxCallbackImpl implements Callback {
|
||||
@DoNotStrip
|
||||
private final HybridData mHybridData;
|
||||
|
||||
@DoNotStrip
|
||||
private CallbackImpl(HybridData hybridData) {
|
||||
private CxxCallbackImpl(HybridData hybridData) {
|
||||
mHybridData = hybridData;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
@@ -1,6 +1,6 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Provider;
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-keepnames class * extends com.facebook.react.bridge.JavaScriptModule { *; }
|
||||
-keepnames class * extends com.facebook.react.cxxbridge.CxxModuleWrapper {*; }
|
||||
-keepnames class * extends com.facebook.react.bridge.CxxModuleWrapper {*; }
|
||||
-keepclassmembers class * extends com.facebook.react.bridge.NativeModule {
|
||||
@com.facebook.react.bridge.ReactMethod *;
|
||||
public <init>(...);
|
||||
@@ -17,3 +17,11 @@
|
||||
void markerAnnotate(int,int,java.lang.String,java.lang.String);
|
||||
void markerTag(int,int,java.lang.String);
|
||||
}
|
||||
|
||||
## Putting this here is kind of a hack. I don't want to modify the OSS bridge.
|
||||
## TODO mhorowitz: add @DoNotStrip to the interface directly.
|
||||
|
||||
-keepclassmembers class com.facebook.react.bridge.queue.MessageQueueThread {
|
||||
public boolean isOnThread();
|
||||
public void assertIsOnThread();
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ android_library(
|
||||
],
|
||||
deps = [
|
||||
":build_config",
|
||||
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
|
||||
react_native_dep("third-party/android/support/v4:lib-support-v4"),
|
||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
||||
import com.facebook.react.bridge.ReadableType;
|
||||
import com.facebook.react.bridge.WritableNativeArray;
|
||||
import com.facebook.react.bridge.WritableNativeMap;
|
||||
|
||||
public class Arguments {
|
||||
private static Object makeNativeObject(Object object) {
|
||||
if (object == null) {
|
||||
return null;
|
||||
} else if (object instanceof Float ||
|
||||
object instanceof Long ||
|
||||
object instanceof Byte ||
|
||||
object instanceof Short) {
|
||||
return new Double(((Number) object).doubleValue());
|
||||
} else if (object.getClass().isArray()) {
|
||||
return makeNativeArray(object);
|
||||
} else if (object instanceof List) {
|
||||
return makeNativeArray((List) object);
|
||||
} else if (object instanceof Map) {
|
||||
return makeNativeMap((Map<String, Object>) object);
|
||||
} else if (object instanceof Bundle) {
|
||||
return makeNativeMap((Bundle) object);
|
||||
} else {
|
||||
// Boolean, Integer, Double, String, WritableNativeArray, WritableNativeMap
|
||||
return object;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a List into a NativeArray. The data types supported
|
||||
* are boolean, int, float, double, and String. List, Map, and Bundle
|
||||
* objects, as well as arrays, containing values of the above types and/or
|
||||
* null, or any recursive arrangement of these, are also supported. The best
|
||||
* way to think of this is a way to generate a Java representation of a json
|
||||
* list, from Java types which have a natural representation in json.
|
||||
*/
|
||||
public static WritableNativeArray makeNativeArray(List objects) {
|
||||
WritableNativeArray nativeArray = new WritableNativeArray();
|
||||
if (objects == null) {
|
||||
return nativeArray;
|
||||
}
|
||||
for (Object elem : objects) {
|
||||
elem = makeNativeObject(elem);
|
||||
if (elem == null) {
|
||||
nativeArray.pushNull();
|
||||
} else if (elem instanceof Boolean) {
|
||||
nativeArray.pushBoolean((Boolean) elem);
|
||||
} else if (elem instanceof Integer) {
|
||||
nativeArray.pushInt((Integer) elem);
|
||||
} else if (elem instanceof Double) {
|
||||
nativeArray.pushDouble((Double) elem);
|
||||
} else if (elem instanceof String) {
|
||||
nativeArray.pushString((String) elem);
|
||||
} else if (elem instanceof WritableNativeArray) {
|
||||
nativeArray.pushArray((WritableNativeArray) elem);
|
||||
} else if (elem instanceof WritableNativeMap) {
|
||||
nativeArray.pushMap((WritableNativeMap) elem);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not convert " + elem.getClass());
|
||||
}
|
||||
}
|
||||
return nativeArray;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This overload is like the above, but uses reflection to operate on any
|
||||
* primitive or object type.
|
||||
*/
|
||||
public static <T> WritableNativeArray makeNativeArray(final Object objects) {
|
||||
if (objects == null) {
|
||||
return new WritableNativeArray();
|
||||
}
|
||||
// No explicit check for objects's type here. If it's not an array, the
|
||||
// Array methods will throw IllegalArgumentException.
|
||||
return makeNativeArray(new AbstractList() {
|
||||
public int size() {
|
||||
return Array.getLength(objects);
|
||||
}
|
||||
public Object get(int index) {
|
||||
return Array.get(objects, index);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void addEntry(WritableNativeMap nativeMap, String key, Object value) {
|
||||
value = makeNativeObject(value);
|
||||
if (value == null) {
|
||||
nativeMap.putNull(key);
|
||||
} else if (value instanceof Boolean) {
|
||||
nativeMap.putBoolean(key, (Boolean) value);
|
||||
} else if (value instanceof Integer) {
|
||||
nativeMap.putInt(key, (Integer) value);
|
||||
} else if (value instanceof Number) {
|
||||
nativeMap.putDouble(key, ((Number) value).doubleValue());
|
||||
} else if (value instanceof String) {
|
||||
nativeMap.putString(key, (String) value);
|
||||
} else if (value instanceof WritableNativeArray) {
|
||||
nativeMap.putArray(key, (WritableNativeArray) value);
|
||||
} else if (value instanceof WritableNativeMap) {
|
||||
nativeMap.putMap(key, (WritableNativeMap) value);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Could not convert " + value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method converts a Map into a NativeMap. Value types are supported as
|
||||
* with makeNativeArray. The best way to think of this is a way to generate
|
||||
* a Java representation of a json object, from Java types which have a
|
||||
* natural representation in json.
|
||||
*/
|
||||
public static WritableNativeMap makeNativeMap(Map<String, Object> objects) {
|
||||
WritableNativeMap nativeMap = new WritableNativeMap();
|
||||
if (objects == null) {
|
||||
return nativeMap;
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : objects.entrySet()) {
|
||||
addEntry(nativeMap, entry.getKey(), entry.getValue());
|
||||
}
|
||||
return nativeMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like the above, but takes a Bundle instead of a Map.
|
||||
*/
|
||||
public static WritableNativeMap makeNativeMap(Bundle bundle) {
|
||||
WritableNativeMap nativeMap = new WritableNativeMap();
|
||||
if (bundle == null) {
|
||||
return nativeMap;
|
||||
}
|
||||
for (String key: bundle.keySet()) {
|
||||
addEntry(nativeMap, key, bundle.get(key));
|
||||
}
|
||||
return nativeMap;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
include_defs("//ReactAndroid/DEFS")
|
||||
|
||||
android_library(
|
||||
name = "bridge",
|
||||
srcs = glob(["**/*.java"]),
|
||||
exported_deps = [
|
||||
react_native_dep("java/com/facebook/jni:jni"),
|
||||
react_native_dep("java/com/facebook/proguard/annotations:annotations"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
],
|
||||
proguard_config = "bridge.pro",
|
||||
visibility = [
|
||||
"PUBLIC",
|
||||
],
|
||||
deps = [
|
||||
react_native_dep("java/com/facebook/systrace:systrace"),
|
||||
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
|
||||
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
|
||||
# TODO mhorowitz:
|
||||
# java/com/facebook/catalyst/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/bridge/
|
||||
# lacks a similar dependency to this. This means that the
|
||||
# loadLibrary calls in it are not guaranteed to succeed. This is
|
||||
# kind of a mess for the jni/jni-internal stuff. In theory, we
|
||||
# should be creating -internal android_library rules, too. In
|
||||
# practice, since these are resolved at runtime, putting the
|
||||
# dependency in the app works, too. gross.
|
||||
# '//native/react/jni:jni-internal',
|
||||
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/common:common"),
|
||||
react_native_target("java/com/facebook/react/devsupport:devsupport"),
|
||||
react_native_target("java/com/facebook/react/module/model:model"),
|
||||
],
|
||||
)
|
||||
@@ -1,52 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.facebook.react.bridge.AssertionException;
|
||||
|
||||
/**
|
||||
* Utility class to make assertions that should not hard-crash the app but instead be handled by the
|
||||
* Catalyst app {@link NativeModuleCallExceptionHandler}. See the javadoc on that class for
|
||||
* more information about our opinion on when these assertions should be used as opposed to
|
||||
* assertions that might throw AssertionError Throwables that will cause the app to hard crash.
|
||||
*/
|
||||
public class SoftAssertions {
|
||||
|
||||
/**
|
||||
* Throw {@link AssertionException} with a given message. Use this method surrounded with
|
||||
* {@code if} block with assert condition in case you plan to do string concatenation to produce
|
||||
* the message.
|
||||
*/
|
||||
public static void assertUnreachable(String message) {
|
||||
throw new AssertionException(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the given condition, throwing an {@link AssertionException} if the condition doesn't
|
||||
* hold.
|
||||
*/
|
||||
public static void assertCondition(boolean condition, String message) {
|
||||
if (!condition) {
|
||||
throw new AssertionException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given Object isn't null, throwing an {@link AssertionException} if it was.
|
||||
*/
|
||||
public static <T> T assertNotNull(@Nullable T instance) {
|
||||
if (instance == null) {
|
||||
throw new AssertionException("Expected object to not be null!");
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.cxxbridge;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
/**
|
||||
* Utility for interacting with the UI thread.
|
||||
*/
|
||||
public class UiThreadUtil {
|
||||
|
||||
@Nullable private static Handler sMainHandler;
|
||||
|
||||
/**
|
||||
* @return {@code true} if the current thread is the UI thread.
|
||||
*/
|
||||
public static boolean isOnUiThread() {
|
||||
return Looper.getMainLooper().getThread() == Thread.currentThread();
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an {@link AssertionException} if the current thread is not the UI thread.
|
||||
*/
|
||||
public static void assertOnUiThread() {
|
||||
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an {@link AssertionException} if the current thread is the UI thread.
|
||||
*/
|
||||
public static void assertNotOnUiThread() {
|
||||
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the given {@code Runnable} on the UI thread.
|
||||
*/
|
||||
public static void runOnUiThread(Runnable runnable) {
|
||||
synchronized (UiThreadUtil.class) {
|
||||
if (sMainHandler == null) {
|
||||
sMainHandler = new Handler(Looper.getMainLooper());
|
||||
}
|
||||
}
|
||||
sMainHandler.post(runnable);
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,5 @@ android_library(
|
||||
],
|
||||
deps = [
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user