forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlinify DeviceInfoModule (facebook#44536)
Summary: Pull Request resolved: facebook#44536 # Changelog: [Internal] - As in the title. Reviewed By: alanleedev Differential Revision: D57248069 fbshipit-source-id: 4187b620483d439da5fc4c81012c1124fa19fc09
- Loading branch information
Showing
3 changed files
with
87 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 0 additions & 109 deletions
109
...ve/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
...tive/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and 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 com.facebook.fbreact.specs.NativeDeviceInfoSpec | ||
import com.facebook.react.bridge.LifecycleEventListener | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReactNoCrashSoftException | ||
import com.facebook.react.bridge.ReactSoftExceptionLogger | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.module.annotations.ReactModule | ||
import com.facebook.react.uimanager.DisplayMetricsHolder.getDisplayMetricsWritableMap | ||
import com.facebook.react.uimanager.DisplayMetricsHolder.initDisplayMetricsIfNotInitialized | ||
|
||
/** Module that exposes Android Constants to JS. */ | ||
@ReactModule(name = NativeDeviceInfoSpec.NAME) | ||
public class DeviceInfoModule : NativeDeviceInfoSpec, LifecycleEventListener { | ||
private var reactApplicationContext: ReactApplicationContext? = null | ||
private var fontScale: Float | ||
private var previousDisplayMetrics: ReadableMap? = null | ||
|
||
public constructor(reactContext: ReactApplicationContext) : super(reactContext) { | ||
initDisplayMetricsIfNotInitialized(reactContext) | ||
fontScale = reactContext.resources.configuration.fontScale | ||
reactContext.addLifecycleEventListener(this) | ||
reactApplicationContext = reactContext | ||
} | ||
|
||
public constructor(context: Context) : super(null) { | ||
reactApplicationContext = null | ||
initDisplayMetricsIfNotInitialized(context) | ||
fontScale = context.resources.configuration.fontScale | ||
} | ||
|
||
override public fun getTypedExportedConstants(): Map<String, Any> { | ||
val displayMetrics = getDisplayMetricsWritableMap(fontScale.toDouble()) | ||
|
||
// Cache the initial dimensions for later comparison in emitUpdateDimensionsEvent | ||
previousDisplayMetrics = displayMetrics.copy() | ||
return mapOf("Dimensions" to displayMetrics.toHashMap()) | ||
} | ||
|
||
override fun onHostResume() { | ||
val newFontScale = reactApplicationContext?.resources?.configuration?.fontScale | ||
if (newFontScale != null && newFontScale != fontScale) { | ||
fontScale = newFontScale | ||
emitUpdateDimensionsEvent() | ||
} | ||
} | ||
|
||
override fun onHostPause(): Unit = Unit | ||
|
||
override fun onHostDestroy(): Unit = Unit | ||
|
||
public fun emitUpdateDimensionsEvent() { | ||
reactApplicationContext?.let { context -> | ||
if (context.hasActiveReactInstance()) { | ||
// Don't emit an event to JS if the dimensions haven't changed | ||
val displayMetrics = getDisplayMetricsWritableMap(fontScale.toDouble()) | ||
if (previousDisplayMetrics == null) { | ||
previousDisplayMetrics = displayMetrics.copy() | ||
} else if (displayMetrics != previousDisplayMetrics) { | ||
previousDisplayMetrics = displayMetrics.copy() | ||
context.emitDeviceEvent("didUpdateDimensions", displayMetrics) | ||
} | ||
} else { | ||
ReactSoftExceptionLogger.logSoftException( | ||
NativeDeviceInfoSpec.NAME, | ||
ReactNoCrashSoftException( | ||
"No active CatalystInstance, cannot emitUpdateDimensionsEvent")) | ||
} | ||
} | ||
} | ||
|
||
override fun invalidate() { | ||
super.invalidate() | ||
reactApplicationContext?.removeLifecycleEventListener(this) | ||
} | ||
} |