Java Code Examples for com.facebook.react.bridge.WritableArray#size()

The following examples show how to use com.facebook.react.bridge.WritableArray#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FabricEventEmitter.java    From react-native-GPay with MIT License 6 votes vote down vote up
/**
 * Destroys `touches` by removing touch objects at indices `indices`. This is
 * to maintain compatibility with W3C touch "end" events, where the active
 * touches don't include the set that has just been "ended".
 *
 * This method was originally in ReactNativeRenderer.js
 *
 * TODO: this method is a copy from ReactNativeRenderer.removeTouchesAtIndices and it needs
 * to be rewritten in a more efficient way,
 *
 * @param touches {@link WritableArray} Deserialized touch objects.
 * @param indices {WritableArray} Indices to remove from `touches`.
 * @return {Array<Touch>} Subsequence of removed touch objects.
 */
private Pair<WritableArray, WritableArray> removeTouchesAtIndices(WritableArray touches, WritableArray indices) {
  WritableArray rippedOut = new WritableNativeArray();
  // use an unsafe downcast to alias to nullable elements,
  // so we can delete and then compact.
  WritableArray tempTouches = new WritableNativeArray();
  Set<Integer> rippedOutIndices = new HashSet<>();
  for (int i = 0; i < indices.size(); i++) {
    int index = indices.getInt(i);
    rippedOut.pushMap(getWritableMap(touches.getMap(index)));
    rippedOutIndices.add(index);
  }
  for (int j = 0 ; j < touches.size() ; j++) {
    if (!rippedOutIndices.contains(j)) {
      tempTouches.pushMap(getWritableMap(touches.getMap(j)));
    }
  }

  return new Pair<>(rippedOut, tempTouches);
}
 
Example 2
Source File: RNMEvaluator.java    From react-native-eval with MIT License 6 votes vote down vote up
/**
 * Marshalls a function call to the javascript layer, via our NativeModule.
 *
 * @param context The context needed to execute this in.
 * @param name The function to execute. e.g. "Math.Pow"
 * @param args The arguments to pass to the function, or null.
 * @param cb The completion callback for the result, or null.
 * @param event The name of the event that our NativeModule is listening for.
 */
private static void callFunction(ReactContext context, String name, @Nullable Object[] args, @Nullable  EvaluatorCallback cb, String event) {
    String callId = UUID.randomUUID().toString();

    if (null != cb) {
        callbacks.put(callId, cb);
    }

    WritableArray arguments = args != null ? Arguments.fromJavaArgs(args) : Arguments.createArray();
    if (arguments.size() == 0) {
        arguments.pushNull();
    }

    WritableMap eventParams = Arguments.createMap();
    eventParams.putString("name", name);
    eventParams.putArray("args", arguments);
    eventParams.putString("callId", callId);

    // TODO: move to AppEventEmitter once App events are supported on android.
    context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(event, eventParams);
}
 
Example 3
Source File: FabricEventEmitter.java    From react-native-GPay with MIT License 5 votes vote down vote up
@Override
public void receiveTouches(String eventTopLevelType, WritableArray touches,
  WritableArray changedIndices) {
  Pair<WritableArray, WritableArray> result =
    TOP_TOUCH_END_KEY.equalsIgnoreCase(eventTopLevelType) ||
      TOP_TOUCH_CANCEL_KEY.equalsIgnoreCase(eventTopLevelType)
      ? removeTouchesAtIndices(touches, changedIndices)
      : touchSubsequence(touches, changedIndices);

  WritableArray changedTouches = result.first;
  touches = result.second;

  for (int jj = 0; jj < changedTouches.size(); jj++) {
    WritableMap touch = getWritableMap(changedTouches.getMap(jj));
    // Touch objects can fulfill the role of `DOM` `Event` objects if we set
    // the `changedTouches`/`touches`. This saves allocations.
    touch.putArray(CHANGED_TOUCHES_KEY, changedTouches);
    touch.putArray(TOUCHES_KEY, touches);
    WritableMap nativeEvent = touch;
    int rootNodeID = 0;
    int target = nativeEvent.getInt(TARGET_KEY);
    if (target < 1) {
      FLog.e(TAG,"A view is reporting that a touch occurred on tag zero.");
    } else {
      rootNodeID = target;
    }
    receiveEvent(rootNodeID, eventTopLevelType, touch);
  }
}
 
Example 4
Source File: Peripheral.java    From react-native-ble-manager with Apache License 2.0 4 votes vote down vote up
public WritableMap asWritableMap(BluetoothGatt gatt) {

		WritableMap map = asWritableMap();

		WritableArray servicesArray = Arguments.createArray();
		WritableArray characteristicsArray = Arguments.createArray();

		if (connected && gatt != null) {
			for (Iterator<BluetoothGattService> it = gatt.getServices().iterator(); it.hasNext();) {
				BluetoothGattService service = it.next();
				WritableMap serviceMap = Arguments.createMap();
				serviceMap.putString("uuid", UUIDHelper.uuidToString(service.getUuid()));

				for (Iterator<BluetoothGattCharacteristic> itCharacteristic = service.getCharacteristics()
						.iterator(); itCharacteristic.hasNext();) {
					BluetoothGattCharacteristic characteristic = itCharacteristic.next();
					WritableMap characteristicsMap = Arguments.createMap();

					characteristicsMap.putString("service", UUIDHelper.uuidToString(service.getUuid()));
					characteristicsMap.putString("characteristic", UUIDHelper.uuidToString(characteristic.getUuid()));

					characteristicsMap.putMap("properties", Helper.decodeProperties(characteristic));

					if (characteristic.getPermissions() > 0) {
						characteristicsMap.putMap("permissions", Helper.decodePermissions(characteristic));
					}

					WritableArray descriptorsArray = Arguments.createArray();

					for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
						WritableMap descriptorMap = Arguments.createMap();
						descriptorMap.putString("uuid", UUIDHelper.uuidToString(descriptor.getUuid()));
						if (descriptor.getValue() != null) {
							descriptorMap.putString("value",
									Base64.encodeToString(descriptor.getValue(), Base64.NO_WRAP));
						} else {
							descriptorMap.putString("value", null);
						}

						if (descriptor.getPermissions() > 0) {
							descriptorMap.putMap("permissions", Helper.decodePermissions(descriptor));
						}
						descriptorsArray.pushMap(descriptorMap);
					}
					if (descriptorsArray.size() > 0) {
						characteristicsMap.putArray("descriptors", descriptorsArray);
					}
					characteristicsArray.pushMap(characteristicsMap);
				}
				servicesArray.pushMap(serviceMap);
			}
			map.putArray("services", servicesArray);
			map.putArray("characteristics", characteristicsArray);
		}

		return map;
	}
 
Example 5
Source File: FabricEventEmitter.java    From react-native-GPay with MIT License 3 votes vote down vote up
/**
 * Selects a subsequence of `Touch`es, without destroying `touches`.
 *
 * This method was originally in ReactNativeRenderer.js
 *
 * @param touches {@link WritableArray} Deserialized touch objects.
 * @param changedIndices {@link WritableArray} Indices by which to pull subsequence.
 * @return {Array<Touch>} Subsequence of touch objects.
 */
private Pair<WritableArray, WritableArray> touchSubsequence(WritableArray touches, WritableArray changedIndices) {
  WritableArray result = new WritableNativeArray();
  for (int i = 0; i < changedIndices.size(); i++) {
    result.pushMap(getWritableMap(touches.getMap(i)));
  }
  return new Pair<>(result, touches);
}