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

1.添加安装时版本对比。

2025_1_17_tb223fc
wangwanlei 1 месяц назад
Родитель
Сommit
da38aac31a

+ 13
- 3
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt Просмотреть файл

@@ -442,7 +442,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
442 442
                                                             "$appName.apk"
443 443
                                                         ).toString(),
444 444
                                                         app.appPackage,
445
-                                                        appName
445
+                                                        appName,
446
+                                                        app.versionNum.toLong()
446 447
                                                     )
447 448
                                                 }
448 449
                                             } else {
@@ -456,7 +457,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
456 457
                                                         "$appName.apk"
457 458
                                                     ).toString(),
458 459
                                                     app.appPackage,
459
-                                                    appName
460
+                                                    appName,
461
+                                                    app.versionNum.toLong()
460 462
                                                 )
461 463
                                             }
462 464
                                         } catch (e: Exception) {
@@ -2394,7 +2396,15 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(), Downloa
2394 2396
         /* ManageApplication.installPackage(downItem.savepath)*/
2395 2397
         //进入应用商店才安装
2396 2398
         if (!Const.startAppStoreFlag) {
2397
-            InstallUtils.addInstall(downItem.savepath, downItem.pkgName, downItem.appName)
2399
+            val appList = AppUtils.GetAppList(this)
2400
+            val firstOrNull = appList.firstOrNull { it.packageName.equals(downItem.pkgName) }
2401
+            if (firstOrNull!=null){
2402
+                if (downItem.appversion>firstOrNull.versionNum){
2403
+                    InstallUtils.addInstall(downItem.savepath, downItem.pkgName, downItem.appName)
2404
+                }
2405
+            }else{
2406
+                InstallUtils.addInstall(downItem.savepath, downItem.pkgName, downItem.appName)
2407
+            }
2398 2408
         }
2399 2409
     }
2400 2410
 

+ 4
- 4
app/src/main/java/com/xhly/manageapp/utils/DownLoadUtils.kt Просмотреть файл

@@ -38,7 +38,7 @@ object DownLoadUtils {
38 38
     /**
39 39
      * 下载文件
40 40
      */
41
-    fun addDownload(url: String, tag: String, savepath: String,pkgName: String,appName:String) {
41
+    fun addDownload(url: String, tag: String, savepath: String,pkgName: String,appName:String,appversion:Long) {
42 42
         LogShow("添加下载任务"+savepath)
43 43
         var isHave = false
44 44
         for (element in queue) {
@@ -47,10 +47,10 @@ object DownLoadUtils {
47 47
             }
48 48
         }
49 49
         if (!isHave) {
50
-            queue.add(DownloadItem(url, tag, savepath, 0,pkgName,appName))
50
+            queue.add(DownloadItem(url, tag, savepath, 0,pkgName,appName,appversion))
51 51
         } else {
52 52
             for (downloadCallback in callbackList) {
53
-                downloadCallback.onFailure(DownloadItem(url, tag, savepath, 0,pkgName,appName), "已经在下载队列")
53
+                downloadCallback.onFailure(DownloadItem(url, tag, savepath, 0,pkgName,appName,appversion), "已经在下载队列")
54 54
             }
55 55
         }
56 56
         downloadAction()
@@ -136,7 +136,7 @@ object DownLoadUtils {
136 136
     }
137 137
 }
138 138
 
139
-data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int,var pkgName:String,var appName:String)
139
+data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int,var pkgName:String,var appName:String,var appversion: Long)
140 140
 
141 141
 interface DownloadCallback {
142 142
     fun onStart(downItem: DownloadItem)

+ 139
- 0
corelib/src/main/java/com/xhly/corelib/network/CustomLogInterceptor.kt Просмотреть файл

@@ -0,0 +1,139 @@
1
+import com.xhly.corelib.utils.LogShow
2
+import okhttp3.Interceptor
3
+import okhttp3.Request
4
+import okhttp3.Response
5
+import okio.Buffer
6
+import java.nio.charset.Charset
7
+import java.text.SimpleDateFormat
8
+
9
+class CustomLogInterceptor : Interceptor {
10
+    override fun intercept(chain: Interceptor.Chain): Response {
11
+        val request = chain.request()
12
+        val requestLog = generateRequestLog(request)
13
+        val response = chain.proceed(request)
14
+        val responseLog = generateResponseLog(response)
15
+        LogShow(requestLog.plus(responseLog))
16
+        return response
17
+    }
18
+
19
+    private fun generateResponseLog(response: Response?): String {
20
+        if (response == null) {
21
+            return ""
22
+        }
23
+        return "Response Time-->:${
24
+            getDateString(System.currentTimeMillis())
25
+        } \r\n Response Result ${
26
+            if (response.code != 200)
27
+                response.code
28
+            else
29
+                ""
30
+        } -->:${
31
+            getResponseText(response)
32
+        }"
33
+    }
34
+
35
+    private fun generateRequestLog(request: Request?): String {
36
+        if (request == null) {
37
+            return ""
38
+        }
39
+        val requestParams = getRequestParams(request)
40
+        val needPrintRequestParams = requestParams.contains("IsFile").not()
41
+        return "自定义日志打印 \r\n Request Time-->:${
42
+            getDateString(System.currentTimeMillis())
43
+        } \r\n Request Url-->:${request.method} ${request.url} \r\n Request Header-->:${
44
+            getRequestHeaders(
45
+                request
46
+            )
47
+        } \r\n Request Parameters-->:${
48
+            if (needPrintRequestParams)
49
+                requestParams
50
+            else
51
+                "文件上传,不打印请求参数"
52
+        } \r\n "
53
+    }
54
+
55
+    @Deprecated("unused")
56
+    private fun printInfo(request: Request?, response: Response?) {
57
+        if (request != null && response != null) {
58
+            val requestParams = getRequestParams(request)
59
+            val needPrintRequestParams = requestParams.contains("IsFile").not()
60
+            val logInfo =
61
+                "自定义日志打印 \r\n Request Url-->:${request.method} ${request.url} \r\n Request Header-->:${
62
+                    getRequestHeaders(
63
+                        request
64
+                    )
65
+                } \r\n Request Parameters-->:${
66
+                    if (needPrintRequestParams)
67
+                        requestParams
68
+                    else
69
+                        "文件上传,不打印请求参数"
70
+                } \r\n Response Result ${
71
+                    if (response.code != 200)
72
+                        response.code
73
+                    else
74
+                        ""
75
+                } -->:${
76
+                    getResponseText(response)
77
+                }"
78
+            LogShow(logInfo)
79
+        }
80
+    }
81
+
82
+    /**
83
+     * 获取请求参数
84
+     */
85
+    private fun getRequestParams(request: Request): String {
86
+        var str: String? = null
87
+        try {
88
+            request.body?.let {
89
+                val buffer = Buffer()
90
+                it.writeTo(buffer)
91
+                val charset = it.contentType()?.charset(Charset.forName("UTF-8"))
92
+                    ?: Charset.forName("UTF-8")
93
+                str = buffer.readString(charset)
94
+            }
95
+        } catch (e: Exception) {
96
+            e.printStackTrace()
97
+        }
98
+
99
+        return if (str.isNullOrEmpty()) "Empty!" else str!!
100
+    }
101
+
102
+    private fun getRequestHeaders(request: Request): String {
103
+        val headers = request.headers
104
+        return if (headers.size > 0) {
105
+            headers.toString()
106
+        } else {
107
+            "Empty!"
108
+        }
109
+    }
110
+
111
+    /**
112
+     * 获取返回数据字符串
113
+     */
114
+    private fun getResponseText(response: Response): String {
115
+        try {
116
+            response.body?.let {
117
+                val source = it.source()
118
+                source.request(Long.MAX_VALUE)
119
+                val buffer = source.buffer
120
+                val charset = it.contentType()?.charset(Charset.forName("UTF-8"))
121
+                    ?: Charset.forName("UTF-8")
122
+                if (it.contentLength().toInt() != 0) {
123
+                    buffer.clone().readString(charset).let { result ->
124
+                        return result
125
+                    }
126
+                }
127
+            }
128
+        } catch (e: Exception) {
129
+            e.printStackTrace()
130
+        }
131
+
132
+        return "Empty!"
133
+    }
134
+
135
+    fun getDateString(time: Long?): String? {
136
+        val dateFormat = SimpleDateFormat("yyyy年MM月d日 HH:mm:ss")
137
+        return dateFormat.format(time)
138
+    }
139
+}

Загрузка…
Отмена
Сохранить