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; diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java index efcb81a..43e3192 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherModule.java @@ -2,30 +2,35 @@ 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; +import java.util.Set; +import java.util.Iterator; /** * Created by poberwong on 16/6/30. */ -public class IntentLauncherModule extends ReactContextBaseJavaModule { +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"; 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) { super(reactContext); + reactContext.addActivityEventListener(this); } @Override @@ -39,23 +44,68 @@ 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))); - } - if (params.hasKey(TAG_EXTRA)) { - intent.putExtras(Arguments.toBundle(params.getMap(TAG_EXTRA))); - } - if (params.hasKey(ATTR_FLAGS)) { - intent.addFlags(params.getInt(ATTR_FLAGS)); + public void startActivity(ReadableMap params, final Promise promise) { + this.promise = promise; + try { + 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))); + } + 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, REQUEST_CODE, null); // 暂时使用当前应用的任务栈 + } catch (Exception ex) { + promise.reject("ERROR", "Could not open intent", ex); } - if (params.hasKey(ATTR_CATEGORY)) { - intent.addCategory(params.getString(ATTR_CATEGORY)); + } + + @Override + public void onNewIntent(Intent intent) { + } + + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) { + if (requestCode != REQUEST_CODE) { + return; } - if (params.hasKey(ATTR_ACTION)) { - intent.setAction(params.getString(ATTR_ACTION)); + WritableMap params = Arguments.createMap(); + if (intent != null) { + params.putInt("resultCode", resultCode); + + Uri data = intent.getData(); + if (data != null) { + params.putString("data", data.toString()); + } + + Bundle extras = intent.getExtras(); + if (extras != null) { + params.putMap("extra", Arguments.fromBundle(extras)); + } } - getReactApplicationContext().startActivityForResult(intent, 0, null); // 暂时使用当前应用的任务栈 + + this.promise.resolve(params); } } diff --git a/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java b/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java index c2d1dc2..e2ecda9 100644 --- a/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java +++ b/android/src/main/java/com/poberwong/launcher/IntentLauncherPackage.java @@ -19,7 +19,6 @@ public List createNativeModules(ReactApplicationContext reactConte return Arrays.asList(new IntentLauncherModule(reactContext)); // 返回一个NativeModule范型的数组就ok } - // @Override public List> createJSModules() { return Collections.emptyList(); }