Skip to content

Commit d350b69

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Migrate MemoryPressureRouter to Kotlin (facebook#50048)
Summary: Migrate com.facebook.react.MemoryPressureRouter to Kotlin. ## Changelog: [INTERNAL] - Migrate com.facebook.react.MemoryPressureRouter to Kotlin Pull Request resolved: facebook#50048 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: cortinico Differential Revision: D71307420 Pulled By: javache fbshipit-source-id: f85efe0a0dc2828cdc128eed952d5298c546b116
1 parent 77cdaa8 commit d350b69

File tree

4 files changed

+59
-64
lines changed

4 files changed

+59
-64
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ public abstract class com/facebook/react/LazyReactPackage : com/facebook/react/R
6363
public fun getViewManagers (Lcom/facebook/react/bridge/ReactApplicationContext;)Ljava/util/List;
6464
}
6565

66-
public class com/facebook/react/MemoryPressureRouter : android/content/ComponentCallbacks2 {
66+
public final class com/facebook/react/MemoryPressureRouter : android/content/ComponentCallbacks2 {
6767
public fun <init> (Landroid/content/Context;)V
68-
public fun addMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
69-
public fun destroy (Landroid/content/Context;)V
68+
public final fun addMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
69+
public final fun destroy (Landroid/content/Context;)V
7070
public fun onConfigurationChanged (Landroid/content/res/Configuration;)V
7171
public fun onLowMemory ()V
7272
public fun onTrimMemory (I)V
73-
public fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
73+
public final fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
7474
}
7575

7676
public final class com/facebook/react/NativeModuleRegistryBuilder {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/MemoryPressureRouter.java

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react
9+
10+
import android.content.ComponentCallbacks2
11+
import android.content.Context
12+
import android.content.res.Configuration
13+
import com.facebook.react.bridge.MemoryPressureListener
14+
import java.util.concurrent.CopyOnWriteArrayList
15+
16+
/** Translates and routes memory pressure events. */
17+
public class MemoryPressureRouter(context: Context) : ComponentCallbacks2 {
18+
private val listeners = CopyOnWriteArrayList<MemoryPressureListener>()
19+
20+
init {
21+
context.applicationContext.registerComponentCallbacks(this)
22+
}
23+
24+
public fun destroy(context: Context): Unit {
25+
context.applicationContext.unregisterComponentCallbacks(this)
26+
}
27+
28+
/** Add a listener to be notified of memory pressure events. */
29+
public fun addMemoryPressureListener(listener: MemoryPressureListener) {
30+
if (!listeners.contains(listener)) {
31+
listeners.add(listener)
32+
}
33+
}
34+
35+
/** Remove a listener previously added with [.addMemoryPressureListener]. */
36+
public fun removeMemoryPressureListener(listener: MemoryPressureListener) {
37+
listeners.remove(listener)
38+
}
39+
40+
override public fun onTrimMemory(level: Int) {
41+
dispatchMemoryPressure(level)
42+
}
43+
44+
override public fun onConfigurationChanged(newConfig: Configuration): Unit = Unit
45+
46+
@Deprecated("onLowMemory is deprecated in the underlying API")
47+
override public fun onLowMemory(): Unit = Unit
48+
49+
private fun dispatchMemoryPressure(level: Int) {
50+
for (listener in listeners) {
51+
listener.handleMemoryPressure(level)
52+
}
53+
}
54+
}

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactHostTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import org.junit.After
2424
import org.junit.Before
2525
import org.junit.Test
2626
import org.junit.runner.RunWith
27-
import org.mockito.ArgumentMatchers.any
2827
import org.mockito.MockedConstruction
2928
import org.mockito.Mockito.mockConstruction
29+
import org.mockito.kotlin.any
3030
import org.mockito.kotlin.doNothing
3131
import org.mockito.kotlin.doReturn
3232
import org.mockito.kotlin.mock

0 commit comments

Comments
 (0)