Skip to content

Commit

Permalink
Merge pull request #3 from godness84/master
Browse files Browse the repository at this point in the history
Add promise callback, activity result and more options for the intent
  • Loading branch information
poberwong authored Dec 3, 2017
2 parents cab2e80 + f319db6 commit 6d65b09
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
16 changes: 11 additions & 5 deletions IntentConstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
return Arrays.<NativeModule>asList(new IntentLauncherModule(reactContext)); // 返回一个NativeModule范型的数组就ok
}

// @Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
Expand Down

0 comments on commit 6d65b09

Please sign in to comment.