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 EC30E17EC1 for ; Wed, 13 May 2015 02:42:46 +0000 (UTC) Received: (qmail 4023 invoked by uid 500); 13 May 2015 02:42:46 -0000 Delivered-To: apmail-cordova-commits-archive@cordova.apache.org Received: (qmail 3971 invoked by uid 500); 13 May 2015 02:42:46 -0000 Mailing-List: contact commits-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list commits@cordova.apache.org Received: (qmail 3952 invoked by uid 99); 13 May 2015 02:42:46 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 May 2015 02:42:46 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B2F02E17E1; Wed, 13 May 2015 02:42:46 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: agrieve@apache.org To: commits@cordova.apache.org Date: Wed, 13 May 2015 02:42:46 -0000 Message-Id: <8ee08c3b52794e87b64fa2c12d86bb77@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [1/9] cordova-app-harness git commit: Update Android code to work with latest 4.0.x Repository: cordova-app-harness Updated Branches: refs/heads/master 101b9ffdf -> 3a496a0cf Update Android code to work with latest 4.0.x Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/675a3d31 Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/675a3d31 Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/675a3d31 Branch: refs/heads/master Commit: 675a3d31df9af18ef396516d3826eb9f9d31d0a4 Parents: 101b9ff Author: Andrew Grieve Authored: Fri Mar 13 10:58:00 2015 -0400 Committer: Andrew Grieve Committed: Tue May 12 22:02:50 2015 -0400 ---------------------------------------------------------------------- AppHarnessUI/android/AppHarnessUI.java | 110 ++++++++++++-------- AppHarnessUI/android/CustomAndroidWebView.java | 73 +++++++------ AppHarnessUI/android/CustomCordovaWebView.java | 4 +- template-overrides/config.xml | 4 +- www/cdvah/js/AppHarnessUI.js | 2 +- 5 files changed, 112 insertions(+), 81 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/675a3d31/AppHarnessUI/android/AppHarnessUI.java ---------------------------------------------------------------------- diff --git a/AppHarnessUI/android/AppHarnessUI.java b/AppHarnessUI/android/AppHarnessUI.java index 2975bf5..661599a 100644 --- a/AppHarnessUI/android/AppHarnessUI.java +++ b/AppHarnessUI/android/AppHarnessUI.java @@ -18,6 +18,8 @@ */ package org.apache.appharness; +import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -27,39 +29,48 @@ import org.apache.cordova.ConfigXmlParser; import org.apache.cordova.CordovaActivity; import org.apache.cordova.CordovaArgs; import org.apache.cordova.CordovaPlugin; -import org.apache.cordova.LinearLayoutSoftKeyboardDetect; +import org.apache.cordova.CordovaWebView; +import org.apache.cordova.CordovaWebViewEngine; +import org.apache.cordova.CordovaWebViewImpl; import org.apache.cordova.PluginEntry; import org.apache.cordova.PluginResult; +import org.apache.cordova.whitelist.WhitelistPlugin; import org.json.JSONArray; import org.json.JSONException; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParserFactory; import android.annotation.TargetApi; +import android.content.res.XmlResourceParser; +import android.net.Uri; import android.os.Build; import android.util.Log; -import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.ViewPropertyAnimator; import android.view.animation.DecelerateInterpolator; -import android.widget.FrameLayout; -import android.widget.LinearLayout; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class AppHarnessUI extends CordovaPlugin { private static final String LOG_TAG = "AppHarnessUI"; ViewGroup contentView; - View origMainView; - CustomCordovaWebView slaveWebView; + CordovaWebView slaveWebView; + CustomCordovaWebView slaveWebViewEngine; boolean slaveVisible; CallbackContext eventsCallback; - LinearLayoutSoftKeyboardDetect layoutView; + + @Override + public void pluginInitialize() { + contentView = (ViewGroup)cordova.getActivity().findViewById(android.R.id.content); + } public boolean isSlaveVisible() { return slaveVisible; } public boolean isSlaveCreated() { - return slaveWebView != null && slaveWebView.getView().getParent() != null && ((ViewGroup)slaveWebView.getView().getParent()).getParent() != null; + return slaveWebView != null && slaveWebView.getView().getParent() != null; } @Override @@ -126,10 +137,10 @@ public class AppHarnessUI extends CordovaPlugin { } private void evalJs(String code, CallbackContext callbackContext) { - if (slaveWebView == null) { + if (slaveWebViewEngine == null) { Log.w(LOG_TAG, "Not evaluating JS since no app is active"); } else { - slaveWebView.evaluateJavascript(code); + slaveWebViewEngine.evaluateJavascript(code); } callbackContext.success(); } @@ -140,24 +151,24 @@ public class AppHarnessUI extends CordovaPlugin { if (slaveWebView != null) { Log.w(LOG_TAG, "create: already exists"); } else { - slaveWebView = new CustomAndroidWebView(this, activity); + slaveWebViewEngine = new CustomAndroidWebView(this, activity); + slaveWebView = new CordovaWebViewImpl(webView.getContext(), (CordovaWebViewEngine)slaveWebViewEngine); + // A consistent view ID is needed for plugins that utilize the background-activity plugin. + slaveWebView.getView().setId(200); // We'll set the plugin entries in initWebView. - slaveWebView.init(cordova, new ArrayList(), webView.getWhitelist(), webView.getExternalWhitelist(), preferences); + slaveWebView.init(cordova, new ArrayList(), preferences); } { - initWebView(slaveWebView, pluginIdWhitelist); - if (preferences.getBoolean("DisallowOverscroll", false)) { - slaveWebView.getView().setOverScrollMode(View.OVER_SCROLL_NEVER); - } + initWebView((CordovaWebViewEngine)slaveWebViewEngine, pluginIdWhitelist); + slaveWebView.clearCache(true); slaveWebView.clearHistory(); slaveWebView.loadUrl(url); - View newView = (View)slaveWebView.getView().getParent(); - contentView.addView(newView); + contentView.addView(slaveWebView.getView()); slaveVisible = true; // Back button capturing breaks without these: webView.getView().setEnabled(false); - newView.requestFocus(); + slaveWebView.getView().requestFocus(); } callbackContext.success(); } @@ -179,13 +190,13 @@ public class AppHarnessUI extends CordovaPlugin { Log.w(LOG_TAG, "destroy: already destroyed"); } else { slaveWebView.loadUrl("data:text/plain;charset=utf-8,"); - contentView.removeView((View)slaveWebView.getView().getParent()); + contentView.removeView(slaveWebView.getView()); webView.getView().setEnabled(true); - origMainView.requestFocus(); + webView.getView().requestFocus(); slaveWebView.getView().setScaleX(1.0f); slaveWebView.getView().setScaleY(1.0f); - slaveWebView.setStealTapEvents(false); + slaveWebViewEngine.setStealTapEvents(false); slaveVisible = false; sendEvent("destroyed"); } @@ -211,14 +222,14 @@ public class AppHarnessUI extends CordovaPlugin { anim.scaleX(1.0f).scaleY(1.0f); webView.getView().setEnabled(false); slaveWebView.getView().setEnabled(true); - ((View)slaveWebView.getView().getParent()).requestFocus(); + slaveWebView.getView().requestFocus(); } else { anim.scaleX(.25f).scaleY(.25f); webView.getView().setEnabled(true); slaveWebView.getView().setEnabled(false); - origMainView.requestFocus(); + webView.getView().requestFocus(); } - slaveWebView.setStealTapEvents(!value); + slaveWebViewEngine.setStealTapEvents(!value); anim.setDuration(300).setInterpolator(new DecelerateInterpolator(2.0f)).start(); } if (callbackContext != null) { @@ -235,36 +246,47 @@ public class AppHarnessUI extends CordovaPlugin { for (PluginEntry p : parser.getPluginEntries()) { if (!pluginIdWhitelist.contains(p.service)) { pluginEntries.remove(p); + } else if (WhitelistPlugin.class.getCanonicalName().equals(p.pluginClass)) { + // TODO: pass through path to launching config.xml. + //p.plugin = createWhitelistPlugin(); } } slaveWebView.getPluginManager().setPluginEntries(pluginEntries); // This is added by cordova-android in code, so we need to re-add it likewise. // Note that we re-route navigator.app.exitApp() in JS to close the webview rather than close the Activity. - slaveWebView.getPluginManager().addService("App", "org.apache.cordova.CoreAndroid"); + slaveWebView.getPluginManager().addService("CoreAndroid", "org.apache.cordova.CoreAndroid"); } - private void initWebView(final CustomCordovaWebView newWebView, Set pluginIdWhitelist) { - setPluginEntries(pluginIdWhitelist); - - CordovaActivity activity = (CordovaActivity)cordova.getActivity(); - if (contentView == null) { - contentView = (ViewGroup)activity.findViewById(android.R.id.content); - origMainView = contentView.getChildAt(0); - } - - if(layoutView == null) { - layoutView = new LinearLayoutSoftKeyboardDetect(activity, contentView.getWidth(), contentView.getHeight()); - layoutView.addView(newWebView.getView()); + private CordovaPlugin createWhitelistPlugin(Uri configXml) { + InputStream istr = null; + try { + istr = webView.getResourceApi().openForRead(configXml).inputStream; + XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); + factory.setNamespaceAware(false); + XmlPullParser parser = factory.newPullParser(); + parser.setInput(istr, "UTF-8"); + return new WhitelistPlugin(parser); + } catch (XmlPullParserException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (istr != null) { + try { + istr.close(); + } catch (IOException e) { + } + } } - layoutView.setOrientation(LinearLayout.VERTICAL); + return null; + } -// layoutView.setBackground(origRootView.getBackground()); - layoutView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.BOTTOM | Gravity.LEFT)); + private void initWebView(final CordovaWebViewEngine newWebView, Set pluginIdWhitelist) { + setPluginEntries(pluginIdWhitelist); - newWebView.getView().setLayoutParams(new LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, + newWebView.getView().setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, - 1.0F)); + ViewGroup.LayoutParams.MATCH_PARENT)); newWebView.getView().setVisibility(View.VISIBLE); } } http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/675a3d31/AppHarnessUI/android/CustomAndroidWebView.java ---------------------------------------------------------------------- diff --git a/AppHarnessUI/android/CustomAndroidWebView.java b/AppHarnessUI/android/CustomAndroidWebView.java index 6d248cd..174d372 100644 --- a/AppHarnessUI/android/CustomAndroidWebView.java +++ b/AppHarnessUI/android/CustomAndroidWebView.java @@ -18,65 +18,48 @@ */ package org.apache.appharness; -import org.apache.cordova.AndroidWebView; +import org.apache.cordova.engine.SystemWebView; +import org.apache.cordova.engine.SystemWebViewEngine; import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; import android.util.Log; import android.view.MotionEvent; -import android.webkit.WebView; -class CustomAndroidWebView extends AndroidWebView implements CustomCordovaWebView { - private static final String LOG_TAG = "AppHarnessUI"; +class CustomAndroidWebView extends SystemWebViewEngine implements CustomCordovaWebView { + private static final String LOG_TAG = "CustomAndroidWebView"; private AppHarnessUI parent; + TwoFingerDoubleTapGestureDetector twoFingerTapDetector; + boolean stealTapEvents; public CustomAndroidWebView(AppHarnessUI parent, Context context) { - super(context); + super(new CustomView(context)); this.parent = parent; + ((CustomView)webView).parent = this; twoFingerTapDetector = new TwoFingerDoubleTapGestureDetector(parent); } + @Override public void setStealTapEvents(boolean value){ stealTapEvents=value; } - TwoFingerDoubleTapGestureDetector twoFingerTapDetector; - boolean stealTapEvents; - - @Override - public boolean onTouchEvent(MotionEvent e) { - if (stealTapEvents) { - if (e.getAction() == MotionEvent.ACTION_UP) { - parent.sendEvent("hideMenu"); - } - return true; - } - twoFingerTapDetector.onTouchEvent(e); - return super.onTouchEvent(e); - } - - @SuppressLint("NewApi") - protected void onSizeChanged(int w, int h, int oldw, int oldh) { - super.onSizeChanged(w, h, oldw, oldh); - // Needed for the view to stay in the bottom when rotating. - setPivotY(h); - } - @SuppressLint("NewApi") + @Override public void evaluateJavascript(String script) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - loadUrl("javascript:" + script); + webView.loadUrl("javascript:" + script); } else { - ((WebView)this).evaluateJavascript(script, null); + webView.evaluateJavascript(script, null); } } - + @Override - public boolean backHistory() { + public boolean goBack() { if (canGoBack()) { - return super.backHistory(); + return super.goBack(); } if (parent.slaveVisible) { parent.sendEvent("showMenu"); @@ -86,4 +69,30 @@ class CustomAndroidWebView extends AndroidWebView implements CustomCordovaWebVie Log.w(LOG_TAG, "Somehow back button was pressed when app not visible"); return false; } + + private static class CustomView extends SystemWebView { + CustomAndroidWebView parent; + public CustomView(Context context) { + super(context); + } + + @Override + public boolean onTouchEvent(MotionEvent e) { + if (parent.stealTapEvents) { + if (e.getAction() == MotionEvent.ACTION_UP) { + parent.parent.sendEvent("hideMenu"); + } + return true; + } + parent.twoFingerTapDetector.onTouchEvent(e); + return super.onTouchEvent(e); + } + + @SuppressLint("NewApi") + protected void onSizeChanged(int w, int h, int oldw, int oldh) { + super.onSizeChanged(w, h, oldw, oldh); + // Needed for the view to stay in the bottom when rotating. + setPivotY(h); + } + } } http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/675a3d31/AppHarnessUI/android/CustomCordovaWebView.java ---------------------------------------------------------------------- diff --git a/AppHarnessUI/android/CustomCordovaWebView.java b/AppHarnessUI/android/CustomCordovaWebView.java index f879151..93906e4 100644 --- a/AppHarnessUI/android/CustomCordovaWebView.java +++ b/AppHarnessUI/android/CustomCordovaWebView.java @@ -18,9 +18,7 @@ */ package org.apache.appharness; -import org.apache.cordova.CordovaWebView; - -interface CustomCordovaWebView extends CordovaWebView { +interface CustomCordovaWebView { void setStealTapEvents(boolean value); void evaluateJavascript(String script); } http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/675a3d31/template-overrides/config.xml ---------------------------------------------------------------------- diff --git a/template-overrides/config.xml b/template-overrides/config.xml index c5e52c9..05f7377 100644 --- a/template-overrides/config.xml +++ b/template-overrides/config.xml @@ -21,8 +21,10 @@ {NAME} - + + + http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/675a3d31/www/cdvah/js/AppHarnessUI.js ---------------------------------------------------------------------- diff --git a/www/cdvah/js/AppHarnessUI.js b/www/cdvah/js/AppHarnessUI.js index f16fbfb..8df095f 100644 --- a/www/cdvah/js/AppHarnessUI.js +++ b/www/cdvah/js/AppHarnessUI.js @@ -30,7 +30,7 @@ }); if (cordova.platformId == 'android') { // This is a plugin bundled with the platform. - ret.push('App'); + ret.push('CoreAndroid'); // Needed for launching to work. ret.push('UrlRemap'); } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org For additional commands, e-mail: commits-help@cordova.apache.org