diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/AndroidOptionController.java b/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/AndroidOptionController.java index d2db54bd..417455de 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/AndroidOptionController.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/ctrl/AndroidOptionController.java @@ -2,6 +2,7 @@ package xyz.wbsite.dbtool.javafx.ctrl; import javafx.fxml.FXML; import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; import javafx.scene.control.TextField; public class AndroidOptionController { @@ -13,6 +14,8 @@ public class AndroidOptionController { @FXML private TextField domain; @FXML + private CheckBox webview; + @FXML private Button ok; @FXML private Button cancel; @@ -56,4 +59,12 @@ public class AndroidOptionController { public void setDomain(TextField domain) { this.domain = domain; } + + public CheckBox getWebview() { + return webview; + } + + public void setWebview(CheckBox webview) { + this.webview = webview; + } } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/AndroidCallable.java b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/AndroidCallable.java index 98b9c6bd..fe163d49 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/AndroidCallable.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/manger/callable/AndroidCallable.java @@ -154,18 +154,22 @@ public class AndroidCallable implements Callable { HashMap ctx = new HashMap(); ctx.put("package", option.packages); ctx.put("domain", option.domain); - freeMarkerManager.outputTemp(new File(fragment, "MainFragment.java"), "Android/app/src/main/java/fragment/MainFragment.java", ctx); + if (option.webview) { + freeMarkerManager.outputTemp(new File(fragment, "WebViewFragment.java"), "Android/app/src/main/java/fragment/WebViewFragment.java", ctx); + } else { + freeMarkerManager.outputTemp(new File(fragment, "MainFragment.java"), "Android/app/src/main/java/fragment/MainFragment.java", ctx); + } } { Tool.outputResource("Android/app/src/main/res/drawable/background.xml", new File(drawable, "background.xml")); - Tool.outputResource("Android/app/src/main/res/drawable/icon_logo.png", new File(drawable, "icon_logo.png")); - Tool.outputResource("Android/app/src/main/res/drawable/icon_password.png", new File(drawable, "icon_password.png")); - Tool.outputResource("Android/app/src/main/res/drawable/icon_username.png", new File(drawable, "icon_username.png")); Tool.outputResource("Android/app/src/main/res/layout/activity_qrcode.xml", new File(layout, "activity_qrcode.xml")); - Tool.outputResource("Android/app/src/main/res/layout/fragment.xml", new File(layout, "fragment.xml")); - Tool.outputResource("Android/app/src/main/res/layout/fragment_main.xml", new File(layout, "fragment_main.xml")); + if (option.webview) { + Tool.outputResource("Android/app/src/main/res/layout/fragment_webview.xml", new File(layout, "fragment_webview.xml")); + } else { + Tool.outputResource("Android/app/src/main/res/layout/fragment_main.xml", new File(layout, "fragment_main.xml")); + } Tool.outputResource("Android/app/src/main/res/mipmap-mdpi/ic_launcher.png", new File(mipmap_mdpi, "ic_launcher.png")); Tool.outputResource("Android/app/src/main/res/mipmap-mdpi/ic_launcher.png", new File(mipmap_mdpi, "ic_launcher_round.png")); diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/po/AndroidOption.java b/src/main/java/xyz/wbsite/dbtool/javafx/po/AndroidOption.java index 326a3f60..16f0b01f 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/po/AndroidOption.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/po/AndroidOption.java @@ -4,4 +4,5 @@ public class AndroidOption { public String projectName; public String packages; public String domain; + public boolean webview; } diff --git a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java index 7e138f14..63e066b2 100644 --- a/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java +++ b/src/main/java/xyz/wbsite/dbtool/javafx/tool/Dialog.java @@ -586,6 +586,7 @@ public class Dialog { androidOption.projectName = controller.getName().getText(); androidOption.packages = controller.getPackages().getText(); androidOption.domain = controller.getDomain().getText(); + androidOption.webview = controller.getWebview().isSelected(); dBmanger.generateAndroid(file.getAbsolutePath(), androidOption); Platform.runLater(new Runnable() { diff --git a/src/main/resources/fxml/androidOption.fxml b/src/main/resources/fxml/androidOption.fxml index 89494455..cfa43201 100644 --- a/src/main/resources/fxml/androidOption.fxml +++ b/src/main/resources/fxml/androidOption.fxml @@ -5,31 +5,41 @@
- - - - - - - - - - - - + + + + + + + + + + + + +
diff --git a/src/main/resources/modules/Android/app/src/main/java/fragment/WebViewFragment.java b/src/main/resources/modules/Android/app/src/main/java/fragment/WebViewFragment.java new file mode 100644 index 00000000..9a87128a --- /dev/null +++ b/src/main/resources/modules/Android/app/src/main/java/fragment/WebViewFragment.java @@ -0,0 +1,142 @@ +package com.example.fragment; + +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.os.Build; +import android.util.Log; +import android.view.View; +import android.webkit.CookieManager; +import android.webkit.CookieSyncManager; +import android.webkit.WebChromeClient; +import android.webkit.WebSettings; +import android.webkit.WebView; +import android.webkit.WebViewClient; + +import ${package}.R; +import ${domain}.base.BaseSPAFragment; +import com.qmuiteam.qmui.util.QMUIStatusBarHelper; + +import butterknife.BindView; + +public class WebViewFragment extends BaseSPAFragment { + + @BindView(R.id.webView) + WebView webView; + + private String cookies = null; + + @Override + protected int getFragmnetLayout() { + return R.layout.fragment_webview; + } + + @Override + protected void onViewInit() { + initWebView("http://192.168.1.103:8080/wap/index#/"); + } + + private void initWebView(final String url) { + QMUIStatusBarHelper.FlymeSetStatusBarLightMode(getActivity().getWindow(), true); + //android 5.0 之后需要开启浏览器的整体缓存才能截取整个Web + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + WebView.enableSlowWholeDocumentDraw(); + } + WebSettings webSettings = webView.getSettings(); + //支持获取手势焦点,输入用户名、密码或其他 + webView.requestFocusFromTouch(); + + webSettings.setJavaScriptEnabled(true); //支持js + webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染的优先级 + + // 设置自适应屏幕,两者合用 + webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小 + webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 + webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。 + webSettings.setBuiltInZoomControls(true); //设置可以缩放 + webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件 + // 若上面是false,则该WebView不可缩放,这个不管设置什么都不能缩放。 + webSettings.setTextZoom(100);//设置文本的缩放倍数,默认为 100 + webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局 + webSettings.supportMultipleWindows(); //多窗口 + webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存 + webSettings.setAllowFileAccess(true); //设置可以访问文件 + webSettings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点 + webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 + webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片 + webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式 + webSettings.setStandardFontFamily("");//设置 WebView 的字体,默认字体为 "sans-serif" + webSettings.setDatabaseEnabled(true); + webSettings.setDomStorageEnabled(true);// 开启 DOM storage API 功能 + webSettings.setAppCacheEnabled(true); + + if (cookies != null) { + syncCookie(url, cookies.substring(0, cookies.indexOf(";"))); + } + + //如果不设置WebViewClient,请求会跳转系统浏览器 + webView.setWebViewClient(new WebViewClient() { + public boolean shouldOverrideUrlLoading(WebView view, String url) { + view.loadUrl(url); + Log.i("----------", url); + return super.shouldOverrideUrlLoading(view, url); + } + }); + webView.setWebChromeClient(new WebChromeClient()); + webView.loadUrl(url); +// webView.addJavascriptInterface(new WebAppInterface(this), "app"); + } + + public void syncCookie(String url, String cookie) { + CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(webView.getContext()); + CookieManager cookieManager = CookieManager.getInstance(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + cookieManager.setAcceptThirdPartyCookies(webView, true); + cookieManager.setCookie(url, cookie); + cookieManager.flush(); //强制立即同步cookie + + } else { + cookieManager.setAcceptCookie(true); + cookieManager.setCookie(url, cookie); + cookieSyncManager.sync(); + } + } + + /** + * 对webview截图 + * + * @param webView + * @param savePath + * @return + */ + public static boolean getFullWebViewSnapshot(WebView webView, String savePath) { + //重新调用WebView的measure方法测量实际View的大小(将测量模式设置为UNSPECIFIED模式也就是需要多大就可以获得多大的空间) + webView.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED), + View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); + //调用layout方法设置布局(使用新测量的大小) + webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight()); + //开启WebView的缓存(当开启这个开关后下次调用getDrawingCache()方法的时候会把view绘制到一个bitmap上) + webView.setDrawingCacheEnabled(true); + //强制绘制缓存(必须在setDrawingCacheEnabled(true)之后才能调用,否者需要手动调用destroyDrawingCache()清楚缓存) + webView.buildDrawingCache(); + //根据测量结果创建一个大小一样的bitmap + Bitmap picture = Bitmap.createBitmap(webView.getMeasuredWidth(), + webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888); + //已picture为背景创建一个画布 + Canvas canvas = new Canvas(picture); // 画布的宽高和 WebView 的网页保持一致 + Paint paint = new Paint(); + //设置画笔的定点位置,也就是左上角 + canvas.drawBitmap(picture, 0, webView.getMeasuredHeight(), paint); + //将webview绘制在刚才创建的画板上 + webView.draw(canvas); +// try { +// //将bitmap保存到SD卡 +// FileTools.saveBitmap(picture, savePath); +// return true; +// } catch (IOException e) { +// e.printStackTrace(); +// return false; +// } + return true; + } +} diff --git a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_logo.png b/src/main/resources/modules/Android/app/src/main/res/drawable/icon_logo.png deleted file mode 100644 index f9f323f2..00000000 Binary files a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_logo.png and /dev/null differ diff --git a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_password.png b/src/main/resources/modules/Android/app/src/main/res/drawable/icon_password.png deleted file mode 100644 index fdb5701f..00000000 Binary files a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_password.png and /dev/null differ diff --git a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_username.png b/src/main/resources/modules/Android/app/src/main/res/drawable/icon_username.png deleted file mode 100644 index 4f969d2f..00000000 Binary files a/src/main/resources/modules/Android/app/src/main/res/drawable/icon_username.png and /dev/null differ diff --git a/src/main/resources/modules/Android/app/src/main/res/layout/activity_qrcode.xml b/src/main/resources/modules/Android/app/src/main/res/layout/activity_qrcode.xml index bb986238..13fb7fc6 100644 --- a/src/main/resources/modules/Android/app/src/main/res/layout/activity_qrcode.xml +++ b/src/main/resources/modules/Android/app/src/main/res/layout/activity_qrcode.xml @@ -1,8 +1,7 @@ + android:layout_height="match_parent"> + android:paddingRight="4dp"/> - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/modules/Android/app/src/main/res/layout/fragment_main.xml b/src/main/resources/modules/Android/app/src/main/res/layout/fragment_main.xml index 3c8115fb..ea73c436 100644 --- a/src/main/resources/modules/Android/app/src/main/res/layout/fragment_main.xml +++ b/src/main/resources/modules/Android/app/src/main/res/layout/fragment_main.xml @@ -1,9 +1,9 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@drawable/background"> + + + + + + +