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

The following are Jave code examples for showing how to use assertOnUiThread() of the com.facebook.react.bridge.UiThreadUtil class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 6 votes
/**
 * Releases all references to given native View.
 */
protected void dropView(View view) {
  UiThreadUtil.assertOnUiThread();
  if (!mRootTags.get(view.getId())) {
    // For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
    resolveViewManager(view.getId()).onDropViewInstance(view);
  }
  ViewManager viewManager = mTagsToViewManagers.get(view.getId());
  if (view instanceof ViewGroup && viewManager instanceof ViewGroupManager) {
    ViewGroup viewGroup = (ViewGroup) view;
    ViewGroupManager viewGroupManager = (ViewGroupManager) viewManager;
    for (int i = viewGroupManager.getChildCount(viewGroup) - 1; i >= 0; i--) {
      View child = viewGroupManager.getChildAt(viewGroup, i);
      if (mTagsToViews.get(child.getId()) != null) {
        dropView(child);
      }
    }
    viewGroupManager.removeAllViews(viewGroup);
  }
  mTagsToViews.remove(view.getId());
  mTagsToViewManagers.remove(view.getId());
}
 
Example 2
Project: RNLearn_Project1   File: HeadlessJsTaskContext.java   Source Code and License Vote up 6 votes
/**
 * Start a JS task. Handles invoking {@link AppRegistry#startHeadlessTask} and notifying
 * listeners.
 *
 * @return a unique id representing this task instance.
 */
public synchronized int startTask(final HeadlessJsTaskConfig taskConfig) {
  UiThreadUtil.assertOnUiThread();
  ReactContext reactContext = Assertions.assertNotNull(
    mReactContext.get(),
    "Tried to start a task on a react context that has already been destroyed");
  if (reactContext.getLifecycleState() == LifecycleState.RESUMED &&
    !taskConfig.isAllowedInForeground()) {
    throw new IllegalStateException(
      "Tried to start task " + taskConfig.getTaskKey() +
        " while in foreground, but this is not allowed.");
  }
  final int taskId = mLastTaskId.incrementAndGet();
  mActiveTasks.add(taskId);
  reactContext.getJSModule(AppRegistry.class)
    .startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
  if (taskConfig.getTimeout() > 0) {
    scheduleTaskTimeout(taskId, taskConfig.getTimeout());
  }
  for (HeadlessJsTaskEventListener listener : mHeadlessJsTaskEventListeners) {
    listener.onHeadlessJsTaskStart(taskId);
  }
  return taskId;
}
 
Example 3
Project: RNLearn_Project1   File: DevSupportManagerImpl.java   Source Code and License Vote up 6 votes
@Override
public void handleReloadJS() {
  UiThreadUtil.assertOnUiThread();

  // dismiss redbox if exists
  if (mRedBoxDialog != null) {
    mRedBoxDialog.dismiss();
  }

  if (mDevSettings.isRemoteJSDebugEnabled()) {
    mDevLoadingViewController.showForRemoteJSEnabled();
    mDevLoadingViewVisible = true;
    reloadJSInProxyMode();
  } else {
    String bundleURL =
      mDevServerHelper.getDevServerBundleURL(Assertions.assertNotNull(mJSAppBundleName));
    reloadJSFromServer(bundleURL);
  }
}
 
Example 4
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 6 votes
/**
 * Returns true on success, false on failure. If successful, after calling, output buffer will be
 * {x, y, width, height}.
 */
public void measure(int tag, int[] outputBuffer) {
  UiThreadUtil.assertOnUiThread();
  View v = mTagsToViews.get(tag);
  if (v == null) {
    throw new NoSuchNativeViewException("No native view for " + tag + " currently exists");
  }

  View rootView = (View) RootViewUtil.getRootView(v);
  // It is possible that the RootView can't be found because this view is no longer on the screen
  // and has been removed by clipping
  if (rootView == null) {
    throw new NoSuchNativeViewException("Native view " + tag + " is no longer on screen");
  }
  rootView.getLocationInWindow(outputBuffer);
  int rootX = outputBuffer[0];
  int rootY = outputBuffer[1];

  v.getLocationInWindow(outputBuffer);

  outputBuffer[0] = outputBuffer[0] - rootX;
  outputBuffer[1] = outputBuffer[1] - rootY;
  outputBuffer[2] = v.getWidth();
  outputBuffer[3] = v.getHeight();
}
 
Example 5
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 6 votes
/**
 * 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 void showPopupMenu(int reactTag, ReadableArray items, Callback success) {
  UiThreadUtil.assertOnUiThread();
  View anchor = mTagsToViews.get(reactTag);
  if (anchor == null) {
    throw new JSApplicationIllegalArgumentException("Could not find view with tag " + reactTag);
  }
  PopupMenu popupMenu = new PopupMenu(getReactContextForView(reactTag), anchor);

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

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

  popupMenu.show();
}
 
Example 6
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 6 votes
/**
 * 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 void showPopupMenu(int reactTag, ReadableArray items, Callback success) {
  UiThreadUtil.assertOnUiThread();
  View anchor = mTagsToViews.get(reactTag);
  if (anchor == null) {
    throw new JSApplicationIllegalArgumentException("Could not find view with tag " + reactTag);
  }
  PopupMenu popupMenu = new PopupMenu(getReactContextForView(reactTag), anchor);

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

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

  popupMenu.show();
}
 
Example 7
Project: RNLearn_Project1   File: ReactChoreographer.java   Source Code and License Vote up 5 votes
public void postFrameCallback(CallbackType type, ChoreographerCompat.FrameCallback frameCallback) {
  UiThreadUtil.assertOnUiThread();
  mCallbackQueues[type.getOrder()].addLast(frameCallback);
  mTotalCallbacks++;
  Assertions.assertCondition(mTotalCallbacks > 0);
  if (!mHasPostedCallback) {
    mChoreographer.postFrameCallback(mReactChoreographerDispatcher);
    mHasPostedCallback = true;
  }
}
 
Example 8
Project: RNLearn_Project1   File: EventDispatcher.java   Source Code and License Vote up 5 votes
@Override
public void onHostResume() {
  UiThreadUtil.assertOnUiThread();
  if (mRCTEventEmitter == null) {
    mRCTEventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
  }
  mCurrentFrameCallback.maybePost();
}
 
Example 9
Project: RNLearn_Project1   File: ReactChoreographer.java   Source Code and License Vote up 5 votes
public static ReactChoreographer getInstance() {
  UiThreadUtil.assertOnUiThread();
  if (sInstance == null) {
    sInstance = new ReactChoreographer();
  }
  return sInstance;
}
 
Example 10
Project: RNLearn_Project1   File: ReactChoreographer.java   Source Code and License Vote up 5 votes
public void postFrameCallback(CallbackType type, ChoreographerCompat.FrameCallback frameCallback) {
  UiThreadUtil.assertOnUiThread();
  mCallbackQueues[type.getOrder()].addLast(frameCallback);
  mTotalCallbacks++;
  Assertions.assertCondition(mTotalCallbacks > 0);
  if (!mHasPostedCallback) {
    mChoreographer.postFrameCallback(mReactChoreographerDispatcher);
    mHasPostedCallback = true;
  }
}
 
Example 11
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public void updateViewExtraData(int tag, Object extraData) {
  UiThreadUtil.assertOnUiThread();

  ViewManager viewManager = resolveViewManager(tag);
  View viewToUpdate = resolveView(tag);
  viewManager.updateExtraData(viewToUpdate, extraData);
}
 
Example 12
Project: RNLearn_Project1   File: DeviceEventManagerModule.java   Source Code and License Vote up 5 votes
public DeviceEventManagerModule(
    ReactApplicationContext reactContext,
    final DefaultHardwareBackBtnHandler backBtnHandler) {
  super(reactContext);
  mInvokeDefaultBackPressRunnable = new Runnable() {
    @Override
    public void run() {
      UiThreadUtil.assertOnUiThread();
      backBtnHandler.invokeDefaultOnBackPressed();
    }
  };
}
 
Example 13
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public void removeRootView(int rootViewTag) {
  UiThreadUtil.assertOnUiThread();
  if (!mRootTags.get(rootViewTag)) {
      SoftAssertions.assertUnreachable(
          "View with tag " + rootViewTag + " is not registered as a root view");
  }
  View rootView = mTagsToViews.get(rootViewTag);
  dropView(rootView);
  mRootTags.delete(rootViewTag);
}
 
Example 14
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public void updateProperties(int tag, ReactStylesDiffMap props) {
  UiThreadUtil.assertOnUiThread();

  try {
    ViewManager viewManager = resolveViewManager(tag);
    View viewToUpdate = resolveView(tag);
    viewManager.updateProperties(viewToUpdate, props);
  } catch (IllegalViewOperationException e) {
    Log.e(TAG, "Unable to update properties for view tag " + tag, e);
  }
}
 
Example 15
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public void createView(
    ThemedReactContext themedContext,
    int tag,
    String className,
    @Nullable ReactStylesDiffMap initialProps) {
  UiThreadUtil.assertOnUiThread();
  SystraceMessage.beginSection(
      Systrace.TRACE_TAG_REACT_VIEW,
      "NativeViewHierarchyManager_createView")
      .arg("tag", tag)
      .arg("className", className)
      .flush();
  try {
    ViewManager viewManager = mViewManagers.get(className);

    View view = viewManager.createView(themedContext, mJSResponderHandler);
    mTagsToViews.put(tag, view);
    mTagsToViewManagers.put(tag, viewManager);

    // Use android View id field to store React tag. This is possible since we don't inflate
    // React views from layout xmls. Thus it is easier to just reuse that field instead of
    // creating another (potentially much more expensive) mapping from view to React tag
    view.setId(tag);
    if (initialProps != null) {
      viewManager.updateProperties(view, initialProps);
    }
  } finally {
    Systrace.endSection(Systrace.TRACE_TAG_REACT_VIEW);
  }
}
 
Example 16
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public void removeRootView(int rootViewTag) {
  UiThreadUtil.assertOnUiThread();
  if (!mRootTags.get(rootViewTag)) {
      SoftAssertions.assertUnreachable(
          "View with tag " + rootViewTag + " is not registered as a root view");
  }
  View rootView = mTagsToViews.get(rootViewTag);
  dropView(rootView);
  mRootTags.delete(rootViewTag);
}
 
Example 17
Project: RNLearn_Project1   File: ReactChoreographer.java   Source Code and License Vote up 5 votes
public void removeFrameCallback(CallbackType type, ChoreographerCompat.FrameCallback frameCallback) {
  UiThreadUtil.assertOnUiThread();
  if (mCallbackQueues[type.getOrder()].removeFirstOccurrence(frameCallback)) {
    mTotalCallbacks--;
    maybeRemoveFrameCallback();
  } else {
    FLog.e(ReactConstants.TAG, "Tried to remove non-existent frame callback");
  }
}
 
Example 18
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 5 votes
public 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 19
Project: RNLearn_Project1   File: NativeViewHierarchyManager.java   Source Code and License Vote up 4 votes
public void updateLayout(
    int parentTag,
    int tag,
    int x,
    int y,
    int width,
    int height) {
  UiThreadUtil.assertOnUiThread();
  SystraceMessage.beginSection(
      Systrace.TRACE_TAG_REACT_VIEW,
      "NativeViewHierarchyManager_updateLayout")
      .arg("parentTag", parentTag)
      .arg("tag", tag)
      .flush();
  try {
    View viewToUpdate = resolveView(tag);

    // Even though we have exact dimensions, we still call measure because some platform views (e.g.
    // Switch) assume that method will always be called before onLayout and onDraw. They use it to
    // calculate and cache information used in the draw pass. For most views, onMeasure can be
    // stubbed out to only call setMeasuredDimensions. For ViewGroups, onLayout should be stubbed
    // out to not recursively call layout on its children: React Native already handles doing that.
    //
    // Also, note measure and layout need to be called *after* all View properties have been updated
    // because of caching and calculation that may occur in onMeasure and onLayout. Layout
    // operations should also follow the native view hierarchy and go top to bottom for consistency
    // with standard layout passes (some views may depend on this).

    viewToUpdate.measure(
        View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
        View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));

    // Check if the parent of the view has to layout the view, or the child has to lay itself out.
    if (!mRootTags.get(parentTag)) {
      ViewManager parentViewManager = mTagsToViewManagers.get(parentTag);
      ViewGroupManager parentViewGroupManager;
      if (parentViewManager instanceof ViewGroupManager) {
        parentViewGroupManager = (ViewGroupManager) parentViewManager;
      } else {
        throw new IllegalViewOperationException(
            "Trying to use view with tag " + tag +
                " as a parent, but its Manager doesn't extends ViewGroupManager");
      }
      if (parentViewGroupManager != null
          && !parentViewGroupManager.needsCustomLayoutForChildren()) {
        updateLayout(viewToUpdate, x, y, width, height);
      }
    } else {
      updateLayout(viewToUpdate, x, y, width, height);
    }
  } finally {
    Systrace.endSection(Systrace.TRACE_TAG_REACT_VIEW);
  }
}
 
Example 20
Project: RNLearn_Project1   File: AnimationRegistry.java   Source Code and License Vote up 4 votes
public Animation getAnimation(int animationID) {
  UiThreadUtil.assertOnUiThread();
  return mRegistry.get(animationID);
}