mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
merging from callback
This commit is contained in:
commit
18bf4cd94b
@ -95,16 +95,6 @@ App.prototype.exitApp = function() {
|
||||
return PhoneGap.exec(null, null, "App", "exitApp", []);
|
||||
};
|
||||
|
||||
/**
|
||||
* Add entry to approved list of URLs (whitelist) that will be loaded into PhoneGap container instead of default browser.
|
||||
*
|
||||
* @param origin URL regular expression to allow
|
||||
* @param subdomains T=include all subdomains under origin
|
||||
*/
|
||||
App.prototype.addWhiteListEntry = function(origin, subdomains) {
|
||||
return PhoneGap.exec(null, null, "App", "addWhiteListEntry", [origin, subdomains]);
|
||||
};
|
||||
|
||||
PhoneGap.addConstructor(function() {
|
||||
navigator.app = new App();
|
||||
});
|
||||
|
@ -83,6 +83,17 @@ FileTransfer.prototype.upload = function(filePath, server, successCallback, erro
|
||||
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'upload', [filePath, server, fileKey, fileName, mimeType, params, debug, chunkedMode]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Downloads a file form a given URL and saves it to the specified directory.
|
||||
* @param source {String} URL of the server to receive the file
|
||||
* @param target {String} Full path of the file on the device
|
||||
* @param successCallback (Function} Callback to be invoked when upload has completed
|
||||
* @param errorCallback {Function} Callback to be invoked upon error
|
||||
*/
|
||||
FileTransfer.prototype.download = function(source, target, successCallback, errorCallback) {
|
||||
PhoneGap.exec(successCallback, errorCallback, 'FileTransfer', 'download', [source, target]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Options to customize the HTTP request used to upload files.
|
||||
* @constructor
|
||||
|
@ -70,9 +70,6 @@ public class App extends Plugin {
|
||||
else if (action.equals("exitApp")) {
|
||||
this.exitApp();
|
||||
}
|
||||
else if (action.equals("addWhiteListEntry")) {
|
||||
this.addWhiteListEntry(args.getString(0), args.optBoolean(1));
|
||||
}
|
||||
return new PluginResult(status, result);
|
||||
} catch (JSONException e) {
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
||||
@ -198,14 +195,4 @@ public class App extends Plugin {
|
||||
public void exitApp() {
|
||||
((DroidGap)this.ctx).endActivity();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add entry to approved list of URLs (whitelist)
|
||||
*
|
||||
* @param origin URL regular expression to allow
|
||||
* @param subdomains T=include all subdomains under origin
|
||||
*/
|
||||
public void addWhiteListEntry(String origin, boolean subdomains) {
|
||||
((DroidGap)this.ctx).addWhiteListEntry(origin, subdomains);
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,6 @@
|
||||
package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Stack;
|
||||
import java.util.regex.Pattern;
|
||||
@ -30,7 +29,6 @@ import java.io.IOException;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
@ -43,12 +41,10 @@ import android.content.res.Configuration;
|
||||
import android.content.res.XmlResourceParser;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Rect;
|
||||
import android.media.AudioManager;
|
||||
import android.net.Uri;
|
||||
import android.net.http.SslError;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.Menu;
|
||||
@ -275,12 +271,10 @@ public class DroidGap extends PhonegapActivity {
|
||||
ViewGroup.LayoutParams.FILL_PARENT,
|
||||
1.0F));
|
||||
|
||||
WebViewReflect.checkCompatibility();
|
||||
|
||||
this.appView.setWebChromeClient(new GapClient(DroidGap.this));
|
||||
this.setWebViewClient(this.appView, new GapViewClient(this));
|
||||
|
||||
this.appView.setInitialScale(100);
|
||||
this.appView.setInitialScale(0);
|
||||
this.appView.setVerticalScrollBarEnabled(false);
|
||||
this.appView.requestFocusFromTouch();
|
||||
|
||||
@ -299,10 +293,10 @@ public class DroidGap extends PhonegapActivity {
|
||||
settings.setDatabasePath(databasePath);
|
||||
|
||||
// Enable DOM storage
|
||||
WebViewReflect.setDomStorage(settings);
|
||||
settings.setDomStorageEnabled(true);
|
||||
|
||||
// Enable built-in geolocation
|
||||
WebViewReflect.setGeolocationEnabled(settings, true);
|
||||
settings.setGeolocationEnabled(true);
|
||||
|
||||
// Add web view but make it invisible while loading URL
|
||||
this.appView.setVisibility(View.INVISIBLE);
|
||||
@ -311,7 +305,6 @@ public class DroidGap extends PhonegapActivity {
|
||||
|
||||
// Clear cancel flag
|
||||
this.cancelLoadUrl = false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1592,7 +1585,7 @@ public class DroidGap extends PhonegapActivity {
|
||||
// Load URL on UI thread
|
||||
me.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
me.showWebPage(errorUrl, true, true, null);
|
||||
me.showWebPage(errorUrl, false, true, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -1767,7 +1760,7 @@ public class DroidGap extends PhonegapActivity {
|
||||
* @param origin URL regular expression to allow
|
||||
* @param subdomains T=include all subdomains under origin
|
||||
*/
|
||||
public void addWhiteListEntry(String origin, boolean subdomains) {
|
||||
private void addWhiteListEntry(String origin, boolean subdomains) {
|
||||
try {
|
||||
// Unlimited access to network resources
|
||||
if(origin.compareTo("*") == 0) {
|
||||
|
@ -20,8 +20,10 @@ package com.phonegap;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
@ -69,17 +71,19 @@ public class FileTransfer extends Plugin {
|
||||
*/
|
||||
@Override
|
||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||
String file = null;
|
||||
String server = null;
|
||||
String source = null;
|
||||
String target = null;
|
||||
try {
|
||||
file = args.getString(0);
|
||||
server = args.getString(1);
|
||||
source = args.getString(0);
|
||||
target = args.getString(1);
|
||||
}
|
||||
catch (JSONException e) {
|
||||
Log.d(LOG_TAG, "Missing filename or server name");
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing filename or server name");
|
||||
Log.d(LOG_TAG, "Missing source or target");
|
||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION, "Missing source or target");
|
||||
}
|
||||
|
||||
try {
|
||||
if (action.equals("upload")) {
|
||||
// Setup the options
|
||||
String fileKey = null;
|
||||
String fileName = null;
|
||||
@ -88,35 +92,35 @@ public class FileTransfer extends Plugin {
|
||||
fileKey = getArgument(args, 2, "file");
|
||||
fileName = getArgument(args, 3, "image.jpg");
|
||||
mimeType = getArgument(args, 4, "image/jpeg");
|
||||
|
||||
try {
|
||||
JSONObject params = args.optJSONObject(5);
|
||||
boolean trustEveryone = args.optBoolean(6);
|
||||
boolean chunkedMode = args.getBoolean(7);
|
||||
|
||||
if (action.equals("upload")) {
|
||||
FileUploadResult r = upload(file, server, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
|
||||
boolean chunkedMode = args.optBoolean(7);
|
||||
FileUploadResult r = upload(source, target, fileKey, fileName, mimeType, params, trustEveryone, chunkedMode);
|
||||
Log.d(LOG_TAG, "****** About to return a result from upload");
|
||||
return new PluginResult(PluginResult.Status.OK, r.toJSONObject());
|
||||
} else if (action.equals("download")) {
|
||||
JSONObject r = download(source, target);
|
||||
Log.d(LOG_TAG, "****** About to return a result from download");
|
||||
return new PluginResult(PluginResult.Status.OK, r, "window.localFileSystem._castEntry");
|
||||
} else {
|
||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(FILE_NOT_FOUND_ERR);
|
||||
JSONObject error = createFileTransferError(FILE_NOT_FOUND_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (IllegalArgumentException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(INVALID_URL_ERR);
|
||||
JSONObject error = createFileTransferError(INVALID_URL_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (SSLException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
Log.d(LOG_TAG, "Got my ssl exception!!!");
|
||||
JSONObject error = createFileUploadError(CONNECTION_ERR);
|
||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (IOException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
JSONObject error = createFileUploadError(CONNECTION_ERR);
|
||||
JSONObject error = createFileTransferError(CONNECTION_ERR, source, target);
|
||||
return new PluginResult(PluginResult.Status.IO_EXCEPTION, error);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
@ -173,11 +177,13 @@ public class FileTransfer extends Plugin {
|
||||
* @param errorCode the error
|
||||
* @return JSONObject containing the error
|
||||
*/
|
||||
private JSONObject createFileUploadError(int errorCode) {
|
||||
private JSONObject createFileTransferError(int errorCode, String source, String target) {
|
||||
JSONObject error = null;
|
||||
try {
|
||||
error = new JSONObject();
|
||||
error.put("code", errorCode);
|
||||
error.put("source", source);
|
||||
error.put("target", target);
|
||||
} catch (JSONException e) {
|
||||
Log.e(LOG_TAG, e.getMessage(), e);
|
||||
}
|
||||
@ -362,6 +368,54 @@ public class FileTransfer extends Plugin {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return JSONObject the downloaded file
|
||||
*/
|
||||
public JSONObject download(String source, String target) throws IOException {
|
||||
try {
|
||||
File file = new File(target);
|
||||
|
||||
// create needed directories
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
// connect to server
|
||||
URL url = new URL(source);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setDoOutput(true);
|
||||
connection.connect();
|
||||
|
||||
Log.d(LOG_TAG, "Download file:" + url);
|
||||
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead = 0;
|
||||
|
||||
FileOutputStream outputStream = new FileOutputStream(file);
|
||||
|
||||
// write bytes to file
|
||||
while ( (bytesRead = inputStream.read(buffer)) > 0 ) {
|
||||
outputStream.write(buffer,0, bytesRead);
|
||||
}
|
||||
|
||||
outputStream.close();
|
||||
|
||||
Log.d(LOG_TAG, "Saved file: " + target);
|
||||
|
||||
// create FileEntry object
|
||||
FileUtils fileUtil = new FileUtils();
|
||||
|
||||
return fileUtil.getEntry(file);
|
||||
} catch (Exception e) {
|
||||
Log.d(LOG_TAG, e.getMessage(), e);
|
||||
throw new IOException("Error while downloading");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an input stream based on file path or content:// uri
|
||||
*
|
||||
|
@ -329,7 +329,7 @@ public class FileUtils extends Plugin {
|
||||
* @throws EncodingException
|
||||
* @throws JSONException
|
||||
*/
|
||||
private JSONObject transferTo(String fileName, JSONObject newParent, String newName, boolean move) throws JSONException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException {
|
||||
private JSONObject transferTo(String fileName, JSONObject newParent, String newName, boolean move) throws JSONException, FileExistsException, NoModificationAllowedException, IOException, InvalidModificationException, EncodingException {
|
||||
// Check for invalid file name
|
||||
if (newName != null && newName.contains(":")) {
|
||||
throw new EncodingException("Bad file name");
|
||||
@ -528,7 +528,6 @@ public class FileUtils extends Plugin {
|
||||
* @return a DirectoryEntry object
|
||||
* @throws JSONException
|
||||
* @throws IOException
|
||||
* @throws NoModificationAllowedException
|
||||
* @throws InvalidModificationException
|
||||
*/
|
||||
private JSONObject moveDirectory(File srcDir, File destinationDir) throws JSONException, InvalidModificationException {
|
||||
@ -838,7 +837,7 @@ public class FileUtils extends Plugin {
|
||||
* @return
|
||||
* @throws JSONException
|
||||
*/
|
||||
private JSONObject getEntry(File file) throws JSONException {
|
||||
public JSONObject getEntry(File file) throws JSONException {
|
||||
JSONObject entry = new JSONObject();
|
||||
|
||||
entry.put("isFile", file.isFile());
|
||||
|
@ -1,140 +0,0 @@
|
||||
/*
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
package com.phonegap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import android.webkit.WebSettings;
|
||||
|
||||
public class WebViewReflect {
|
||||
private static Method mWebSettings_setDatabaseEnabled;
|
||||
private static Method mWebSettings_setDatabasePath;
|
||||
private static Method mWebSettings_setDomStorageEnabled;
|
||||
private static Method mWebSettings_setGeolocationEnabled;
|
||||
|
||||
static
|
||||
{
|
||||
checkCompatibility();
|
||||
}
|
||||
|
||||
private static void setDatabaseEnabled(boolean e) throws IOException {
|
||||
try
|
||||
{
|
||||
mWebSettings_setDatabaseEnabled.invoke(e);
|
||||
}
|
||||
catch (InvocationTargetException ite) {
|
||||
/* unpack original exception when possible */
|
||||
Throwable cause = ite.getCause();
|
||||
if (cause instanceof IOException) {
|
||||
throw (IOException) cause;
|
||||
} else if (cause instanceof RuntimeException) {
|
||||
throw (RuntimeException) cause;
|
||||
} else if (cause instanceof Error) {
|
||||
throw (Error) cause;
|
||||
} else {
|
||||
/* unexpected checked exception; wrap and re-throw */
|
||||
throw new RuntimeException(ite);
|
||||
}
|
||||
} catch (IllegalAccessException ie) {
|
||||
System.err.println("unexpected " + ie);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void checkCompatibility() {
|
||||
try {
|
||||
mWebSettings_setDatabaseEnabled = WebSettings.class.getMethod(
|
||||
"setDatabaseEnabled", new Class[] { boolean.class } );
|
||||
mWebSettings_setDatabasePath = WebSettings.class.getMethod(
|
||||
"setDatabasePath", new Class[] { String.class });
|
||||
mWebSettings_setDomStorageEnabled = WebSettings.class.getMethod(
|
||||
"setDomStorageEnabled", new Class[] { boolean.class });
|
||||
mWebSettings_setGeolocationEnabled = WebSettings.class.getMethod(
|
||||
"setGeolocationEnabled", new Class[] { boolean.class });
|
||||
/* success, this is a newer device */
|
||||
} catch (NoSuchMethodException nsme) {
|
||||
/* failure, must be older device */
|
||||
}
|
||||
}
|
||||
|
||||
public static void setStorage(WebSettings setting, boolean enable, String path) {
|
||||
if (mWebSettings_setDatabaseEnabled != null) {
|
||||
/* feature is supported */
|
||||
try {
|
||||
mWebSettings_setDatabaseEnabled.invoke(setting, enable);
|
||||
mWebSettings_setDatabasePath.invoke(setting, path);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
/* feature not supported, do something else */
|
||||
}
|
||||
}
|
||||
public static void setGeolocationEnabled(WebSettings setting, boolean enable) {
|
||||
if (mWebSettings_setGeolocationEnabled != null) {
|
||||
/* feature is supported */
|
||||
try {
|
||||
mWebSettings_setGeolocationEnabled.invoke(setting, enable);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
/* feature not supported, do something else */
|
||||
System.out.println("Native Geolocation not supported - we're ok");
|
||||
}
|
||||
}
|
||||
public static void setDomStorage(WebSettings setting)
|
||||
{
|
||||
if(mWebSettings_setDomStorageEnabled != null)
|
||||
{
|
||||
/* feature is supported */
|
||||
try {
|
||||
mWebSettings_setDomStorageEnabled.invoke(setting, true);
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
/* feature not supported, do something else */
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user