Skip to content

Commit

Permalink
Add the onNewIntent listener to React Native Android
Browse files Browse the repository at this point in the history
Reviewed By: foghina

Differential Revision: D3475896

fbshipit-source-id: d8e5d7734974132307a85d21e4c1602327a479fa
  • Loading branch information
ginandi authored and Facebook Github Bot 4 committed Jun 27, 2016
1 parent 93a1244 commit 2fc0f40
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@
import com.facebook.soloader.SoLoader;
import com.facebook.systrace.Systrace;

import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_JS_MODULE_CONFIG_END;
import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_JS_MODULE_CONFIG_START;
import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_END;
import static com.facebook.react.bridge.ReactMarkerConstants.BUILD_NATIVE_MODULE_REGISTRY_START;
import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_CATALYST_INSTANCE_END;
Expand Down Expand Up @@ -480,6 +478,8 @@ public void onNewIntent(Intent intent) {
Assertions.assertNotNull(mCurrentReactContext).getNativeModule(DeviceEventManagerModule.class);
deviceEventManagerModule.emitNewIntentReceived(uri);
}

mCurrentReactContext.onNewIntent(mCurrentActivity, intent);
}
}

Expand Down Expand Up @@ -519,7 +519,6 @@ public void onHostPause() {
public void onHostResume(Activity activity, DefaultHardwareBackBtnHandler defaultBackButtonImpl) {
UiThreadUtil.assertOnUiThread();


mDefaultBackButtonImpl = defaultBackButtonImpl;
if (mUseDeveloperSupport) {
mDevSupportManager.setDevSupportEnabled(true);
Expand Down Expand Up @@ -852,7 +851,6 @@ private ReactApplicationContext createReactContext(
ReactMarker.logMarker(BUILD_NATIVE_MODULE_REGISTRY_END);
}


NativeModuleCallExceptionHandler exceptionHandler = mNativeModuleCallExceptionHandler != null
? mNativeModuleCallExceptionHandler
: mDevSupportManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ public void onNewIntent(Intent intent) {
Assertions.assertNotNull(mCurrentReactContext).getNativeModule(DeviceEventManagerModule.class);
deviceEventManagerModule.emitNewIntentReceived(uri);
}

mCurrentReactContext.onNewIntent(mCurrentActivity, intent);
}
}

Expand Down Expand Up @@ -862,7 +864,6 @@ private ReactApplicationContext createReactContext(
catalystInstance.addBridgeIdleDebugListener(mBridgeIdleDebugListener);
}


ReactMarker.logMarker(RUN_JS_BUNDLE_START);
try {
catalystInstance.getReactQueueConfiguration().getJSQueueThread().callOnQueue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public interface ActivityEventListener {
* Called when host (activity/service) receives an {@link Activity#onActivityResult} call.
*/
void onActivityResult(int requestCode, int resultCode, Intent data);

/**
* Called when a new intent is passed to the activity
*/
void onNewIntent(Intent intent);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.react.bridge;

import android.content.Intent;

/**
* An empty implementation of {@link ActivityEventListener}
*/
public class BaseActivityEventListener implements ActivityEventListener {

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { }

@Override
public void onNewIntent(Intent intent) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ public void onHostResume(@Nullable Activity activity) {
}
}

public void onNewIntent(@Nullable Activity activity, Intent intent) {
UiThreadUtil.assertOnUiThread();
mCurrentActivity = new WeakReference(activity);
for (ActivityEventListener listener : mActivityEventListeners) {
listener.onNewIntent(intent);
}
}

/**
* Should be called by the hosting Fragment in {@link Fragment#onPause}
*/
Expand Down

5 comments on commit 2fc0f40

@ginandi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@grabbou heads-up: this is a breaking change. I changed the ActivityEventListener interface, so now it allows modules to listen to Activity.onNewIntent. There are many functionalities in Android that require that, like listening to NFC data. If you implement that interface, you will now need to add an implementation of public void onNewIntent(Intent intent) I've added the BaseActivityEventListener abstract class as a convenience, but you will obviously only be able to use it if you don't extend some other class.

Let me know if you need more info!

@satya164
Copy link
Contributor

@satya164 satya164 commented on 2fc0f40 Jun 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should be moved to IntentAndroid module now that there is a onNewIntent event -

@ginandi
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@satya164 Not sure I understand your comment

@satya164
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following code belongs to the IntentAndroid module,

String action = intent.getAction();
      Uri uri = intent.getData();

      if (Intent.ACTION_VIEW.equals(action) && uri != null) {
        DeviceEventManagerModule deviceEventManagerModule =
            Assertions.assertNotNull(mCurrentReactContext).getNativeModule(DeviceEventManagerModule.class);
        deviceEventManagerModule.emitNewIntentReceived(uri);
      }

It was added here since there was no way for a module to know when onNewIntent was called.

@skilesare
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this help with InputMediaService applications? I have a keyboard that won't work on Android(iOS) is fine because getCurrentActivity returns null from my InputMediaService context. Perhaps there is a another interface that can be added to handle that?:

#7762

Please sign in to comment.