Unverified Commit 96928efc authored by nitinseshadri's avatar nitinseshadri Committed by GitHub

Add ErrorOccurred event and RunJavaScript method to WebView component (#2070)

parent 4385caa7
......@@ -1616,7 +1616,7 @@ public final class YoungAndroidFormUpgrader {
private static int upgradeWebViewerProperties(Map<String, JSONValue> componentProperties,
int srcCompVersion) {
if (srcCompVersion < 9) {
if (srcCompVersion < 10) {
// The CanGoForward and CanGoBack methods were added.
// No properties need to be modified to upgrade to version 2.
// UsesLocation property added.
......@@ -1628,7 +1628,8 @@ public final class YoungAndroidFormUpgrader {
// WebViewStringChange event was added (version 7)
// PageLoaded event was added (version 8)
// BeforePageLoad event and Stop, Reload, and ClearCookies methods added (version 9)
srcCompVersion = 9;
// ErrorOccurred event and RunJavaScript method added (version 10)
srcCompVersion = 10;
}
return srcCompVersion;
}
......
......@@ -2683,7 +2683,10 @@ Blockly.Versioning.AllUpgradeMaps =
8: "noUpgrade",
// AI2: Added BeforePageLoad event and Stop, Reload, and ClearCookies methods
9: "noUpgrade"
9: "noUpgrade",
// AI2: Added ErrorOccurred event and RunJavaScript method
10: "noUpgrade"
}, // End WebViewer upgraders
......
......@@ -1319,7 +1319,10 @@ public class YaVersion {
// For WEBVIEWER_COMPONENT_VERSION 9:
// - Added BeforePageLoad event
// - Added Stop, Reload, and ClearCookies methods
public static final int WEBVIEWER_COMPONENT_VERSION = 9;
// For WEBVIEWER_COMPONENT_VERSION 10:
// - Added ErrorOccurred event
// - Added RunJavaScript method
public static final int WEBVIEWER_COMPONENT_VERSION = 10;
// For MEDIASTORE_COMPONENT_VERSION 1:
// - Initial Version.
......
......@@ -222,6 +222,15 @@ public final class WebViewer extends AndroidViewComponent {
public void onPageFinished(WebView view, String url) {
PageLoaded(url);
}
@Override
public void onReceivedError(WebView view, final int errorCode, final String description, final String failingUrl) {
container.$form().runOnUiThread(new Runnable() {
public void run() {
ErrorOccurred(errorCode, description, failingUrl);
}
});
}
}
// Components don't normally override Width and Height, but we do it here so that
......@@ -537,6 +546,16 @@ public final class WebViewer extends AndroidViewComponent {
}
}
/**
* Run JavaScript in the current page.
*/
@SimpleFunction(description = "Run JavaScript in the current page.")
public void RunJavaScript(String js) {
// evaluateJavascript() was added in API 19
// and is therefore not used here for compatibility purposes.
webview.loadUrl("javascript:(function(){" + js + "})()");
}
/**
* Event that runs when the `AppInventor.setWebViewString` method is called from JavaScript.
* The new {@link #WebViewString()} is given by the `value`{:.variable.block} parameter.
......@@ -557,6 +576,11 @@ public final class WebViewer extends AndroidViewComponent {
EventDispatcher.dispatchEvent(this, "PageLoaded", url);
}
@SimpleEvent(description = "When an error occurs this event is run.")
public void ErrorOccurred(int errorCode, String description, String failingUrl) {
EventDispatcher.dispatchEvent(this, "ErrorOccurred", errorCode, description, failingUrl);
}
private void loadUrl(final String caller, final String url) {
if (!havePermission && MediaUtil.isExternalFileUrl(url)) {
container.$form().askPermission(Manifest.permission.READ_EXTERNAL_STORAGE,
......
......@@ -153,6 +153,15 @@ public class FroyoUtil {
public void onPageFinished(WebView view, String url) {
EventDispatcher.dispatchEvent(component, "PageLoaded", url);
}
@Override
public void onReceivedError(WebView view, final int errorCode, final String description, final String failingUrl) {
form.runOnUiThread(new Runnable() {
public void run() {
EventDispatcher.dispatchEvent(component, "ErrorOccurred", errorCode, description, failingUrl);
}
});
}
};
}
......
......@@ -1497,6 +1497,8 @@ having dark grey components.</li>
<dl class="events">
<dt id="WebViewer.BeforePageLoad">BeforePageLoad(<em class="text">url</em>)</dt>
<dd>When a page is about to load this event is run.</dd>
<dt id="WebViewer.ErrorOccurred">ErrorOccurred(<em class="number">errorCode</em>,<em class="text">description</em>,<em class="text">failingUrl</em>)</dt>
<dd>When an error occurs this event is run.</dd>
<dt id="WebViewer.PageLoaded">PageLoaded(<em class="text">url</em>)</dt>
<dd>When a page is finished loading this event is run.</dd>
<dt id="WebViewer.WebViewStringChange">WebViewStringChange(<em class="text">value</em>)</dt>
......@@ -1538,6 +1540,8 @@ Eclair, this function is a no-op on older phones.</p>
<dd>Load the page at the given URL.</dd>
<dt id="WebViewer.Reload" class="method"><i></i> Reload()</dt>
<dd>Reload the current page.</dd>
<dt id="WebViewer.RunJavaScript" class="method"><i></i> RunJavaScript(<em class="text">js</em>)</dt>
<dd>Run JavaScript in the current page.</dd>
<dt id="WebViewer.StopLoading" class="method"><i></i> StopLoading()</dt>
<dd>Stop loading a page.</dd>
</dl>
......
......@@ -1705,6 +1705,9 @@ Component for viewing Web pages.
{:id="WebViewer.BeforePageLoad"} BeforePageLoad(*url*{:.text})
: When a page is about to load this event is run.
{:id="WebViewer.ErrorOccurred"} ErrorOccurred(*errorCode*{:.number},*description*{:.text},*failingUrl*{:.text})
: When an error occurs this event is run.
{:id="WebViewer.PageLoaded"} PageLoaded(*url*{:.text})
: When a page is finished loading this event is run.
......@@ -1756,5 +1759,8 @@ Component for viewing Web pages.
{:id="WebViewer.Reload" class="method"} <i/> Reload()
: Reload the current page.
{:id="WebViewer.RunJavaScript" class="method"} <i/> RunJavaScript(*js*{:.text})
: Run JavaScript in the current page.
{:id="WebViewer.StopLoading" class="method"} <i/> StopLoading()
: Stop loading a page.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment