Просмотр исходного кода

版本更新功能增加权限申请,系统版本兼容分区

songw 1 месяц назад
Родитель
Сommit
9774db5950

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

@@ -23,6 +23,8 @@
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 
 
+    <!-- Android 11+ 查询已安装应用权限 -->
+    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
 
     <!-- 8.0手机安装软件 -->
     <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

+ 1 - 0
app/src/main/java/uas/erp/huiyan_wms/application/PdaApplication.java

@@ -112,6 +112,7 @@ public class PdaApplication extends Application {
             MyLog.isFileCreatedTodayDel();
         }
     }
+
     private CountDownTimer countDownTimer = new CountDownTimer(COUNTDOWN_TIME, COUNTDOWN_INTERVAL) {
 
         @Override

+ 36 - 34
app/src/main/java/uas/erp/huiyan_wms/fragment/VersionUpgradeFragment.java

@@ -5,6 +5,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Build;
+import android.os.Environment;
 import android.os.Handler;
 import android.os.Message;
 import android.text.TextUtils;
@@ -19,18 +20,15 @@ import com.alibaba.fastjson.JSONObject;
 import com.android.volley.Request;
 
 import java.io.File;
-import java.io.IOException;
 
 import androidx.annotation.RequiresApi;
 import androidx.core.content.FileProvider;
 import uas.erp.huiyan_wms.R;
-import uas.erp.huiyan_wms.application.PdaApplication;
 import uas.erp.huiyan_wms.global.GloableParams;
 import uas.erp.huiyan_wms.util.AndroidUtil;
 import uas.erp.huiyan_wms.util.CommonUtil;
 import uas.erp.huiyan_wms.util.Constants;
 import uas.erp.huiyan_wms.util.FastjsonUtil;
-import uas.erp.huiyan_wms.util.FileUtils;
 import uas.erp.huiyan_wms.util.HttpCallback;
 import uas.erp.huiyan_wms.util.HttpParams;
 import uas.erp.huiyan_wms.util.VolleyRequest;
@@ -38,8 +36,6 @@ import uas.erp.huiyan_wms.util.download.ProgressDownloader;
 import uas.erp.huiyan_wms.util.download.ProgressResponseBody;
 import uas.erp.huiyan_wms.view.business.CommonProgressPop;
 
-import static uas.erp.huiyan_wms.util.Constants.CONSTANT.APK_FILE_NAME;
-
 
 /**
  * Created by RaoMeng on 2020/4/9
@@ -54,6 +50,8 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
     private File file;
     private String mAttachId;
 
+    private File apkFile;
+
     @Override
     protected int getLayout() {
         return R.layout.fragment_version_upgrade;
@@ -81,27 +79,23 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
 
                 breakPoints = 0L;
 
-//                File directory = new File(APK_FILE_PATH);
-                //String filesDir = getActivity().getFilesDir().getAbsolutePath();
-                String filesDir = "/sdcard/Android/data/" + PdaApplication.getmContext().getPackageName();
-                File directory = new File(filesDir + "/huiyan_wms/apk");
-                if (!directory.exists() && !directory.isDirectory()) {
-                    boolean mkdirs = directory.mkdirs();
-                } else {
-//                    FileUtils.delAllFile(APK_FILE_PATH);
-                    FileUtils.delAllFile(directory + "/huiyan_wms/apk");
-                }
-
-//                file = new File(APK_FILE_PATH, APK_FILE_NAME);
-                file = new File(filesDir + "/huiyan_wms/apk", APK_FILE_NAME);
-
-                try {
-                    file.createNewFile();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
+//                String filesDir = "/sdcard/Android/data/" + PdaApplication.getmContext().getPackageName();
+//                File directory = new File(filesDir + "/huiyan_wms/apk");
+//                if (!directory.exists() && !directory.isDirectory()) {
+//                    boolean mkdirs = directory.mkdirs();
+//                } else {
+//                    FileUtils.delAllFile(directory + "/huiyan_wms/apk");
+//                }
+//                file = new File(filesDir + "/huiyan_wms/apk", APK_FILE_NAME);
+//
+//                try {
+//                    file.createNewFile();
+//                } catch (IOException e) {
+//                    e.printStackTrace();
+//                }
                 String downloadUrl = GloableParams.ADDRESS_COMMON_DOWNLOADBYID + "?id=" + mAttachId;
-                mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
+//                mDownloader = new ProgressDownloader(downloadUrl, file, VersionUpgradeFragment.this);
+                mDownloader = new ProgressDownloader(downloadUrl, apkFile, VersionUpgradeFragment.this);
                 mDownloader.download(0L);
             }
         });
@@ -112,6 +106,20 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
         mOldVersionTv.setText(AndroidUtil.getVersionName(mActivity));
         getNewVersionMsg();
 
+        //不同版本分区处理
+        File downloadDir;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+            // Android 10+ 使用私有目录
+            downloadDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
+        } else {
+            downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
+        }
+        if (!downloadDir.exists()) {
+            downloadDir.mkdirs();
+        }
+        String fileName = "update_" + System.currentTimeMillis() + ".apk";
+        apkFile = new File(downloadDir, fileName);
+
         //从API 26开始需要申请安装权限
         if (Build.VERSION.SDK_INT >= 26) {
             if (!isHasInstallPermissionWithO(getActivity())) {
@@ -233,20 +241,14 @@ public class VersionUpgradeFragment extends BaseFragment implements ProgressResp
                     CommonUtil.toastNoRepeat(mActivity, "下载完成");
                     mProgressPop.dismiss();
                     try {
-//                        File apk = new File(APK_FILE_PATH, APK_FILE_NAME);
-//                        String filesDir = getActivity().getFilesDir().getAbsolutePath();
-                        String filesDir = "/sdcard/Android/data/" + PdaApplication.getmContext().getPackageName();
-                        File apk = new File(filesDir + "/huiyan_wms/apk", APK_FILE_NAME);
                         Intent intent = new Intent(Intent.ACTION_VIEW);
+                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                         if (Build.VERSION.SDK_INT >= 24) {
-                            Uri apkUri = FileProvider.getUriForFile(mActivity,
-                                    "uas.erp.huiyan_wms.fileprovider", apk);
-                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                            Uri apkUri = FileProvider.getUriForFile(mActivity, getActivity().getPackageName() +".fileprovider", apkFile);
                             intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                             intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
                         } else {
-                            intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
-                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
                         }
                         mActivity.startActivity(intent);
                     } catch (Exception e) {

+ 15 - 11
app/src/main/java/uas/erp/huiyan_wms/util/CommonUtil.java

@@ -861,18 +861,22 @@ public class CommonUtil {
 //                    return true;
 //                }
 //                return false;
-
-                //解决部分PDA机器的重复调用,(按下和抬起都执行一次)
-                if (event != null && actionId == EditorInfo.IME_ACTION_SEND ||
-                        (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
-                    switch (event.getAction()) {
-                        case KeyEvent.ACTION_UP:
-                            String text = textView.getText().toString().trim();
-                            myEditorActionListener.MyEditorAction(text, actionId, event);
-                            return true;
-                        default:
-                            return true;
+                try {
+                    int keycode = event.getKeyCode();
+                    //解决部分PDA机器的重复调用,(按下和抬起都执行一次)
+                    if (event != null && actionId == EditorInfo.IME_ACTION_SEND ||
+                            (keycode == KeyEvent.KEYCODE_ENTER)) {
+                        switch (event.getAction()) {
+                            case KeyEvent.ACTION_UP:
+                                String text = textView.getText().toString().trim();
+                                myEditorActionListener.MyEditorAction(text, actionId, event);
+                                return true;
+                            default:
+                                return true;
+                        }
                     }
+                }catch (Exception e) {
+                    e.printStackTrace();
                 }
                 return false;
             }

+ 63 - 1
app/src/main/java/uas/erp/huiyan_wms/util/MyLog.java

@@ -2,6 +2,7 @@ package uas.erp.huiyan_wms.util;
 
 import android.annotation.SuppressLint;
 import android.os.Build;
+import android.os.Environment;
 import android.util.Log;
 
 import java.io.BufferedWriter;
@@ -44,6 +45,36 @@ public class MyLog {
     private static boolean isDebug = true;        //普通log的开关
     private static String TAG = "英唐" + "-----------";
 
+    private static volatile MyLog myLog; // 使用 volatile 关键字确保多线程环境下的可见性
+
+    private MyLog() {
+    }
+
+    public static MyLog getInstance() {
+        if (myLog == null) { // 第一次检查,提高效率,避免每次都进行同步
+            synchronized (MyLog.class) { // 同步代码块,保证线程安全
+                if (myLog == null) { // 第二次检查,防止多个线程同时进入同步块时重复创建实例
+                    myLog = new MyLog();
+                    //兼容不同版本分区处理
+                    File downloadDir = null;
+                    if (Build.VERSION.SDK_INT >= 29) {
+                        // Android 10+ 使用私有目录
+                        downloadDir = PdaApplication.getmContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
+                    } else {
+                        downloadDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
+                    }
+                    if (!downloadDir.exists()) {
+                        downloadDir.mkdirs();
+                    }
+                    MYLOG_PATH_SDCARD_DIR1 = downloadDir.getAbsolutePath() + File.separator + PdaApplication.getmContext().getPackageName();
+
+                    //Log.e("aaa", "MyLog被创建了: " + MYLOG_PATH_SDCARD_DIR1);
+                }
+            }
+        }
+        return myLog;
+    }
+
     public static void e(String msg) {
         if (isDebug) {
             Log.e(TAG, msg);
@@ -115,7 +146,21 @@ public class MyLog {
         msg = "\r\n" + msg;
         if (MYLOG_SWITCH) {
             if ('e' == level && ('e' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { // 输出错误信息
-                Log.e(tag, msg);
+                //Log.e(tag, msg);
+                if (msg.length() > 4000) {
+                    // 因为Logcat一次最多能输出4000个字符左右,所以我们按4000字符分段
+                    int chunkCount = msg.length() / 4000; // 分割成多少段
+                    for (int i = 0; i <= chunkCount; i++) {
+                        int max = 4000 * (i + 1);
+                        if (max >= msg.length()) {
+                            Log.e(tag, msg.substring(4000 * i));
+                        } else {
+                            Log.e(tag, msg.substring(4000 * i, max));
+                        }
+                    }
+                } else {
+                    Log.d(tag, msg);
+                }
             } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                 Log.w(tag, msg);
             } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
@@ -131,6 +176,23 @@ public class MyLog {
         }
     }
 
+    public static void logLongMessage(String tag, String message) {
+        if (message.length() > 4000) {
+            // 因为Logcat一次最多能输出4000个字符左右,所以我们按4000字符分段
+            int chunkCount = message.length() / 4000; // 分割成多少段
+            for (int i = 0; i <= chunkCount; i++) {
+                int max = 4000 * (i + 1);
+                if (max >= message.length()) {
+                    Log.d(tag, message.substring(4000 * i));
+                } else {
+                    Log.d(tag, message.substring(4000 * i, max));
+                }
+            }
+        } else {
+            Log.d(tag, message);
+        }
+    }
+
     /**
      * 打开日志文件并写入日志,
      **/

+ 27 - 6
app/src/main/res/xml/file_paths.xml

@@ -1,13 +1,34 @@
 <?xml version="1.0" encoding="utf-8"?>
-<paths xmlns:android="http://schemas.android.com/apk/res/android">
-
-   <!-- <external-path
-        path="Android/data/erp.erp.huiyan_wms/"
-        name="files_root" />
-    <external-path path="." name="external_storage_root" />-->
+<!--<paths xmlns:android="http://schemas.android.com/apk/res/android">
 
     <external-path name="files_root" path="Android/data/uas.erp.huiyan_wms"/>
 
     <external-path name="external_storage_root" path="."/>
 
+</paths>-->
+<paths>
+<!-- 外部存储根目录 -->
+<external-path
+    name="external"
+    path="." />
+
+<!-- 下载目录 -->
+<external-path
+    name="download"
+    path="Download" />
+
+<!-- 应用私有目录 -->
+<external-files-path
+    name="files"
+    path="." />
+
+<!-- 缓存目录 -->
+<external-cache-path
+    name="cache"
+    path="." />
+
+<!-- 内部存储目录 -->
+<files-path
+    name="internal"
+    path="." />
 </paths>

+ 2 - 2
build.gradle

@@ -53,8 +53,8 @@ ext {
             targetSdkVersion : 31,
             compileSdkVersion: 31,
             buildToolsVersion: "35.0.0",
-            versionCode      : 28,
-            versionName      : "1.2.7"
+            versionCode      : 29,
+            versionName      : "1.2.8"
     ]
 
     depsVersion = [