Return-Path: X-Original-To: apmail-cordova-commits-archive@www.apache.org Delivered-To: apmail-cordova-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 14DD3F7EA for ; Tue, 30 Apr 2013 21:35:21 +0000 (UTC) Received: (qmail 26058 invoked by uid 500); 30 Apr 2013 21:35:19 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 25970 invoked by uid 500); 30 Apr 2013 21:35:19 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@cordova.apache.org Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 25314 invoked by uid 99); 30 Apr 2013 21:35:18 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 30 Apr 2013 21:35:18 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 948EC88499D; Tue, 30 Apr 2013 21:35:18 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: steven@apache.org To: commits@cordova.apache.org Date: Tue, 30 Apr 2013 21:35:41 -0000 Message-Id: <6d9d208cf88642a5bcbdc0b675e7bebd@git.apache.org> In-Reply-To: <0ac98d5cd85a4468b21219edb0ba5f71@git.apache.org> References: <0ac98d5cd85a4468b21219edb0ba5f71@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [25/53] android commit: Accidentally copied non-plugin Cordova files in core, whoops Accidentally copied non-plugin Cordova files in core, whoops Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/f3c224ea Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/f3c224ea Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/f3c224ea Branch: refs/heads/3.0.0 Commit: f3c224ea77f89fdf39f2c94481329808d4fbf73b Parents: 904d158 Author: Joe Bowser Authored: Wed Apr 3 14:51:37 2013 -0700 Committer: Joe Bowser Committed: Wed Apr 3 14:51:37 2013 -0700 ---------------------------------------------------------------------- .../src/org/apache/cordova/core/ExposedJsApi.java | 65 -- .../cordova/core/NativeToJsMessageQueue.java | 485 --------------- .../src/org/apache/cordova/core/StandAlone.java | 33 - 3 files changed, 0 insertions(+), 583 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-android/blob/f3c224ea/framework/src/org/apache/cordova/core/ExposedJsApi.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/core/ExposedJsApi.java b/framework/src/org/apache/cordova/core/ExposedJsApi.java deleted file mode 100755 index f903877..0000000 --- a/framework/src/org/apache/cordova/core/ExposedJsApi.java +++ /dev/null @@ -1,65 +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 org.apache.cordova.core; - -import android.webkit.JavascriptInterface; - -import org.apache.cordova.api.PluginManager; -import org.json.JSONException; - -/** - * Contains APIs that the JS can call. All functions in here should also have - * an equivalent entry in CordovaChromeClient.java, and be added to - * cordova-js/lib/android/plugin/android/promptbasednativeapi.js - */ -/* package */ class ExposedJsApi { - - private PluginManager pluginManager; - private NativeToJsMessageQueue jsMessageQueue; - - public ExposedJsApi(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue) { - this.pluginManager = pluginManager; - this.jsMessageQueue = jsMessageQueue; - } - - @JavascriptInterface - public String exec(String service, String action, String callbackId, String arguments) throws JSONException { - jsMessageQueue.setPaused(true); - try { - boolean wasSync = pluginManager.exec(service, action, callbackId, arguments); - String ret = ""; - if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING || wasSync) { - ret = jsMessageQueue.popAndEncode(); - } - return ret; - } finally { - jsMessageQueue.setPaused(false); - } - } - - @JavascriptInterface - public void setNativeToJsBridgeMode(int value) { - jsMessageQueue.setBridgeMode(value); - } - - @JavascriptInterface - public String retrieveJsMessages() { - return jsMessageQueue.popAndEncode(); - } -} http://git-wip-us.apache.org/repos/asf/cordova-android/blob/f3c224ea/framework/src/org/apache/cordova/core/NativeToJsMessageQueue.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/core/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/core/NativeToJsMessageQueue.java deleted file mode 100755 index 4ed2c82..0000000 --- a/framework/src/org/apache/cordova/core/NativeToJsMessageQueue.java +++ /dev/null @@ -1,485 +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 org.apache.cordova.core; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.LinkedList; - -import org.apache.cordova.CordovaWebView; -import org.apache.cordova.api.CordovaInterface; -import org.apache.cordova.api.PluginResult; - -import android.os.Message; -import android.util.Log; -import android.webkit.WebView; - -/** - * Holds the list of messages to be sent to the WebView. - */ -public class NativeToJsMessageQueue { - private static final String LOG_TAG = "JsMessageQueue"; - - // This must match the default value in incubator-cordova-js/lib/android/exec.js - private static final int DEFAULT_BRIDGE_MODE = 2; - - // Set this to true to force plugin results to be encoding as - // JS instead of the custom format (useful for benchmarking). - private static final boolean FORCE_ENCODE_USING_EVAL = false; - - // Disable URL-based exec() bridge by default since it's a bit of a - // security concern. - static final boolean ENABLE_LOCATION_CHANGE_EXEC_MODE = false; - - // Disable sending back native->JS messages during an exec() when the active - // exec() is asynchronous. Set this to true when running bridge benchmarks. - static final boolean DISABLE_EXEC_CHAINING = false; - - // Upper limit for how much data to send to JS in one shot. - // TODO(agrieve): This is currently disable. It should be re-enabled once we - // remove support for returning values from exec() calls. This was - // deprecated in 2.2.0. - // Also, this currently only chops up on message boundaries. It may be useful - // to allow it to break up messages. - private static int MAX_PAYLOAD_SIZE = -1; //50 * 1024 * 10240; - - /** - * The index into registeredListeners to treat as active. - */ - private int activeListenerIndex; - - /** - * When true, the active listener is not fired upon enqueue. When set to false, - * the active listener will be fired if the queue is non-empty. - */ - private boolean paused; - - /** - * The list of JavaScript statements to be sent to JavaScript. - */ - private final LinkedList queue = new LinkedList(); - - /** - * The array of listeners that can be used to send messages to JS. - */ - private final BridgeMode[] registeredListeners; - - private final CordovaInterface cordova; - private final CordovaWebView webView; - - public NativeToJsMessageQueue(CordovaWebView webView, CordovaInterface cordova) { - this.cordova = cordova; - this.webView = webView; - registeredListeners = new BridgeMode[4]; - registeredListeners[0] = null; // Polling. Requires no logic. - registeredListeners[1] = new LoadUrlBridgeMode(); - registeredListeners[2] = new OnlineEventsBridgeMode(); - registeredListeners[3] = new PrivateApiBridgeMode(); - reset(); - } - - /** - * Changes the bridge mode. - */ - public void setBridgeMode(int value) { - if (value < 0 || value >= registeredListeners.length) { - Log.d(LOG_TAG, "Invalid NativeToJsBridgeMode: " + value); - } else { - if (value != activeListenerIndex) { - Log.d(LOG_TAG, "Set native->JS mode to " + value); - synchronized (this) { - activeListenerIndex = value; - BridgeMode activeListener = registeredListeners[value]; - if (!paused && !queue.isEmpty() && activeListener != null) { - activeListener.onNativeToJsMessageAvailable(); - } - } - } - } - } - - /** - * Clears all messages and resets to the default bridge mode. - */ - public void reset() { - synchronized (this) { - queue.clear(); - setBridgeMode(DEFAULT_BRIDGE_MODE); - } - } - - private int calculatePackedMessageLength(JsMessage message) { - int messageLen = message.calculateEncodedLength(); - String messageLenStr = String.valueOf(messageLen); - return messageLenStr.length() + messageLen + 1; - } - - private void packMessage(JsMessage message, StringBuilder sb) { - int len = message.calculateEncodedLength(); - sb.append(len) - .append(' '); - message.encodeAsMessage(sb); - } - - /** - * Combines and returns queued messages combined into a single string. - * Combines as many messages as possible, while staying under MAX_PAYLOAD_SIZE. - * Returns null if the queue is empty. - */ - public String popAndEncode() { - synchronized (this) { - if (queue.isEmpty()) { - return null; - } - int totalPayloadLen = 0; - int numMessagesToSend = 0; - for (JsMessage message : queue) { - int messageSize = calculatePackedMessageLength(message); - if (numMessagesToSend > 0 && totalPayloadLen + messageSize > MAX_PAYLOAD_SIZE && MAX_PAYLOAD_SIZE > 0) { - break; - } - totalPayloadLen += messageSize; - numMessagesToSend += 1; - } - - StringBuilder sb = new StringBuilder(totalPayloadLen); - for (int i = 0; i < numMessagesToSend; ++i) { - JsMessage message = queue.removeFirst(); - packMessage(message, sb); - } - - if (!queue.isEmpty()) { - // Attach a char to indicate that there are more messages pending. - sb.append('*'); - } - String ret = sb.toString(); - return ret; - } - } - - /** - * Same as popAndEncode(), except encodes in a form that can be executed as JS. - */ - private String popAndEncodeAsJs() { - synchronized (this) { - int length = queue.size(); - if (length == 0) { - return null; - } - int totalPayloadLen = 0; - int numMessagesToSend = 0; - for (JsMessage message : queue) { - int messageSize = message.calculateEncodedLength() + 50; // overestimate. - if (numMessagesToSend > 0 && totalPayloadLen + messageSize > MAX_PAYLOAD_SIZE && MAX_PAYLOAD_SIZE > 0) { - break; - } - totalPayloadLen += messageSize; - numMessagesToSend += 1; - } - boolean willSendAllMessages = numMessagesToSend == queue.size(); - StringBuilder sb = new StringBuilder(totalPayloadLen + (willSendAllMessages ? 0 : 100)); - // Wrap each statement in a try/finally so that if one throws it does - // not affect the next. - for (int i = 0; i < numMessagesToSend; ++i) { - JsMessage message = queue.removeFirst(); - if (willSendAllMessages && (i + 1 == numMessagesToSend)) { - message.encodeAsJsMessage(sb); - } else { - sb.append("try{"); - message.encodeAsJsMessage(sb); - sb.append("}finally{"); - } - } - if (!willSendAllMessages) { - sb.append("window.setTimeout(function(){cordova.require('cordova/plugin/android/polling').pollOnce();},0);"); - } - for (int i = willSendAllMessages ? 1 : 0; i < numMessagesToSend; ++i) { - sb.append('}'); - } - String ret = sb.toString(); - return ret; - } - } - - /** - * Add a JavaScript statement to the list. - */ - public void addJavaScript(String statement) { - enqueueMessage(new JsMessage(statement)); - } - - /** - * Add a JavaScript statement to the list. - */ - public void addPluginResult(PluginResult result, String callbackId) { - if (callbackId == null) { - Log.e(LOG_TAG, "Got plugin result with no callbackId", new Throwable()); - return; - } - // Don't send anything if there is no result and there is no need to - // clear the callbacks. - boolean noResult = result.getStatus() == PluginResult.Status.NO_RESULT.ordinal(); - boolean keepCallback = result.getKeepCallback(); - if (noResult && keepCallback) { - return; - } - JsMessage message = new JsMessage(result, callbackId); - if (FORCE_ENCODE_USING_EVAL) { - StringBuilder sb = new StringBuilder(message.calculateEncodedLength() + 50); - message.encodeAsJsMessage(sb); - message = new JsMessage(sb.toString()); - } - - enqueueMessage(message); - } - - private void enqueueMessage(JsMessage message) { - synchronized (this) { - queue.add(message); - if (!paused && registeredListeners[activeListenerIndex] != null) { - registeredListeners[activeListenerIndex].onNativeToJsMessageAvailable(); - } - } - } - - public void setPaused(boolean value) { - if (paused && value) { - // This should never happen. If a use-case for it comes up, we should - // change pause to be a counter. - Log.e(LOG_TAG, "nested call to setPaused detected.", new Throwable()); - } - paused = value; - if (!value) { - synchronized (this) { - if (!queue.isEmpty() && registeredListeners[activeListenerIndex] != null) { - registeredListeners[activeListenerIndex].onNativeToJsMessageAvailable(); - } - } - } - } - - public boolean getPaused() { - return paused; - } - - private interface BridgeMode { - void onNativeToJsMessageAvailable(); - } - - /** Uses webView.loadUrl("javascript:") to execute messages. */ - private class LoadUrlBridgeMode implements BridgeMode { - final Runnable runnable = new Runnable() { - public void run() { - String js = popAndEncodeAsJs(); - if (js != null) { - webView.loadUrlNow("javascript:" + js); - } - } - }; - - public void onNativeToJsMessageAvailable() { - cordova.getActivity().runOnUiThread(runnable); - } - } - - /** Uses online/offline events to tell the JS when to poll for messages. */ - private class OnlineEventsBridgeMode implements BridgeMode { - boolean online = true; - final Runnable runnable = new Runnable() { - public void run() { - if (!queue.isEmpty()) { - online = !online; - webView.setNetworkAvailable(online); - } - } - }; - OnlineEventsBridgeMode() { - webView.setNetworkAvailable(true); - } - public void onNativeToJsMessageAvailable() { - cordova.getActivity().runOnUiThread(runnable); - } - } - - /** - * Uses Java reflection to access an API that lets us eval JS. - * Requires Android 3.2.4 or above. - */ - private class PrivateApiBridgeMode implements BridgeMode { - // Message added in commit: - // http://omapzoom.org/?p=platform/frameworks/base.git;a=commitdiff;h=9497c5f8c4bc7c47789e5ccde01179abc31ffeb2 - // Which first appeared in 3.2.4ish. - private static final int EXECUTE_JS = 194; - - Method sendMessageMethod; - Object webViewCore; - boolean initFailed; - - @SuppressWarnings("rawtypes") - private void initReflection() { - Object webViewObject = webView; - Class webViewClass = WebView.class; - try { - Field f = webViewClass.getDeclaredField("mProvider"); - f.setAccessible(true); - webViewObject = f.get(webView); - webViewClass = webViewObject.getClass(); - } catch (Throwable e) { - // mProvider is only required on newer Android releases. - } - - try { - Field f = webViewClass.getDeclaredField("mWebViewCore"); - f.setAccessible(true); - webViewCore = f.get(webViewObject); - - if (webViewCore != null) { - sendMessageMethod = webViewCore.getClass().getDeclaredMethod("sendMessage", Message.class); - sendMessageMethod.setAccessible(true); - } - } catch (Throwable e) { - initFailed = true; - Log.e(LOG_TAG, "PrivateApiBridgeMode failed to find the expected APIs.", e); - } - } - - public void onNativeToJsMessageAvailable() { - if (sendMessageMethod == null && !initFailed) { - initReflection(); - } - // webViewCore is lazily initialized, and so may not be available right away. - if (sendMessageMethod != null) { - String js = popAndEncodeAsJs(); - Message execJsMessage = Message.obtain(null, EXECUTE_JS, js); - try { - sendMessageMethod.invoke(webViewCore, execJsMessage); - } catch (Throwable e) { - Log.e(LOG_TAG, "Reflection message bridge failed.", e); - } - } - } - } - private static class JsMessage { - final String jsPayloadOrCallbackId; - final PluginResult pluginResult; - JsMessage(String js) { - if (js == null) { - throw new NullPointerException(); - } - jsPayloadOrCallbackId = js; - pluginResult = null; - } - JsMessage(PluginResult pluginResult, String callbackId) { - if (callbackId == null || pluginResult == null) { - throw new NullPointerException(); - } - jsPayloadOrCallbackId = callbackId; - this.pluginResult = pluginResult; - } - - int calculateEncodedLength() { - if (pluginResult == null) { - return jsPayloadOrCallbackId.length() + 1; - } - int statusLen = String.valueOf(pluginResult.getStatus()).length(); - int ret = 2 + statusLen + 1 + jsPayloadOrCallbackId.length() + 1; - switch (pluginResult.getMessageType()) { - case PluginResult.MESSAGE_TYPE_BOOLEAN: // f or t - case PluginResult.MESSAGE_TYPE_NULL: // N - ret += 1; - break; - case PluginResult.MESSAGE_TYPE_NUMBER: // n - ret += 1 + pluginResult.getMessage().length(); - break; - case PluginResult.MESSAGE_TYPE_STRING: // s - ret += 1 + pluginResult.getStrMessage().length(); - break; - case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: - ret += 1 + pluginResult.getMessage().length(); - break; - case PluginResult.MESSAGE_TYPE_JSON: - default: - ret += pluginResult.getMessage().length(); - } - return ret; - } - - void encodeAsMessage(StringBuilder sb) { - if (pluginResult == null) { - sb.append('J') - .append(jsPayloadOrCallbackId); - return; - } - int status = pluginResult.getStatus(); - boolean noResult = status == PluginResult.Status.NO_RESULT.ordinal(); - boolean resultOk = status == PluginResult.Status.OK.ordinal(); - boolean keepCallback = pluginResult.getKeepCallback(); - - sb.append((noResult || resultOk) ? 'S' : 'F') - .append(keepCallback ? '1' : '0') - .append(status) - .append(' ') - .append(jsPayloadOrCallbackId) - .append(' '); - switch (pluginResult.getMessageType()) { - case PluginResult.MESSAGE_TYPE_BOOLEAN: - sb.append(pluginResult.getMessage().charAt(0)); // t or f. - break; - case PluginResult.MESSAGE_TYPE_NULL: // N - sb.append('N'); - break; - case PluginResult.MESSAGE_TYPE_NUMBER: // n - sb.append('n') - .append(pluginResult.getMessage()); - break; - case PluginResult.MESSAGE_TYPE_STRING: // s - sb.append('s'); - sb.append(pluginResult.getStrMessage()); - break; - case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: - sb.append('A'); - sb.append(pluginResult.getMessage()); - break; - case PluginResult.MESSAGE_TYPE_JSON: - default: - sb.append(pluginResult.getMessage()); // [ or { - } - } - - void encodeAsJsMessage(StringBuilder sb) { - if (pluginResult == null) { - sb.append(jsPayloadOrCallbackId); - } else { - int status = pluginResult.getStatus(); - boolean success = (status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal()); - sb.append("cordova.callbackFromNative('") - .append(jsPayloadOrCallbackId) - .append("',") - .append(success) - .append(",") - .append(status) - .append(",[") - .append(pluginResult.getMessage()) - .append("],") - .append(pluginResult.getKeepCallback()) - .append(");"); - } - } - } -} http://git-wip-us.apache.org/repos/asf/cordova-android/blob/f3c224ea/framework/src/org/apache/cordova/core/StandAlone.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/core/StandAlone.java b/framework/src/org/apache/cordova/core/StandAlone.java deleted file mode 100644 index 0c204ef..0000000 --- a/framework/src/org/apache/cordova/core/StandAlone.java +++ /dev/null @@ -1,33 +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 org.apache.cordova.core; - - -import android.os.Bundle; - -public class StandAlone extends DroidGap { - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - super.loadUrl("file:///android_asset/www/index.html"); - } - -}