Search in sources :

Example 56 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class FileTransfer method upload.

/**
 * Uploads the specified file to the server URL provided using an HTTP multipart request.
 * @param source        Full path of the file on the file system
 * @param target        URL of the server to receive the file
 * @param args          JSON Array of args
 * @param callbackContext    callback id for optional progress reports
 *
 * args[2] fileKey       Name of file request parameter
 * args[3] fileName      File name to be used on server
 * args[4] mimeType      Describes file content type
 * args[5] params        key:value pairs of user-defined parameters
 * @return FileUploadResult containing result of upload request
 */
private void upload(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException {
    Log.d(LOG_TAG, "upload " + source + " to " + target);
    // Setup the options
    final String fileKey = getArgument(args, 2, "file");
    final String fileName = getArgument(args, 3, "image.jpg");
    final String mimeType = getArgument(args, 4, "image/jpeg");
    final JSONObject params = args.optJSONObject(5) == null ? new JSONObject() : args.optJSONObject(5);
    final boolean trustEveryone = args.optBoolean(6);
    // Always use chunked mode unless set to false as per API
    final boolean chunkedMode = args.optBoolean(7) || args.isNull(7);
    // Look for headers on the params map for backwards compatibility with older Cordova versions.
    final JSONObject headers = args.optJSONObject(8) == null ? params.optJSONObject("headers") : args.optJSONObject(8);
    final String objectId = args.getString(9);
    final String httpMethod = getArgument(args, 10, "POST");
    final CordovaResourceApi resourceApi = webView.getResourceApi();
    Log.d(LOG_TAG, "fileKey: " + fileKey);
    Log.d(LOG_TAG, "fileName: " + fileName);
    Log.d(LOG_TAG, "mimeType: " + mimeType);
    Log.d(LOG_TAG, "params: " + params);
    Log.d(LOG_TAG, "trustEveryone: " + trustEveryone);
    Log.d(LOG_TAG, "chunkedMode: " + chunkedMode);
    Log.d(LOG_TAG, "headers: " + headers);
    Log.d(LOG_TAG, "objectId: " + objectId);
    Log.d(LOG_TAG, "httpMethod: " + httpMethod);
    final Uri targetUri = resourceApi.remapUri(Uri.parse(target));
    // Accept a path or a URI for the source.
    Uri tmpSrc = Uri.parse(source);
    final Uri sourceUri = resourceApi.remapUri(tmpSrc.getScheme() != null ? tmpSrc : Uri.fromFile(new File(source)));
    int uriType = CordovaResourceApi.getUriType(targetUri);
    final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS;
    if (uriType != CordovaResourceApi.URI_TYPE_HTTP && !useHttps) {
        JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target, null, 0, null);
        Log.e(LOG_TAG, "Unsupported URI: " + targetUri);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
        return;
    }
    final RequestContext context = new RequestContext(source, target, callbackContext);
    synchronized (activeRequests) {
        activeRequests.put(objectId, context);
    }
    cordova.getThreadPool().execute(new Runnable() {

        public void run() {
            if (context.aborted) {
                return;
            }
            HttpURLConnection conn = null;
            HostnameVerifier oldHostnameVerifier = null;
            SSLSocketFactory oldSocketFactory = null;
            int totalBytes = 0;
            int fixedLength = -1;
            try {
                // Create return object
                FileUploadResult result = new FileUploadResult();
                FileProgressResult progress = new FileProgressResult();
                // ------------------ CLIENT REQUEST
                // Open a HTTP connection to the URL based on protocol
                conn = resourceApi.createHttpConnection(targetUri);
                if (useHttps && trustEveryone) {
                    // Setup the HTTPS connection class to trust everyone
                    HttpsURLConnection https = (HttpsURLConnection) conn;
                    oldSocketFactory = trustAllHosts(https);
                    // Save the current hostnameVerifier
                    oldHostnameVerifier = https.getHostnameVerifier();
                    // Setup the connection not to verify hostnames
                    https.setHostnameVerifier(DO_NOT_VERIFY);
                }
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                conn.setRequestMethod(httpMethod);
                conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
                // Set the cookies on the response
                String cookie = CookieManager.getInstance().getCookie(target);
                if (cookie != null) {
                    conn.setRequestProperty("Cookie", cookie);
                }
                // Handle the other headers
                if (headers != null) {
                    addHeadersToRequest(conn, headers);
                }
                /*
                        * Store the non-file portions of the multipart data as a string, so that we can add it
                        * to the contentSize, since it is part of the body of the HTTP request.
                        */
                StringBuilder beforeData = new StringBuilder();
                try {
                    for (Iterator<?> iter = params.keys(); iter.hasNext(); ) {
                        Object key = iter.next();
                        if (!String.valueOf(key).equals("headers")) {
                            beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
                            beforeData.append("Content-Disposition: form-data; name=\"").append(key.toString()).append('"');
                            beforeData.append(LINE_END).append(LINE_END);
                            beforeData.append(params.getString(key.toString()));
                            beforeData.append(LINE_END);
                        }
                    }
                } catch (JSONException e) {
                    Log.e(LOG_TAG, e.getMessage(), e);
                }
                beforeData.append(LINE_START).append(BOUNDARY).append(LINE_END);
                beforeData.append("Content-Disposition: form-data; name=\"").append(fileKey).append("\";");
                beforeData.append(" filename=\"").append(fileName).append('"').append(LINE_END);
                beforeData.append("Content-Type: ").append(mimeType).append(LINE_END).append(LINE_END);
                byte[] beforeDataBytes = beforeData.toString().getBytes("UTF-8");
                byte[] tailParamsBytes = (LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END).getBytes("UTF-8");
                // Get a input stream of the file on the phone
                OpenForReadResult readResult = resourceApi.openForRead(sourceUri);
                int stringLength = beforeDataBytes.length + tailParamsBytes.length;
                if (readResult.length >= 0) {
                    fixedLength = (int) readResult.length + stringLength;
                    progress.setLengthComputable(true);
                    progress.setTotal(fixedLength);
                }
                Log.d(LOG_TAG, "Content Length: " + fixedLength);
                // setFixedLengthStreamingMode causes and OutOfMemoryException on pre-Froyo devices.
                // http://code.google.com/p/android/issues/detail?id=3164
                // It also causes OOM if HTTPS is used, even on newer devices.
                boolean useChunkedMode = chunkedMode && (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO || useHttps);
                useChunkedMode = useChunkedMode || (fixedLength == -1);
                if (useChunkedMode) {
                    conn.setChunkedStreamingMode(MAX_BUFFER_SIZE);
                    // Although setChunkedStreamingMode sets this header, setting it explicitly here works
                    // around an OutOfMemoryException when using https.
                    conn.setRequestProperty("Transfer-Encoding", "chunked");
                } else {
                    conn.setFixedLengthStreamingMode(fixedLength);
                }
                conn.connect();
                OutputStream sendStream = null;
                try {
                    sendStream = conn.getOutputStream();
                    synchronized (context) {
                        if (context.aborted) {
                            return;
                        }
                        context.connection = conn;
                    }
                    // We don't want to change encoding, we just want this to write for all Unicode.
                    sendStream.write(beforeDataBytes);
                    totalBytes += beforeDataBytes.length;
                    // create a buffer of maximum size
                    int bytesAvailable = readResult.inputStream.available();
                    int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
                    byte[] buffer = new byte[bufferSize];
                    // read file and write it into form...
                    int bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
                    long prevBytesRead = 0;
                    while (bytesRead > 0) {
                        result.setBytesSent(totalBytes);
                        sendStream.write(buffer, 0, bytesRead);
                        totalBytes += bytesRead;
                        if (totalBytes > prevBytesRead + 102400) {
                            prevBytesRead = totalBytes;
                            Log.d(LOG_TAG, "Uploaded " + totalBytes + " of " + fixedLength + " bytes");
                        }
                        bytesAvailable = readResult.inputStream.available();
                        bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
                        bytesRead = readResult.inputStream.read(buffer, 0, bufferSize);
                        // Send a progress event.
                        progress.setLoaded(totalBytes);
                        PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
                        progressResult.setKeepCallback(true);
                        context.sendPluginResult(progressResult);
                    }
                    // send multipart form data necessary after file data...
                    sendStream.write(tailParamsBytes);
                    totalBytes += tailParamsBytes.length;
                    sendStream.flush();
                } finally {
                    safeClose(readResult.inputStream);
                    safeClose(sendStream);
                }
                synchronized (context) {
                    context.connection = null;
                }
                Log.d(LOG_TAG, "Sent " + totalBytes + " of " + fixedLength);
                // ------------------ read the SERVER RESPONSE
                String responseString;
                int responseCode = conn.getResponseCode();
                Log.d(LOG_TAG, "response code: " + responseCode);
                Log.d(LOG_TAG, "response headers: " + conn.getHeaderFields());
                TrackingInputStream inStream = null;
                try {
                    inStream = getInputStream(conn);
                    synchronized (context) {
                        if (context.aborted) {
                            return;
                        }
                        context.connection = conn;
                    }
                    ByteArrayOutputStream out = new ByteArrayOutputStream(Math.max(1024, conn.getContentLength()));
                    byte[] buffer = new byte[1024];
                    int bytesRead = 0;
                    // write bytes to file
                    while ((bytesRead = inStream.read(buffer)) > 0) {
                        out.write(buffer, 0, bytesRead);
                    }
                    responseString = out.toString("UTF-8");
                } finally {
                    synchronized (context) {
                        context.connection = null;
                    }
                    safeClose(inStream);
                }
                Log.d(LOG_TAG, "got response from server");
                Log.d(LOG_TAG, responseString.substring(0, Math.min(256, responseString.length())));
                // send request and retrieve response
                result.setResponseCode(responseCode);
                result.setResponse(responseString);
                context.sendPluginResult(new PluginResult(PluginResult.Status.OK, result.toJSONObject()));
            } catch (FileNotFoundException e) {
                JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, conn, e);
                Log.e(LOG_TAG, error.toString(), e);
                context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
            } catch (IOException e) {
                JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, conn, e);
                Log.e(LOG_TAG, error.toString(), e);
                Log.e(LOG_TAG, "Failed after uploading " + totalBytes + " of " + fixedLength + " bytes.");
                context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
            } catch (JSONException e) {
                Log.e(LOG_TAG, e.getMessage(), e);
                context.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
            } catch (Throwable t) {
                // Shouldn't happen, but will
                JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, conn, t);
                Log.e(LOG_TAG, error.toString(), t);
                context.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, error));
            } finally {
                synchronized (activeRequests) {
                    activeRequests.remove(objectId);
                }
                if (conn != null) {
                    // Revert back to the proper verifier and socket factories
                    if (trustEveryone && useHttps) {
                        HttpsURLConnection https = (HttpsURLConnection) conn;
                        https.setHostnameVerifier(oldHostnameVerifier);
                        https.setSSLSocketFactory(oldSocketFactory);
                    }
                }
            }
        }
    });
}
Also used : PluginResult(org.apache.cordova.PluginResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) HttpURLConnection(java.net.HttpURLConnection) Iterator(java.util.Iterator) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) JSONException(org.json.JSONException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CordovaResourceApi(org.apache.cordova.CordovaResourceApi) HostnameVerifier(javax.net.ssl.HostnameVerifier) JSONObject(org.json.JSONObject) OpenForReadResult(org.apache.cordova.CordovaResourceApi.OpenForReadResult) JSONObject(org.json.JSONObject) File(java.io.File) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 57 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

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, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
    if (action.equals("open")) {
        this.callbackContext = callbackContext;
        final String url = args.getString(0);
        String t = args.optString(1);
        if (t == null || t.equals("") || t.equals(NULL)) {
            t = SELF;
        }
        final String target = t;
        final HashMap<String, Boolean> features = parseFeature(args.optString(2));
        Log.d(LOG_TAG, "target = " + target);
        this.cordova.getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                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)) {
                        Log.d(LOG_TAG, "loading in webview");
                        webView.loadUrl(url);
                    } else // Load the dialer
                    if (url.startsWith(WebView.SCHEME_TEL)) {
                        try {
                            Log.d(LOG_TAG, "loading in dialer");
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            intent.setData(Uri.parse(url));
                            cordova.getActivity().startActivity(intent);
                        } catch (android.content.ActivityNotFoundException e) {
                            LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
                        }
                    } else // load in InAppBrowser
                    {
                        Log.d(LOG_TAG, "loading in InAppBrowser");
                        result = showWebPage(url, features);
                    }
                } else // SYSTEM
                if (SYSTEM.equals(target)) {
                    Log.d(LOG_TAG, "in system");
                    result = openExternal(url);
                } else // BLANK - or anything else
                {
                    Log.d(LOG_TAG, "in blank");
                    result = showWebPage(url, features);
                }
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
                pluginResult.setKeepCallback(true);
                callbackContext.sendPluginResult(pluginResult);
            }
        });
    } else if (action.equals("close")) {
        closeDialog();
    } 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")) {
        this.cordova.getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                dialog.show();
            }
        });
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
        pluginResult.setKeepCallback(true);
        this.callbackContext.sendPluginResult(pluginResult);
    } else {
        return false;
    }
    return true;
}
Also used : PluginResult(org.apache.cordova.PluginResult) Intent(android.content.Intent)

Example 58 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class Capture method onActivityResult.

/**
 * Called when the video view exits.
 *
 * @param requestCode       The request code originally supplied to startActivityForResult(),
 *                          allowing you to identify who this result came from.
 * @param resultCode        The integer result code returned by the child activity through its setResult().
 * @param intent            An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
 * @throws JSONException
 */
public void onActivityResult(int requestCode, int resultCode, final Intent intent) {
    // Result received okay
    if (resultCode == Activity.RESULT_OK) {
        // An audio clip was requested
        if (requestCode == CAPTURE_AUDIO) {
            final Capture that = this;
            Runnable captureAudio = new Runnable() {

                @Override
                public void run() {
                    // Get the uri of the audio clip
                    Uri data = intent.getData();
                    // create a file object from the uri
                    results.put(createMediaFile(data));
                    if (results.length() >= limit) {
                        // Send Uri back to JavaScript for listening to audio
                        that.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
                    } else {
                        // still need to capture more audio clips
                        captureAudio();
                    }
                }
            };
            this.cordova.getThreadPool().execute(captureAudio);
        } else if (requestCode == CAPTURE_IMAGE) {
            // For some reason if I try to do:
            // Uri data = intent.getData();
            // It crashes in the emulator and on my phone with a null pointer exception
            // To work around it I had to grab the code from CameraLauncher.java
            final Capture that = this;
            Runnable captureImage = new Runnable() {

                @Override
                public void run() {
                    try {
                        // TODO Auto-generated method stub
                        // Create entry in media store for image
                        // (Don't use insertImage() because it uses default compression setting of 50 - no way to change it)
                        ContentValues values = new ContentValues();
                        values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, IMAGE_JPEG);
                        Uri uri = null;
                        try {
                            uri = that.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                        } catch (UnsupportedOperationException e) {
                            LOG.d(LOG_TAG, "Can't write to external media storage.");
                            try {
                                uri = that.cordova.getActivity().getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
                            } catch (UnsupportedOperationException ex) {
                                LOG.d(LOG_TAG, "Can't write to internal media storage.");
                                that.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image - no media storage found."));
                                return;
                            }
                        }
                        FileInputStream fis = new FileInputStream(getTempDirectoryPath() + "/Capture.jpg");
                        OutputStream os = that.cordova.getActivity().getContentResolver().openOutputStream(uri);
                        byte[] buffer = new byte[4096];
                        int len;
                        while ((len = fis.read(buffer)) != -1) {
                            os.write(buffer, 0, len);
                        }
                        os.flush();
                        os.close();
                        fis.close();
                        // Add image to results
                        results.put(createMediaFile(uri));
                        checkForDuplicateImage();
                        if (results.length() >= limit) {
                            // Send Uri back to JavaScript for viewing image
                            that.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
                        } else {
                            // still need to capture more images
                            captureImage();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        that.fail(createErrorObject(CAPTURE_INTERNAL_ERR, "Error capturing image."));
                    }
                }
            };
            this.cordova.getThreadPool().execute(captureImage);
        } else if (requestCode == CAPTURE_VIDEO) {
            final Capture that = this;
            Runnable captureVideo = new Runnable() {

                @Override
                public void run() {
                    Uri data = null;
                    if (intent != null) {
                        // Get the uri of the video clip
                        data = intent.getData();
                    }
                    if (data == null) {
                        File movie = new File(getTempDirectoryPath(), "Capture.avi");
                        data = Uri.fromFile(movie);
                    }
                    // create a file object from the uri
                    if (data == null) {
                        that.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Error: data is null"));
                    } else {
                        results.put(createMediaFile(data));
                        if (results.length() >= limit) {
                            // Send Uri back to JavaScript for viewing video
                            that.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
                        } else {
                            // still need to capture more video clips
                            captureVideo(duration);
                        }
                    }
                }
            };
            this.cordova.getThreadPool().execute(captureVideo);
        }
    } else // If canceled
    if (resultCode == Activity.RESULT_CANCELED) {
        // If we have partial results send them back to the user
        if (results.length() > 0) {
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
        } else // user canceled the action
        {
            this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Canceled."));
        }
    } else // If something else
    {
        // If we have partial results send them back to the user
        if (results.length() > 0) {
            this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, results));
        } else // something bad happened
        {
            this.fail(createErrorObject(CAPTURE_NO_MEDIA_FILES, "Did not complete!"));
        }
    }
}
Also used : ContentValues(android.content.ContentValues) PluginResult(org.apache.cordova.PluginResult) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Uri(android.net.Uri) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 59 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class NetworkManager method execute.

/**
 * 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;
}
Also used : PluginResult(org.apache.cordova.PluginResult) NetworkInfo(android.net.NetworkInfo) JSONException(org.json.JSONException)

Example 60 with PluginResult

use of org.apache.cordova.PluginResult in project jpHolo by teusink.

the class Appstore method execute.

@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) {
    cordova.getThreadPool().execute(new Runnable() {

        @Override
        public void run() {
            try {
                if (action.equals("show")) {
                    final JSONObject jo = args.getJSONObject(0);
                    final String appstoreLink = jo.getString("link");
                    final String appstoreType = jo.getString("type");
                    final Intent intent = new Intent(Intent.ACTION_VIEW);
                    if (appInstalledOrNot("com.amazon.venezia") == true) {
                        if (appstoreType.equals("app")) {
                            // org.teusink.droidpapers
                            intent.setData(Uri.parse("amzn://apps/android?p=" + appstoreLink));
                        } else if (appstoreType.equals("pub")) {
                            // Teusink.org
                            intent.setData(Uri.parse("amzn://apps/android?s=" + appstoreLink));
                        }
                    } else if (appInstalledOrNot("com.android.vending") == true) {
                        if (appstoreType.equals("app")) {
                            // org.teusink.droidpapers
                            intent.setData(Uri.parse("market://details?id=" + appstoreLink));
                        } else if (appstoreType.equals("pub")) {
                            // Teusink.org
                            intent.setData(Uri.parse("market://search?q=pub:" + appstoreLink));
                        }
                    } else {
                        if (appstoreType.equals("app")) {
                            // org.teusink.droidpapers
                            // intent.setData(Uri.parse("https://play.google.com/store/apps/details?id="
                            // + appstoreLink));
                            intent.setData(Uri.parse("http://droidpapers.teusink.org/about.php"));
                        } else if (appstoreType.equals("pub")) {
                            // Teusink.org
                            // intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id="
                            // + appstoreLink));
                            intent.setData(Uri.parse("http://droidpapers.teusink.org/about.php"));
                        }
                    }
                    try {
                        cordova.getActivity().startActivityForResult(intent, 0);
                    } catch (final ActivityNotFoundException e) {
                        Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.ERROR);
                        e.printStackTrace();
                        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR));
                    }
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
                } else if (action.equals("check")) {
                    String appstore = "unknown";
                    if (appInstalledOrNot("com.amazon.venezia")) {
                        appstore = "amazon";
                    } else if (appInstalledOrNot("com.android.vending")) {
                        appstore = "google";
                    }
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, appstore));
                } else {
                    Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
                }
            } catch (final JSONException e) {
                Log.e(LOG_PROV, LOG_NAME + "Error: " + PluginResult.Status.JSON_EXCEPTION);
                e.printStackTrace();
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
            }
        }
    });
    return true;
}
Also used : JSONObject(org.json.JSONObject) PluginResult(org.apache.cordova.PluginResult) ActivityNotFoundException(android.content.ActivityNotFoundException) JSONException(org.json.JSONException) Intent(android.content.Intent)

Aggregations

PluginResult (org.apache.cordova.PluginResult)68 JSONException (org.json.JSONException)23 JSONObject (org.json.JSONObject)21 JSONArray (org.json.JSONArray)14 IOException (java.io.IOException)8 NativeToJsMessageQueue (org.apache.cordova.NativeToJsMessageQueue)6 Test (org.junit.Test)6 Uri (android.net.Uri)4 FileNotFoundException (java.io.FileNotFoundException)4 AlertDialog (android.app.AlertDialog)3 DialogInterface (android.content.DialogInterface)3 Intent (android.content.Intent)3 TextView (android.widget.TextView)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 CallbackContext (org.apache.cordova.CallbackContext)3 CordovaResourceApi (org.apache.cordova.CordovaResourceApi)3 SharedPreferences (android.content.SharedPreferences)2