Java tutorial
/* 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 me.ncu.quickgap; import java.util.Hashtable; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationUtils; import android.webkit.JavascriptInterface; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import org.apache.cordova.*; public class QuickGap extends CordovaActivity { private Hashtable<String, CordovaWebView> webviewPool; private String currentDisplayView = "root"; private View webviewContainer; private Display display; private long animationDuration = 300; private int leftPaddingForHiddenView = 9999; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.init(); webviewPool = new Hashtable<String, CordovaWebView>(); webviewPool.put("root", this.appView); display = getWindowManager().getDefaultDisplay(); RelativeLayout.LayoutParams rootWebviewLayout = new RelativeLayout.LayoutParams(display.getWidth(), display.getHeight()); this.appView.setLayoutParams(rootWebviewLayout); this.appView.addJavascriptInterface(new QGJavaScriptInterface(this), "quickgap"); super.loadUrl(Config.getStartUrl()); //super.loadUrl("file:///android_asset/www/index.html"); } private void createCordovaWebview(String id, String url) { CordovaWebView webview = new CordovaWebView(this); CordovaWebViewClient webviewc = null; if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) { webviewc = new CordovaWebViewClient(this, webview); } else { webviewc = new IceCreamCordovaWebViewClient(this, webview); } webview.setWebViewClient(webviewc); webviewc.setWebView(webview); CordovaChromeClient webviewcc = null; webviewcc = new CordovaChromeClient(this, webview); webview.setWebChromeClient(webviewcc); webviewcc.setWebView(webview); RelativeLayout.LayoutParams webviewLayout = new RelativeLayout.LayoutParams(display.getWidth(), display.getHeight()); webviewLayout.alignWithParent = true; webview.setLayoutParams(webviewLayout); //this.root.addView(webview); webview.loadUrl(url); webview.addJavascriptInterface(new QGJavaScriptInterface(this), "quickgap"); webviewPool.put(id, webview); final String viewId = id; Animation animation = AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left); animation.setFillAfter(true); animation.setFillEnabled(true); animation.setDuration(animationDuration); animation.setAnimationListener(new AnimationListener() { public void onAnimationEnd(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationStart(Animation animation) { } }); webviewPool.get(viewId).startAnimation(animation); } private void showCordovaWebview(String id) { if (!webviewPool.containsKey(id)) { return; } final String viewId = id; Animation animation = AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left); animation.setFillAfter(true); animation.setFillEnabled(true); animation.setDuration(animationDuration); animation.setAnimationListener(new AnimationListener() { public void onAnimationEnd(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationStart(Animation animation) { CordovaWebView webView = webviewPool.get(viewId); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) webView.getLayoutParams(); Log.d("WizViewManager", layoutParams.toString()); layoutParams.setMargins(0, 0, 0, 0); webView.setLayoutParams(layoutParams); webviewPool.get(viewId).setVisibility(View.VISIBLE); } }); webviewPool.get(viewId).startAnimation(animation); this.root.addView(webviewPool.get(viewId)); } private void hideCordovaWebview(String id) { if (!webviewPool.containsKey(id)) { return; } final String viewId = id; Animation animation = AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_out_right); animation.setFillAfter(true); animation.setFillEnabled(true); animation.setDuration(animationDuration); animation.setAnimationListener(new AnimationListener() { public void onAnimationEnd(Animation animation) { CordovaWebView webView = webviewPool.get(viewId); RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) webView.getLayoutParams(); Log.d("WizViewManager", layoutParams.toString()); layoutParams.setMargins(leftPaddingForHiddenView, 0, 0, 0); webView.setLayoutParams(layoutParams); webviewPool.get(viewId).setVisibility(View.INVISIBLE); } public void onAnimationRepeat(Animation animation) { } public void onAnimationStart(Animation animation) { } }); webviewPool.get(viewId).startAnimation(animation); this.root.removeView(webviewPool.get(viewId)); } private void closeCordovaWebview(String id) { if (!webviewPool.containsKey(id)) { return; } final String viewId = id; this.root.removeView(webviewPool.get(viewId)); webviewPool.remove(viewId); } public class QGJavaScriptInterface { Context mContext; QGJavaScriptInterface(Context c) { mContext = c; } @JavascriptInterface public void showToast(String webMessage) { Toast.makeText(mContext, webMessage, Toast.LENGTH_SHORT).show(); } @JavascriptInterface public void createWindow(final String id, final String url) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { createCordovaWebview(id, url); } }); } @JavascriptInterface public void hideWindow(final String id) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { hideCordovaWebview(id); } }); } @JavascriptInterface public void showWindow(final String id) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { showCordovaWebview(id); } }); } @JavascriptInterface public void closeWindow(final String id) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { closeCordovaWebview(id); } }); } } }