Pārlūkot izejas kodu

1.修复规章制度文件无法下载卡在加载过程的BUG

JaycePC 1 nedēļu atpakaļ
vecāks
revīzija
90b8e41a65

+ 3 - 0
app/src/main/AndroidManifest.xml

@@ -34,6 +34,9 @@
         android:supportsRtl="true"
         android:supportsRtl="true"
         android:theme="@style/Theme.西农电子信息牌"
         android:theme="@style/Theme.西农电子信息牌"
         android:usesCleartextTraffic="true">
         android:usesCleartextTraffic="true">
+        <activity
+            android:name=".main.monitor.MonitorActivity"
+            android:exported="false" />
         <activity
         <activity
             android:name=".main.rule.RuleDetailActivity"
             android:name=".main.rule.RuleDetailActivity"
             android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
             android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"

+ 7 - 2
app/src/main/java/http/net/DownloadHelper.java

@@ -13,6 +13,8 @@ import android.os.Looper;
 import android.util.Log;
 import android.util.Log;
 import android.util.SparseArray;
 import android.util.SparseArray;
 
 
+import com.blankj.utilcode.util.FileUtils;
+
 import java.lang.ref.WeakReference;
 import java.lang.ref.WeakReference;
 import java.util.function.Consumer;
 import java.util.function.Consumer;
 
 
@@ -79,11 +81,14 @@ public class DownloadHelper {
         }
         }
 
 
         try {
         try {
+            @SuppressLint("SdCardPath") final String filePathDir = "/sdcard/" + Environment.DIRECTORY_DOWNLOADS + "/file/";
+            FileUtils.createOrExistsDir(filePathDir);
+            FileUtils.deleteAllInDir(filePathDir);
             // 创建下载请求
             // 创建下载请求
             DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
             DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
                     .setTitle(fileName)
                     .setTitle(fileName)
                     .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // 显示系统通知
                     .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) // 显示系统通知
-                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); // 保存到公共下载目录
+                    .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file/" + fileName); // 保存到公共下载目录
 
 
             long downloadId = downloadManager.enqueue(request); // 加入下载队列
             long downloadId = downloadManager.enqueue(request); // 加入下载队列
             ContentObserver observer = registerObserver(downloadId, callback); // 注册内容观察者
             ContentObserver observer = registerObserver(downloadId, callback); // 注册内容观察者
@@ -123,7 +128,7 @@ public class DownloadHelper {
     private ContentObserver registerObserver(long downloadId, DownloadCallback callback) {
     private ContentObserver registerObserver(long downloadId, DownloadCallback callback) {
         // 构建指定下载任务的URI
         // 构建指定下载任务的URI
         Uri uri = ContentUris.withAppendedId(
         Uri uri = ContentUris.withAppendedId(
-                Uri.parse("content://downloads/file"), downloadId);
+                Uri.parse("content://downloads/my_downloads"), downloadId);
 
 
         ContentObserver observer = new ContentObserver(handler) {
         ContentObserver observer = new ContentObserver(handler) {
             private int lastProgress = -1; // 上次进度,用于避免重复回调
             private int lastProgress = -1; // 上次进度,用于避免重复回调

+ 0 - 14
app/src/main/java/http/net/DownloadListener.kt

@@ -1,14 +0,0 @@
-package http.net
-
-interface DownloadListener {
-
-    fun onProgress(progress: Int)
-
-    fun onSuccess()
-
-    fun onFailed(errMsg: String?)
-
-    fun onPaused()
-
-    fun onCanceled()
-}

+ 65 - 0
app/src/main/java/http/net/UrlValidator.java

@@ -0,0 +1,65 @@
+package http.net;
+
+import android.os.AsyncTask;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class UrlValidator {
+
+    // 使用HEAD方法检查URL有效性(推荐)
+    public static boolean isUrlValid(String urlString) {
+        HttpURLConnection connection = null;
+        try {
+            URL url = new URL(urlString);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("HEAD");
+            connection.setConnectTimeout(5000); // 5秒连接超时
+            connection.setReadTimeout(5000);    // 5秒读取超时
+
+            int responseCode = connection.getResponseCode();
+            return (responseCode == HttpURLConnection.HTTP_OK);
+        } catch (IOException e) {
+            return false;
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+
+    // 检查URL是否可下载(检查Content-Type和Content-Length)
+    public static boolean isDownloadable(String urlString) {
+        HttpURLConnection connection = null;
+        try {
+            URL url = new URL(urlString);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("HEAD");
+            connection.setConnectTimeout(5000);
+            connection.setReadTimeout(5000);
+
+            int responseCode = connection.getResponseCode();
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                return false;
+            }
+
+            String contentType = connection.getContentType();
+            long contentLength = connection.getContentLengthLong();
+
+            // 排除HTML/text内容和小文件
+            if (contentType != null &&
+                    (contentType.startsWith("text/") || contentType.contains("html"))) {
+                return false;
+            }
+
+            return contentLength > 0;
+        } catch (IOException e) {
+            return false;
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+}

+ 20 - 0
app/src/main/java/xn/xxp/main/monitor/MonitorActivity.java

@@ -0,0 +1,20 @@
+package xn.xxp.main.monitor;
+
+import android.os.Bundle;
+
+import androidx.activity.EdgeToEdge;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.graphics.Insets;
+import androidx.core.view.ViewCompat;
+import androidx.core.view.WindowInsetsCompat;
+
+import xn.xxp.R;
+
+public class MonitorActivity extends AppCompatActivity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_monitor);
+    }
+}

+ 27 - 9
app/src/main/java/xn/xxp/main/rule/RuleActivity.java

@@ -10,6 +10,7 @@ import android.widget.AdapterView;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 
 
 import com.blankj.utilcode.util.LogUtils;
 import com.blankj.utilcode.util.LogUtils;
+import com.blankj.utilcode.util.ThreadUtils;
 
 
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
@@ -17,6 +18,7 @@ import java.util.List;
 import core.ui.activity.BaseCountDownActivity;
 import core.ui.activity.BaseCountDownActivity;
 import core.util.FastClickDelegate;
 import core.util.FastClickDelegate;
 import http.client.ApiRepository;
 import http.client.ApiRepository;
+import http.net.UrlValidator;
 import http.vo.request.SafetyListReq;
 import http.vo.request.SafetyListReq;
 import http.vo.response.SafeBook;
 import http.vo.response.SafeBook;
 import io.reactivex.rxjava3.functions.Consumer;
 import io.reactivex.rxjava3.functions.Consumer;
@@ -91,15 +93,31 @@ public class RuleActivity extends BaseCountDownActivity<ActivityRuleBinding> {
         binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                SafeBook safeBook = ruleAdapter.getItem(position);
-                if (TextUtils.isEmpty(safeBook.content)) {
-                    showToast("数据异常,无法展示,请重新上传!");
-                    return;
-                }
-                Intent intent = new Intent(RuleActivity.this, RuleDetailActivity.class);
-                intent.putExtra("data", safeBook);
-                intent.putExtra("onEboard", true);
-                startActivity(intent);
+                showLoading("请稍候...");
+                ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<SafeBook>() {
+                    @Override
+                    public SafeBook doInBackground() throws Throwable {
+                        SafeBook safeBook = ruleAdapter.getItem(position);
+                        if (TextUtils.isEmpty(safeBook.content)) {
+                            showToast("数据异常,无法展示,请重新上传!");
+                            return null;
+                        }
+                        if (!UrlValidator.isDownloadable(safeBook.content)) {
+                            return null;
+                        }
+
+                        return safeBook;
+                    }
+
+                    @Override
+                    public void onSuccess(SafeBook result) {
+                        dismissLoading();
+                        Intent intent = new Intent(RuleActivity.this, RuleDetailActivity.class);
+                        intent.putExtra("data", result);
+                        intent.putExtra("onEboard", true);
+                        startActivity(intent);
+                    }
+                });
             }
             }
         });
         });
 
 

+ 4 - 1
app/src/main/java/xn/xxp/main/rule/RuleDetailActivity.java

@@ -130,6 +130,7 @@ public class RuleDetailActivity extends BaseCountDownActivity<ActivityRuleDetail
 
 
             }
             }
         });
         });
+
         ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Bitmap>() {
         ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Bitmap>() {
             @Override
             @Override
             public Bitmap doInBackground() throws Throwable {
             public Bitmap doInBackground() throws Throwable {
@@ -138,7 +139,9 @@ public class RuleDetailActivity extends BaseCountDownActivity<ActivityRuleDetail
 
 
             @Override
             @Override
             public void onSuccess(Bitmap result) {
             public void onSuccess(Bitmap result) {
-                Glide.with(binding.qrCode).asBitmap().load(result).error(R.mipmap.img_error).into(binding.qrCode);
+                if (!isDestroyed()) {
+                    Glide.with(binding.qrCode).asBitmap().load(result).error(R.mipmap.img_error).into(binding.qrCode);
+                }
             }
             }
         });
         });
     }
     }

+ 10 - 0
app/src/main/res/layout/activity_monitor.xml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/main"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".main.monitor.MonitorActivity">
+
+</androidx.constraintlayout.widget.ConstraintLayout>