Example usage for org.apache.cordova CallbackContext sendPluginResult

List of usage examples for org.apache.cordova CallbackContext sendPluginResult

Introduction

In this page you can find the example usage for org.apache.cordova CallbackContext sendPluginResult.

Prototype

public void sendPluginResult(PluginResult pluginResult) 

Source Link

Usage

From source file:BarcodeReaderPlugin.java

private void SendNoResult(CallbackContext callbackContext, boolean keepCallback) {
    // Return "no result" result - neither success nor failure callbacks will be called
    PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
    pluginResult.setKeepCallback(keepCallback);
    callbackContext.sendPluginResult(pluginResult);
}

From source file:br.com.hotforms.StatusBarManager.java

License:Apache License

/**
 * 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.
 *//*w  ww. ja  v  a 2s  .c  o  m*/
@Override
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext)
        throws JSONException {
    Log.v(TAG, "Executing action: " + action);
    final Activity activity = this.cordova.getActivity();
    final Window window = activity.getWindow();

    if ("_ready".equals(action)) {
        boolean statusBarVisible = (window.getAttributes().flags
                & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0;
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, statusBarVisible));
    } else if ("show".equals(action)) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
        });
        return true;
    } else if ("hide".equals(action)) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

                if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                }
            }
        });
        return true;
    } else if ("setTranslucent".equals(action)) {
        if (currentapiVersion >= android.os.Build.VERSION_CODES.KITKAT) {
            this.cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
                    if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                        window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                        window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                    }

                    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                }
            });
            return true;
        } else {
            return false;
        }
    } else if ("setTransparent".equals(action)) {
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            this.cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

                    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                    window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                }
            });
            return true;
        } else {
            return false;
        }
    } else if ("setColor".equals(action)) {
        if (currentapiVersion >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            this.setColor(args.getInt(0), args.getInt(1), args.getInt(2));
            return true;
        } else {
            return false;
        }
    }

    return false;
}

From source file:ch.liquidconcept.cordova.appversion.AppVersion.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    PluginResult result = new PluginResult(PluginResult.Status.INVALID_ACTION);
    PackageManager packageManager = cordova.getActivity().getPackageManager();

    if (action.equals("name")) {
        try {//from   w  ww . j a v a 2  s . c  o m
            PackageInfo packageInfo = packageManager
                    .getPackageInfo(cordova.getActivity().getApplicationContext().getPackageName(), 0);
            result = new PluginResult(PluginResult.Status.OK, packageInfo.versionName);
        } catch (PackageManager.NameNotFoundException exception) {
            result = new PluginResult(PluginResult.Status.ERROR, exception.getMessage());
        }
    }

    callbackContext.sendPluginResult(result);
    return true;
}

From source file:ch.rts.cordova.is.tablet.IsTablet.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    Log.d(LOG_TAG, "Plugin execute called - " + this.toString());
    Context context = this.cordova.getActivity().getApplicationContext();
    boolean result = isTabletDevice(context);
    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
    return true;//from w  w  w  .j a  va 2s .  c  om
}

From source file:cn.edu.gdmec.t00385.lightsensor.LightSensorListener.java

License:Apache License

/**
 * Executes the request and returns PluginResult.
 *
 * @param action                The action to execute.
 * @param args                 JSONArry of arguments for the plugin.
 * @param callbackS=Context     The callback id used when calling back into JavaScript.
 * @return                     True if the action was valid.
 * @throws JSONException /*from w ww.  j av  a  2 s .  c o  m*/
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("start")) {
        this.start();
    } else if (action.equals("stop")) {
        this.stop();
    } else if (action.equals("getStatus")) {
        int i = this.getStatus();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i));
    } else if (action.equals("getLumen")) {
        // If not running, then this is an async call, so don't worry about waiting
        if (this.status != LightSensorListener.RUNNING) {
            int r = this.start();
            if (r == LightSensorListener.ERROR_FAILED_TO_START) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION,
                        LightSensorListener.ERROR_FAILED_TO_START));
                return true;
            }
            // Set a timeout callback on the main thread.
            Handler handler = new Handler(Looper.getMainLooper());
            handler.postDelayed(new Runnable() {
                public void run() {
                    LightSensorListener.this.timeout();
                }
            }, 2000);
        }
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading()));
    } else if (action.equals("setTimeout")) {
        this.setTimeout(args.getLong(0));
    } else if (action.equals("getTimeout")) {
        long l = this.getTimeout();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
    } else {
        // Unsupported action
        return false;
    }
    return true;
}

From source file:cn.push.cordova.plugin.localnotification.LocalNotification.java

License:Apache License

/**
 * Checks wether a notification with an ID is scheduled.
 *
 * @param id/*  ww  w  .  j  av  a2 s  .  c o m*/
 *          The notification ID to be check.
 * @param callbackContext
 */
public static void isScheduled(String id, CallbackContext callbackContext) {
    SharedPreferences settings = getSharedPreferences();
    Map<String, ?> alarms = settings.getAll();
    boolean isScheduled = alarms.containsKey(id);
    PluginResult result = new PluginResult(PluginResult.Status.OK, isScheduled);

    callbackContext.sendPluginResult(result);
}

From source file:co.mwater.foregroundcameraplugin.ForegroundCameraLauncher.java

License:Apache License

/**
 * Executes the request and returns PluginResult.
 * //www.  j  a va  2  s  .  c  o m
 * @param action
 *            The action to execute.
 * @param args
 *            JSONArry of arguments for the plugin.
 * @param callbackId
 *            The callback id used when calling back into JavaScript.
 * @return A PluginResult object with a status and message.
 */
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    this.callbackContext = callbackContext;

    try {
        if (action.equals("takePicture")) {
            this.mQuality = args.getInt(0);
            this.destinationType = args.getInt(1);
            this.targetWidth = args.getInt(3);
            this.targetHeight = args.getInt(4);

            this.takePicture();

            PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
            r.setKeepCallback(true);
            callbackContext.sendPluginResult(r);
            return true;
        }
        return false;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
        return true;
    }
}

From source file:com.acequare.browserlauncher.BrowserLauncher.java

License:Apache License

/**
 * 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 used when calling back into JavaScript.
 * @return                       A PluginResult object with a status and message.
 *//*  w  w  w  .ja  v a  2 s .  co  m*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    if (action.equals("open")) {
        String url = args.getString(0);

        Context context = this.cordova.getActivity().getApplicationContext();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(browserIntent);
    }

    callbackContext.sendPluginResult(new PluginResult(status, result));
    return true;
}

From source file:com.adobe.phonegap.contentsync.Sync.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    if (action.equals("sync")) {
        sync(args, callbackContext);//from w w w  .jav a  2s. c o m
        return true;
    } else if (action.equals("download")) {
        final String source = args.getString(0);
        // Production
        String outputDirectory = cordova.getActivity().getCacheDir().getAbsolutePath();
        // Testing
        //String outputDirectory = cordova.getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        String filename = source.substring(source.lastIndexOf("/") + 1, source.length());
        final File target = new File(outputDirectory, filename);
        // @TODO we need these
        final JSONObject headers = new JSONObject();
        final CallbackContext finalContext = callbackContext;
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                if (download(source, target, headers, createProgressEvent("download"), finalContext, false)) {
                    JSONObject retval = new JSONObject();
                    try {
                        retval.put("archiveURL", target.getAbsolutePath());
                    } catch (JSONException e) {
                        // never happens
                    }
                    finalContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, retval));
                }
            }
        });
        return true;
    } else if (action.equals("unzip")) {
        String tempPath = args.getString(0);
        if (tempPath.startsWith("file://")) {
            tempPath = tempPath.substring(7);
        }
        final File source = new File(tempPath);
        final String target = args.getString(1);
        final CallbackContext finalContext = callbackContext;
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                unzipSync(source, target, createProgressEvent("unzip"), finalContext);
                finalContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
            }
        });
        return true;
    } else if (action.equals("cancel")) {
        ProgressEvent progress = activeRequests.get(args.getString(0));
        if (progress != null) {
            progress.setAborted(true);
        }
    }
    return false;
}

From source file:com.adobe.phonegap.contentsync.Sync.java

License:Apache License

private void sync(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
    // get args//  w  w w  .j av a  2  s  .c o  m
    final String src = args.getString(0);
    final String id = args.getString(1);
    final JSONObject headers;
    if (args.optJSONObject(3) != null) {
        headers = args.optJSONObject(3);
    } else {
        headers = new JSONObject();
    }
    final boolean copyCordovaAssets;
    final boolean copyRootApp = args.getBoolean(5);
    final boolean trustEveryone = args.getBoolean(7);
    if (copyRootApp) {
        copyCordovaAssets = true;
    } else {
        copyCordovaAssets = args.getBoolean(4);
    }
    final String manifestFile = args.getString(8);
    Log.d(LOG_TAG, "sync called with id = " + id + " and src = " + src + "!");

    final ProgressEvent progress = createProgressEvent(id);

    /**
     * need to clear cache or Android won't pick up on the replaced
     * content
     */
    cordova.getActivity().runOnUiThread(new Runnable() {
        public void run() {
            webView.clearCache(true);
        }
    });

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            synchronized (progress) {
                if (progress.isAborted()) {
                    return;
                }
            }

            String outputDirectory = getOutputDirectory(id);

            // Check to see if we should just return the cached version
            String type = args.optString(2, TYPE_REPLACE);
            Log.d(LOG_TAG, "type = " + type);
            File dir = new File(outputDirectory);
            Log.d(LOG_TAG, "dir = " + dir.exists());

            if (type.equals(TYPE_LOCAL) && hasAppBeenUpdated()) {
                savePrefs();

                if ("null".equals(src) && (copyRootApp || copyCordovaAssets)) {
                    if (copyRootApp) {
                        Log.d(LOG_TAG, "doing copy root app");
                        copyRootApp(outputDirectory, manifestFile);
                    }
                    if (copyCordovaAssets) {
                        Log.d(LOG_TAG, "doing copy cordova app");
                        copyCordovaAssets(outputDirectory);
                    }

                } else {
                    type = TYPE_REPLACE;
                }
            }

            if (!dir.exists()) {
                dir.mkdirs();
            }

            if (!type.equals(TYPE_LOCAL)) {
                // download file
                if (download(src, createDownloadFileLocation(id), headers, progress, callbackContext,
                        trustEveryone)) {
                    // update progress with zip file
                    File targetFile = progress.getTargetFile();
                    Log.d(LOG_TAG, "downloaded = " + targetFile.getAbsolutePath());

                    // Backup existing directory
                    File backup = backupExistingDirectory(outputDirectory, type, dir);

                    // @TODO: Do we do this even when type is local?
                    if (copyRootApp) {
                        copyRootApp(outputDirectory, manifestFile);
                    }

                    boolean win = false;
                    if (isZipFile(targetFile)) {
                        win = unzipSync(targetFile, outputDirectory, progress, callbackContext);
                    } else {
                        // copy file to ID
                        win = targetFile.renameTo(new File(outputDirectory));
                        progress.setLoaded(1);
                        progress.setTotal(1);
                        progress.setStatus(STATUS_EXTRACTING);
                        progress.updatePercentage();
                    }

                    // delete temp file
                    targetFile.delete();

                    if (copyCordovaAssets) {
                        copyCordovaAssets(outputDirectory);
                    }

                    if (win) {
                        // success, remove backup
                        removeFolder(backup);
                    } else {
                        // failure, revert backup
                        removeFolder(dir);
                        backup.renameTo(dir);
                    }
                } else {
                    return;
                }
            }

            // complete
            synchronized (activeRequests) {
                activeRequests.remove(id);
            }

            // Send last progress event
            progress.setStatus(STATUS_COMPLETE);
            updateProgress(callbackContext, progress);

            // Send completion message
            try {
                JSONObject result = new JSONObject();
                result.put(PROP_LOCAL_PATH, outputDirectory);

                if (dir.list() != null) {
                    Log.d(LOG_TAG, "size of output dir = " + dir.list().length);
                }
                boolean cached = false;
                if (type.equals(TYPE_LOCAL) && dir.exists() && dir.isDirectory() && dir.list() != null
                        && dir.list().length > 0) {
                    Log.d(LOG_TAG, "we have a dir with some files in it.");
                    cached = true;
                }

                result.put(PROP_CACHED, cached);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
            } catch (JSONException e) {
                // never happens
            }
        }
    });
}