Skip to content

Commit

Permalink
Support onNewIntent in Bridgeless (#43401)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43401

Implement `onNewIntent` in Bridgeless by adding it to ReactHostImpl

Changelog:
[Android][Breaking] Implement `onNewIntent` in Bridgeless

Reviewed By: fkgozali, RSNara

Differential Revision: D54703159

fbshipit-source-id: fd8589d8131f4fa57188d493331dc68fb38c4520
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Mar 13, 2024
1 parent 1150f21 commit 5d711f8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
3 changes: 3 additions & 0 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public class com/facebook/react/ReactDelegate {
public fun onHostDestroy ()V
public fun onHostPause ()V
public fun onHostResume ()V
public fun onNewIntent (Landroid/content/Intent;)Z
public fun onWindowFocusChanged (Z)V
public fun shouldShowDevMenuOrReload (ILandroid/view/KeyEvent;)Z
}
Expand Down Expand Up @@ -204,6 +205,7 @@ public abstract interface class com/facebook/react/ReactHost {
public abstract fun onHostPause (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;)V
public abstract fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public abstract fun onNewIntent (Landroid/content/Intent;)V
public abstract fun onWindowFocusChange (Z)V
public abstract fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public abstract fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
Expand Down Expand Up @@ -3643,6 +3645,7 @@ public class com/facebook/react/runtime/ReactHostImpl : com/facebook/react/React
public fun onHostPause (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;)V
public fun onHostResume (Landroid/app/Activity;Lcom/facebook/react/modules/core/DefaultHardwareBackBtnHandler;)V
public fun onNewIntent (Landroid/content/Intent;)V
public fun onWindowFocusChange (Z)V
public fun reload (Ljava/lang/String;)Lcom/facebook/react/interfaces/TaskInterface;
public fun removeBeforeDestroyListener (Lkotlin/jvm/functions/Function0;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,7 @@ public boolean onBackPressed() {
}

public boolean onNewIntent(Intent intent) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
// TODO T156475655: support onNewIntent
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
return true;
}
}
return false;
return mReactDelegate.onNewIntent(intent);
}

public void onWindowFocusChanged(boolean hasFocus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ public boolean onBackPressed() {
return false;
}

public boolean onNewIntent(Intent intent) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
mReactHost.onNewIntent(intent);
return true;
} else {
if (getReactNativeHost().hasInstance()) {
getReactNativeHost().getReactInstanceManager().onNewIntent(intent);
return true;
}
}
return false;
}

public void onActivityResult(
int requestCode, int resultCode, Intent data, boolean shouldForwardToReactInstance) {
if (ReactFeatureFlags.enableBridgelessArchitecture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public interface ReactHost {
/* To be called when focus has changed for the hosting window. */
public fun onWindowFocusChange(hasFocus: Boolean)

/* This method will give JS the opportunity to receive intents via Linking. */
public fun onNewIntent(intent: Intent)

public fun addBeforeDestroyListener(onBeforeDestroy: () -> Unit)

public fun removeBeforeDestroyListener(onBeforeDestroy: () -> Unit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -667,6 +669,36 @@ public void onWindowFocusChange(boolean hasFocus) {
"Tried to access onWindowFocusChange while context is not ready"));
}

/* This method will give JS the opportunity to receive intents via Linking.
*
* @param intent The incoming intent
*/
@ThreadConfined(UI)
@Override
public void onNewIntent(Intent intent) {
log("onNewIntent()");

ReactContext currentContext = getCurrentReactContext();
if (currentContext != null) {
String action = intent.getAction();
Uri uri = intent.getData();

if (uri != null
&& (Intent.ACTION_VIEW.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action))) {
DeviceEventManagerModule deviceEventManagerModule =
currentContext.getNativeModule(DeviceEventManagerModule.class);
if (deviceEventManagerModule != null) {
deviceEventManagerModule.emitNewIntentReceived(uri);
}
}
currentContext.onNewIntent(getCurrentActivity(), intent);
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException("Tried to access onNewIntent while context is not ready"));
}

@Nullable
JavaScriptContextHolder getJavaScriptContextHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
Expand Down

0 comments on commit 5d711f8

Please sign in to comment.