SystemModule.java 4.42 KB
package com.drp.util.react;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
//import com.drp.mobliemall.support.QRCodeScanActivity;
import com.drp.mobliemall.app.GlobalContext;
import com.drp.mobliemall.config.Constants;
import com.drp.mobliemall.utils.SharePreferenceUtil;
import com.facebook.react.bridge.*;

import java.util.List;

/**
 * 通信Module类
 * Created by Song on 2017/2/17.
 */
public class SystemModule extends ReactContextBaseJavaModule {
//
    private ReactApplicationContext mContext;
    public static final String MODULE_NAME = "system";
//    /**
//     * 构造方法必须实现
//     */
    public SystemModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.mContext = reactContext;
    }
//
    /**
     * 在rn代码里面是需要这个名字来调用该类的方法
     */
    @Override
    public String getName() {
        return MODULE_NAME;
    }
//
//    /**
//     * 打电话
//     * @param phone 电话号码
//     */
//    @ReactMethod
//    public void call(String phone) {
//        XnDeviceApiUtils.getInstance().call(phone);
//    }
//
    /**
     * 跳转至首页
     */
    @ReactMethod
    public void navTo(String page) {
        Activity currentActivity = getCurrentActivity();
        // 跳转到其它页面
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 跳转需要添加flag, 否则报错
        if(page == null) {
            page = "";
        }
        ComponentName component = null;
        switch(page.toUpperCase()) {
//            case "APP":
//                // 跳转至应用中心
//                component = new ComponentName(currentActivity, MicroApplicationManagerActivity.class);
//                break;
            case "BACK":
                // 后退
                if(currentActivity != null) {
                    currentActivity.finish();
                }
                return;
        }
        intent.setComponent(component);
        mContext.startActivity(intent);
    }
//
//    /**
//     * 使用默认浏览器打开
//     * @param url
//     */
//    @ReactMethod
//    public void openUrl(String url) {
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        Uri uri = Uri.parse(url);
//        intent.setData(uri);
//        if (null != intent.resolveActivity(mContext.getPackageManager())) {
//            mContext.startActivity(intent);
//        }
//    }
//
//    /**
//     * 判断是否安装了某软件
//     * @param packageName  软件的应用ID
//     */
//    @ReactMethod
//    public void hasApp(String packageName, Promise promise) {
//        final PackageManager packageManager = mContext.getPackageManager();
//        // 获取所有已安装程序的包信息
//        List<PackageInfo> infoList = packageManager.getInstalledPackages(0);
//        for(PackageInfo info : infoList) {
//            if(info.packageName.equalsIgnoreCase(packageName)) {
//                promise.resolve("true");
//            }
//        }
//        promise.resolve("false");
//    }

    /**
     * 获取运行模式
     */
    @ReactMethod
    public void getMode(Promise promise) {
        promise.resolve(Constants.APP_ENV);
    }


    @ReactMethod
    public void getMemberId(Promise promise) {
        promise.resolve(String.valueOf(GlobalContext.getInstance().getSpUtil().getUserInfo().getId()));
    }

//
//    /**
//     * 获取当前位置
//     * @param callback
//     */
//    @ReactMethod
//    public void getLocation(Callback callback) {
//        XnLocation location = XnDeviceApiUtils.getInstance().getLocation();
//        callback.invoke(JSON.toJSONString(location));
//    }

    /**
     * 使用摄像头
     */
    @ReactMethod
    public void scan(Callback callback) {
       // QRCodeScanActivity qrCodeScanActivity = new QRCodeScanActivity();
//        QRCodeScanActivity.callback = callback;
//        Intent intent = new Intent(getCurrentActivity(), QRCodeScanActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 跳转需要添加flag, 否则报错
//        getCurrentActivity().startActivity(intent);
        //(intent);

      //  promise.resolve(Constants.FLAVOR);
    }

}