Java Code Examples for com.facebook.react.bridge.UiThreadUtil#assertOnUiThread()

The following examples show how to use com.facebook.react.bridge.UiThreadUtil#assertOnUiThread() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: react-native-GPay   File: EventDispatcher.java    License: MIT License 6 votes vote down vote up
@Override
public void doFrame(long frameTimeNanos) {
  UiThreadUtil.assertOnUiThread();

  if (mShouldStop) {
    mIsPosted = false;
  } else {
    post();
  }

  Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ScheduleDispatchFrameCallback");
  try {
    moveStagedEventsToDispatchQueue();

    if (!mHasDispatchScheduled) {
      mHasDispatchScheduled = true;
      Systrace.startAsyncFlow(
          Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
          "ScheduleDispatchFrameCallback",
          mHasDispatchScheduledCount.get());
      mReactContext.runOnJSQueueThread(mDispatchEventsRunnable);
    }
  } finally {
    Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
  }
}
 
Example 2
/**
 * Returns the coordinates of a view relative to the window (not just the RootView
 * which is what measure will return)
 *
 * @param tag - the tag for the view
 * @param outputBuffer - output buffer that contains [x,y,width,height] of the view in coordinates
 *  relative to the device window
 */
public synchronized void measureInWindow(int tag, int[] outputBuffer) {
  UiThreadUtil.assertOnUiThread();
  View v = mTagsToViews.get(tag);
  if (v == null) {
    throw new NoSuchNativeViewException("No native view for " + tag + " currently exists");
  }

  v.getLocationOnScreen(outputBuffer);

  // We need to remove the status bar from the height.  getLocationOnScreen will include the
  // status bar.
  Resources resources = v.getContext().getResources();
  int statusBarId = resources.getIdentifier("status_bar_height", "dimen", "android");
  if (statusBarId > 0) {
    int height = (int) resources.getDimension(statusBarId);
    outputBuffer[1] -= height;
  }

  // outputBuffer[0,1] already contain what we want
  outputBuffer[2] = v.getWidth();
  outputBuffer[3] = v.getHeight();
}
 
Example 3
/**
 * Show a {@link PopupMenu}.
 *
 * @param reactTag the tag of the anchor view (the PopupMenu is displayed next to this view); this
 *        needs to be the tag of a native view (shadow views can not be anchors)
 * @param items the menu items as an array of strings
 * @param success will be called with the position of the selected item as the first argument, or
 *        no arguments if the menu is dismissed
 */
public synchronized void showPopupMenu(int reactTag, ReadableArray items, Callback success,
                                       Callback error) {
  UiThreadUtil.assertOnUiThread();
  View anchor = mTagsToViews.get(reactTag);
  if (anchor == null) {
    error.invoke("Can't display popup. Could not find view with tag " + reactTag);
    return;
  }
  mPopupMenu = new PopupMenu(getReactContextForView(reactTag), anchor);

  Menu menu = mPopupMenu.getMenu();
  for (int i = 0; i < items.size(); i++) {
    menu.add(Menu.NONE, Menu.NONE, i, items.getString(i));
  }

  PopupMenuCallbackHandler handler = new PopupMenuCallbackHandler(success);
  mPopupMenu.setOnMenuItemClickListener(handler);
  mPopupMenu.setOnDismissListener(handler);

  mPopupMenu.show();
}
 
Example 4
Source Project: react-native-GPay   File: ReactInstanceManager.java    License: MIT License 6 votes vote down vote up
/**
 * This method will give JS the opportunity to receive intents via Linking.
 */
@ThreadConfined(UI)
public void onNewIntent(Intent intent) {
  UiThreadUtil.assertOnUiThread();
  ReactContext currentContext = getCurrentReactContext();
  if (currentContext == null) {
    FLog.w(ReactConstants.TAG, "Instance detached from instance manager");
  } else {
    String action = intent.getAction();
    Uri uri = intent.getData();

    if (Intent.ACTION_VIEW.equals(action) && uri != null) {
      DeviceEventManagerModule deviceEventManagerModule =
        currentContext.getNativeModule(DeviceEventManagerModule.class);
      deviceEventManagerModule.emitNewIntentReceived(uri);
    }

    currentContext.onNewIntent(mCurrentActivity, intent);
  }
}
 
Example 5
Source Project: react-native-GPay   File: ReactInstanceManager.java    License: MIT License 6 votes vote down vote up
private void tearDownReactContext(ReactContext reactContext) {
  Log.d(ReactConstants.TAG, "ReactInstanceManager.tearDownReactContext()");
  UiThreadUtil.assertOnUiThread();
  if (mLifecycleState == LifecycleState.RESUMED) {
    reactContext.onHostPause();
  }

  synchronized (mAttachedRootViews) {
    for (ReactRootView rootView : mAttachedRootViews) {
      rootView.removeAllViews();
      rootView.setId(View.NO_ID);
    }
  }

  reactContext.destroy();
  mDevSupportManager.onReactInstanceDestroyed(reactContext);
  mMemoryPressureRouter.removeMemoryPressureListener(reactContext.getCatalystInstance());
}
 
Example 6
/**
 * Simplified version of manageChildren that only deals with adding children views
 */
public synchronized void setChildren(
  int tag,
  ReadableArray childrenTags) {
  UiThreadUtil.assertOnUiThread();
  ViewGroup viewToManage = (ViewGroup) mTagsToViews.get(tag);
  ViewGroupManager viewManager = (ViewGroupManager) resolveViewManager(tag);

  for (int i = 0; i < childrenTags.size(); i++) {
    View viewToAdd = mTagsToViews.get(childrenTags.getInt(i));
    if (viewToAdd == null) {
      throw new IllegalViewOperationException(
        "Trying to add unknown view tag: "
          + childrenTags.getInt(i) + "\n detail: " +
          constructSetChildrenErrorMessage(
            viewToManage,
            viewManager,
            childrenTags));
    }
    viewManager.addView(viewToManage, viewToAdd, i);
  }
}
 
Example 7
public synchronized void updateViewExtraData(int tag, Object extraData) {
  UiThreadUtil.assertOnUiThread();

  ViewManager viewManager = resolveViewManager(tag);
  View viewToUpdate = resolveView(tag);
  viewManager.updateExtraData(viewToUpdate, extraData);
}
 
Example 8
public synchronized void updateInstanceHandle(int tag, long instanceHandle) {
  UiThreadUtil.assertOnUiThread();

  try {
    updateInstanceHandle(resolveView(tag), instanceHandle);
  } catch (IllegalViewOperationException e) {
    FLog.e(TAG, "Unable to update properties for view tag " + tag, e);
  }
}
 
Example 9
public DeviceEventManagerModule(
    ReactApplicationContext reactContext,
    final DefaultHardwareBackBtnHandler backBtnHandler) {
  super(reactContext);
  mInvokeDefaultBackPressRunnable = new Runnable() {
    @Override
    public void run() {
      UiThreadUtil.assertOnUiThread();
      backBtnHandler.invokeDefaultOnBackPressed();
    }
  };
}
 
Example 10
public static int findTargetTagAndCoordinatesForTouch(float eventX, float eventY, ViewGroup viewGroup, float[] viewCoords) {
    UiThreadUtil.assertOnUiThread();
    int targetTag = viewGroup.getId();
    viewCoords[0] = eventX;
    viewCoords[1] = eventY;
    View nativeTargetView = findTouchTargetView(viewCoords, viewGroup);
    if (nativeTargetView == null) {
        return targetTag;
    }
    View reactTargetView = findClosestReactAncestor(nativeTargetView);
    if (reactTargetView != null) {
        return getTouchTargetForView(reactTargetView, viewCoords[0], viewCoords[1]);
    }
    return targetTag;
}
 
Example 11
public synchronized void dispatchCommand(
  int reactTag,
  int commandId,
  @Nullable ReadableArray args) {
  UiThreadUtil.assertOnUiThread();
  View view = mTagsToViews.get(reactTag);
  if (view == null) {
    throw new IllegalViewOperationException("Trying to send command to a non-existing view " +
        "with tag " + reactTag);
  }

  ViewManager viewManager = resolveViewManager(reactTag);
  viewManager.receiveCommand(view, commandId, args);
}
 
Example 12
Source Project: react-native-GPay   File: ReactInstanceManager.java    License: MIT License 5 votes vote down vote up
/**
 * Destroy this React instance and the attached JS context.
 */
@ThreadConfined(UI)
public void destroy() {
  UiThreadUtil.assertOnUiThread();
  PrinterHolder.getPrinter().logMessage(ReactDebugOverlayTags.RN_CORE, "RNCore: Destroy");

  mHasStartedDestroying = true;

  if (mUseDeveloperSupport) {
    mDevSupportManager.setDevSupportEnabled(false);
    mDevSupportManager.stopInspector();
  }

  moveToBeforeCreateLifecycleState();

  if (mCreateReactContextThread != null) {
    mCreateReactContextThread = null;
  }

  mMemoryPressureRouter.destroy(mApplicationContext);

  synchronized (mReactContextLock) {
    if (mCurrentReactContext != null) {
      mCurrentReactContext.destroy();
      mCurrentReactContext = null;
    }
  }
  mHasStartedCreatingInitialContext = false;
  mCurrentActivity = null;

  ResourceDrawableIdHelper.getInstance().clear();
  mHasStartedDestroying = false;
  synchronized (mHasStartedDestroying) {
    mHasStartedDestroying.notifyAll();
  }
}
 
Example 13
Source Project: react-native-GPay   File: ReactInstanceManager.java    License: MIT License 5 votes vote down vote up
/**
 * Detach given {@param rootView} from current catalyst instance. It's safe to call this method
 * multiple times on the same {@param rootView} - in that case view will be detached with the
 * first call.
 */
@ThreadConfined(UI)
public void detachRootView(ReactRootView rootView) {
  UiThreadUtil.assertOnUiThread();
  if (mAttachedRootViews.remove(rootView)) {
    ReactContext currentContext = getCurrentReactContext();
    if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
      detachViewFromInstance(rootView, currentContext.getCatalystInstance());
    }
  }
}
 
Example 14
public synchronized int findTargetTagForTouch(int reactTag, float touchX, float touchY) {
  UiThreadUtil.assertOnUiThread();
  View view = mTagsToViews.get(reactTag);
  if (view == null) {
    throw new JSApplicationIllegalArgumentException("Could not find view with tag " + reactTag);
  }
  return TouchTargetHelper.findTargetTagForTouch(touchX, touchY, (ViewGroup) view);
}
 
Example 15
public synchronized void updateProperties(int tag, ReactStylesDiffMap props) {
  UiThreadUtil.assertOnUiThread();

  try {
    ViewManager viewManager = resolveViewManager(tag);
    View viewToUpdate = resolveView(tag);

    if (props != null) {
      viewManager.updateProperties(viewToUpdate, props);
    }
  } catch (IllegalViewOperationException e) {
    FLog.e(TAG, "Unable to update properties for view tag " + tag, e);
  }
}
 
Example 16
Source Project: react-native-GPay   File: ReactChoreographer.java    License: MIT License 4 votes vote down vote up
public static void initialize() {
  if (sInstance == null) {
    UiThreadUtil.assertOnUiThread();
    sInstance = new ReactChoreographer();
  }
}
 
Example 17
private void reload() {
  UiThreadUtil.assertOnUiThread();

  // reload settings, show/hide debug overlay if required & start/stop shake detector
  if (mIsDevSupportEnabled) {
    // update visibility of FPS debug overlay depending on the settings
    if (mDebugOverlayController != null) {
      mDebugOverlayController.setFpsDebugViewVisible(mDevSettings.isFpsDebugEnabled());
    }

    // start shake gesture detector
    if (!mIsShakeDetectorStarted) {
      mShakeDetector.start(
          (SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE));
      mIsShakeDetectorStarted = true;
    }

    // register reload app broadcast receiver
    if (!mIsReceiverRegistered) {
      IntentFilter filter = new IntentFilter();
      filter.addAction(getReloadAppAction(mApplicationContext));
      mApplicationContext.registerReceiver(mReloadAppBroadcastReceiver, filter);
      mIsReceiverRegistered = true;
    }

    // show the dev loading if it should be
    if (mDevLoadingViewVisible) {
      mDevLoadingViewController.showMessage("Reloading...");
    }

    mDevServerHelper.openPackagerConnection(this.getClass().getSimpleName(), this);
    if (mDevSettings.isReloadOnJSChangeEnabled()) {
      mDevServerHelper.startPollingOnChangeEndpoint(
          new DevServerHelper.OnServerContentChangeListener() {
        @Override
        public void onServerContentChanged() {
          handleReloadJS();
        }
      });
    } else {
      mDevServerHelper.stopPollingOnChangeEndpoint();
    }
  } else {
    // hide FPS debug overlay
    if (mDebugOverlayController != null) {
      mDebugOverlayController.setFpsDebugViewVisible(false);
    }

    // stop shake gesture detector
    if (mIsShakeDetectorStarted) {
      mShakeDetector.stop();
      mIsShakeDetectorStarted = false;
    }

    // unregister app reload broadcast receiver
    if (mIsReceiverRegistered) {
      mApplicationContext.unregisterReceiver(mReloadAppBroadcastReceiver);
      mIsReceiverRegistered = false;
    }

    // hide redbox dialog
    hideRedboxDialog();

    // hide dev options dialog
    hideDevOptionsDialog();

    // hide loading view
    mDevLoadingViewController.hide();
    mDevServerHelper.closePackagerConnection();
    mDevServerHelper.stopPollingOnChangeEndpoint();
  }
}
 
Example 18
Source Project: react-native-GPay   File: ReactInstanceManager.java    License: MIT License 4 votes vote down vote up
@ThreadConfined(UI)
public void showDevOptionsDialog() {
  UiThreadUtil.assertOnUiThread();
  mDevSupportManager.showDevOptionsDialog();
}
 
Example 19
Source Project: react-native-GPay   File: EventDispatcher.java    License: MIT License 4 votes vote down vote up
private void stopFrameCallback() {
  UiThreadUtil.assertOnUiThread();
  mCurrentFrameCallback.stop();
}
 
Example 20
Source Project: react-native-GPay   File: UIImplementation.java    License: MIT License 2 votes vote down vote up
/**
 * Used by native animated module to bypass the process of updating the values through the shadow
 * view hierarchy. This method will directly update native views, which means that updates for
 * layout-related propertied won't be handled properly.
 * Make sure you know what you're doing before calling this method :)
 */
public void synchronouslyUpdateViewOnUIThread(int tag, ReactStylesDiffMap props) {
  UiThreadUtil.assertOnUiThread();
  mOperationsQueue.getNativeViewHierarchyManager().updateProperties(tag, props);
}