-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
DeviceInfoModule.java
110 lines (94 loc) · 3.63 KB
/
DeviceInfoModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* 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.modules.deviceinfo;
import android.content.Context;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactNoCrashSoftException;
import com.facebook.react.bridge.ReactSoftException;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import java.util.HashMap;
import java.util.Map;
/** Module that exposes Android Constants to JS. */
@ReactModule(name = DeviceInfoModule.NAME)
public class DeviceInfoModule extends ReactContextBaseJavaModule
implements LifecycleEventListener, TurboModule {
public static final String NAME = "DeviceInfo";
private @Nullable ReactApplicationContext mReactApplicationContext;
private float mFontScale;
private @Nullable ReadableMap mPreviousDisplayMetrics;
public DeviceInfoModule(ReactApplicationContext reactContext) {
super(reactContext);
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext);
mFontScale = reactContext.getResources().getConfiguration().fontScale;
mReactApplicationContext = reactContext;
mReactApplicationContext.addLifecycleEventListener(this);
}
public DeviceInfoModule(Context context) {
super(null);
mReactApplicationContext = null;
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context);
mFontScale = context.getResources().getConfiguration().fontScale;
}
@Override
public String getName() {
return NAME;
}
@Override
public @Nullable Map<String, Object> getConstants() {
HashMap<String, Object> constants = new HashMap<>();
constants.put("Dimensions", DisplayMetricsHolder.getDisplayMetricsMap(mFontScale));
return constants;
}
@Override
public void onHostResume() {
if (mReactApplicationContext == null) {
return;
}
float fontScale = mReactApplicationContext.getResources().getConfiguration().fontScale;
if (mFontScale != fontScale) {
mFontScale = fontScale;
emitUpdateDimensionsEvent();
}
}
@Override
public void onHostPause() {}
@Override
public void onHostDestroy() {}
public void emitUpdateDimensionsEvent() {
if (mReactApplicationContext == null) {
return;
}
if (mReactApplicationContext.hasActiveCatalystInstance()) {
// Don't emit an event to JS if the dimensions haven't changed
WritableNativeMap displayMetrics =
DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale);
if (mPreviousDisplayMetrics == null) {
mPreviousDisplayMetrics = displayMetrics.copy();
} else if (!displayMetrics.equals(mPreviousDisplayMetrics)) {
mPreviousDisplayMetrics = displayMetrics.copy();
mReactApplicationContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("didUpdateDimensions", displayMetrics);
}
} else {
ReactSoftException.logSoftException(
NAME,
new ReactNoCrashSoftException(
"No active CatalystInstance, cannot emitUpdateDimensionsEvent"));
}
}
@Override
public void invalidate() {}
}