add WebViewDemo sample

This commit is contained in:
Laurent Sansonetti
2014-04-22 23:14:05 +02:00
parent 737f50d95b
commit d0eb3cac36
5 changed files with 83 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
$:.unshift("../../lib")
require 'motion/project/template/android'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'WebViewDemo'
app.main_activity = 'WebViewDemo'
app.api_version = '18'
app.sdk_path = File.expand_path('~/src/android-sdk-macosx')
app.ndk_path = File.expand_path('~/src/android-ndk-r9d')
end

View File

@@ -0,0 +1,51 @@
class WebViewDemo < Android::App::Activity
def onCreate(savedInstanceState)
super
@handler = Android::Os::Handler.new
@webview = Android::Webkit::WebView.new(self)
settings = @webview.getSettings
settings.savePassword = false
settings.saveFormData = false
settings.javaScriptEnabled = true
settings.supportZoom = false
@webview.webChromeClient = MyWebChromeClient.new
js_interface = DemoJavaScriptInterface.new
js_interface.set_context(self)
@webview.addJavascriptInterface(js_interface, "demo")
@webview.loadUrl("file:///android_asset/demo.html")
self.contentView = @webview
end
def webview
@webview
end
def handler
@handler
end
end
class DemoJavaScriptInterface < Java::Lang::Object
def set_context(context)
@context = context
end
__annotation__('@android.webkit.JavascriptInterface')
def clickOnAndroid
# This method will be called from another thread, and WebView calls must
# happen in the main thread, so we dispatch it via a Handler object.
@context.handler.post -> { @context.webview.loadUrl("javascript:wave()") }
end
end
class MyWebChromeClient < Android::Webkit::WebChromeClient
def onJsAlert(view, url, message, result)
puts message
result.confirm
true
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@@ -0,0 +1,20 @@
<html>
<script language="javascript">
/* This function is invoked by the activity */
function wave() {
document.getElementById("droid").src="android_waving.png";
alert("Hello from JavaScript!");
}
</script>
<body>
<!-- Calls into the javascript interface for the activity -->
<a onClick="window.demo.clickOnAndroid()"><div style="width:80px;
margin:0px auto;
padding:10px;
text-align:center;
border:2px solid #202020;" >
<img id="droid" src="android_normal.png"/><br>
Click me!
</div></a>
</body>
</html>