管灌系统巡查员智能手机App
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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.dao.DaoSingleton;
import com.dayu.pipirrapp.net.ApiManager;
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.List;
 
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<InspectionLocationBean> 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 -> {
                    aginShowLocation(inspectionLocationBeans);
                });
    }
 
    /**
     * 意外退出后继续显示之前的坐标
     */
    public void aginShowLocation(List<InspectionLocationBean> 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;
                    }
                    Log.i("mWebView", "showHistoryLocation" + inspectionLocationBean.getLng() + "\",\"" + inspectionLocationBean.getLat());
                    mWebView.evaluateJavascript("javascript:showHistoryLocation(\"" + inspectionLocationBean.getLng() + "\",\"" + inspectionLocationBean.getLat() + "\",\"" + isStart + "\",\"" + isEnd + "\")", value -> {
                    });
                    index++;
                }
                // 向 WebView 注入数据
                aginShowlocationBeans.clear();
            }
        }
    }
 
 
    public void getHttpLocation() {
 
    }
}