CloudRoomService.java 2.22 KB
Newer Older
Jennie Shi's avatar
Jennie Shi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
package com.hls.cordova.cloudroom;

import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.app.demo.Business;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class CloudRoomService extends CordovaPlugin {
    private CallbackContext mCallbackContext;
    private JSONObject obj;
    private Business business;
    private static final String TAG = "CloudRoomService";

    // methods
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        this.mCallbackContext = callbackContext;
       // Log.e(TAG, "execute: " + action + "," + webView);
        String userId = "king";
        JSONObject info = new JSONObject();
        // if (args.length() > 0) {
        //     String a = args.getString(0);
        //     a = a.replace("\\", "");
        //     if (a.equals("null")) {
        //         obj = null;
        //     } else {
        //         obj = new JSONObject(a);
        //     }
        // }
        // 登录
        if (action.equals("loginCloudRoom")) {
            // if (obj.has("name")) {
            //     userId = obj.getString("name");  //接收js传过来的参数name password serverURL等
            // }
            if (business == null) {
            business = new Business(webView.getContext());
            }
            business.doLogin(userId, callbackContext);
            info.put("success", "1");
            this.echo(info);
            return true;
        }else if (action.equals("helpYourSelf")) {
            if(business == null){
            Toast.makeText(webView.getContext(), "请先登录", Toast.LENGTH_SHORT).show();
            }
            business.helpYourSelf(callbackContext);
            info.put("success", "1");
            this.echo(info);
            return true;
        }

        // default
        return false;
    }

     private void echo(final JSONObject data) {
        mCallbackContext.success(data);
    }
    private void echo(final String data) {
        mCallbackContext.success(data);
    }
}