Procházet zdrojové kódy

1.修改FileProvider的名称

20241218TB223FC(测试jar包)
wangwanlei před 9 měsíci
rodič
revize
fac5060e73

+ 1
- 1
app/src/main/AndroidManifest.xml Zobrazit soubor

@@ -161,7 +161,7 @@
161 161
 
162 162
         <provider
163 163
             android:name="androidx.core.content.FileProvider"
164
-            android:authorities="com.xhly.manageapp.fileprovider"
164
+            android:authorities="com.xhkjedu.manageapp.fileprovider"
165 165
             android:exported="false"
166 166
             android:grantUriPermissions="true">
167 167
             <!-- 元数据 -->

+ 121
- 97
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt Zobrazit soubor

@@ -7,7 +7,6 @@ import android.app.ActivityManager
7 7
 import android.content.Context
8 8
 import android.content.Intent
9 9
 import android.content.IntentFilter
10
-import android.content.pm.PackageInfo
11 10
 import android.content.pm.PackageManager
12 11
 import android.graphics.Color
13 12
 import android.graphics.drawable.BitmapDrawable
@@ -44,7 +43,7 @@ import com.xhly.corelib.utils.FileUtils
44 43
 import com.xhly.corelib.utils.GsonUtils
45 44
 import com.xhly.corelib.utils.ImageUtils
46 45
 import com.xhly.corelib.utils.LogShow
47
-import com.xhly.corelib.utils.MD5Utils
46
+import com.xhly.corelib.utils.ModelNameUtils
48 47
 import com.xhly.corelib.utils.SharedPreferencesUtils
49 48
 import com.xhly.corelib.utils.SystemUtil
50 49
 import com.xhly.manageapp.AppAdapter
@@ -52,7 +51,6 @@ import com.xhly.manageapp.ManageApplication
52 51
 import com.xhly.manageapp.bean.EventLog
53 52
 import com.xhly.manageapp.bean.SocketMsgBean
54 53
 import com.xhly.manageapp.bean.app.AppInstallBean
55
-import com.xhly.manageapp.bean.app.AppModel
56 54
 import com.xhly.manageapp.bean.log.LogPushBean
57 55
 import com.xhly.manageapp.bean.log.LogdOperateBean
58 56
 import com.xhly.manageapp.bean.log.UpdateBean
@@ -89,12 +87,15 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
89 87
     private var userBean: UserBean? = null
90 88
     private var timer: Timer? = null
91 89
     private val currentSpUtils by lazy { CurrentAppSharedPreferencesUtils.getInstance(this) }
90
+
92 91
     //标记是否请求了时间策略
93
-    private var updateTimeFlag=false
92
+    private var updateTimeFlag = false
93
+
94 94
     //标记是否请求了应用策略,都为真则提示
95
-    private var updateStrategyFlag=false
95
+    private var updateStrategyFlag = false
96 96
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
97 97
 
98
+    @SuppressLint("CheckResult")
98 99
     override fun initData() {
99 100
         val json = spUtils.getFromJson(Const.USERINFO, UserBean().javaClass)
100 101
         if (json == null) {
@@ -104,19 +105,19 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
104 105
             userBean = json as UserBean
105 106
         }
106 107
         lifecycleScope.launch {
107
-            withContext(Dispatchers.IO){
108
+            withContext(Dispatchers.IO) {
108 109
                 val file = File(filesDir, "wallpaper.png")
109
-                if (file.exists()){
110
+                if (file.exists()) {
110 111
                     try {
111 112
                         val bitmap = ImageUtils().getBitmapFromFile(file.toString())
112
-                        withContext(Dispatchers.Main){
113
+                        withContext(Dispatchers.Main) {
113 114
                             try {
114
-                                mBinding.root.background=BitmapDrawable(resources,bitmap)
115
-                            }catch (e:Exception){
115
+                                mBinding.root.background = BitmapDrawable(resources, bitmap)
116
+                            } catch (e: Exception) {
116 117
                                 LogShow("图片设置失败")
117 118
                             }
118 119
                         }
119
-                    }catch (e:Exception){
120
+                    } catch (e: Exception) {
120 121
                         LogShow("图片不存在")
121 122
                     }
122 123
                 }
@@ -176,51 +177,63 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
176 177
         }
177 178
         viewModel.forceListAppData.observe(this) { modelList ->
178 179
             //获得必装应用,未安装或者版本较低则更新,先删除目录中的文件
179
-            val appList = AppUtils.GetAppList(this)
180
-            getExternalFilesDir(null)?.let {
181
-                FileUtils.deleteCurrentFlie(it)
182
-            }
183
-            modelList.forEach { app ->
184
-               try {
185
-                   //获得已安装的应用判断版本,未安装的则直接静默安装。
186
-                   val filter = appList.filter { it.packageName.equals(app.appPackage) }
187
-                   //列表中不存在,或者版本较低则更新
188
-                   val apkPath = app.apkPath
189
-                   val appName = app.appName
190
-                   if (filter.isNotEmpty()) {
191
-                       val currentApp = filter[0]
192
-                       if (app.versionNum > currentApp.versionNum) {
193
-                           //版本低则更新安装。
194
-                           viewModel.viewModelScope.launch {
195
-                               AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
196
-                           }
197
-                       }
198
-                   } else {
199
-                       //不存在,直接下载安装。
200
-                       viewModel.viewModelScope.launch {
201
-                           AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
202
-                       }
203
-                   }
204
-               }catch (e:Exception){
205
-                   LogShow("问题"+e.toString())
206
-               }
207
-            }
180
+            val rxPermissions = RxPermissions(this)
181
+            rxPermissions.request(Manifest.permission.REQUEST_INSTALL_PACKAGES)
182
+                .subscribe { granted ->
183
+                    if (granted || ModelNameUtils.IS_LianxX505f()) {
184
+                        // 权限已授予,执行安装操作
185
+                        val appList = AppUtils.GetAppList(this)
186
+                        getExternalFilesDir(null)?.let {
187
+                            FileUtils.deleteCurrentFlie(it)
188
+                        }
189
+                        modelList.forEach { app ->
190
+                            try {
191
+                                //获得已安装的应用判断版本,未安装的则直接静默安装。
192
+                                val filter =
193
+                                    appList.filter { it.packageName.equals(app.appPackage) }
194
+                                //列表中不存在,或者版本较低则更新
195
+                                val apkPath = app.apkPath
196
+                                val appName = app.appName
197
+                                if (filter.isNotEmpty()) {
198
+                                    val currentApp = filter[0]
199
+                                    if (app.versionNum > currentApp.versionNum) {
200
+                                        //版本低则更新安装。
201
+                                        viewModel.viewModelScope.launch {
202
+                                            AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
203
+                                        }
204
+                                    }
205
+                                } else {
206
+                                    //不存在,直接下载安装。
207
+                                    viewModel.viewModelScope.launch {
208
+                                        AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
209
+                                    }
210
+                                }
211
+                            } catch (e: Exception) {
212
+                                LogShow("问题" + e.toString())
213
+                            }
214
+                        }
215
+                    } else {
216
+                        // 用户拒绝了安装权限,处理相应逻辑
217
+                        viewModel.getAppList()
218
+                    }
219
+
220
+                }
208 221
         }
209 222
 
210 223
         viewModel.strategyData.observe(this) {
211 224
             spUtils.saveJson(Const.STRATEGYBEANKEY, it)
212 225
             StrategyUtils.setStrategy(it)
213 226
             useTimeStrategy()
214
-            updateStrategyFlag=true
215
-            if (updateTimeFlag){
227
+            updateStrategyFlag = true
228
+            if (updateTimeFlag) {
216 229
                 showSucessToast()
217 230
             }
218 231
         }
219 232
         viewModel.appStrategyData.observe(this) {
220 233
             //存储app时间策略
221 234
             spUtils.saveJson(Const.APPSTRATEGYBEANKEY, it)
222
-            updateTimeFlag=true
223
-            if (updateStrategyFlag){
235
+            updateTimeFlag = true
236
+            if (updateStrategyFlag) {
224 237
                 showSucessToast()
225 238
             }
226 239
         }
@@ -270,8 +283,11 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
270 283
                             transition: Transition<in Drawable>?
271 284
                         ) {
272 285
                             mBinding.root.background = resource
273
-                            val imageUtil=ImageUtils()
274
-                            imageUtil.saveBitmapToFile(imageUtil.drawableToBitmap(resource), File(filesDir,"wallpaper.png"))
286
+                            val imageUtil = ImageUtils()
287
+                            imageUtil.saveBitmapToFile(
288
+                                imageUtil.drawableToBitmap(resource),
289
+                                File(filesDir, "wallpaper.png")
290
+                            )
275 291
                         }
276 292
                     })
277 293
             }
@@ -286,55 +302,61 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
286 302
                 Toast(getString(R.string.codeerror))
287 303
             }
288 304
         }
289
-        viewModel.lockResultData.observe(this){
290
-            if (it){
305
+        viewModel.lockResultData.observe(this) {
306
+            if (it) {
291 307
                 spUtils.setParam(Const.DISABLEPAD, true)
292 308
                 startIntentActivity(MainActivity().javaClass)
293 309
                 val appList = AppUtils.GetAppList(this)
294 310
                 StrategyUtils.clearMemory(this, appList)
295 311
             }
296 312
         }
297
-        viewModel.resetlResultData.observe(this){
298
-            if (it){
313
+        viewModel.resetlResultData.observe(this) {
314
+            if (it) {
299 315
                 ManageApplication.launchFactoryReset()
300 316
             }
301 317
         }
302
-        viewModel.logResultData.observe(this){
303
-            if (it.enableFlag){
304
-                when(it.pushType){
305
-                    2->{
318
+        viewModel.logResultData.observe(this) {
319
+            if (it.enableFlag) {
320
+                when (it.pushType) {
321
+                    2 -> {
306 322
                         spUtils.setParam(Const.DISABLEPAD, false)
307 323
                         useTimeStrategy()
308 324
                     }
309
-                    3->{
325
+
326
+                    3 -> {
310 327
                         val appList = AppUtils.GetAppList(this)
311 328
                         StrategyUtils.clearMemory(this, appList)
312 329
                         spUtils.setParam(Const.DISABLEPAD, true)
313 330
                         startIntentActivity(MainActivity().javaClass)
314 331
                     }
315
-                    4->{
332
+
333
+                    4 -> {
316 334
                         //请求接口更新策略
317 335
                         updateAllStrategy()
318 336
                     }
319
-                    5->{
337
+
338
+                    5 -> {
320 339
                         ManageApplication.rebootDevice()
321 340
                     }
322
-                    6->{
341
+
342
+                    6 -> {
323 343
                         ManageApplication.launchFactoryReset()
324 344
                     }
325
-                    7->{
326
-                        currentSpUtils.setParam(Const.EXIT_CONTROL,true)
345
+
346
+                    7 -> {
347
+                        currentSpUtils.setParam(Const.EXIT_CONTROL, true)
327 348
                         StrategyUtils.releaseControl(this)
328 349
                     }
329
-                    8->{
350
+
351
+                    8 -> {
330 352
                         loginOut()
331 353
                     }
332 354
                 }
333 355
             }
334 356
         }
335
-        viewModel.logEventData.observe(this){
336
-            when(it.logEventType){
337
-                EventLog.ACCOUNTQUITEVENT->{
357
+        viewModel.logEventData.observe(this) {
358
+            when (it.logEventType) {
359
+                EventLog.ACCOUNTQUITEVENT -> {
338 360
                     loginOutSucess()
339 361
                 }
340 362
             }
@@ -345,8 +367,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
345 367
 
346 368
     private fun showSucessToast() {
347 369
         Toaster.show(R.string.updatetips)
348
-        updateStrategyFlag=false
349
-        updateTimeFlag=false
370
+        updateStrategyFlag = false
371
+        updateTimeFlag = false
350 372
     }
351 373
 
352 374
 
@@ -389,8 +411,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
389 411
         onBackPressedDispatcher.addCallback {
390 412
             //屏蔽返回键
391 413
             Const.backNum++
392
-            if (Const.backNum>Const.UPDATESTRATEGYNUM){
393
-                Const.backNum=0
414
+            if (Const.backNum > Const.UPDATESTRATEGYNUM) {
415
+                Const.backNum = 0
394 416
                 Toaster.show(R.string.updatetips)
395 417
                 updateAllStrategy()
396 418
             }
@@ -486,7 +508,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
486 508
         getLocation()
487 509
     }
488 510
 
489
-    private fun updateAllStrategy(){
511
+    private fun updateAllStrategy() {
490 512
         userBean?.let {
491 513
             Const.USERID = it.userid
492 514
             Const.USERNAME = it.username.toString()
@@ -523,7 +545,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
523 545
                 val removeList = arrayListOf<AppInfo>()
524 546
                 currentList.forEach { pkg ->
525 547
                     //除了管控之类的我们的app之外的预装都不在统计范围
526
-                    if (Const.CURRENTAPPPKG!=pkg&&Const.STOREAPPPKG!=pkg) {
548
+                    if (Const.CURRENTAPPPKG != pkg && Const.STOREAPPPKG != pkg) {
527 549
                         appList.filter { it.packageName == pkg }.forEach {
528 550
                             removeList.add(it)
529 551
                         }
@@ -535,27 +557,27 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
535 557
 
536 558
             }
537 559
             val userBean = spUtils.getFromJson(Const.USERINFO, UserBean().javaClass) as UserBean
538
-            val appInstallList= arrayListOf<AppInstallBean>()
560
+            val appInstallList = arrayListOf<AppInstallBean>()
539 561
             appList.forEach {
540
-                val appInstallBean=AppInstallBean()
541
-                appInstallBean.userid=userBean.userid
542
-                appInstallBean.appName=it.name
543
-                appInstallBean.appVersion=it.versionName
544
-                appInstallBean.appNum=it.versionNum
545
-                appInstallBean.appPackage=it.packageName
546
-                ManageApplication.getDeviceInfo()?.let {sn->
562
+                val appInstallBean = AppInstallBean()
563
+                appInstallBean.userid = userBean.userid
564
+                appInstallBean.appName = it.name
565
+                appInstallBean.appVersion = it.versionName
566
+                appInstallBean.appNum = it.versionNum
567
+                appInstallBean.appPackage = it.packageName
568
+                ManageApplication.getDeviceInfo()?.let { sn ->
547 569
                     appInstallBean.sn = sn
548 570
                 }
549
-                ManageApplication.getDeviceInfo(1)?.let {mac->
571
+                ManageApplication.getDeviceInfo(1)?.let { mac ->
550 572
                     appInstallBean.mac = mac
551 573
                 }
552
-                appInstallBean.schoolid=userBean.schoolid
553
-                appInstallBean.regionid=userBean.regionid
574
+                appInstallBean.schoolid = userBean.schoolid
575
+                appInstallBean.regionid = userBean.regionid
554 576
                 appInstallList.add(appInstallBean)
555 577
             }
556 578
 
557 579
             viewModel.updateAppInstallInfo(appInstallList)
558
-        }catch (e:Exception){
580
+        } catch (e: Exception) {
559 581
 
560 582
         }
561 583
     }
@@ -594,7 +616,6 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
594 616
     }
595 617
 
596 618
 
597
-
598 619
     private fun updateMcApp() {
599 620
         //每天请求一次app更新接口,这个接口是用于提交管控自身的版本信息统计
600 621
         try {
@@ -618,7 +639,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
618 639
                 updateBean.schoolid = userBean.schoolid
619 640
                 updateBean.userid = userBean.userid
620 641
                 updateBean.regionid = userBean.regionid
621
-               viewModel.updateMcApp(updateBean)
642
+                viewModel.updateMcApp(updateBean)
622 643
             }
623 644
         } catch (e: Exception) {
624 645
 
@@ -807,7 +828,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
807 828
                     data.doEvent = EventLog.APPINSTALLEVENT
808 829
                     viewModel.eventLog(data)
809 830
                     updateAppInstallInfo()
810
-                }catch (e:Exception){
831
+                } catch (e: Exception) {
811 832
 
812 833
                 }
813 834
             }
@@ -891,22 +912,25 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
891 912
 
892 913
             Const.CODE3002.toString() -> {
893 914
                 val deviceInfo = ManageApplication.getDeviceInfo()
894
-                val sn= deviceInfo ?: ""
915
+                val sn = deviceInfo ?: ""
895 916
                 viewModel.postExitLocked(sn)
896 917
             }
897 918
 
898 919
             Const.CODE3006.toString() -> {
899 920
                 val deviceInfo = ManageApplication.getDeviceInfo()
900
-                val sn= deviceInfo ?: ""
921
+                val sn = deviceInfo ?: ""
901 922
                 viewModel.postExitReseted(sn)
902 923
             }
924
+            Const.INSTALL_FORCEAPP->{
925
+                ManageApplication.installPackage(uiEvent.message)
926
+            }
903 927
         }
904 928
     }
905 929
 
906 930
     private fun postLogPush(pushType: Int) {
907 931
         val pushBean = LogPushBean()
908 932
         pushBean.pushType = pushType
909
-        pushBean.sn=ManageApplication.getDeviceInfo()?:""
933
+        pushBean.sn = ManageApplication.getDeviceInfo() ?: ""
910 934
         viewModel.postLogPushAdd(pushBean)
911 935
     }
912 936
 
@@ -915,13 +939,13 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
915 939
             //获得预装应用和商店应用,商店应用和预装重叠直接显示,其余预装直接隐藏
916 940
             var schoolDeviceSetBean: SchoolDeviceSetBean? = null
917 941
             schoolDeviceSetBean = StrategyUtils.getSchoolDeviceSetBean(this)
918
-            val storeList= arrayListOf<String>()
942
+            val storeList = arrayListOf<String>()
919 943
             try {
920 944
                 val fromJson = spUtils.getFromJson(Const.APPSTOREKEY, ArrayList<String>().javaClass)
921
-                if (fromJson!=null){
945
+                if (fromJson != null) {
922 946
                     storeList.addAll(fromJson as ArrayList<String>)
923 947
                 }
924
-            }catch (e:Exception){
948
+            } catch (e: Exception) {
925 949
                 LogShow("商店应用为空")
926 950
             }
927 951
             dataList.clear()
@@ -937,7 +961,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
937 961
                     ) as ArrayList<String>
938 962
                 currentList.forEach { pkg ->
939 963
                     //学校控制之外的预装应用加入移除集合,应用商店列表包含的不需要加入移除集合
940
-                    if (!Const.schoolSetAppList.contains(pkg)&&!storeList.contains(pkg)) {
964
+                    if (!Const.schoolSetAppList.contains(pkg) && !storeList.contains(pkg)) {
941 965
                         appList.filter { it.packageName == pkg }.forEach {
942 966
                             removeList.add(it)
943 967
                         }
@@ -947,7 +971,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
947 971
 
948 972
             }
949 973
             //如果包含应用商店也移除,因为会在自定义app中添加
950
-            appList.filter { it.packageName == Const.STOREAPPPKG}.forEach {
974
+            appList.filter { it.packageName == Const.STOREAPPPKG }.forEach {
951 975
                 removeList.add(it)
952 976
             }
953 977
             //移除学校控制之外预装应用
@@ -1068,9 +1092,9 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
1068 1092
             .setAlign(CustomDialog.ALIGN.CENTER).show()
1069 1093
     }
1070 1094
 
1071
-    private fun loginOut(hasUser:Boolean=true) {
1095
+    private fun loginOut(hasUser: Boolean = true) {
1072 1096
         //没有用户信息时,判断为初次调用,直接跳转登录页面
1073
-        if (hasUser){
1097
+        if (hasUser) {
1074 1098
             //调用接口传递退出事件,成功则退出登录
1075 1099
             val data = LogdOperateBean()
1076 1100
             ManageApplication.getDeviceInfo()?.let {
@@ -1079,12 +1103,12 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
1079 1103
             data.comm = "退出登录"
1080 1104
             data.doEvent = EventLog.ACCOUNTQUITEVENT
1081 1105
             viewModel.eventLog(data)
1082
-        }else{
1106
+        } else {
1083 1107
             loginOutSucess()
1084 1108
         }
1085 1109
     }
1086 1110
 
1087
-    private fun loginOutSucess(){
1111
+    private fun loginOutSucess() {
1088 1112
         WorkManager.getInstance(this).cancelAllWork()
1089 1113
         deleteSharedPreferences(SharedPreferencesUtils.SP_FILE_NAME)
1090 1114
         Const.USERID = null

+ 8
- 3
app/src/main/java/com/xhly/manageapp/utils/AppDownLoadUtils.kt Zobrazit soubor

@@ -3,6 +3,7 @@ package com.xhly.manageapp.utils
3 3
 import com.billbook.lib.downloader.Download
4 4
 import com.billbook.lib.downloader.Downloader
5 5
 import com.xhly.corelib.Const
6
+import com.xhly.corelib.eventbus.UIEvent
6 7
 import com.xhly.corelib.utils.LogShow
7 8
 import com.xhly.manageapp.ManageApplication
8 9
 import kotlinx.coroutines.Dispatchers
@@ -49,9 +50,7 @@ object AppDownLoadUtils {
49 50
                         successful = execute.isSuccessful()
50 51
                         LogShow("下载结果" + successful)
51 52
                         if (successful) {
52
-                            withContext(Dispatchers.Main) {
53
-                                ManageApplication.installPackage(File(name, "$appName.apk").toString())
54
-                            }
53
+                            UIEvent(Const.INSTALL_FORCEAPP).setMessage(File(name, "$appName.apk").toString()).post()
55 54
                         } else {
56 55
                             currentRetry++
57 56
                         }
@@ -65,4 +64,10 @@ object AppDownLoadUtils {
65 64
             }
66 65
         }
67 66
     }
67
+
68
+    data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int)
69
+
70
+    interface DownloadCallback {
71
+        fun onSuccess(downItem: DownloadItem)
72
+    }
68 73
 }

+ 126
- 0
app/src/main/java/com/xhly/manageapp/utils/DownLoadUtils.kt Zobrazit soubor

@@ -0,0 +1,126 @@
1
+import android.util.Log
2
+import com.billbook.lib.downloader.Download
3
+import com.billbook.lib.downloader.Downloader
4
+import kotlinx.coroutines.CoroutineScope
5
+import kotlinx.coroutines.Dispatchers
6
+import kotlinx.coroutines.launch
7
+import java.util.LinkedList
8
+import java.util.Queue
9
+
10
+object DownLoadUtils {
11
+    val queue: Queue<DownloadItem> = LinkedList()
12
+    var callbackList = mutableListOf<DownloadCallback>()
13
+    var isRunning = false
14
+
15
+    fun isEmpty(): Boolean {
16
+        return queue.isEmpty()
17
+    }
18
+
19
+    /**
20
+     * 页面启动 添加回调
21
+     */
22
+    fun addCallback(callback: DownloadCallback) {
23
+        callbackList.add(callback)
24
+    }
25
+
26
+
27
+    /**
28
+     * 页面关闭 删除回调
29
+     */
30
+    fun removeCallback(callback: DownloadCallback) {
31
+        callbackList.remove(callback)
32
+    }
33
+
34
+
35
+    /**
36
+     * 下载文件
37
+     */
38
+    fun addDownload(url: String, tag: String, savepath: String) {
39
+        var isHave = false
40
+        for (element in queue) {
41
+            if (element.tag === tag) {
42
+                isHave = true
43
+            }
44
+        }
45
+        if (!isHave) {
46
+            queue.add(DownloadItem(url, tag, savepath, 0))
47
+        } else {
48
+            for (downloadCallback in callbackList) {
49
+                downloadCallback.onFailure(DownloadItem(url, tag, savepath, 0), "已经在下载队列")
50
+            }
51
+        }
52
+        downloadAction()
53
+    }
54
+
55
+    private fun downloadAction() {
56
+        if (isRunning) {
57
+            return
58
+        }
59
+        val coroutineScope = CoroutineScope(Dispatchers.IO)
60
+        coroutineScope.launch {
61
+            while (queue.size > 0) {
62
+                isRunning = true
63
+                val downItem = queue.peek()
64
+                downItem?.let {
65
+                    try {
66
+                        val downloader = Downloader.Builder().build()
67
+                        val request = Download.Request.Builder()
68
+                            .url(it.url)
69
+                            .into(it.savepath)
70
+                            .build()
71
+                        val call = downloader.newCall(request)
72
+                        call.execute(object : Download.Callback {
73
+                            override fun onStart(call: Download.Call) {
74
+                                for (downloadCallback in callbackList) {
75
+                                    downloadCallback.onStart(downItem)
76
+                                }
77
+                            }
78
+
79
+                            override fun onSuccess(call: Download.Call, response: Download.Response) {
80
+                                queue.poll()
81
+                                for (downloadCallback in callbackList) {
82
+                                    downloadCallback.onSuccess(downItem)
83
+                                }
84
+                            }
85
+
86
+                            override fun onFailure(call: Download.Call, response: Download.Response) {
87
+                                queue.poll()
88
+                                for (downloadCallback in callbackList) {
89
+                                    downloadCallback.onFailure(downItem, response.message!!)
90
+                                }
91
+                            }
92
+
93
+                            override fun onLoading(call: Download.Call, current: Long, total: Long) {
94
+                                val progress = (current * 100 / total).toInt()
95
+//                                Log.i("详情", "$progress ${downItem.progress} ${callbackList.size}")
96
+                                if (progress != downItem.progress) {
97
+                                    for (downloadCallback in callbackList) {
98
+                                        downItem.progress = progress
99
+                                        downloadCallback.onLoading(downItem)
100
+                                    }
101
+                                }
102
+                            }
103
+                        })
104
+                    } catch (e: Exception) {
105
+                        queue.poll()
106
+                        for (downloadCallback in callbackList) {
107
+                            downloadCallback.onFailure(downItem, e.toString())
108
+                        }
109
+                    }
110
+                }
111
+
112
+            }
113
+            isRunning = false
114
+        }
115
+
116
+    }
117
+}
118
+
119
+data class DownloadItem(val url: String, val tag: String, val savepath: String, var progress: Int)
120
+
121
+interface DownloadCallback {
122
+    fun onStart(downItem: DownloadItem)
123
+    fun onSuccess(downItem: DownloadItem)
124
+    fun onFailure(downItem: DownloadItem, errMsg: String)
125
+    fun onLoading(downItem: DownloadItem)
126
+}

+ 1
- 0
app/src/main/res/xml/file_paths.xml Zobrazit soubor

@@ -3,5 +3,6 @@
3 3
     <paths>
4 4
         <external-path path="Android/data/com.example.iu/" name="files_root" />
5 5
         <external-path path="." name="external_storage_root" />
6
+        <files-path name="data" path="."/>
6 7
     </paths>
7 8
 </resources>

+ 4
- 1
corelib/src/main/java/com/xhly/corelib/Const.kt Zobrazit soubor

@@ -71,7 +71,10 @@ object Const {
71 71
      */
72 72
     const val ACCOUNTLOGINOUT = "ACCOUNTLOGINOUT"
73 73
 
74
-
74
+    /**
75
+     * 调用安装app方法
76
+     */
77
+    const val INSTALL_FORCEAPP="INSTALL_FORCEAPP"
75 78
     /**
76 79
      * 存储应用商店应用包名集合
77 80
      */

Načítá se…
Zrušit
Uložit