Files
react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java
T
Adam Comella 186f308aec BREAKING: Android: Correct value of Dimensions.get('screen').fontScale
Summary:
The PR description has been updated to reflect the new approach.

**Breaking Change Summary**

On Android, the following properties now return a different number:
  - `Dimensions.get('window').fontScale`
  - `Dimensions.get('screen').fontScale`
  - `PixelRatio.getFontScale()`

This is a breaking change to anyone who was using these properties because the meaning of these properties has now changed.

These properties used to return a value representing font scale times density ([`DisplayMetrics.scaledDensity`](https://developer.android.com/reference/android/util/DisplayMetrics.html#scaledDensity)). Now they return a value representing just font scale ([`Configuration.fontScale`](https://developer.android.com/reference/android/content/res/Configuration.html#fontScale)).

**PR Description**

This PR changes a few things:
  - Correctly exposes the font scale to JavaScript as `Dimensions.get('screen').fontScale`. UIManager was exporting `DisplayMetrics.scaledDensity` under the name `fontScale`. How
Closes https://github.com/facebook/react-native/pull/11008

Differential Revision: D4558207

Pulled By: astreet

fbshipit-source-id: 096ce7b28051325dfd45fdb2a14b5e9b7d3bc46f
2017-02-14 08:45:38 -08:00

162 lines
5.9 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.uimanager;
import java.util.HashMap;
import java.util.Map;
import android.util.DisplayMetrics;
import android.view.accessibility.AccessibilityEvent;
import android.widget.ImageView;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.events.TouchEventType;
/**
* Constants exposed to JS from {@link UIManagerModule}.
*/
/* package */ class UIManagerModuleConstants {
public static final String ACTION_DISMISSED = "dismissed";
public static final String ACTION_ITEM_SELECTED = "itemSelected";
/* package */ static Map getBubblingEventTypeConstants() {
return MapBuilder.builder()
.put(
"topChange",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onChange", "captured", "onChangeCapture")))
.put(
"topSelect",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onSelect", "captured", "onSelectCapture")))
.put(
TouchEventType.START.getJSEventName(),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
"bubbled",
"onTouchStart",
"captured",
"onTouchStartCapture")))
.put(
TouchEventType.MOVE.getJSEventName(),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
"bubbled",
"onTouchMove",
"captured",
"onTouchMoveCapture")))
.put(
TouchEventType.END.getJSEventName(),
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of(
"bubbled",
"onTouchEnd",
"captured",
"onTouchEndCapture")))
.build();
}
/* package */ static Map getDirectEventTypeConstants() {
return MapBuilder.builder()
.put("topContentSizeChange", MapBuilder.of("registrationName", "onContentSizeChange"))
.put("topLayout", MapBuilder.of("registrationName", "onLayout"))
.put("topLoadingError", MapBuilder.of("registrationName", "onLoadingError"))
.put("topLoadingFinish", MapBuilder.of("registrationName", "onLoadingFinish"))
.put("topLoadingStart", MapBuilder.of("registrationName", "onLoadingStart"))
.put("topSelectionChange", MapBuilder.of("registrationName", "onSelectionChange"))
.put("topMessage", MapBuilder.of("registrationName", "onMessage"))
.build();
}
public static Map<String, Object> getConstants(float fontScale) {
HashMap<String, Object> constants = new HashMap<String, Object>();
constants.put(
"UIView",
MapBuilder.of(
"ContentMode",
MapBuilder.of(
"ScaleAspectFit",
ImageView.ScaleType.FIT_CENTER.ordinal(),
"ScaleAspectFill",
ImageView.ScaleType.CENTER_CROP.ordinal(),
"ScaleAspectCenter",
ImageView.ScaleType.CENTER_INSIDE.ordinal())));
constants.put(
"Dimensions",
getDimensionsConstants(fontScale));
constants.put(
"StyleConstants",
MapBuilder.of(
"PointerEventsValues",
MapBuilder.of(
"none",
PointerEvents.NONE.ordinal(),
"boxNone",
PointerEvents.BOX_NONE.ordinal(),
"boxOnly",
PointerEvents.BOX_ONLY.ordinal(),
"unspecified",
PointerEvents.AUTO.ordinal())));
constants.put(
"PopupMenu",
MapBuilder.of(
ACTION_DISMISSED,
ACTION_DISMISSED,
ACTION_ITEM_SELECTED,
ACTION_ITEM_SELECTED));
constants.put(
"AccessibilityEventTypes",
MapBuilder.of(
"typeWindowStateChanged",
AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
"typeViewClicked",
AccessibilityEvent.TYPE_VIEW_CLICKED));
return constants;
}
public static WritableMap getDimensionsConstants(float fontScale) {
DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics();
DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
WritableMap windowDisplayMetricsMap = Arguments.createMap();
windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels);
windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels);
windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density);
windowDisplayMetricsMap.putDouble("fontScale", fontScale);
windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi);
WritableMap screenDisplayMetricsMap = Arguments.createMap();
screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels);
screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels);
screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density);
screenDisplayMetricsMap.putDouble("fontScale", fontScale);
screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi);
WritableMap dimensionsMap = Arguments.createMap();
dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap);
dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap);
return dimensionsMap;
}
}