List of usage examples for org.apache.cordova PluginManager getPlugin
public CordovaPlugin getPlugin(String service)
From source file:br.com.denguezerocidadao.Capture.java
License:Apache License
/** * Creates a JSONObject that represents a File from the Uri * * @param data the Uri of the audio/image/video * @return a JSONObject that represents a File * @throws IOException//www . j a v a2s . c o m */ private JSONObject createMediaFile(Uri data) { File fp = webView.getResourceApi().mapUriToFile(data); JSONObject obj = new JSONObject(); Class webViewClass = webView.getClass(); PluginManager pm = null; try { Method gpm = webViewClass.getMethod("getPluginManager"); pm = (PluginManager) gpm.invoke(webView); } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } if (pm == null) { try { Field pmf = webViewClass.getField("pluginManager"); pm = (PluginManager) pmf.get(webView); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } } FileUtils filePlugin = (FileUtils) pm.getPlugin("File"); LocalFilesystemURL url = filePlugin.filesystemURLforLocalPath(fp.getAbsolutePath()); try { // File properties obj.put("name", fp.getName()); obj.put("fullPath", fp.toURI().toString()); if (url != null) { obj.put("localURL", url.toString()); } // Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files // are reported as video/3gpp. I'm doing this hacky check of the URI to see if it // is stored in the audio or video content store. if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) { if (data.toString().contains("/audio/")) { obj.put("type", AUDIO_3GPP); } else { obj.put("type", VIDEO_3GPP); } } else { obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova)); } obj.put("lastModifiedDate", fp.lastModified()); obj.put("size", fp.length()); } catch (JSONException e) { // this will never happen e.printStackTrace(); } return obj; }
From source file:com.filetransfer.baidu.bcs.FileTransferBCS.java
License:Apache License
/** * Downloads a file form a given URL and saves it to the specified directory. * * @param source URL of the server to receive the file * @param target Full path of the file on the file system *//*from ww w . j a v a2 s . com*/ private void download(final String source, final String target, JSONArray args, CallbackContext callbackContext) throws JSONException { Log.d(LOG_TAG, "download " + source + " to " + target); final CordovaResourceApi resourceApi = webView.getResourceApi(); final boolean trustEveryone = args.optBoolean(2); final String objectId = args.getString(3); final JSONObject headers = args.optJSONObject(4); final Uri sourceUri = resourceApi.remapUri(Uri.parse(source)); // Accept a path or a URI for the source. Uri tmpTarget = Uri.parse(target); final Uri targetUri = resourceApi .remapUri(tmpTarget.getScheme() != null ? tmpTarget : Uri.fromFile(new File(target))); int uriType = CordovaResourceApi.getUriType(sourceUri); final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS; final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP; if (uriType == CordovaResourceApi.URI_TYPE_UNKNOWN) { 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; } // TODO: refactor to also allow resources & content: if (!isLocalTransfer) { Log.w(LOG_TAG, "Source URL is not in white list: '" + source + "'"); JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, null, 401, null); 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 connection = null; HostnameVerifier oldHostnameVerifier = null; SSLSocketFactory oldSocketFactory = null; File file = null; PluginResult result = null; TrackingInputStream inputStream = null; boolean cached = false; OutputStream outputStream = null; try { OpenForReadResult readResult = null; file = resourceApi.mapUriToFile(targetUri); context.targetFile = file; Log.d(LOG_TAG, "Download file:" + sourceUri); FileProgressBCSResult progress = new FileProgressBCSResult(); if (isLocalTransfer) { readResult = resourceApi.openForRead(sourceUri); if (readResult.length != -1) { progress.setLengthComputable(true); progress.setTotal(readResult.length); } inputStream = new SimpleTrackingInputStream(readResult.inputStream); } else { // connect to server // Open a HTTP connection to the URL based on protocol connection = resourceApi.createHttpConnection(sourceUri); if (useHttps && trustEveryone) { // Setup the HTTPS connection class to trust everyone HttpsURLConnection https = (HttpsURLConnection) connection; oldSocketFactory = trustAllHosts(https); // Save the current hostnameVerifier oldHostnameVerifier = https.getHostnameVerifier(); // Setup the connection not to verify hostnames https.setHostnameVerifier(DO_NOT_VERIFY); } connection.setRequestMethod("GET"); // TODO: Make OkHttp use this CookieManager by default. String cookie = CookieManager.getInstance().getCookie(sourceUri.toString()); if (cookie != null) { connection.setRequestProperty("cookie", cookie); } // This must be explicitly set for gzip progress tracking to work. connection.setRequestProperty("Accept-Encoding", "gzip"); // Handle the other headers if (headers != null) { addHeadersToRequest(connection, headers); } connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { cached = true; connection.disconnect(); Log.d(LOG_TAG, "Resource not modified: " + source); JSONObject error = createFileTransferError(NOT_MODIFIED_ERR, source, target, connection, null); result = new PluginResult(PluginResult.Status.ERROR, error); } else { if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) { // Only trust content-length header if we understand // the encoding -- identity or gzip if (connection.getContentLength() != -1) { progress.setLengthComputable(true); progress.setTotal(connection.getContentLength()); } } inputStream = getInputStream(connection); } } if (!cached) { try { synchronized (context) { if (context.aborted) { return; } context.connection = connection; } // write bytes to file byte[] buffer = new byte[MAX_BUFFER_SIZE]; int bytesRead = 0; outputStream = resourceApi.openOutputStream(targetUri); while ((bytesRead = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, bytesRead); // Send a progress event. progress.setLoaded(inputStream.getTotalRawBytesRead()); PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject()); progressResult.setKeepCallback(true); context.sendPluginResult(progressResult); } } finally { synchronized (context) { context.connection = null; } safeClose(inputStream); safeClose(outputStream); } Log.d(LOG_TAG, "Saved file: " + target); // create FileEntry object Class webViewClass = webView.getClass(); PluginManager pm = null; try { Method gpm = webViewClass.getMethod("getPluginManager"); pm = (PluginManager) gpm.invoke(webView); } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } if (pm == null) { try { Field pmf = webViewClass.getField("pluginManager"); pm = (PluginManager) pmf.get(webView); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } } file = resourceApi.mapUriToFile(targetUri); context.targetFile = file; FileUtils filePlugin = (FileUtils) pm.getPlugin("File"); if (filePlugin != null) { JSONObject fileEntry = filePlugin.getEntryForFile(file); if (fileEntry != null) { result = new PluginResult(PluginResult.Status.OK, fileEntry); } else { JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, null); Log.e(LOG_TAG, "File plugin cannot represent download path"); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } } else { Log.e(LOG_TAG, "File plugin not found; cannot save downloaded file"); result = new PluginResult(PluginResult.Status.ERROR, "File plugin not found; cannot save downloaded file"); } } } catch (FileNotFoundException e) { JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target, connection, e); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } catch (IOException e) { JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, e); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); result = new PluginResult(PluginResult.Status.JSON_EXCEPTION); } catch (Throwable e) { JSONObject error = createFileTransferError(CONNECTION_ERR, source, target, connection, e); Log.e(LOG_TAG, error.toString(), e); result = new PluginResult(PluginResult.Status.IO_EXCEPTION, error); } finally { synchronized (activeRequests) { activeRequests.remove(objectId); } if (connection != null) { // Revert back to the proper verifier and socket factories if (trustEveryone && useHttps) { HttpsURLConnection https = (HttpsURLConnection) connection; https.setHostnameVerifier(oldHostnameVerifier); https.setSSLSocketFactory(oldSocketFactory); } } if (result == null) { result = new PluginResult(PluginResult.Status.ERROR, createFileTransferError(CONNECTION_ERR, source, target, connection, null)); } // Remove incomplete download. if (!cached && result.getStatus() != PluginResult.Status.OK.ordinal() && file != null) { file.delete(); } context.sendPluginResult(result); } } }); }
From source file:com.oe.phonegap.plugins.AutoRecordVideo.java
License:Apache License
/** * Creates a JSONObject that represents a File from the Uri * * @param data the Uri of the audio/image/video * @return a JSONObject that represents a File * @throws IOException/*from www.ja v a 2 s . c om*/ */ private JSONObject createMediaFile(Uri data) { File fp = webView.getResourceApi().mapUriToFile(data); JSONObject obj = new JSONObject(); Class webViewClass = webView.getClass(); PluginManager pm = null; try { Method gpm = webViewClass.getMethod("getPluginManager"); pm = (PluginManager) gpm.invoke(webView); } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } if (pm == null) { try { Field pmf = webViewClass.getField("pluginManager"); pm = (PluginManager) pmf.get(webView); } catch (NoSuchFieldException e) { } catch (IllegalAccessException e) { } } FileUtils filePlugin = (FileUtils) pm.getPlugin("File"); LocalFilesystemURL url = filePlugin.filesystemURLforLocalPath(fp.getAbsolutePath()); try { // File properties obj.put("name", fp.getName()); obj.put("fullPath", fp.toURI().toString()); if (url != null) { obj.put("localURL", url.toString()); } // Because of an issue with MimeTypeMap.getMimeTypeFromExtension() all .3gpp files // are reported as video/3gpp. I'm doing this hacky check of the URI to see if it // is stored in the audio or video content store. if (fp.getAbsoluteFile().toString().endsWith(".3gp") || fp.getAbsoluteFile().toString().endsWith(".3gpp")) { if (data.toString().contains("/audio/")) { obj.put("type", AUDIO_3GPP); } else { obj.put("type", VIDEO_3GPP); } } else { obj.put("type", FileHelper.getMimeType(Uri.fromFile(fp), cordova)); } obj.put("lastModifiedDate", fp.lastModified()); obj.put("size", fp.length()); } catch (JSONException e) { // this will never happen e.printStackTrace(); } return obj; }
From source file:org.chromium.ChromeI18n.java
License:Open Source License
@Override public void pluginInitialize() { PluginManager pm = null; try {/*from www . j a va 2 s .c o m*/ Method gpm = webView.getClass().getMethod("getPluginManager"); pm = (PluginManager) gpm.invoke(webView); } catch (Exception e) { } if (pm == null) { try { Field pmf = webView.getClass().getField("pluginManager"); pm = (PluginManager) pmf.get(webView); } catch (Exception e) { } } chromeExtensionURLsPlugin = (ChromeExtensionURLs) pm.getPlugin("ChromeExtensionURLs"); chromeExtensionURLsPlugin.i18nPlugin = this; // TODO: This should probably be a build step. availableLocales = new ArrayList<String>(); try { File localesDir = new File( webView.getResourceApi().remapUri(Uri.parse("file:///android_asset/www/_locales")).getPath()); String[] localesArr; if (localesDir.getAbsolutePath().startsWith("/android_asset/")) { String assetPath = localesDir.getAbsolutePath().substring("/android_asset/".length()); localesArr = webView.getContext().getAssets().list(assetPath); } else { localesArr = localesDir.list(); } availableLocales.addAll(Arrays.asList(localesArr)); } catch (IOException e) { } }
From source file:org.chromium.SyncFileSystem.java
License:Open Source License
@Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); String syncableRoot = cordova.getActivity().getFilesDir().getAbsolutePath() + "/syncfs/"; new File(syncableRoot).mkdirs(); PluginManager pm = null; try {//from ww w .j a v a 2 s . co m Method gpm = webView.getClass().getMethod("getPluginManager"); pm = (PluginManager) gpm.invoke(webView); } catch (Exception e) { } if (pm == null) { try { Field pmf = webView.getClass().getField("pluginManager"); pm = (PluginManager) pmf.get(webView); } catch (Exception e) { } } FileUtils filePlugin = (FileUtils) pm.getPlugin("File"); LocalFilesystem syncFs = new LocalFilesystem("syncable", cordova, syncableRoot); filePlugin.registerFilesystem(syncFs); }
From source file:org.codaco.networkCanvas.plugin.NetworkCanvasClient.java
License:Apache License
private FileUtils getCdvFilePlugin(CordovaWebView webView) { PluginManager pluginManager = null; try {/*from w w w. ja v a 2 s. c om*/ Method method = webView.getClass().getMethod("getPluginManager"); pluginManager = (PluginManager) method.invoke(webView); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { // try getField } if (pluginManager == null) { try { Field field = webView.getClass().getField("pluginManager"); pluginManager = (PluginManager) field.get(webView); } catch (IllegalAccessException | NoSuchFieldException e) { // Log warning below } } if (pluginManager != null) { return (FileUtils) pluginManager.getPlugin("File"); } LOG.w(LogTag, "Could not get FilePlugin; download interface will not be available"); return null; }