CB-4858 - Run IAB methods on the UI thread.

This commit is contained in:
Andrew Grieve 2013-10-10 12:40:27 -04:00
parent 8a6bc01814
commit 5ef5171003

View File

@ -48,11 +48,11 @@ import android.widget.RelativeLayout;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.Config; import org.apache.cordova.Config;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG; import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -95,29 +95,30 @@ public class InAppBrowser extends CordovaPlugin {
* @param callbackId The callback id used when calling back into JavaScript. * @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("open")) { if (action.equals("open")) {
this.callbackContext = callbackContext; this.callbackContext = callbackContext;
String url = args.getString(0); final String url = args.getString(0);
String target = args.optString(1); String t = args.optString(1);
if (target == null || target.equals("") || target.equals(NULL)) { if (t == null || t.equals("") || t.equals(NULL)) {
target = SELF; t = SELF;
} }
HashMap<String, Boolean> features = parseFeature(args.optString(2)); final String target = t;
final HashMap<String, Boolean> features = parseFeature(args.optString(2));
Log.d(LOG_TAG, "target = " + target); Log.d(LOG_TAG, "target = " + target);
url = updateUrl(url); this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
String result = ""; String result = "";
// SELF // SELF
if (SELF.equals(target)) { if (SELF.equals(target)) {
Log.d(LOG_TAG, "in self"); Log.d(LOG_TAG, "in self");
// load in webview // load in webview
if (url.startsWith("file://") || url.startsWith("javascript:") if (url.startsWith("file://") || url.startsWith("javascript:")
|| Config.isUrlWhiteListed(url)) { || Config.isUrlWhiteListed(url)) {
this.webView.loadUrl(url); webView.loadUrl(url);
} }
//Load the dialer //Load the dialer
else if (url.startsWith(WebView.SCHEME_TEL)) else if (url.startsWith(WebView.SCHEME_TEL))
@ -125,30 +126,32 @@ public class InAppBrowser extends CordovaPlugin {
try { try {
Intent intent = new Intent(Intent.ACTION_DIAL); Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url)); intent.setData(Uri.parse(url));
this.cordova.getActivity().startActivity(intent); cordova.getActivity().startActivity(intent);
} catch (android.content.ActivityNotFoundException e) { } catch (android.content.ActivityNotFoundException e) {
LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString()); LOG.e(LOG_TAG, "Error dialing " + url + ": " + e.toString());
} }
} }
// load in InAppBrowser // load in InAppBrowser
else { else {
result = this.showWebPage(url, features); result = showWebPage(url, features);
} }
} }
// SYSTEM // SYSTEM
else if (SYSTEM.equals(target)) { else if (SYSTEM.equals(target)) {
Log.d(LOG_TAG, "in system"); Log.d(LOG_TAG, "in system");
result = this.openExternal(url); result = openExternal(url);
} }
// BLANK - or anything else // BLANK - or anything else
else { else {
Log.d(LOG_TAG, "in blank"); Log.d(LOG_TAG, "in blank");
result = this.showWebPage(url, features); result = showWebPage(url, features);
} }
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
pluginResult.setKeepCallback(true); pluginResult.setKeepCallback(true);
this.callbackContext.sendPluginResult(pluginResult); callbackContext.sendPluginResult(pluginResult);
}
});
} }
else if (action.equals("close")) { else if (action.equals("close")) {
closeDialog(); closeDialog();
@ -188,13 +191,12 @@ public class InAppBrowser extends CordovaPlugin {
injectDeferredObject(args.getString(0), jsWrapper); injectDeferredObject(args.getString(0), jsWrapper);
} }
else if (action.equals("show")) { else if (action.equals("show")) {
Runnable runnable = new Runnable() { this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
dialog.show(); dialog.show();
} }
}; });
this.cordova.getActivity().runOnUiThread(runnable);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
pluginResult.setKeepCallback(true); pluginResult.setKeepCallback(true);
this.callbackContext.sendPluginResult(pluginResult); this.callbackContext.sendPluginResult(pluginResult);
@ -202,9 +204,6 @@ public class InAppBrowser extends CordovaPlugin {
else { else {
return false; return false;
} }
} catch (JSONException e) {
this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
}
return true; return true;
} }
@ -251,8 +250,14 @@ public class InAppBrowser extends CordovaPlugin {
} else { } else {
scriptToInject = source; scriptToInject = source;
} }
final String finalScriptToInject = scriptToInject;
// This action will have the side-effect of blurring the currently focused element // This action will have the side-effect of blurring the currently focused element
this.inAppWebView.loadUrl("javascript:" + scriptToInject); this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
inAppWebView.loadUrl("javascript:" + finalScriptToInject);
}
});
} }
/** /**
@ -284,20 +289,6 @@ public class InAppBrowser extends CordovaPlugin {
} }
} }
/**
* Convert relative URL to full path
*
* @param url
* @return
*/
private String updateUrl(String url) {
Uri newUrl = Uri.parse(url);
if (newUrl.isRelative()) {
url = this.webView.getUrl().substring(0, this.webView.getUrl().lastIndexOf("/")+1) + url;
}
return url;
}
/** /**
* Display a new browser with the specified URL. * Display a new browser with the specified URL.
* *
@ -328,27 +319,23 @@ public class InAppBrowser extends CordovaPlugin {
if (childView == null) { if (childView == null) {
return; return;
} }
try { this.cordova.getActivity().runOnUiThread(new Runnable() {
Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {
childView.loadUrl("about:blank"); childView.loadUrl("about:blank");
if (dialog != null) {
dialog.dismiss();
} }
}
}; });
try {
this.cordova.getActivity().runOnUiThread(runnable);
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("type", EXIT_EVENT); obj.put("type", EXIT_EVENT);
sendUpdate(obj, false); sendUpdate(obj, false);
} catch (JSONException ex) { } catch (JSONException ex) {
Log.d(LOG_TAG, "Should never happen"); Log.d(LOG_TAG, "Should never happen");
} }
if (dialog != null) {
dialog.dismiss();
}
} }
/** /**