From 41e0d7cc0f04e5e2e2b448f33bd2515e515fbb51 Mon Sep 17 00:00:00 2001 From: zhaodengke Date: Tue, 22 Jul 2025 15:27:46 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/nsgk/ruralWeb/FullscreenActivity.java | 179 +++++++++++++++--- .../com/nsgk/ruralWeb/WelcomeActivity.java | 6 +- 2 files changed, 156 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/nsgk/ruralWeb/FullscreenActivity.java b/app/src/main/java/com/nsgk/ruralWeb/FullscreenActivity.java index ff2a1ea..3873a02 100644 --- a/app/src/main/java/com/nsgk/ruralWeb/FullscreenActivity.java +++ b/app/src/main/java/com/nsgk/ruralWeb/FullscreenActivity.java @@ -3,35 +3,52 @@ package com.nsgk.ruralWeb; import android.annotation.SuppressLint; import android.content.Intent; import android.content.pm.PackageManager; +import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.KeyEvent; import android.view.ViewGroup; +import android.webkit.WebBackForwardList; +import android.webkit.WebHistoryItem; import android.webkit.WebView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Toast; import com.just.agentweb.AgentWeb; +import com.just.agentweb.AgentWebConfig; import com.just.agentweb.DefaultWebClient; -import com.nsgk.ruralWeb.utils.ContextUtils; +import com.just.agentweb.WebChromeClient; +import com.just.agentweb.WebViewClient; +import com.nsgk.ruralWeb.sys.NSConstants; +import com.nsgk.ruralWeb.sys.NSPreference; +import com.nsgk.ruralWeb.utils.NSContextUtils; +import com.nsgk.ruralWeb.utils.NSStr; +import com.nsgk.ruralWeb.web.NSEnvWindowObject; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import cn.hutool.core.util.StrUtil; + /** * An example full-screen activity that shows and hides the system UI (i.e. * status bar and navigation/system bar) with user interaction. */ public class FullscreenActivity extends AppCompatActivity { + private static final String ID_TAG = FullscreenActivity.class.getName(); public static final int PERMISSION_LOCATION_REQUEST_CODE = 0x1001; private AgentWeb mAgentWeb; - private Preference preference; + private NSPreference preference; + private NSEnvWindowObject envWindowObject; + + private String m_lastUrl; + private String m_mainUrl; @SuppressLint("SetJavaScriptEnabled") @Override @@ -41,24 +58,34 @@ public class FullscreenActivity extends AppCompatActivity { setContentView(R.layout.activity_fullscreen); String appHomeUrl = GetHomeUrl(); //appHomeUrl = "http://192.168.0.250:85/sunVillage_info/login"; - Log.i("NSGK", "App home url: " + appHomeUrl); + Log.i(ID_TAG, "App home url: " + appHomeUrl); // init(); - mAgentWeb = AgentWeb.with(this)// - .setAgentWebParent((RelativeLayout) findViewById(R.id.ll), -1, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//传入AgentWeb的父控件。 + AgentWeb.PreAgentWeb builder = AgentWeb.with(this)// + .setAgentWebParent(findViewById(R.id.ll), -1, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))//传入AgentWeb的父控件。 .useDefaultIndicator(-1, 3)//设置进度条颜色与高度,-1为默认值,高度为2,单位为dp。 -/* .setWebChromeClient(new WebChromeClient() { + .setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); - if(newProgress == 100) + if(newProgress != 100) + return; + String url = view.getUrl(); + if(IsInDefaultPage()) + { + m_mainUrl = url; + DumpMainUrl(url); + Log.i(ID_TAG, "设定主页地址: " + m_mainUrl); + } + else if(IsDefaultPage(url)) { - GetPreference().SetString(Preference.LAST_ACCESS_URL, view.getUrl()); + m_mainUrl = null; + Log.i(ID_TAG, "清空主页地址"); } + m_lastUrl = url; } - })*/ - .setWebViewClient(new com.just.agentweb.WebViewClient() { - + }) + .setWebViewClient(new WebViewClient() { })//WebViewClient , 与 WebView 使用一致 ,但是请勿获取WebView调用setWebViewClient(xx)方法了,会覆盖AgentWeb DefaultWebClient,同时相应的中间件也会失效。 .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK) //严格模式 Android 4.2.2 以下会放弃注入对象 ,使用AgentWebView没影响。 .setMainFrameErrorView(R.layout.agentweb_error_page, -1) //参数1是错误显示的布局,参数2点击刷新控件ID -1表示点击整个布局都刷新, AgentWeb 3.0.0 加入。 @@ -66,10 +93,32 @@ public class FullscreenActivity extends AppCompatActivity { .interceptUnkownUrl() //拦截找不到相关页面的Url AgentWeb 3.0.0 加入。 .createAgentWeb()//创建AgentWeb。 .ready()//设置 WebSettings。 - .go(appHomeUrl); //WebView载入该url地址的页面并显示。 + ; + + boolean openDef = IsDefaultPage(appHomeUrl); + + if(!openDef) + { + Log.i(ID_TAG, "载入主页: " + appHomeUrl); + RestoreCookie(); // 重载cookie + m_mainUrl = appHomeUrl; + } + else + { + Log.i(ID_TAG, "载入登录页"); + GetPreference().Remove(NSPreference.COOKIES); // Remove cookies + } + + mAgentWeb = builder.go(appHomeUrl); //WebView载入该url地址的页面并显示。 + + int fontScale = GetPreference().GetInt(NSPreference.FONT_SCALE); + Log.i(ID_TAG, "全局字体缩放: " + fontScale + "%"); + if(fontScale > 0 && fontScale != 100) + GetWebView().getSettings().setTextZoom(fontScale); // 注入宿主对象 - mAgentWeb.getJsInterfaceHolder().addJavaObject("_Native_object", new EnvWindowObject(this, new Handler(), mAgentWeb.getWebCreator().getWebView())); + envWindowObject = new NSEnvWindowObject(this, new Handler(), GetWebView()); + mAgentWeb.getJsInterfaceHolder().addJavaObject("_Native_object", envWindowObject); /* 上边url是各个APP项目的入口地址 事项审批 http://116.255.223.226:82/yinnongLogin 图标 ic_launcher_sxsp 或者 ic_launcher_yhzl @@ -85,10 +134,15 @@ public class FullscreenActivity extends AppCompatActivity { 一体机 http://47.98.113.57:81 图标 ic_launcher_njytj */ + Log.d(ID_TAG, "UI线程: " + Thread.currentThread().getId()); } @Override protected void onPause() { + if(IsRecordUrl()) + { + DumpCookie(); + } mAgentWeb.getWebLifeCycle().onPause(); super.onPause(); } @@ -101,7 +155,14 @@ public class FullscreenActivity extends AppCompatActivity { @Override protected void onDestroy() { + if(!IsRecordUrl()) + { + Log.d(ID_TAG, "Remove url and cookie record"); + GetPreference().Remove(NSPreference.COOKIES); + GetPreference().Remove(NSPreference.LAST_ACCESS_URL); + } mAgentWeb.getWebLifeCycle().onDestroy(); + envWindowObject.OnDestroy(); super.onDestroy(); } @@ -131,38 +192,102 @@ public class FullscreenActivity extends AppCompatActivity { for(int i = 0; i < permissions.length; i++) { boolean b = grantResults[i] == PackageManager.PERMISSION_GRANTED; - Log.i("NSGK", String.format("请求权限: %s -> %s", permissions[i], b ? "通过" : "拒绝")); + Log.i(ID_TAG, String.format("请求权限: %s -> %s", permissions[i], b ? "通过" : "拒绝")); if(b) granted++; } if (granted < permissions.length) { Toast.makeText(this, "需要定位权限", Toast.LENGTH_LONG).show(); - ContextUtils.RequestLocationPermission(this, FullscreenActivity.PERMISSION_LOCATION_REQUEST_CODE); + NSContextUtils.RequestLocationPermission(this, FullscreenActivity.PERMISSION_LOCATION_REQUEST_CODE); } } } - private Preference GetPreference() + @Override + public void onBackPressed() + { + WebView webView = GetWebView(); + if (null != webView && !webView.canGoBack() && !IsInDefaultPage()) { + webView.loadUrl(NSConstants.AppHomeUrl()); + webView.clearHistory(); + //History(); + return; + } + super.onBackPressed(); + } + + private NSPreference GetPreference() { if(null == preference) - preference = new Preference(this); + preference = new NSPreference(this); return preference; } + private boolean IsRecordUrl() + { + return GetPreference().GetBool(NSPreference.OPEN_LAST_URL); + } + private String GetHomeUrl() { - return Constants.AppHomeUrl(); - // return GetPreference().GetString(GetPreference().LAST_ACCESS_URL, Constants.AppHomeUrl()); + String url = null; + if(IsRecordUrl()) + url = GetPreference().GetString(NSPreference.LAST_ACCESS_URL, NSConstants.AppHomeUrl()); + if(StrUtil.isBlank(url)) + url = NSConstants.AppHomeUrl(); + return url; } - private void DumpLastUrl() + private WebView GetWebView() { - if(null == mAgentWeb) - return; - WebView webView = mAgentWeb.getWebCreator().getWebView(); - if(null == webView) - return; - String url = webView.getUrl(); - GetPreference().SetString(Preference.LAST_ACCESS_URL, url); + return mAgentWeb.getWebCreator().getWebView(); + } + + private void DumpMainUrl(String url) + { + if(StrUtil.isNotEmpty(url)) + GetPreference().SetString(NSPreference.LAST_ACCESS_URL, url); + else + GetPreference().Remove(NSPreference.LAST_ACCESS_URL); + Log.d(ID_TAG, "Dump URL: " + url); + } + + private void DumpCookie() + { + String url = NSStr.GetUrlPart(NSConstants.AppHomeUrl()); + String cookies = AgentWebConfig.getCookiesByUrl(url); + GetPreference().SetString(NSPreference.COOKIES, cookies); + Log.d(ID_TAG, "Dump cookie: " + "url=" + url + ", cookie=" + cookies); + } + + private void RestoreCookie() + { + String cookies = GetPreference().GetString(NSPreference.COOKIES); + if(StrUtil.isNotBlank(cookies)) + { + String url = NSStr.GetUrlPart(NSConstants.AppHomeUrl()); + AgentWebConfig.syncCookie(url, cookies); + Log.d(ID_TAG, "Restore cookie: " + "url=" + url + ", cookie=" + cookies); + } + } + + private boolean IsInDefaultPage() + { + return IsDefaultPage(m_lastUrl); + } + + private boolean IsDefaultPage(String url) + { + return NSConstants.AppHomeUrl().equals(url); + } + + private void History() + { + WebBackForwardList webBackForwardList = GetWebView().copyBackForwardList(); + for(int i = 0; i < webBackForwardList.getSize(); i++) + { + WebHistoryItem item = webBackForwardList.getItemAtIndex(i); + Log.d(ID_TAG, StrUtil.format("{}: {} {}", i, item.getTitle(), item.getUrl())); + } } } \ No newline at end of file diff --git a/app/src/main/java/com/nsgk/ruralWeb/WelcomeActivity.java b/app/src/main/java/com/nsgk/ruralWeb/WelcomeActivity.java index 19daef0..f68d558 100644 --- a/app/src/main/java/com/nsgk/ruralWeb/WelcomeActivity.java +++ b/app/src/main/java/com/nsgk/ruralWeb/WelcomeActivity.java @@ -14,6 +14,8 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import com.nsgk.ruralWeb.sys.NSConstants; + public class WelcomeActivity extends AppCompatActivity { private ImageView imageView; private LinearLayout llcenter; @@ -55,14 +57,14 @@ public class WelcomeActivity extends AppCompatActivity { llcenter = findViewById(R.id.llcenter); // 动态设置启动页版权信息 - String text = GetTrimString(Constants.AppCopyright()); + String text = GetTrimString(NSConstants.AppCopyright()); if(null != text) { TextView copyrightText = findViewById(R.id.welcome_text_copyright); copyrightText.setText(text); } // 动态设置启动页厂商名称 - text = GetTrimString(Constants.AppVendor()); + text = GetTrimString(NSConstants.AppVendor()); if(null != text) { TextView vendorText = findViewById(R.id.welcome_text_vendor);