private void notifyNotification(Bundle bundle) {
    String bundleString = convertJSON(bundle);

    WritableMap params = Arguments.createMap();
    params.putString("dataJSON", bundleString);

    sendEvent("remoteNotificationReceived", params);
  }
 @ReactProp(name = PROP_ZOOM_ON_MARKERS, defaultBoolean = false)
 public void setPropZoomOnMarkers(MapView view, Boolean shallZoomOnMarkers) {
   WritableMap properties = getProperties();
   properties.putBoolean(PROP_ZOOM_ON_MARKERS, shallZoomOnMarkers);
   if (shallZoomOnMarkers) {
     zoomOnMarkers();
   }
 }
  private void sendMapError(String message, String type) {
    WritableMap error = Arguments.createMap();
    error.putString("message", message);
    error.putString("type", type);

    reactContext
        .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
        .emit("mapError", error);
  }
  private WritableMap serializeEventData() {
    WritableMap eventData = Arguments.createMap();

    WritableMap selectionData = Arguments.createMap();
    selectionData.putInt("start", mSelectionStart);
    selectionData.putInt("end", mSelectionEnd);

    eventData.putMap("selection", selectionData);
    return eventData;
  }
 public void disconnect() {
   connectCallback = null;
   connected = false;
   if (gatt != null) {
     gatt.close();
     gatt = null;
     Log.d(LOG_TAG, "Disconnect");
     WritableMap map = Arguments.createMap();
     map.putString("peripheral", device.getAddress());
     sendEvent("BleManagerDisconnectPeripheral", map);
   } else Log.d(LOG_TAG, "GATT is null");
 }
  @Override
  public void onCharacteristicChanged(
      BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    super.onCharacteristicChanged(gatt, characteristic);

    // Log.d(LOG_TAG, "onCharacteristicChanged " + characteristic);
    byte[] dataValue = characteristic.getValue();
    Log.d(LOG_TAG, "Letto:" + BleManager.bytesToHex(dataValue) + " da:" + device.getAddress());

    WritableMap map = Arguments.createMap();
    map.putString("peripheral", device.getAddress());
    map.putString("characteristic", characteristic.getUuid().toString());
    map.putString("value", BleManager.bytesToHex(dataValue));
    sendEvent("BleManagerDidUpdateValueForCharacteristic", map);
  }
  // NOTE: Currently not reentrant / doesn't support concurrent requests
  @ReactMethod
  public void launchImageLibrary(final ReadableMap options, final Callback callback) {
    response = Arguments.createMap();

    if (options.hasKey("noData")) {
      noData = options.getBoolean("noData");
    }
    if (options.hasKey("maxWidth")) {
      maxWidth = options.getInt("maxWidth");
    }
    if (options.hasKey("maxHeight")) {
      maxHeight = options.getInt("maxHeight");
    }
    if (options.hasKey("aspectX")) {
      aspectX = options.getInt("aspectX");
    }
    if (options.hasKey("aspectY")) {
      aspectY = options.getInt("aspectY");
    }
    if (options.hasKey("quality")) {
      quality = (int) (options.getDouble("quality") * 100);
    }
    tmpImage = true;
    if (options.hasKey("storageOptions")) {
      tmpImage = false;
    }
    if (options.hasKey("allowsEditing")) {
      allowEditing = options.getBoolean("allowsEditing");
    }
    forceAngle = false;
    if (options.hasKey("angle")) {
      forceAngle = true;
      angle = options.getInt("angle");
    }
    if (options.hasKey("assetProperties")) {
      assetProperties = options.getBoolean("assetProperties");
    }

    Intent libraryIntent =
        new Intent(
            Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    if (libraryIntent.resolveActivity(mMainActivity.getPackageManager()) == null) {
      response.putString("error", "Cannot launch photo library");
      callback.invoke(response);
      return;
    }

    mCallback = callback;

    try {
      mMainActivity.startActivityForResult(libraryIntent, REQUEST_LAUNCH_IMAGE_LIBRARY);
    } catch (ActivityNotFoundException e) {
      e.printStackTrace();
    }
  }
  @Override
  public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    Log.d(
        LOG_TAG,
        "onConnectionStateChange da "
            + status
            + " a "
            + newState
            + " peripheral:"
            + device.getAddress());

    this.gatt = gatt;

    if (newState == BluetoothGatt.STATE_CONNECTED) {

      connected = true;
      gatt.discoverServices();

    } else if (newState == BluetoothGatt.STATE_DISCONNECTED) {

      if (connected) {
        connected = false;

        if (gatt != null) {
          gatt.close();
          gatt = null;
        }

        WritableMap map = Arguments.createMap();
        map.putString("peripheral", device.getAddress());
        sendEvent("BleManagerDisconnectPeripheral", map);
        Log.d(LOG_TAG, "BleManagerDisconnectPeripheral peripheral:" + device.getAddress());
      }
      if (connectFailCallback != null) {
        connectFailCallback.invoke();
        connectFailCallback = null;
        connectCallback = null;
      }
    }
  }
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // robustness code
    if (mCallback == null
        || (mCameraCaptureURI == null && requestCode == REQUEST_LAUNCH_CAMERA)
        || (requestCode != REQUEST_LAUNCH_CAMERA && requestCode != REQUEST_LAUNCH_IMAGE_LIBRARY)) {
      return;
    }

    // user cancel
    if (resultCode != Activity.RESULT_OK) {
      mCallback.invoke(true, Arguments.createMap());
      return;
    }

    WritableMap response = Arguments.createMap();
    Uri uri = (requestCode == REQUEST_LAUNCH_CAMERA) ? mCameraCaptureURI : data.getData();

    // let's set data
    String realPath = getRealPathFromURI(uri);

    response.putString("path", uri.toString());
    response.putString("uri", realPath);
    if (!noData) {
      response.putString("data", getBase64StringFromFile(realPath));
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(realPath, options);
    response.putInt("width", options.outWidth);
    response.putInt("height", options.outHeight);

    try {
      ExifInterface exif = new ExifInterface(realPath);
      int orientation =
          exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      boolean isVertical = true;
      switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
          isVertical = false;
          break;
        case ExifInterface.ORIENTATION_ROTATE_90:
          isVertical = false;
          break;
      }
      response.putBoolean("isVertical", isVertical);
    } catch (IOException e) {
      e.printStackTrace();
    }

    mCallback.invoke(false, response);
  }
 @ReactProp(name = PROP_CLICK_MARKER)
 public void setPropClickMarker(MapView view, @Nullable Integer clickMarker) {
   WritableMap properties = getProperties();
   String key = String.valueOf(clickMarker);
   if (clickMarker == null) {
     if (properties.hasKey(PROP_CLICK_MARKER)) {
       if (markerLookup.containsKey(String.valueOf(properties.getInt(PROP_CLICK_MARKER)))) {
         Marker marker =
             mapMarkers.get(
                 Integer.parseInt(
                     markerLookup.get(String.valueOf(properties.getInt(PROP_CLICK_MARKER)))));
         marker.hideInfoWindow();
         Log.i(REACT_CLASS, "hideInfoWindow");
       }
     }
   } else {
     properties.putInt(PROP_CLICK_MARKER, clickMarker);
     if (markerLookup.containsKey(key)) {
       Marker marker = mapMarkers.get(Integer.parseInt(markerLookup.get(key)));
       marker.showInfoWindow();
       Log.i(REACT_CLASS, "showInfoWindow" + String.valueOf(marker));
     }
   }
 }
  @ReactProp(name = PROP_CENTER)
  public void setPropCenter(MapView view, @Nullable ReadableMap center) {
    WritableMap properties = getProperties();
    if (center != null) {
      WritableMap centerLatLng = Arguments.createMap();
      WritableMap centerMap = Arguments.createMap();
      centerLatLng.putDouble("lat", center.getDouble("lat"));
      centerLatLng.putDouble("lng", center.getDouble("lng"));

      centerMap.putMap("center", centerLatLng);
      properties.merge(centerMap);
      updateCenter();
    }
  }
  private Boolean updateCenter() {
    WritableMap properties = getProperties();
    if (properties.hasKey(PROP_CENTER)) {
      try {
        CameraUpdate cameraUpdate;

        Double lng = properties.getMap(PROP_CENTER).getDouble("lng");
        Double lat = properties.getMap(PROP_CENTER).getDouble("lat");

        if (properties.hasKey(PROP_ZOOM_LEVEL)) {
          int zoomLevel = properties.getInt(PROP_ZOOM_LEVEL);
          mlastZoom = zoomLevel;
          Log.i(REACT_CLASS, "Zoom: " + Integer.toString(properties.getInt(PROP_ZOOM_LEVEL)));
          cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), zoomLevel);
        } else {
          Log.i(REACT_CLASS, "Default Zoom.");
          /*
           * Changed from cameraUpdate = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
           * as it gave me "zoom" Bugs (defaulted to zoom factor 2) as soon as I put in
           * "real" LNG and LAT values...
           */
          cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), mlastZoom);
        }

        map.animateCamera(cameraUpdate);

        return true;
      } catch (Exception e) {
        // ERROR!
        e.printStackTrace();
        return false;
      }
    } else {
      return false;
    }
  }
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // robustness code
    if (mCallback == null
        || (mCameraCaptureURI == null && requestCode == REQUEST_LAUNCH_CAMERA)
        || (requestCode != REQUEST_LAUNCH_CAMERA
            && requestCode != REQUEST_LAUNCH_IMAGE_LIBRARY
            && requestCode != REQUEST_IMAGE_CROPPING)) {
      return;
    }

    // user cancel
    if (resultCode != Activity.RESULT_OK) {
      response.putBoolean("didCancel", true);
      mCallback.invoke(response);
      return;
    }

    Uri uri;
    switch (requestCode) {
      case REQUEST_LAUNCH_CAMERA:
        uri = mCameraCaptureURI;
        break;
      case REQUEST_IMAGE_CROPPING:
        uri = mCropImagedUri;
        break;
      default:
        uri = data.getData();
    }

    if (requestCode != REQUEST_IMAGE_CROPPING && allowEditing == true) {
      Intent cropIntent = new Intent("com.android.camera.action.CROP");
      cropIntent.setDataAndType(uri, "image/*");
      cropIntent.putExtra("crop", "true");

      if (aspectX > 0 && aspectY > 0) {
        // aspectX:aspectY, the ratio of width to height
        cropIntent.putExtra("aspectX", aspectX);
        cropIntent.putExtra("aspectY", aspectY);
        cropIntent.putExtra("scale", true);
      }

      // we create a file to save the result
      File imageFile = createNewFile();
      mCropImagedUri = Uri.fromFile(imageFile);
      cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri);

      try {
        mMainActivity.startActivityForResult(cropIntent, REQUEST_IMAGE_CROPPING);
      } catch (ActivityNotFoundException e) {
        e.printStackTrace();
      }
      return;
    }

    String realPath = getRealPathFromURI(uri);
    boolean isUrl = false;

    if (realPath != null) {
      try {
        URL url = new URL(realPath);
        isUrl = true;
      } catch (MalformedURLException e) {
        // not a url
      }
    }

    if (realPath == null || isUrl) {
      try {
        File file = createFileFromURI(uri);
        realPath = file.getAbsolutePath();
        uri = Uri.fromFile(file);
      } catch (Exception e) {
        response.putString("error", "Could not read photo");
        response.putString("uri", uri.toString());
        mCallback.invoke(response);
        return;
      }
    }

    ExifInterface exif = null;
    int CurrentAngle = 0;
    try {
      exif = new ExifInterface(realPath);
      int orientation =
          exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
      boolean isVertical = true;
      switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
          isVertical = false;
          CurrentAngle = 270;
          break;
        case ExifInterface.ORIENTATION_ROTATE_90:
          isVertical = false;
          CurrentAngle = 90;
          break;
        case ExifInterface.ORIENTATION_ROTATE_180:
          CurrentAngle = 180;
          break;
      }
      response.putBoolean("isVertical", isVertical);
    } catch (IOException e) {
      e.printStackTrace();
      response.putString("error", e.getMessage());
      mCallback.invoke(response);
      return;
    }

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap photo = BitmapFactory.decodeFile(realPath, options);
    int initialWidth = options.outWidth;
    int initialHeight = options.outHeight;

    // don't create a new file if contraint are respected
    if (((initialWidth < maxWidth && maxWidth > 0) || maxWidth == 0)
        && ((initialHeight < maxHeight && maxHeight > 0) || maxHeight == 0)
        && quality == 100
        && (!forceAngle || (forceAngle && CurrentAngle == angle))) {
      response.putInt("width", initialWidth);
      response.putInt("height", initialHeight);
    } else {
      File resized = getResizedImage(getRealPathFromURI(uri), initialWidth, initialHeight);
      realPath = resized.getAbsolutePath();
      uri = Uri.fromFile(resized);
      photo = BitmapFactory.decodeFile(realPath, options);
      response.putInt("width", options.outWidth);
      response.putInt("height", options.outHeight);
    }

    response.putString("uri", uri.toString());
    response.putString("path", realPath);

    if (!noData) {
      response.putString("data", getBase64StringFromFile(realPath));
    }

    if (assetProperties) {
      WritableNativeMap assetPropertiesMap = new WritableNativeMap();

      if (options.outMimeType != null) {
        assetPropertiesMap.putString("mimeType", options.outMimeType);
      }

      String[] splitPath = realPath.split("/");
      if (splitPath.length > 0) {
        assetPropertiesMap.putString("fileName", splitPath[splitPath.length - 1]);
      }

      File file = new File(realPath);
      if (file != null) {
        assetPropertiesMap.putDouble("fileSize", file.length());
      }

      response.putMap("assetProperties", assetPropertiesMap);
      mCallback.invoke(response);
    } else {
      mCallback.invoke(response);
    }
  }
 @ReactMethod
 public void getScriptText(final Promise promise) {
   WritableMap map = new WritableNativeMap();
   map.putString("fullSourceMappingURL", mSourceMapUrl);
   promise.resolve(map);
 }
  // NOTE: Currently not reentrant / doesn't support concurrent requests
  @ReactMethod
  public void launchCamera(final ReadableMap options, final Callback callback) {
    response = Arguments.createMap();

    if (options.hasKey("noData")) {
      noData = options.getBoolean("noData");
    }
    if (options.hasKey("maxWidth")) {
      maxWidth = options.getInt("maxWidth");
    }
    if (options.hasKey("maxHeight")) {
      maxHeight = options.getInt("maxHeight");
    }
    if (options.hasKey("aspectX")) {
      aspectX = options.getInt("aspectX");
    }
    if (options.hasKey("aspectY")) {
      aspectY = options.getInt("aspectY");
    }
    if (options.hasKey("quality")) {
      quality = (int) (options.getDouble("quality") * 100);
    }
    tmpImage = true;
    if (options.hasKey("storageOptions")) {
      tmpImage = false;
    }
    if (options.hasKey("allowsEditing")) {
      allowEditing = options.getBoolean("allowsEditing");
    }
    forceAngle = false;
    if (options.hasKey("angle")) {
      forceAngle = true;
      angle = options.getInt("angle");
    }
    if (options.hasKey("assetProperties")) {
      assetProperties = options.getBoolean("assetProperties");
    }

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (cameraIntent.resolveActivity(mMainActivity.getPackageManager()) == null) {
      response.putString("error", "Cannot launch camera");
      callback.invoke(response);
      return;
    }

    // we create a tmp file to save the result
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile;
    try {
      // Make sure the Pictures directory exists.
      path.mkdirs();
      imageFile = File.createTempFile("capture", ".jpg", path);
    } catch (IOException e) {
      e.printStackTrace();
      response.putString("error", e.getMessage());
      callback.invoke(response);
      return;
    }
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
    mCameraCaptureURI = Uri.fromFile(imageFile);
    mCallback = callback;

    try {
      mMainActivity.startActivityForResult(cameraIntent, REQUEST_LAUNCH_CAMERA);
    } catch (ActivityNotFoundException e) {
      e.printStackTrace();
    }
  }
 private WritableMap serializeEventData() {
   WritableMap eventData = Arguments.createMap();
   eventData.putInt("selected", getPosition());
   eventData.putString("value", getValue());
   return eventData;
 }
 private WritableMap serializeEventData() {
   WritableMap eventData = Arguments.createMap();
   eventData.putInt("target", getViewTag());
   eventData.putBoolean("value", getIsChecked());
   return eventData;
 }
 public void onReceiveNativeEvent(String value) {
   WritableMap event = Arguments.createMap();
   event.putString("message", value);
   ReactContext reactContext = (ReactContext) getContext();
   reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "topChange", event);
 }
 @ReactProp(name = PROP_ZOOM_LEVEL, defaultInt = 10)
 public void setPropZoomLevel(MapView view, int zoomLevel) {
   WritableMap properties = getProperties();
   properties.putInt(PROP_ZOOM_LEVEL, zoomLevel);
   updateCenter();
 }