From 7d10865df1fd10525967f865ca6330e6337e4737 Mon Sep 17 00:00:00 2001 From: Anderson Date: Wed, 24 May 2017 14:44:25 -0300 Subject: [PATCH 1/7] add intent with callback --- .../launcher/IntentLauncherModule.java | 64 ++++++++++++------- 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index efcb81a..2ede7d8 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -2,30 +2,29 @@ import android.app.Activity; import android.content.Intent; +import android.content.ComponentName; import android.net.Uri; import android.os.Bundle; import android.util.Log; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.ReactMethod; -import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.*; import java.io.Console; /** * Created by poberwong on 16/6/30. */ -public class IntentLauncherModule extends ReactContextBaseJavaModule { +public class IntentLauncherModule extends ReactContextBaseJavaModule implements ActivityEventListener { private static final String ATTR_ACTION = "action"; private static final String ATTR_CATEGORY = "category"; private static final String TAG_EXTRA = "extra"; private static final String ATTR_DATA = "data"; private static final String ATTR_FLAGS = "flags"; + Promise promise; public IntentLauncherModule(ReactApplicationContext reactContext) { super(reactContext); + reactContext.addActivityEventListener(this); } @Override @@ -39,23 +38,42 @@ public String getName() { * getReactApplicationContext().startActivity(intent); */ @ReactMethod - public void startActivity(ReadableMap params){ - Intent intent = new Intent(); - if (params.hasKey(ATTR_DATA)) { - intent.setData(Uri.parse(params.getString(ATTR_DATA))); + public void startActivity(ReadableMap params, final Promise promise){ + this.promise = promise; + try { + ComponentName cn = new ComponentName(params.getString(ATTR_DATA), params.getString(ATTR_ACTION)); + Intent intent = new Intent(); + intent.setComponent(cn); + Log.d("IntentLauncherModule", "Opening"); + if (params.hasKey(TAG_EXTRA)) { + intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); + } + if (params.hasKey(ATTR_FLAGS)) { + intent.addFlags(params.getInt(ATTR_FLAGS)); + } + if (params.hasKey(ATTR_CATEGORY)) { + intent.addCategory(params.getString(ATTR_CATEGORY)); + } + getReactApplicationContext().startActivityForResult(intent, 12, null); // 暂时使用当前应用的任务栈 + } catch (Exception e) { + promise.reject("Could not open intent"); } - if (params.hasKey(TAG_EXTRA)) { - intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); - } - if (params.hasKey(ATTR_FLAGS)) { - intent.addFlags(params.getInt(ATTR_FLAGS)); - } - if (params.hasKey(ATTR_CATEGORY)) { - intent.addCategory(params.getString(ATTR_CATEGORY)); - } - if (params.hasKey(ATTR_ACTION)) { - intent.setAction(params.getString(ATTR_ACTION)); - } - getReactApplicationContext().startActivityForResult(intent, 0, null); // 暂时使用当前应用的任务栈 + } + + @Override + public void onNewIntent(Intent intent) { } + + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + if (requestCode != 12) { + return; + } + WritableMap params; + Bundle extras = data.getExtras(); + + params = Arguments.fromBundle(extras); + + this.promise.resolve(params); + } } From ec5d56eacbb7de62cbcaf9ca4d35abb1d3f8fb2f Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 19 Jun 2017 17:47:51 -0300 Subject: [PATCH 2/7] added intent without Component --- .../launcher/IntentLauncherModule.java | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index 2ede7d8..5910ad9 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -10,6 +10,8 @@ import com.facebook.react.bridge.*; import java.io.Console; +import java.util.Set; +import java.util.Iterator; /** * Created by poberwong on 16/6/30. @@ -56,7 +58,27 @@ public void startActivity(ReadableMap params, final Promise promise){ } getReactApplicationContext().startActivityForResult(intent, 12, null); // 暂时使用当前应用的任务栈 } catch (Exception e) { - promise.reject("Could not open intent"); + promise.reject("ERROR", "Could not open intent"); + } + } + + @ReactMethod + public void startActivityOnlyIntent (ReadableMap params, final Promise promise) { + this.promise = promise; + try { + Intent intent = new Intent(params.getString(ATTR_DATA)); + if (params.hasKey(TAG_EXTRA)) { + intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); + } + if (params.hasKey(ATTR_FLAGS)) { + intent.addFlags(params.getInt(ATTR_FLAGS)); + } + if (params.hasKey(ATTR_CATEGORY)) { + intent.addCategory(params.getString(ATTR_CATEGORY)); + } + getReactApplicationContext().startActivityForResult(intent, 12, null); // 暂时使用当前应用的任务栈 + } catch (Exception e) { + promise.reject("ERROR", "Could not open intent"); } } @@ -68,10 +90,26 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, if (requestCode != 12) { return; } - WritableMap params; + WritableMap params = Arguments.createMap(); Bundle extras = data.getExtras(); - params = Arguments.fromBundle(extras); + Set keys = extras.keySet(); + Iterator it = keys.iterator(); + Log.e("LGC","Dumping Intent start"); + while (it.hasNext()) { + String key = it.next(); + if (extras.get(key) instanceof String) { + params.putString(key, (String) extras.get(key)); + } + + if (extras.get(key) instanceof Integer) { + params.putInt(key, (Integer) extras.get(key)); + } + + Log.e("LGC", "[" + key + "=" + extras.get(key) + "]"); + } + + // params = Arguments.fromBundle(extras); this.promise.resolve(params); From 42758d05c4788236804a28d9f16e3c5f34f1d5a6 Mon Sep 17 00:00:00 2001 From: Anderson Date: Mon, 26 Jun 2017 14:59:25 -0300 Subject: [PATCH 3/7] fixed intent data --- .../com/poberwong/launcher/IntentLauncherModule.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index 5910ad9..5a8e317 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -98,6 +98,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, Log.e("LGC","Dumping Intent start"); while (it.hasNext()) { String key = it.next(); + if (extras.get(key) instanceof String) { params.putString(key, (String) extras.get(key)); } @@ -106,6 +107,14 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, params.putInt(key, (Integer) extras.get(key)); } + if (extras.get(key) instanceof Double) { + params.putDouble(key, (Double) extras.get(key)); + } + + if (extras.get(key) instanceof Boolean) { + params.putBoolean(key, (Boolean) extras.get(key)); + } + Log.e("LGC", "[" + key + "=" + extras.get(key) + "]"); } From 530c286a76f99894b5f81be2383febb7337ff389 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 5 Jul 2017 22:08:09 -0500 Subject: [PATCH 4/7] Fix compatible of class properties syntax --- IntentConstant.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/IntentConstant.js b/IntentConstant.js index 7f92a79..efca906 100644 --- a/IntentConstant.js +++ b/IntentConstant.js @@ -659,7 +659,7 @@ export default class Intent { * A synonym for {@link #ACTION_VIEW}, the "standard" action that is * performed on a piece of data. */ - static ACTION_DEFAULT = Intent.ACTION_VIEW; + // static ACTION_DEFAULT = Intent.ACTION_VIEW; /** * Used to indicate that some piece of data should be attached to some other @@ -4184,7 +4184,7 @@ export default class Intent { * @see android.R.attr#documentLaunchMode * @see #FLAG_ACTIVITY_MULTIPLE_TASK */ - static FLAG_ACTIVITY_NEW_DOCUMENT = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET; + // static FLAG_ACTIVITY_NEW_DOCUMENT = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET; /** * If set, this flag will prevent the normal {@link android.app.Activity#onUserLeaveHint} * callback from occurring on the current frontmost activity before it is @@ -4317,9 +4317,9 @@ export default class Intent { /** * @hide Flags that can't be changed with PendingIntent. */ - static IMMUTABLE_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION | - Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | - Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; + // static IMMUTABLE_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION | + // Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | + // Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; // --------------------------------------------------------------------- // --------------------------------------------------------------------- @@ -8326,3 +8326,9 @@ export default class Intent { // return (mFlags & FLAG_ACTIVITY_NEW_DOCUMENT) == FLAG_ACTIVITY_NEW_DOCUMENT; // } } + +Intent.ACTION_DEFAULT = Intent.ACTION_VIEW; +Intent.FLAG_ACTIVITY_NEW_DOCUMENT = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET; +Intent.IMMUTABLE_FLAGS = Intent.FLAG_GRANT_READ_URI_PERMISSION | + Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | + Intent.FLAG_GRANT_PREFIX_URI_PERMISSION; From e4206db3921fa1c0e1f96d06bff37ccfb44d2b37 Mon Sep 17 00:00:00 2001 From: Filippo Dossena Date: Wed, 6 Sep 2017 19:57:53 +0200 Subject: [PATCH 5/7] merged 'startActivity' and 'startActivityOnlyIntent' in one method and added few other options: 'type', 'packageName', 'className'. --- .../launcher/IntentLauncherModule.java | 108 +++++++++--------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index 5a8e317..5b744a4 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -18,10 +18,13 @@ */ public class IntentLauncherModule extends ReactContextBaseJavaModule implements ActivityEventListener { private static final String ATTR_ACTION = "action"; + private static final String ATTR_TYPE = "type"; private static final String ATTR_CATEGORY = "category"; private static final String TAG_EXTRA = "extra"; private static final String ATTR_DATA = "data"; private static final String ATTR_FLAGS = "flags"; + private static final String ATTR_PACKAGE_NAME = "packageName"; + private static final String ATTR_CLASS_NAME = "className"; Promise promise; public IntentLauncherModule(ReactApplicationContext reactContext) { @@ -40,33 +43,29 @@ public String getName() { * getReactApplicationContext().startActivity(intent); */ @ReactMethod - public void startActivity(ReadableMap params, final Promise promise){ + public void startActivity(ReadableMap params, final Promise promise) { this.promise = promise; try { - ComponentName cn = new ComponentName(params.getString(ATTR_DATA), params.getString(ATTR_ACTION)); - Intent intent = new Intent(); - intent.setComponent(cn); - Log.d("IntentLauncherModule", "Opening"); - if (params.hasKey(TAG_EXTRA)) { - intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); - } - if (params.hasKey(ATTR_FLAGS)) { - intent.addFlags(params.getInt(ATTR_FLAGS)); - } - if (params.hasKey(ATTR_CATEGORY)) { - intent.addCategory(params.getString(ATTR_CATEGORY)); - } - getReactApplicationContext().startActivityForResult(intent, 12, null); // 暂时使用当前应用的任务栈 - } catch (Exception e) { - promise.reject("ERROR", "Could not open intent"); - } - } - - @ReactMethod - public void startActivityOnlyIntent (ReadableMap params, final Promise promise) { - this.promise = promise; - try { - Intent intent = new Intent(params.getString(ATTR_DATA)); + Intent intent = new Intent(); + + if (params.hasKey(ATTR_CLASS_NAME)) { + ComponentName cn; + if (params.hasKey(ATTR_PACKAGE_NAME)) { + cn = new ComponentName(params.getString(ATTR_PACKAGE_NAME), params.getString(ATTR_CLASS_NAME)); + } else { + cn = new ComponentName(getReactApplicationContext(), params.getString(ATTR_CLASS_NAME)); + } + intent.setComponent(cn); + } + if (params.hasKey(ATTR_ACTION)) { + intent.setAction(params.getString(ATTR_ACTION)); + } + if (params.hasKey(ATTR_DATA)) { + intent.setData(Uri.parse(params.getString(ATTR_DATA))); + } + if (params.hasKey(ATTR_TYPE)) { + intent.setType(params.getString(ATTR_TYPE)); + } if (params.hasKey(TAG_EXTRA)) { intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); } @@ -83,44 +82,45 @@ public void startActivityOnlyIntent (ReadableMap params, final Promise promise) } @Override - public void onNewIntent(Intent intent) { } + public void onNewIntent(Intent intent) { + } @Override public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { - if (requestCode != 12) { - return; - } - WritableMap params = Arguments.createMap(); - Bundle extras = data.getExtras(); - - Set keys = extras.keySet(); - Iterator it = keys.iterator(); - Log.e("LGC","Dumping Intent start"); - while (it.hasNext()) { - String key = it.next(); - - if (extras.get(key) instanceof String) { - params.putString(key, (String) extras.get(key)); - } + if (requestCode != 12) { + return; + } + WritableMap params = Arguments.createMap(); + if (data != null) { + Bundle extras = data.getExtras(); - if (extras.get(key) instanceof Integer) { - params.putInt(key, (Integer) extras.get(key)); - } + Set keys = extras.keySet(); + Iterator it = keys.iterator(); + Log.e("LGC", "Dumping Intent start"); + while (it.hasNext()) { + String key = it.next(); - if (extras.get(key) instanceof Double) { - params.putDouble(key, (Double) extras.get(key)); - } + if (extras.get(key) instanceof String) { + params.putString(key, (String) extras.get(key)); + } + + if (extras.get(key) instanceof Integer) { + params.putInt(key, (Integer) extras.get(key)); + } + + if (extras.get(key) instanceof Double) { + params.putDouble(key, (Double) extras.get(key)); + } - if (extras.get(key) instanceof Boolean) { - params.putBoolean(key, (Boolean) extras.get(key)); + if (extras.get(key) instanceof Boolean) { + params.putBoolean(key, (Boolean) extras.get(key)); + } + + Log.e("LGC", "[" + key + "=" + extras.get(key) + "]"); } - - Log.e("LGC", "[" + key + "=" + extras.get(key) + "]"); } - // params = Arguments.fromBundle(extras); - - this.promise.resolve(params); + this.promise.resolve(params); } } From b42d6f755bc4556036ee8b06e04843153e0d17c2 Mon Sep 17 00:00:00 2001 From: Filippo Dossena Date: Thu, 7 Sep 2017 12:09:14 +0200 Subject: [PATCH 6/7] promise is resolved with more data ('resultCode', 'data' and 'extra') --- .../launcher/IntentLauncherModule.java | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index 5b744a4..43e3192 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -17,6 +17,7 @@ * Created by poberwong on 16/6/30. */ public class IntentLauncherModule extends ReactContextBaseJavaModule implements ActivityEventListener { + private static final int REQUEST_CODE = 12; private static final String ATTR_ACTION = "action"; private static final String ATTR_TYPE = "type"; private static final String ATTR_CATEGORY = "category"; @@ -75,9 +76,9 @@ public void startActivity(ReadableMap params, final Promise promise) { if (params.hasKey(ATTR_CATEGORY)) { intent.addCategory(params.getString(ATTR_CATEGORY)); } - getReactApplicationContext().startActivityForResult(intent, 12, null); // 暂时使用当前应用的任务栈 - } catch (Exception e) { - promise.reject("ERROR", "Could not open intent"); + getReactApplicationContext().startActivityForResult(intent, REQUEST_CODE, null); // 暂时使用当前应用的任务栈 + } catch (Exception ex) { + promise.reject("ERROR", "Could not open intent", ex); } } @@ -86,41 +87,25 @@ public void onNewIntent(Intent intent) { } @Override - public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { - if (requestCode != 12) { + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) { + if (requestCode != REQUEST_CODE) { return; } WritableMap params = Arguments.createMap(); - if (data != null) { - Bundle extras = data.getExtras(); + if (intent != null) { + params.putInt("resultCode", resultCode); - Set keys = extras.keySet(); - Iterator it = keys.iterator(); - Log.e("LGC", "Dumping Intent start"); - while (it.hasNext()) { - String key = it.next(); - - if (extras.get(key) instanceof String) { - params.putString(key, (String) extras.get(key)); - } - - if (extras.get(key) instanceof Integer) { - params.putInt(key, (Integer) extras.get(key)); - } - - if (extras.get(key) instanceof Double) { - params.putDouble(key, (Double) extras.get(key)); - } - - if (extras.get(key) instanceof Boolean) { - params.putBoolean(key, (Boolean) extras.get(key)); - } + Uri data = intent.getData(); + if (data != null) { + params.putString("data", data.toString()); + } - Log.e("LGC", "[" + key + "=" + extras.get(key) + "]"); + Bundle extras = intent.getExtras(); + if (extras != null) { + params.putMap("extra", Arguments.fromBundle(extras)); } } this.promise.resolve(params); - } } From 8a02cfd81d0f4ea59a5776a2dc203865ffb2fd4d Mon Sep 17 00:00:00 2001 From: Filippo Dossena Date: Fri, 15 Sep 2017 13:07:45 +0200 Subject: [PATCH 7/7] Fix compilation error in RN 0.47.0 --- .../java/com/poberwong/launcher/IntentLauncherPackage.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java index 5c341ce..fd60c3a 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java @@ -18,8 +18,7 @@ public class IntentLauncherPackage implements ReactPackage{ public List createNativeModules(ReactApplicationContext reactContext) { return Arrays.asList(new IntentLauncherModule(reactContext)); // 返回一个NativeModule范型的数组就ok } - - @Override + public List> createJSModules() { return Collections.emptyList(); }