Move CheckBox Android files to FB internal (#28658)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/28658

This moves the Java files to FB internal and updates all the buck files.

## Changelog:

[Android] [Removed] This diff removes the CheckBox export from React Native. Internally, we are requiring CheckBox directly now and externally people will have to use the community maintained module.

Reviewed By: cpojer

Differential Revision: D21066998

fbshipit-source-id: 76821fcae899ff7342697ea7dd4737ef3b008213
This commit is contained in:
Lauren Tan
2020-04-16 17:23:34 -07:00
committed by Facebook GitHub Bot
parent abde0154ba
commit 8c9c402baf
6 changed files with 0 additions and 256 deletions
@@ -52,7 +52,6 @@ rn_android_library(
react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"),
react_native_target("java/com/facebook/react/uimanager:uimanager"),
react_native_target("java/com/facebook/react/views/art:art"),
react_native_target("java/com/facebook/react/views/checkbox:checkbox"),
react_native_target("java/com/facebook/react/views/drawer:drawer"),
react_native_target("java/com/facebook/react/views/image:image"),
react_native_target("java/com/facebook/react/views/modal:modal"),
@@ -45,7 +45,6 @@ import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.art.ARTRenderableViewManager;
import com.facebook.react.views.art.ARTSurfaceViewManager;
import com.facebook.react.views.checkbox.ReactCheckBoxManager;
import com.facebook.react.views.drawer.ReactDrawerLayoutManager;
import com.facebook.react.views.image.ReactImageManager;
import com.facebook.react.views.modal.ReactModalHostManager;
@@ -178,7 +177,6 @@ public class MainReactPackage extends TurboReactPackage {
viewManagers.add(ARTRenderableViewManager.createARTGroupViewManager());
viewManagers.add(ARTRenderableViewManager.createARTShapeViewManager());
viewManagers.add(ARTRenderableViewManager.createARTTextViewManager());
viewManagers.add(new ReactCheckBoxManager());
viewManagers.add(new ReactDialogPickerManager());
viewManagers.add(new ReactDrawerLayoutManager());
viewManagers.add(new ReactDropdownPickerManager());
@@ -1,25 +0,0 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library")
rn_android_library(
name = "checkbox",
srcs = glob(["*.java"]),
is_androidx = True,
provided_deps = [
react_native_dep("third-party/android/androidx:annotation"),
react_native_dep("third-party/android/androidx:appcompat"),
react_native_dep("third-party/android/androidx:core"),
react_native_dep("third-party/android/androidx:fragment"),
react_native_dep("third-party/android/androidx:legacy-support-core-ui"),
react_native_dep("third-party/android/androidx:legacy-support-core-utils"),
],
visibility = [
"PUBLIC",
],
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/uimanager:uimanager"),
react_native_target("java/com/facebook/react/uimanager/annotations:annotations"),
],
)
@@ -1,38 +0,0 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.checkbox;
import android.content.Context;
import androidx.appcompat.widget.AppCompatCheckBox;
/** CheckBox that has its value controlled by JS. */
/*package*/ class ReactCheckBox extends AppCompatCheckBox {
private boolean mAllowChange;
public ReactCheckBox(Context context) {
super(context);
mAllowChange = true;
}
@Override
public void setChecked(boolean checked) {
if (mAllowChange) {
mAllowChange = false;
super.setChecked(checked);
}
}
/*package*/ void setOn(boolean on) {
// If the checkbox has a different value than the value sent by JS, we must change it.
if (isChecked() != on) {
super.setChecked(on);
}
mAllowChange = true;
}
}
@@ -1,53 +0,0 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.checkbox;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.Event;
import com.facebook.react.uimanager.events.RCTEventEmitter;
/** Event emitted by a ReactCheckBoxManager once a checkbox is manipulated. */
/*package*/ class ReactCheckBoxEvent extends Event<ReactCheckBoxEvent> {
public static final String EVENT_NAME = "topChange";
private final boolean mIsChecked;
public ReactCheckBoxEvent(int viewId, boolean isChecked) {
super(viewId);
mIsChecked = isChecked;
}
public boolean getIsChecked() {
return mIsChecked;
}
@Override
public String getEventName() {
return EVENT_NAME;
}
@Override
public short getCoalescingKey() {
// All checkbox events for a given view can be coalesced.
return 0;
}
@Override
public void dispatch(RCTEventEmitter rctEventEmitter) {
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData());
}
private WritableMap serializeEventData() {
WritableMap eventData = Arguments.createMap();
eventData.putInt("target", getViewTag());
eventData.putBoolean("value", getIsChecked());
return eventData;
}
}
@@ -1,137 +0,0 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.views.checkbox;
import android.content.Context;
import android.content.res.ColorStateList;
import android.util.TypedValue;
import android.widget.CompoundButton;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.TintContextWrapper;
import androidx.core.widget.CompoundButtonCompat;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
/** View manager for {@link ReactCheckBox} components. */
public class ReactCheckBoxManager extends SimpleViewManager<ReactCheckBox> {
public static final String REACT_CLASS = "AndroidCheckBox";
private static final CompoundButton.OnCheckedChangeListener ON_CHECKED_CHANGE_LISTENER =
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
ReactContext reactContext = getReactContext(buttonView);
reactContext
.getNativeModule(UIManagerModule.class)
.getEventDispatcher()
.dispatchEvent(new ReactCheckBoxEvent(buttonView.getId(), isChecked));
}
private ReactContext getReactContext(CompoundButton buttonView) {
ReactContext reactContext;
Context ctx = buttonView.getContext();
if (ctx instanceof TintContextWrapper) {
reactContext = (ReactContext) ((TintContextWrapper) ctx).getBaseContext();
} else {
reactContext = (ReactContext) buttonView.getContext();
}
return reactContext;
}
};
@Override
public String getName() {
return REACT_CLASS;
}
@Override
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactCheckBox view) {
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
}
@Override
protected ReactCheckBox createViewInstance(ThemedReactContext context) {
ReactCheckBox view = new ReactCheckBox(context);
return view;
}
@ReactProp(name = ViewProps.ENABLED, defaultBoolean = true)
public void setEnabled(ReactCheckBox view, boolean enabled) {
view.setEnabled(enabled);
}
@ReactProp(name = ViewProps.ON)
public void setOn(ReactCheckBox view, boolean on) {
// we set the checked change listener to null and then restore it so that we don't fire an
// onChange event to JS when JS itself is updating the value of the checkbox
view.setOnCheckedChangeListener(null);
view.setOn(on);
view.setOnCheckedChangeListener(ON_CHECKED_CHANGE_LISTENER);
}
@Override
public void receiveCommand(
@NonNull ReactCheckBox root, String commandId, @Nullable ReadableArray args) {
switch (commandId) {
case "setNativeValue":
if (args != null) {
setOn(root, args.getBoolean(0));
break;
}
}
}
private static int getThemeColor(final Context context, String colorId) {
final TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(getIdentifier(context, colorId), value, true);
return value.data;
}
/**
* The appcompat-v7 BUCK dep is listed as a provided_dep, which complains that
* com.facebook.react.R doesn't exist. Since the attributes are provided from a parent, we can
* access those attributes dynamically.
*/
private static int getIdentifier(Context context, String name) {
return context.getResources().getIdentifier(name, "attr", context.getPackageName());
}
@ReactProp(name = "tintColors")
public void setTintColors(ReactCheckBox view, @Nullable ReadableMap colorsMap) {
String defaultColorIdOfCheckedState = "colorAccent";
int trueColor =
colorsMap == null || !colorsMap.hasKey("true")
? getThemeColor(view.getContext(), defaultColorIdOfCheckedState)
: colorsMap.getInt("true");
String defaultColorIdOfUncheckedState = "colorPrimaryDark";
int falseColor =
colorsMap == null || !colorsMap.hasKey("false")
? getThemeColor(view.getContext(), defaultColorIdOfUncheckedState)
: colorsMap.getInt("false");
ColorStateList csl =
new ColorStateList(
new int[][] {
new int[] {android.R.attr.state_checked}, new int[] {-android.R.attr.state_checked}
},
new int[] {
trueColor, falseColor,
});
CompoundButtonCompat.setButtonTintList(view, csl);
}
}