Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate MemoryPressureRouter to Kotlin #48354

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public abstract class com/facebook/react/LazyReactPackage : com/facebook/react/R
public fun getViewManagers (Lcom/facebook/react/bridge/ReactApplicationContext;)Ljava/util/List;
}

public class com/facebook/react/MemoryPressureRouter : android/content/ComponentCallbacks2 {
public final class com/facebook/react/MemoryPressureRouter : android/content/ComponentCallbacks2 {
public fun <init> (Landroid/content/Context;)V
public fun addMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
public fun destroy (Landroid/content/Context;)V
public final fun addMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
public final fun destroy (Landroid/content/Context;)V
public fun onConfigurationChanged (Landroid/content/res/Configuration;)V
public fun onLowMemory ()V
public fun onTrimMemory (I)V
public fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
public final fun removeMemoryPressureListener (Lcom/facebook/react/bridge/MemoryPressureListener;)V
}

public final class com/facebook/react/NativeModuleRegistryBuilder {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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

import android.content.ComponentCallbacks2
import android.content.Context
import android.content.res.Configuration
import com.facebook.react.bridge.MemoryPressureListener
import java.util.concurrent.CopyOnWriteArrayList

/** Translates and routes memory pressure events. */
public class MemoryPressureRouter(context: Context) : ComponentCallbacks2 {

private val listeners: CopyOnWriteArrayList<MemoryPressureListener> =
CopyOnWriteArrayList<MemoryPressureListener>()

init {
context.applicationContext.registerComponentCallbacks(this)
}

public fun destroy(context: Context): Unit {
context.applicationContext.unregisterComponentCallbacks(this)
}

/** Add a listener to be notified of memory pressure events. */
public fun addMemoryPressureListener(listener: MemoryPressureListener?): Unit {
if (!listeners.contains(listener)) {
listeners.add(listener)
}
}

/** Remove a listener previously added with [addMemoryPressureListener]. */
public fun removeMemoryPressureListener(listener: MemoryPressureListener?): Unit {
listeners.remove(listener)
}

override fun onTrimMemory(level: Int): Unit {
dispatchMemoryPressure(level)
}

override fun onConfigurationChanged(newConfig: Configuration): Unit = Unit

@Deprecated("onLowMemory is deprecated") override fun onLowMemory(): Unit = Unit

private fun dispatchMemoryPressure(level: Int) {
for (listener in listeners) {
listener.handleMemoryPressure(level)
}
}
}
Loading