Search in sources :

Example 11 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class InAppBrowser method execute.

/**
     * Executes the request and returns PluginResult.
     *
     * @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.
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {
        if (action.equals("open")) {
            this.callbackContext = callbackContext;
            String url = args.getString(0);
            String target = args.optString(1);
            if (target == null || target.equals("") || target.equals(NULL)) {
                target = SELF;
            }
            HashMap<String, Boolean> features = parseFeature(args.optString(2));
            Log.d(LOG_TAG, "target = " + target);
            url = updateUrl(url);
            String result = "";
            // SELF
            if (SELF.equals(target)) {
                Log.d(LOG_TAG, "in self");
                // load in webview
                if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)) {
                    this.webView.loadUrl(url);
                } else //Load the dialer
                if (url.startsWith(WebView.SCHEME_TEL)) {
                    try {
                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse(url));
                        this.cordova.getActivity().startActivity(intent);
                    } catch (android.content.ActivityNotFoundException e) {
                        LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
                    }
                } else // load in InAppBrowser
                {
                    result = this.showWebPage(url, features);
                }
            } else // SYSTEM
            if (SYSTEM.equals(target)) {
                Log.d(LOG_TAG, "in system");
                result = this.openExternal(url);
            } else // BLANK - or anything else
            {
                Log.d(LOG_TAG, "in blank");
                result = this.showWebPage(url, features);
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
            pluginResult.setKeepCallback(true);
            this.callbackContext.sendPluginResult(pluginResult);
        } else if (action.equals("close")) {
            closeDialog();
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        } else if (action.equals("injectScriptCode")) {
            String jsWrapper = null;
            if (args.getBoolean(1)) {
                jsWrapper = String.format("prompt(JSON.stringify([eval(%%s)]), 'gap-iab://%s')", callbackContext.getCallbackId());
            }
            injectDeferredObject(args.getString(0), jsWrapper);
        } else if (action.equals("injectScriptFile")) {
            String jsWrapper;
            if (args.getBoolean(1)) {
                jsWrapper = String.format("(function(d) { var c = d.createElement('script'); c.src = %%s; c.onload = function() { prompt('', 'gap-iab://%s'); }; d.body.appendChild(c); })(document)", callbackContext.getCallbackId());
            } else {
                jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document)";
            }
            injectDeferredObject(args.getString(0), jsWrapper);
        } else if (action.equals("injectStyleCode")) {
            String jsWrapper;
            if (args.getBoolean(1)) {
                jsWrapper = String.format("(function(d) { var c = d.createElement('style'); c.innerHTML = %%s; d.body.appendChild(c); prompt('', 'gap-iab://%s');})(document)", callbackContext.getCallbackId());
            } else {
                jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document)";
            }
            injectDeferredObject(args.getString(0), jsWrapper);
        } else if (action.equals("injectStyleFile")) {
            String jsWrapper;
            if (args.getBoolean(1)) {
                jsWrapper = String.format("(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %%s; d.head.appendChild(c); prompt('', 'gap-iab://%s');})(document)", callbackContext.getCallbackId());
            } else {
                jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document)";
            }
            injectDeferredObject(args.getString(0), jsWrapper);
        } else if (action.equals("show")) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    dialog.show();
                }
            };
            this.cordova.getActivity().runOnUiThread(runnable);
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
        } else {
            return false;
        }
    } catch (JSONException e) {
        this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return true;
}
Also used : PluginResult(org.apache.cordova.api.PluginResult) JSONException(org.json.JSONException) Intent(android.content.Intent)

Example 12 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class NetworkManager method sendUpdate.

/**
     * Create a new plugin result and send it back to JavaScript
     *
     * @param connection the network info to set as navigator.connection
     */
private void sendUpdate(String type) {
    if (connectionCallbackContext != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, type);
        result.setKeepCallback(true);
        connectionCallbackContext.sendPluginResult(result);
    }
    webView.postMessage("networkconnection", type);
}
Also used : PluginResult(org.apache.cordova.api.PluginResult)

Example 13 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class Notification method alert.

/**
     * Builds and shows a native Android alert with given Strings
     * @param message           The message the alert should display
     * @param title             The title of the alert
     * @param buttonLabel       The label of the button
     * @param callbackContext   The callback context
     */
public synchronized void alert(final String message, final String title, final String buttonLabel, final CallbackContext callbackContext) {
    final CordovaInterface cordova = this.cordova;
    Runnable runnable = new Runnable() {

        public void run() {
            AlertDialog.Builder dlg = new AlertDialog.Builder(cordova.getActivity());
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(true);
            dlg.setPositiveButton(buttonLabel, new AlertDialog.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
                }
            });
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {

                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
                }
            });
            dlg.create();
            dlg.show();
        }

        ;
    };
    this.cordova.getActivity().runOnUiThread(runnable);
}
Also used : AlertDialog(android.app.AlertDialog) CordovaInterface(org.apache.cordova.api.CordovaInterface) PluginResult(org.apache.cordova.api.PluginResult) DialogInterface(android.content.DialogInterface)

Example 14 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class ActivityPlugin method execute.

/**
     * Executes the request and returns PluginResult.
     *
     * @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.
     */
public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) {
    PluginResult result = new PluginResult(PluginResult.Status.OK, "");
    try {
        if (action.equals("start")) {
            this.startActivity(args.getString(0));
            callbackContext.sendPluginResult(result);
            callbackContext.success();
            return true;
        }
    } catch (JSONException e) {
        result = new PluginResult(PluginResult.Status.JSON_EXCEPTION, "JSON Exception");
        callbackContext.sendPluginResult(result);
        return false;
    }
    return false;
}
Also used : PluginResult(org.apache.cordova.api.PluginResult) JSONException(org.json.JSONException)

Example 15 with PluginResult

use of org.apache.cordova.api.PluginResult in project cordova-android-chromeview by thedracle.

the class FileUtils method execute.

/**
     * Executes the request and returns whether the action was valid.
     *
     * @param action 		The action to execute.
     * @param args 		JSONArray of arguments for the plugin.
     * @param callbackContext	The callback context used when calling back into JavaScript.
     * @return 			True if the action was valid, false otherwise.
     */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {
        if (action.equals("testSaveLocationExists")) {
            boolean b = DirectoryManager.testSaveLocationExists();
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("getFreeDiskSpace")) {
            long l = DirectoryManager.getFreeDiskSpace(false);
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l));
        } else if (action.equals("testFileExists")) {
            boolean b = DirectoryManager.testFileExists(args.getString(0));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("testDirectoryExists")) {
            boolean b = DirectoryManager.testFileExists(args.getString(0));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, b));
        } else if (action.equals("readAsText")) {
            String encoding = args.getString(1);
            int start = args.getInt(2);
            int end = args.getInt(3);
            this.readFileAs(args.getString(0), start, end, callbackContext, encoding, PluginResult.MESSAGE_TYPE_STRING);
        } else if (action.equals("readAsDataURL")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, -1);
        } else if (action.equals("readAsArrayBuffer")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_ARRAYBUFFER);
        } else if (action.equals("readAsBinaryString")) {
            int start = args.getInt(1);
            int end = args.getInt(2);
            this.readFileAs(args.getString(0), start, end, callbackContext, null, PluginResult.MESSAGE_TYPE_BINARYSTRING);
        } else if (action.equals("write")) {
            long fileSize = this.write(args.getString(0), args.getString(1), args.getInt(2), args.getBoolean(3));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
        } else if (action.equals("truncate")) {
            long fileSize = this.truncateFile(args.getString(0), args.getLong(1));
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, fileSize));
        } else if (action.equals("requestFileSystem")) {
            long size = args.optLong(1);
            if (size != 0 && size > (DirectoryManager.getFreeDiskSpace(true) * 1024)) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, FileUtils.QUOTA_EXCEEDED_ERR));
            } else {
                JSONObject obj = requestFileSystem(args.getInt(0));
                callbackContext.success(obj);
            }
        } else if (action.equals("resolveLocalFileSystemURI")) {
            JSONObject obj = resolveLocalFileSystemURI(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getMetadata")) {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getMetadata(args.getString(0))));
        } else if (action.equals("getFileMetadata")) {
            JSONObject obj = getFileMetadata(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getParent")) {
            JSONObject obj = getParent(args.getString(0));
            callbackContext.success(obj);
        } else if (action.equals("getDirectory")) {
            JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), true);
            callbackContext.success(obj);
        } else if (action.equals("getFile")) {
            JSONObject obj = getFile(args.getString(0), args.getString(1), args.optJSONObject(2), false);
            callbackContext.success(obj);
        } else if (action.equals("remove")) {
            boolean success;
            success = remove(args.getString(0));
            if (success) {
                notifyDelete(args.getString(0));
                callbackContext.success();
            } else {
                callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
            }
        } else if (action.equals("removeRecursively")) {
            boolean success = removeRecursively(args.getString(0));
            if (success) {
                callbackContext.success();
            } else {
                callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
            }
        } else if (action.equals("moveTo")) {
            JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), true);
            callbackContext.success(entry);
        } else if (action.equals("copyTo")) {
            JSONObject entry = transferTo(args.getString(0), args.getString(1), args.getString(2), false);
            callbackContext.success(entry);
        } else if (action.equals("readEntries")) {
            JSONArray entries = readEntries(args.getString(0));
            callbackContext.success(entries);
        } else {
            return false;
        }
    } catch (FileNotFoundException e) {
        callbackContext.error(FileUtils.NOT_FOUND_ERR);
    } catch (FileExistsException e) {
        callbackContext.error(FileUtils.PATH_EXISTS_ERR);
    } catch (NoModificationAllowedException e) {
        callbackContext.error(FileUtils.NO_MODIFICATION_ALLOWED_ERR);
    } catch (InvalidModificationException e) {
        callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
    } catch (MalformedURLException e) {
        callbackContext.error(FileUtils.ENCODING_ERR);
    } catch (IOException e) {
        callbackContext.error(FileUtils.INVALID_MODIFICATION_ERR);
    } catch (EncodingException e) {
        callbackContext.error(FileUtils.ENCODING_ERR);
    } catch (TypeMismatchException e) {
        callbackContext.error(FileUtils.TYPE_MISMATCH_ERR);
    }
    return true;
}
Also used : MalformedURLException(java.net.MalformedURLException) PluginResult(org.apache.cordova.api.PluginResult) EncodingException(org.apache.cordova.file.EncodingException) TypeMismatchException(org.apache.cordova.file.TypeMismatchException) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) InvalidModificationException(org.apache.cordova.file.InvalidModificationException) NoModificationAllowedException(org.apache.cordova.file.NoModificationAllowedException) IOException(java.io.IOException) JSONObject(org.json.JSONObject) FileExistsException(org.apache.cordova.file.FileExistsException)

Aggregations

PluginResult (org.apache.cordova.api.PluginResult)27 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)9 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 MalformedURLException (java.net.MalformedURLException)3 CordovaInterface (org.apache.cordova.api.CordovaInterface)3 Intent (android.content.Intent)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 HttpURLConnection (java.net.HttpURLConnection)2 URL (java.net.URL)2 HostnameVerifier (javax.net.ssl.HostnameVerifier)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 JSONArray (org.json.JSONArray)2