package com.dayu.pipirrapp.activity; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.webkit.ConsoleMessage; import android.webkit.WebChromeClient; import android.webkit.WebResourceError; import android.webkit.WebResourceRequest; import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.annotation.Nullable; import com.dayu.pipirrapp.R; import com.dayu.pipirrapp.bean.db.InspectionLocationBean; import com.dayu.pipirrapp.bean.net.InspectListResult; import com.dayu.pipirrapp.bean.net.InspectPointResult; import com.dayu.pipirrapp.dao.DaoSingleton; import com.dayu.pipirrapp.net.ApiManager; import com.dayu.pipirrapp.net.BaseResponse; import com.dayu.pipirrapp.net.Constants; import com.dayu.pipirrapp.net.subscribers.SubscriberListener; import com.dayu.pipirrapp.utils.CommonData; import com.dayu.pipirrapp.utils.MapJpgUtils; import com.dayu.pipirrapp.view.TitleBar; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; import io.reactivex.rxjava3.schedulers.Schedulers; /** * InspectDetailActivity - 巡检详情页 * * @author zuoxiao * @version 1.0 * @since 2025-02-20 */ public class InspectDetailActivity extends BaseActivity { WebView mWebView; String inspectId; List aginShowlocationBeans; boolean webViewIsFinished; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); inspectId = getIntent().getStringExtra("inspectId"); setContentView(R.layout.activity_inspect_detail); new TitleBar(this).setTitleText("巡检详情").setLeftIco().setLeftIcoListening(v -> InspectDetailActivity.this.finish()); mWebView = findViewById(R.id.InspectWebView); Log.d("InspectDetail", "开始初始化WebView"); // 初始化WebView设置 WebSettings webSettings = mWebView.getSettings(); mWebView.clearCache(true); webSettings.setJavaScriptEnabled(true); webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); webSettings.setAllowUniversalAccessFromFileURLs(true); webSettings.setDomStorageEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setDefaultTextEncodingName("utf-8"); webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); webSettings.setBlockNetworkImage(false); webSettings.setBlockNetworkLoads(false); webSettings.setDatabaseEnabled(true); webSettings.setGeolocationEnabled(true); // 设置独立的数据目录 File cacheDir = new File(getDir("webview", MODE_PRIVATE), "cache"); if (!cacheDir.exists()) { cacheDir.mkdirs(); } webSettings.setDatabasePath(cacheDir.getAbsolutePath()); webSettings.setGeolocationDatabasePath(cacheDir.getAbsolutePath()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); } initWeb(); Log.d("InspectDetail", "开始加载HTML页面"); mWebView.loadUrl("file:///android_asset/index.html"); showLocation(); } @Override protected void onDestroy() { if (mWebView != null) { mWebView.loadUrl("about:blank"); mWebView.clearHistory(); mWebView.clearCache(true); mWebView.removeAllViews(); mWebView.destroy(); mWebView = null; } super.onDestroy(); } @Override protected void onPause() { super.onPause(); if (mWebView != null) { mWebView.onPause(); mWebView.pauseTimers(); } } @Override protected void onResume() { super.onResume(); if (mWebView != null) { mWebView.onResume(); mWebView.resumeTimers(); } } private void initWeb() { mWebView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { String url = request.getUrl().toString(); //判断当前是否为加载瓦片 if (MapJpgUtils.getInsatance().isTianDiTuTileRequest(url)) { String androidUrl = url.replace(CommonData.webKey, CommonData.androidKey); // 检查本地缓存 File cachedTile = MapJpgUtils.getInsatance().getCachedTile(androidUrl); if (cachedTile != null && cachedTile.exists()) { // Log.d(TAG, "本地缓存>>>" + androidUrl); // if (MapJpgUtils.getInsatance().validateImageFile(androidUrl,request.)) // 判断缓存是否过期 // if (!MapJpgUtils.getInsatance(MapFragment.this.getContext()).isCacheExpired(cachedTile)) { try { // 从缓存加载瓦片 return new WebResourceResponse("image/jpg", "utf-8", new FileInputStream(cachedTile)); } catch (FileNotFoundException e) { e.printStackTrace(); } // } } else { //下载瓦片 ApiManager.getInstance().donwLoadTile(InspectDetailActivity.this, androidUrl); } } return super.shouldInterceptRequest(view, request); } @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); String url = request.getUrl().toString(); int errorCode = error.getErrorCode(); String description = error.getDescription().toString(); Log.e("InspectDetail", String.format("加载错误 - URL: %s\n错误码: %d\n描述: %s", url, errorCode, description)); } @Override public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) { super.onReceivedHttpError(view, request, errorResponse); String url = request.getUrl().toString(); int statusCode = errorResponse.getStatusCode(); String description = errorResponse.getReasonPhrase(); Log.e("InspectDetail", String.format("HTTP错误 - URL: %s\n状态码: %d\n描述: %s", url, statusCode, description)); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); Log.d("InspectDetail", "页面加载完成: " + url); webViewIsFinished = true; // 注入一个标识,避免与MapFragment冲突 mWebView.evaluateJavascript( "window.WEBVIEW_TYPE = 'INSPECT_DETAIL';", null ); aginShowLocation(null); } }); mWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { Log.d("InspectDetail", "Console: " + consoleMessage.message() + " at " + consoleMessage.sourceId() + ":" + consoleMessage.lineNumber()); return true; } }); } /** * 加载巡检记录 */ private void showLocation() { // 查询当前未关闭的巡检记录下所有的坐标 DaoSingleton.getAsynchInstance(this).inspectionLocationDao().findByInspectId(inspectId).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()).subscribe(inspectionLocationBeans -> { if (inspectionLocationBeans != null && inspectionLocationBeans.size() > 0) { aginShowLocation(inspectionLocationBeans); } else { getHttpLocation(); } }); } /** * 意外退出后继续显示之前的坐标 */ public void aginShowLocation(List locationBeans) { if (locationBeans != null) { aginShowlocationBeans = locationBeans; } if (webViewIsFinished) { if (aginShowlocationBeans != null) { int index = 0; int size = aginShowlocationBeans.size(); boolean isStart, isEnd; for (InspectionLocationBean inspectionLocationBean : aginShowlocationBeans) { if (index == 0) { isStart = true; } else { isStart = false; } if (index == size - 1) { isEnd = true; } else { isEnd = false; } putDataToWeb(inspectionLocationBean.getLng(), inspectionLocationBean.getLat(), isStart + "", isEnd + ""); index++; } // 向 WebView 注入数据 aginShowlocationBeans.clear(); } } } /** * 向 WebView 注入数据 * * @param lng * @param lat * @param isStart * @param isEnd */ private void putDataToWeb(String lng, String lat, String isStart, String isEnd) { Log.i("mWebView", "showHistoryLocation" + lng + "\",\"" + lat); mWebView.evaluateJavascript("javascript:showHistoryLocation(\"" + lng + "\",\"" + lat + "\",\"" + isStart + "\",\"" + isEnd + "\")", value -> { }); } public void getHttpLocation() { Map params = new HashMap<>(); params.put("inspectId", inspectId); ApiManager.getInstance().requestGet(this, Constants.BASE_URL + "/app/inspect/getInspects", InspectListResult.class, params, new SubscriberListener>() { @Override public void onNext(BaseResponse t) { if (t.isSuccess()) { if (t.getContent() != null) { List locationBeans = new ArrayList<>(); if (t.getContent().getObj()!=null&&t.getContent().getObj().size()>0){ for (InspectPointResult inspectPointResult : t.getContent().getObj().get(0).getTracks()) { InspectionLocationBean inspectionLocationBean = new InspectionLocationBean(); inspectionLocationBean.setLng(inspectPointResult.getLng()); inspectionLocationBean.setLat(inspectPointResult.getLat()); inspectionLocationBean.setInspectId(inspectId); inspectionLocationBean.setId(inspectPointResult.getId()); inspectionLocationBean.setLocateTime(inspectPointResult.getLocateTime()); locationBeans.add(inspectionLocationBean); } } aginShowLocation(locationBeans); } } } @Override public void onCloose() { super.onCloose(); } @Override public void onError(Throwable e) { super.onError(e); } }); } }