Browse Source

Add support for getLaunchArgs on Android (#5466)

Add support for getLaunchArgs on Android

**Usage:** 

1. **Pass** args in order to run the application on the device using ADB or xcrun or Detox:
    - Detox: `device.launchApp(bundleId,{launchArgs: {'-key1': 'val1'}})` must use launchArgs key, see [docs](https://github.com/wix/Detox/blob/master/docs/APIRef.DeviceObjectAPI.md#7-additional-launch-arguments).
   - ADB: `adb shell am start -n com.yourpackage/com.yourpackage.YourActivity --es launchArgs "{some stringyfied json}"`
   - Xcrun: `/usr/bin/xcrun simctl launch ${udid} ${bundleId} --args -key1 value1 -key2 value2`

2. **Retrieve** the arguments: `const launchArgs = await Navigation.getLaunchArgs();`
   - There is a small difference between platforms on how they pass the launch args:
       - on iOS, the args will be passed as an array of pairs (argv,argc)
       - on Android, the args will be passed in the form of a dictionary 


Extract args as a dictionary example:
```javascript
const launchArgs = await Navigation.getLaunchArgs();
let resultArgs = {};
    if (Platform.OS === 'android') {
      resultArgs = launchArgs; // no need to proccess its a dictionary 
    } else {
      for (let i = 0; i < launchArgs.length; i += 2) {
        if (i + 1 < launchArgs.length) {
          resultArgs[launchArgs[i]] = launchArgs[i + 1];
        }
      }
    }
```
Ward Abbass 5 years ago
parent
commit
16646e7c88

+ 6
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java View File

@@ -1,5 +1,6 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3
+import com.reactnativenavigation.utils.LaunchArgsParser;
3 4
 import com.facebook.react.ReactInstanceManager;
4 5
 import com.facebook.react.bridge.Arguments;
5 6
 import com.facebook.react.bridge.Promise;
@@ -72,6 +73,11 @@ public class NavigationModule extends ReactContextBaseJavaModule {
72 73
         return NAME;
73 74
     }
74 75
 
76
+    @ReactMethod
77
+    public void getLaunchArgs(String commandId, Promise promise) {
78
+        promise.resolve(LaunchArgsParser.parse(activity()));
79
+    }
80
+    
75 81
     @ReactMethod
76 82
     public void getConstants(Promise promise) {
77 83
         ReactApplicationContext ctx = getReactApplicationContext();

+ 31
- 0
lib/android/app/src/main/java/com/reactnativenavigation/utils/LaunchArgsParser.java View File

@@ -0,0 +1,31 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.app.Activity;
4
+import android.content.Intent;
5
+import android.os.Bundle;
6
+
7
+import com.facebook.react.bridge.Arguments;
8
+import com.facebook.react.bridge.WritableMap;
9
+
10
+public final class LaunchArgsParser {
11
+
12
+    private static final String LAUNCH_ARGS = "launchArgs";
13
+
14
+    /**
15
+     * Parses launch args passed to activity intent to WritableMap
16
+     * @param activity to fetch the extra launch args
17
+     * @return parsed writable map if it exist, otherwise empty map will be returned
18
+     */
19
+    public static WritableMap parse(Activity activity) {
20
+        if (activity != null) {
21
+            Intent intent = activity.getIntent();
22
+            if (intent != null) {
23
+                Bundle launchArgs = intent.getBundleExtra(LAUNCH_ARGS);
24
+                if (launchArgs != null) {
25
+                    return Arguments.fromBundle(launchArgs);
26
+                }
27
+            }
28
+        }
29
+        return Arguments.createMap();
30
+    }
31
+}