org.apache.cordova.CallbackContext#sendPluginResult ( )源码实例Demo

下面列出了org.apache.cordova.CallbackContext#sendPluginResult ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jpHolo   文件: NetworkManager.java
/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                  True if the action was valid, false otherwise.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("getConnectionInfo")) {
        this.connectionCallbackContext = callbackContext;
        NetworkInfo info = sockMan.getActiveNetworkInfo();
        String connectionType = "";
        try {
            connectionType = this.getConnectionInfo(info).get("type").toString();
        } catch (JSONException e) { }

        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        return true;
    }
    return false;
}
 
源代码2 项目: WebSocket-for-Android   文件: DisconnectionTask.java
@Override
public void execute(String rawArgs, CallbackContext ctx) {
    try {
        JSONArray args = new JSONArray(rawArgs);
        int id = Integer.parseInt(args.getString(0), 16);
        int code = args.getInt(1);
        String reason = args.getString(2);
        Connection conn = _map.get(id);

        if (conn != null) {
            if (code > 0) {
                conn.close(code, reason);
            } else {
                conn.close();
            }
        }
    } catch (Exception e) {
        if (!ctx.isFinished()) {
            PluginResult result = new PluginResult(Status.ERROR);
            result.setKeepCallback(true);
            ctx.sendPluginResult(result);
        }
    }
}
 
源代码3 项目: showCaseCordova   文件: AccelListener.java
/**
 * Executes the request.
 *
 * @param action        The action to execute.
 * @param args          The exec() arguments.
 * @param callbackId    The callback id used when calling back into JavaScript.
 * @return              Whether the action was valid.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("start")) {
        this.callbackContext = callbackContext;
        if (this.status != AccelListener.RUNNING) {
            // If not running, then this is an async call, so don't worry about waiting
            // We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
            this.start();
        }
    }
    else if (action.equals("stop")) {
        if (this.status == AccelListener.RUNNING) {
            this.stop();
        }
    } else {
      // Unsupported action
        return false;
    }

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, "");
    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);
    return true;
}
 
源代码4 项目: reacteu-app   文件: FileTransfer.java
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("upload") || action.equals("download")) {
        String source = args.getString(0);
        String target = args.getString(1);

        if (action.equals("upload")) {
            try {
                source = URLDecoder.decode(source, "UTF-8");
                upload(source, target, args, callbackContext);
            } catch (UnsupportedEncodingException e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "UTF-8 error."));
            }
        } else {
            download(source, target, args, callbackContext);
        }
        return true;
    } else if (action.equals("abort")) {
        String objectId = args.getString(0);
        abort(objectId);
        callbackContext.success();
        return true;
    }
    return false;
}
 
源代码5 项目: reader   文件: FileTransfer.java
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("upload") || action.equals("download")) {
        String source = args.getString(0);
        String target = args.getString(1);

        if (action.equals("upload")) {
            try {
                source = URLDecoder.decode(source, "UTF-8");
                upload(source, target, args, callbackContext);
            } catch (UnsupportedEncodingException e) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.MALFORMED_URL_EXCEPTION, "UTF-8 error."));
            }
        } else {
            download(source, target, args, callbackContext);
        }
        return true;
    } else if (action.equals("abort")) {
        String objectId = args.getString(0);
        abort(objectId);
        callbackContext.success();
        return true;
    }
    return false;
}
 
源代码6 项目: showCaseCordova   文件: NetworkManager.java
/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                  True if the action was valid, false otherwise.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    if (action.equals("getConnectionInfo")) {
        this.connectionCallbackContext = callbackContext;
        NetworkInfo info = sockMan.getActiveNetworkInfo();
        String connectionType = "";
        try {
            connectionType = this.getConnectionInfo(info).get("type").toString();
        } catch (JSONException e) { }

        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        return true;
    }
    return false;
}
 
源代码7 项目: cordova-hot-code-push   文件: HotCodePushPlugin.java
/**
 * Get information about app and web versions.
 *
 * @param callback callback where to send the result
 */
private void jsGetVersionInfo(final CallbackContext callback) {
    final Context context = cordova.getActivity();
    final Map<String, Object> data = new HashMap<String, Object>();
    data.put("currentWebVersion", pluginInternalPrefs.getCurrentReleaseVersionName());
    data.put("readyToInstallWebVersion", pluginInternalPrefs.getReadyForInstallationReleaseVersionName());
    data.put("previousWebVersion", pluginInternalPrefs.getPreviousReleaseVersionName());
    data.put("appVersion", VersionHelper.applicationVersionName(context));
    data.put("buildVersion", VersionHelper.applicationVersionCode(context));

    final PluginResult pluginResult = PluginResultHelper.createPluginResult(null, data, null);
    callback.sendPluginResult(pluginResult);
}
 
源代码8 项目: cordova-plugin-imei   文件: IMEIPlugin.java
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    if (action.equals("get")) {
        TelephonyManager telephonyManager = (TelephonyManager)this.cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
        result = telephonyManager.getDeviceId();
    }
    else {
        status = PluginResult.Status.INVALID_ACTION;
    }
    callbackContext.sendPluginResult(new PluginResult(status, result));
    return true;
}
 
源代码9 项目: reacteu-app   文件: FileUtils.java
/**
 * Requests a filesystem in which to store application data.
 *
 * @param type of file system requested
 * @param requiredSize required free space in the file system in bytes
 * @param callbackContext context for returning the result or error
 * @throws JSONException
 */
private void requestFileSystem(int type, long requiredSize, final CallbackContext callbackContext) throws JSONException {
    Filesystem rootFs = null;
    try {
        rootFs = this.filesystems.get(type);
    } catch (ArrayIndexOutOfBoundsException e) {
        // Pass null through
    }
    if (rootFs == null) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR));
    } else {
        // If a nonzero required size was specified, check that the retrieved filesystem has enough free space.
        long availableSize = 0;
        if (requiredSize > 0) {
            availableSize = rootFs.getFreeSpaceInBytes();
        }

        if (availableSize < requiredSize) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
        } else {
            JSONObject fs = new JSONObject();
            fs.put("name", rootFs.name);
            fs.put("root", rootFs.getRootEntry());
            callbackContext.success(fs);
        }
    }
}
 
源代码10 项目: keemob   文件: FileUtils.java
/**
 * Requests a filesystem in which to store application data.
 *
 * @param type of file system requested
 * @param requiredSize required free space in the file system in bytes
 * @param callbackContext context for returning the result or error
 * @throws JSONException
 */
private void requestFileSystem(int type, long requiredSize, final CallbackContext callbackContext) throws JSONException {
    Filesystem rootFs = null;
    try {
        rootFs = this.filesystems.get(type);
    } catch (ArrayIndexOutOfBoundsException e) {
        // Pass null through
    }
    if (rootFs == null) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.NOT_FOUND_ERR));
    } else {
        // If a nonzero required size was specified, check that the retrieved filesystem has enough free space.
        long availableSize = 0;
        if (requiredSize > 0) {
            availableSize = rootFs.getFreeSpaceInBytes();
        }

        if (availableSize < requiredSize) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
        } else {
            JSONObject fs = new JSONObject();
            fs.put("name", rootFs.name);
            fs.put("root", rootFs.getRootEntry());
            callbackContext.success(fs);
        }
    }
}
 
源代码11 项目: cbsp   文件: BackgroundServicePlugin.java
private void sendUpdateToListener(ExecuteResult logicResult, Object[] listenerExtras) {
	try {
		if (listenerExtras != null && listenerExtras.length > 0) {
			Log.d(TAG, "Sending update");
			CallbackContext callback = (CallbackContext)listenerExtras[0];
	
			callback.sendPluginResult(transformResult(logicResult));
			Log.d(TAG, "Sent update");
		}
	} catch (Exception ex) {
		Log.d(TAG, "Sending update failed", ex);
	}
}
 
private void callStatus(CallbackContext callbackContext) {
    if (mCall == null) {
        callbackContext.sendPluginResult(new PluginResult(
                PluginResult.Status.ERROR));
        return;
    }
    String state = getCallState(mCall.getState());
    if (state == null) {
        state = "";
    }
    PluginResult result = new PluginResult(PluginResult.Status.OK, state);
    callbackContext.sendPluginResult(result);
}
 
private void getIntent(CallbackContext callbackContext) throws JSONException  {
    Intent intent = this.cordova.getActivity().getIntent();
    PluginResult result = new PluginResult(PluginResult.Status.OK, buildIntent(intent));
    callbackContext.sendPluginResult(result);
}
 
/**
 * Start a device discovery.
 *
 * @param args			Arguments given.
 * @param callbackCtx	Where to send results.
 */
private void startDiscovery(JSONArray args, CallbackContext callbackCtx)
{
	// TODO Someday add an option to fetch UUIDs at the same time

	try
	{
		if(_bluetooth.isConnecting())
		{
			this.error(callbackCtx, "A Connection attempt is in progress.", BluetoothError.ERR_CONNECTING_IN_PROGRESS);
		}
		else
		{
			if(_bluetooth.isDiscovering())
			{
				_wasDiscoveryCanceled = true;
				_bluetooth.stopDiscovery();

				if(_discoveryCallback != null)
				{
					this.error(_discoveryCallback,
						"Discovery was stopped because a new discovery was started.",
						BluetoothError.ERR_DISCOVERY_RESTARTED
					);
					_discoveryCallback = null;
				}
			}

			_bluetooth.startDiscovery();

			PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
			result.setKeepCallback(true);
			callbackCtx.sendPluginResult(result);

			_discoveryCallback = callbackCtx;
		}
	}
	catch(Exception e)
	{
		this.error(callbackCtx, e.getMessage(), BluetoothError.ERR_UNKNOWN);
	}
}
 
源代码15 项目: phonegap-plugin-loading-spinner   文件: App.java
/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback context from which we were invoked.
 * @return                  A PluginResult object with a status and message.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("clearCache")) {
            this.clearCache();
        }
        else if (action.equals("show")) {
            // This gets called from JavaScript onCordovaReady to show the webview.
            // I recommend we change the name of the Message as spinner/stop is not
            // indicative of what this actually does (shows the webview).
            cordova.getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    webView.postMessage("spinner", "stop");
                }
            });
        }
        else if (action.equals("loadUrl")) {
            this.loadUrl(args.getString(0), args.optJSONObject(1));
        }
        else if (action.equals("cancelLoadUrl")) {
            //this.cancelLoadUrl();
        }
        else if (action.equals("clearHistory")) {
            this.clearHistory();
        }
        else if (action.equals("backHistory")) {
            this.backHistory();
        }
        else if (action.equals("overrideButton")) {
            this.overrideButton(args.getString(0), args.getBoolean(1));
        }
        else if (action.equals("overrideBackbutton")) {
            this.overrideBackbutton(args.getBoolean(0));
        }
        else if (action.equals("exitApp")) {
            this.exitApp();
        }
        callbackContext.sendPluginResult(new PluginResult(status, result));
        return true;
    } catch (JSONException e) {
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
        return false;
    }
}
 
源代码16 项目: atomic-plugins-inapps   文件: InAppServicePlugin.java
public void canPurchase(CordovaArgs args, CallbackContext ctx) {
	ctx.sendPluginResult(new PluginResult(Status.OK, service.canPurchase()));
}
 
源代码17 项目: jpHolo   文件: Globalization.java
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
    JSONObject obj = new JSONObject();

    try{
        if (action.equals(GETLOCALENAME)){
            obj = getLocaleName();
        }else if (action.equals(GETPREFERREDLANGUAGE)){
            obj = getPreferredLanguage();
        } else if (action.equalsIgnoreCase(DATETOSTRING)) {
            obj = getDateToString(data);
        }else if(action.equalsIgnoreCase(STRINGTODATE)){
            obj = getStringtoDate(data);
        }else if(action.equalsIgnoreCase(GETDATEPATTERN)){
            obj = getDatePattern(data);
        }else if(action.equalsIgnoreCase(GETDATENAMES)){
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD) {
                throw new GlobalizationError(GlobalizationError.UNKNOWN_ERROR);
            } else {
                obj = getDateNames(data);
            }
        }else if(action.equalsIgnoreCase(ISDAYLIGHTSAVINGSTIME)){
            obj = getIsDayLightSavingsTime(data);
        }else if(action.equalsIgnoreCase(GETFIRSTDAYOFWEEK)){
            obj = getFirstDayOfWeek(data);
        }else if(action.equalsIgnoreCase(NUMBERTOSTRING)){
            obj = getNumberToString(data);
        }else if(action.equalsIgnoreCase(STRINGTONUMBER)){
            obj = getStringToNumber(data);
        }else if(action.equalsIgnoreCase(GETNUMBERPATTERN)){
            obj = getNumberPattern(data);
        }else if(action.equalsIgnoreCase(GETCURRENCYPATTERN)){
            obj = getCurrencyPattern(data);
        }else {
            return false;
        }

        callbackContext.success(obj);
    }catch (GlobalizationError ge){
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, ge.toJson()));
    }catch (Exception e){
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return true;
}
 
源代码18 项目: atomic-plugins-inapps   文件: InAppServicePlugin.java
public void getProducts(CordovaArgs args, CallbackContext ctx) {
	ctx.sendPluginResult(new PluginResult(Status.OK, productsToJSON(service.getProducts())));
}
 
源代码19 项目: atomic-plugins-inapps   文件: InAppServicePlugin.java
public void isPurchased(CordovaArgs args, CallbackContext ctx) {
	String productId = args.optString(0);
	boolean purchased = productId != null ? service.isPurchased(productId) : false;
	ctx.sendPluginResult(new PluginResult(Status.OK, purchased));
}
 
源代码20 项目: a2cardboard   文件: CoreAndroid.java
/**
  * Executes the request and returns PluginResult.
  *
  * @param action            The action to execute.
  * @param args              JSONArry of arguments for the plugin.
  * @param callbackContext   The callback context from which we were invoked.
  * @return                  A PluginResult object with a status and message.
  */
 public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
     PluginResult.Status status = PluginResult.Status.OK;
     String result = "";

     try {
         if (action.equals("clearCache")) {
             this.clearCache();
         }
         else if (action.equals("show")) {
             // This gets called from JavaScript onCordovaReady to show the webview.
             // I recommend we change the name of the Message as spinner/stop is not
             // indicative of what this actually does (shows the webview).
             cordova.getActivity().runOnUiThread(new Runnable() {
                 public void run() {
                     webView.getPluginManager().postMessage("spinner", "stop");
                 }
             });
         }
         else if (action.equals("loadUrl")) {
             this.loadUrl(args.getString(0), args.optJSONObject(1));
         }
         else if (action.equals("cancelLoadUrl")) {
             //this.cancelLoadUrl();
         }
         else if (action.equals("clearHistory")) {
             this.clearHistory();
         }
         else if (action.equals("backHistory")) {
             this.backHistory();
         }
         else if (action.equals("overrideButton")) {
             this.overrideButton(args.getString(0), args.getBoolean(1));
         }
         else if (action.equals("overrideBackbutton")) {
             this.overrideBackbutton(args.getBoolean(0));
         }
         else if (action.equals("exitApp")) {
             this.exitApp();
         }
else if (action.equals("messageChannel")) {
             messageChannel = callbackContext;
             return true;
         }

         callbackContext.sendPluginResult(new PluginResult(status, result));
         return true;
     } catch (JSONException e) {
         callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
         return false;
     }
 }