You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2013/10/03 04:45:17 UTC

git commit: CB-4986 Add Android WebSQL plugin.

Updated Branches:
  refs/heads/plugins 750fef0a0 -> 3d26e5d6d


CB-4986 Add Android WebSQL plugin.


Project: http://git-wip-us.apache.org/repos/asf/cordova-labs/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-labs/commit/3d26e5d6
Tree: http://git-wip-us.apache.org/repos/asf/cordova-labs/tree/3d26e5d6
Diff: http://git-wip-us.apache.org/repos/asf/cordova-labs/diff/3d26e5d6

Branch: refs/heads/plugins
Commit: 3d26e5d6d06a2bc0cf9cc43a41bd2e58ee0d3b95
Parents: 750fef0
Author: Andrew Grieve <ag...@chromium.org>
Authored: Wed Oct 2 22:40:40 2013 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Wed Oct 2 22:44:28 2013 -0400

----------------------------------------------------------------------
 websql/README.md         |  8 ++++++++
 websql/WebSQLPlugin.java | 33 ++++++++++++++++++++++++++++++
 websql/android_websql.js | 47 +++++++++++++++++++++++++++++++++++++++++++
 websql/plugin.xml        | 25 +++++++++++++++++++++++
 4 files changed, 113 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/3d26e5d6/websql/README.md
----------------------------------------------------------------------
diff --git a/websql/README.md b/websql/README.md
new file mode 100644
index 0000000..56becaa
--- /dev/null
+++ b/websql/README.md
@@ -0,0 +1,8 @@
+WebSQL Plugin for Android
+-------------------------------
+
+Adds WebSQL support on Android.
+
+* iOS supports WebSQL out of the box.
+* Android supports it as well, but needs a hack (provided by this plugin) for 3.0+
+

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/3d26e5d6/websql/WebSQLPlugin.java
----------------------------------------------------------------------
diff --git a/websql/WebSQLPlugin.java b/websql/WebSQLPlugin.java
new file mode 100644
index 0000000..cf2d126
--- /dev/null
+++ b/websql/WebSQLPlugin.java
@@ -0,0 +1,33 @@
+/*
+       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 org.apache.cordova.websql;
+
+import android.net.Uri;
+
+import org.apache.cordova.CordovaPlugin;
+
+public class WebSQLPlugin extends CordovaPlugin {
+    public Uri remapUri(Uri uri) {
+        if ("websql".equals(uri.getScheme())) {
+            // <html></html>
+            return Uri.parse("data:text/html;base64,PGh0bWw+PC9odG1sPg==");
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/3d26e5d6/websql/android_websql.js
----------------------------------------------------------------------
diff --git a/websql/android_websql.js b/websql/android_websql.js
new file mode 100644
index 0000000..1a1fd1c
--- /dev/null
+++ b/websql/android_websql.js
@@ -0,0 +1,47 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+// WebSQL is broken only for file: URLs.
+if (location.protocol == 'file:') {
+    var androidVersion = /Android (\d+)/.exec(navigator.userAgent);
+    // Don't apply hack for Android 2 since the native side can't override the URL loading.
+    // The database isn't broken on 2 anyways.
+    if (!androidVersion || +androidVersion[1] > 2) {
+        var channel = require('cordova/channel');
+
+        channel.createSticky('onWebSQLReady');
+        channel.waitForInitialization('onWebSQLReady');
+
+        channel.onCordovaReady.subscribe(function() {
+            var ifr = document.createElement('iframe');
+            ifr.src = "websql://foo";
+            ifr.onload = function() {
+                ifr.onload = null;
+                openDatabase = function() {
+                    return ifr.contentWindow.openDatabase.apply(ifr.contentWindow, arguments);
+                };
+                channel.initializationComplete('onWebSQLReady');
+            };
+            ifr.style.display = 'none';
+            document.body.appendChild(ifr);
+        });
+    }
+}

http://git-wip-us.apache.org/repos/asf/cordova-labs/blob/3d26e5d6/websql/plugin.xml
----------------------------------------------------------------------
diff --git a/websql/plugin.xml b/websql/plugin.xml
new file mode 100644
index 0000000..5cfb9b7
--- /dev/null
+++ b/websql/plugin.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
+           id="org.apache.cordova.websql"
+      version="0.1.0">
+
+    <name>Android WebSQL</name>
+
+    <!-- android -->
+    <platform name="android">
+        <js-module src="android_websql.js" name="storage">
+            <runs />
+        </js-module>
+
+        <config-file target="res/xml/config.xml" parent="/*">
+            <access origin="websql://foo" />
+            <feature name="WebSQL">
+                <param name="android-package" value="org.apache.cordova.websql.WebSQLPlugin" />
+                <param name="onload" value="true" />
+            </feature>
+        </config-file>
+
+        <source-file src="WebSQLPlugin.java" target-dir="src/org/apache/cordova/websql" />
+    </platform>
+</plugin>