package com.poberwong.launcher;

import android.app.Activity;
import android.content.Intent;
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 java.io.Console;

/**
 * Created by poberwong on 16/6/30.
 */
public class IntentLauncherModule extends ReactContextBaseJavaModule {
    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";

    public IntentLauncherModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "IntentLauncher";
    }

    /**
     * 选用方案
     * intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     * 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));
        }
        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); // 暂时使用当前应用的任务栈
    }
}

@@ -86,16 +86,54 @@
 Intent;%0A
+import android.content.ComponentName;%0A
 import a
@@ -237,228 +237,9 @@
 dge.
-Arguments;%0Aimport com.facebook.react.bridge.ReactApplicationContext;%0Aimport com.facebook.react.bridge.ReactContextBaseJavaModule;%0Aimport com.facebook.react.bridge.ReactMethod;%0Aimport com.facebook.react.bridge.ReadableMap
+*
 ;%0A%0Ai
@@ -375,16 +375,49 @@
 aModule 
+implements ActivityEventListener 
 %7B%0A    pr
@@ -684,16 +684,37 @@
 %22flags%22;
+%0A    Promise promise;
 %0A%0A    pu
@@ -804,24 +804,77 @@
 ctContext);%0A
+        reactContext.addActivityEventListener(this);%0A
     %7D%0A%0A    @
@@ -1163,167 +1163,317 @@
 rams
-)%7B%0A        Intent intent = new Intent();%0A        if (params.hasKey(ATTR_DATA)) %7B%0A            intent.setData(Uri.parse(params.getString(ATTR_DATA)));%0A        %7D%0A
+, final Promise promise)%7B%0A        this.promise = promise;%0A        try %7B%0A          ComponentName cn = new ComponentName(params.getString(ATTR_DATA), params.getString(ATTR_ACTION));%0A          Intent intent = new Intent();%0A          intent.setComponent(cn);%0A          Log.d(%22IntentLauncherModule%22, %22Opening%22);%0A  
     
@@ -1504,24 +1504,26 @@
 G_EXTRA)) %7B%0A
+  
             
@@ -1582,24 +1582,26 @@
 G_EXTRA)));%0A
+  
         %7D%0A  
@@ -1598,32 +1598,34 @@
       %7D%0A        
+  
 if (params.hasKe
@@ -1637,24 +1637,26 @@
 R_FLAGS)) %7B%0A
+  
             
@@ -1699,32 +1699,34 @@
 LAGS));%0A        
+  
 %7D%0A        if (pa
@@ -1711,32 +1711,34 @@
       %7D%0A        
+  
 if (params.hasKe
@@ -1753,24 +1753,26 @@
 ATEGORY)) %7B%0A
+  
             
@@ -1824,32 +1824,34 @@
 GORY));%0A        
+  
 %7D%0A        if (pa
@@ -1848,100 +1848,177 @@
     
-if (params.hasKey(ATTR_ACTION)) %7B%0A            intent.setAction(params.getString(ATTR_ACTION)
+  getReactApplicationContext().startActivityForResult(intent, 12, null); // %E6%9A%82%E6%97%B6%E4%BD%BF%E7%94%A8%E5%BD%93%E5%89%8D%E5%BA%94%E7%94%A8%E7%9A%84%E4%BB%BB%E5%8A%A1%E6%A0%88%0A        %7D catch (Exception e) %7B%0A          promise.reject(%22Could not open intent%22
 );%0A 
@@ -2034,54 +2034,113 @@
     
+%7D%0A%0A
     
-getReactApplicationContext().start
+@Override%0A    public void onNewIntent(Intent intent) %7B %7D%0A%0A    @Override%0A    public void on
 Activity
 ForR
@@ -2139,51 +2139,286 @@
 vity
-For
 Result(
-intent, 0, null); // %E6%9A%82%E6%97%B6%E4%BD%BF%E7%94%A8%E5%BD%93%E5%89%8D%E5%BA%94%E7%94%A8%E7%9A%84%E4%BB%BB%E5%8A%A1%E6%A0%88
+Activity activity, int requestCode, int resultCode, Intent data) %7B%0A      if (requestCode != 12) %7B%0A        return;%0A      %7D%0A      WritableMap params;%0A      Bundle extras = data.getExtras();%0A%0A      params = Arguments.fromBundle(extras);%0A%0A      this.promise.resolve(params);%0A
 %0A   

add intent with callback