Przeglądaj źródła

1.增加socket消息处理。

2.拦截器临时使用其他设备型号用来请求app列表。
3.修改通知弹窗布局。
4.应用下载添加队列。
master
wangwanlei 9 miesięcy temu
rodzic
commit
0f6eebb7a3

+ 15
- 10
app/src/main/java/com/xhly/manageapp/AppAdapter.kt Wyświetl plik

@@ -3,6 +3,8 @@ package com.xhly.manageapp
3 3
 import android.annotation.SuppressLint
4 4
 import android.app.ActivityManager
5 5
 import android.content.Context
6
+import android.content.Intent
7
+import android.provider.Settings
6 8
 import android.view.LayoutInflater
7 9
 import android.view.ViewGroup
8 10
 import android.widget.Toast
@@ -61,16 +63,19 @@ class AppAdapter(var context: Context, var data: ArrayList<AppInfo>) :
61 63
             holder.mBinding.itemCv.setImageDrawable(appInfo.ico)
62 64
             holder.mBinding.itemCv.setText(appInfo.name)
63 65
             holder.mBinding.itemCv.setOnClickListener {
64
-                var instance = Calendar.getInstance()
65
-                instance.set(Calendar.HOUR_OF_DAY, 11)
66
-                instance.set(Calendar.MINUTE, 0)
67
-               /* context.startActivity(appInfo.intent)*/
68
-                val appTimeStrategy = StrategyUtils.getAppTimeStrategy(context, appInfo.packageName)
69
-                LogShow("可以打开吗"+appTimeStrategy)
70
-                 if (appTimeStrategy){
71
-                    context.startActivity(appInfo.intent)
72
-                }else{
73
-                    Toaster.showLong("不在开放时间。")
66
+                try {
67
+                    var instance = Calendar.getInstance()
68
+                    instance.set(Calendar.HOUR_OF_DAY, 11)
69
+                    instance.set(Calendar.MINUTE, 0)
70
+                    /* context.startActivity(appInfo.intent)*/
71
+                    val appTimeStrategy = StrategyUtils.getAppTimeStrategy(context, appInfo.packageName)
72
+                    if (appTimeStrategy){
73
+                        context.startActivity(appInfo.intent)
74
+                    }else{
75
+                        Toaster.showLong(context.getString(R.string.apptimestoptips))
76
+                    }
77
+                }catch (e:Exception){
78
+                    LogShow("应用跳转问题是"+e.toString())
74 79
                 }
75 80
             }
76 81
         }

+ 6
- 0
app/src/main/java/com/xhly/manageapp/bean/SocketMsgBean.kt Wyświetl plik

@@ -0,0 +1,6 @@
1
+package com.xhly.manageapp.bean
2
+
3
+class SocketMsgBean {
4
+    var content:String?=null
5
+    var title:String?=null
6
+}

+ 81
- 21
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt Wyświetl plik

@@ -16,6 +16,7 @@ import android.widget.EditText
16 16
 import android.widget.TextView
17 17
 import android.widget.Toast
18 18
 import androidx.activity.addCallback
19
+import androidx.lifecycle.viewModelScope
19 20
 import androidx.recyclerview.widget.GridLayoutManager
20 21
 import androidx.work.PeriodicWorkRequest
21 22
 import androidx.work.WorkManager
@@ -27,6 +28,7 @@ import com.xhly.corelib.bean.AppInfo
27 28
 import com.xhly.corelib.eventbus.UIEvent
28 29
 import com.xhly.corelib.utils.AppUtils
29 30
 import com.xhly.corelib.utils.CustomOSUtils
31
+import com.xhly.corelib.utils.GsonUtils
30 32
 import com.xhly.corelib.utils.LogShow
31 33
 import com.xhly.corelib.utils.SharedPreferencesUtils
32 34
 import com.xhly.corelib.utils.SystemUtil
@@ -34,6 +36,7 @@ import com.xhly.manageapp.AppAdapter
34 36
 import com.xhly.manageapp.ManageApplication
35 37
 import com.xhly.manageapp.R
36 38
 import com.xhly.manageapp.bean.EventLog
39
+import com.xhly.manageapp.bean.SocketMsgBean
37 40
 import com.xhly.manageapp.bean.UpdateBean
38 41
 import com.xhly.manageapp.bean.log.LogdOperateBean
39 42
 import com.xhly.manageapp.bean.strategy.StrategyBean
@@ -45,15 +48,17 @@ import com.xhly.manageapp.service.websocket.AppSocket
45 48
 import com.xhly.manageapp.ui.ManageActivity
46 49
 import com.xhly.manageapp.ui.login.activity.LoginActivity
47 50
 import com.xhly.manageapp.ui.main.viewmodel.MainViewModel
51
+import com.xhly.manageapp.utils.AppDownLoadUtils
48 52
 import com.xhly.manageapp.utils.PadInfoUtils
49 53
 import com.xhly.manageapp.utils.StrategyUtils
50 54
 import com.xhly.manageapp.workmanager.TimingWorker
55
+import kotlinx.coroutines.launch
51 56
 import java.util.Calendar
52 57
 import java.util.concurrent.TimeUnit
53 58
 
54 59
 
55 60
 class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
56
-    lateinit var adapter: AppAdapter
61
+    var adapter: AppAdapter? = null
57 62
 
58 63
     private var mMyInstallReceiver: AppInstallReceiver? = null
59 64
     private var dataList: ArrayList<AppInfo> = arrayListOf()
@@ -84,11 +89,41 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
84 89
           startActivity(permissionIntent)*/
85 90
         // csdkManagerUtils.csdkManager.enableAccessibility("com.xhly.manageapp","com.xhly.manageapp.service.TestService",true)
86 91
         viewModel.getAppList()
87
-        viewModel.listAppData.observe(this) {
88
-            //获得应用集合,必装应用和推荐应用,再加上预装应用,其余应用需要静默卸载,必装应用需要进行静默下载和安装。
89
-            var appList = AppUtils.GetAppList(this)
90
-
92
+        viewModel.listAppData.observe(this) { modelList ->
93
+            //获得应用集合,必装应用和推荐应用,再加上预装应用,其余应用需要静默卸载,必装应用需要进行静默下载和安装。安装应用从forceapp接口获取
94
+            val appList = AppUtils.GetAppList(this)
95
+            appList.forEach { app ->
96
+                val filter = modelList.filter { it.appPackage.equals(app.packageName) }
97
+                //列表中不存在则将应用冻结或者卸载,目前是冻结
98
+                ManageApplication.setPackageEnabled(app.packageName, (filter.isNotEmpty()))
99
+            }
91 100
         }
101
+        viewModel.forceListAppData.observe(this) { modelList ->
102
+            //获得必装应用,未安装或者版本较低则更新
103
+            val appList = AppUtils.GetAppList(this)
104
+            modelList.forEach { app ->
105
+                //获得已安装的应用判断版本,未安装的则直接静默安装。
106
+                val filter = appList.filter { it.packageName.equals(app.appPackage) }
107
+                //列表中不存在,或者版本较低则更新
108
+                val apkPath = app.apkPath
109
+                val appName = app.appName
110
+                if (filter.isNotEmpty()) {
111
+                    val currentApp = filter[0]
112
+                    if (app.versionNum > currentApp.versionNum) {
113
+                        //版本低则更新安装。
114
+                        viewModel.viewModelScope.launch {
115
+                            AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
116
+                        }
117
+                    }
118
+                } else {
119
+                    //不存在,直接下载安装。
120
+                    viewModel.viewModelScope.launch {
121
+                        AppDownLoadUtils.addToDownloadQueue(apkPath, appName)
122
+                    }
123
+                }
124
+            }
125
+        }
126
+
92 127
         viewModel.strategyData.observe(this) {
93 128
             spUtils.saveJson(Const.STRATEGYBEANKEY, it)
94 129
             StrategyUtils.setStrategy(it)
@@ -137,7 +172,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
137 172
         }
138 173
         onBackPressedDispatcher.addCallback {
139 174
             //屏蔽返回
140
-            viewModel.getAppList()
175
+            /*    viewModel.getAppList()
176
+                viewModel.getForceAppList()*/
141 177
         }
142 178
     }
143 179
 
@@ -151,6 +187,9 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
151 187
         updatePadInfo()
152 188
         updateMcApp()
153 189
         getLocation()
190
+        ManageApplication.lockTaskPackages().forEach {
191
+            LogShow("获得的姓名是"+it)
192
+        }
154 193
         try {
155 194
             var s =
156 195
                 "厂商" + CustomOSUtils.getSystemBrand() + ",型号" + CustomOSUtils.getSystemDevice() + ",系统版本" + CustomOSUtils.getSystemDisplay()
@@ -298,13 +337,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
298 337
         super.onUiEvent(uiEvent)
299 338
         when (uiEvent.getEvent()) {
300 339
             "update" -> {
301
-                dataList.clear()
302
-                val getAppList1 = AppUtils.GetAppList(this)
303
-                dataList.addAll(getAppList1)
304
-                adapter.notifyDataSetChanged()
305
-                dataList.forEach {
306
-                    LogShow("app包名" + it.packageName)
307
-                }
340
+                updateAppRv()
308 341
             }
309 342
 
310 343
             Const.CODE2001.toString() -> {
@@ -323,6 +356,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
323 356
             }
324 357
 
325 358
             Const.CODE2004.toString() -> {
359
+                //TODO 更新策略是否需要通过应用接口处理应用
326 360
                 //请求接口更新策略
327 361
                 viewModel.uploadAppStrategy()
328 362
                 viewModel.updateStrategy()
@@ -355,14 +389,11 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
355 389
             }
356 390
 
357 391
             Const.APPINSTALL -> {
358
-                try {
359
-                    dataList.clear()
360
-                    val getAppList1 = AppUtils.GetAppList(this)
361
-                    dataList.addAll(getAppList1)
362
-                    adapter.notifyDataSetChanged()
363
-                } catch (e: Exception) {
392
+                updateAppRv()
393
+            }
364 394
 
365
-                }
395
+            Const.APPUNINSTALL -> {
396
+                updateAppRv()
366 397
             }
367 398
 
368 399
             Const.SDINSTALL -> {
@@ -431,6 +462,23 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
431 462
         }
432 463
     }
433 464
 
465
+    private fun updateAppRv() {
466
+        try {
467
+            dataList.clear()
468
+            val appList = AppUtils.GetAppList(this)
469
+            dataList.addAll(appList)
470
+            if (adapter == null) {
471
+                adapter = AppAdapter(this, dataList)
472
+                mBinding.rv.adapter = adapter
473
+            }
474
+            adapter?.let {
475
+                it.notifyDataSetChanged()
476
+            }
477
+        } catch (e: Exception) {
478
+
479
+        }
480
+    }
481
+
434 482
     fun checkAccessibility(context: Context): Boolean {
435 483
         // 判断辅助功能是否开启
436 484
         if (!isServiceON(this, ManageAccessibilityService::class.java.name)) {
@@ -509,9 +557,21 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
509 557
             .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_notice_dialog) {
510 558
                 override fun onBind(dialog: CustomDialog, v: View) {
511 559
                     val closeBtn = v.findViewById<Button>(R.id.dialog_close_btn)
560
+                    val titleTv = v.findViewById<TextView>(R.id.dialog_title_tv)
561
+                    val contentTv = v.findViewById<TextView>(R.id.dialog_content_tv)
562
+                    try {
563
+                        val msgBean =
564
+                            GsonUtils.parseJsonWithGson(
565
+                                msg,
566
+                                SocketMsgBean().javaClass
567
+                            ) as SocketMsgBean
568
+                        titleTv.text = msgBean.title
569
+                        contentTv.text = msgBean.content
570
+                    } catch (e: Exception) {
571
+
572
+                    }
512 573
                     closeBtn.setOnClickListener {
513 574
                         dialog.dismiss()
514
-
515 575
                     }
516 576
                 }
517 577
             }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))

+ 4
- 2
app/src/main/java/com/xhly/manageapp/ui/main/viewmodel/MainViewModel.kt Wyświetl plik

@@ -56,8 +56,10 @@ class MainViewModel : CommonBaseViewModel() {
56 56
 
57 57
     fun getForceAppList(){
58 58
         launchUI {
59
-            var postForceApp = appService.postForceApp()
60
-
59
+            val result= appService.postForceApp()
60
+            if (result.code==0&&result.obj!=null&&result.obj.isNotEmpty()){
61
+                forceData.value=result.obj
62
+            }
61 63
         }
62 64
     }
63 65
 

+ 68
- 0
app/src/main/java/com/xhly/manageapp/utils/AppDownLoadUtils.kt Wyświetl plik

@@ -0,0 +1,68 @@
1
+package com.xhly.manageapp.utils
2
+
3
+import com.billbook.lib.downloader.Download
4
+import com.billbook.lib.downloader.Downloader
5
+import com.xhly.corelib.Const
6
+import com.xhly.corelib.utils.LogShow
7
+import com.xhly.manageapp.ManageApplication
8
+import kotlinx.coroutines.Dispatchers
9
+import kotlinx.coroutines.withContext
10
+import java.io.File
11
+import java.util.LinkedList
12
+
13
+object AppDownLoadUtils {
14
+    private val downloadQueue = LinkedList<Pair<String, String>>()
15
+    private var isDownloading = false
16
+    suspend fun addToDownloadQueue(url: String, appName: String) {
17
+        downloadQueue.add(Pair(url, appName))
18
+        if (!isDownloading) {
19
+            startNextDownload()
20
+        }
21
+    }
22
+
23
+    private suspend fun startNextDownload() {
24
+        if (downloadQueue.isNotEmpty()) {
25
+            val (url, appName) = downloadQueue.removeFirst()
26
+            downLoadFile(url, appName)
27
+        } else {
28
+            isDownloading = false
29
+        }
30
+    }
31
+
32
+    private suspend fun downLoadFile(url: String, appName: String) {
33
+        withContext(Dispatchers.IO) {
34
+            try {
35
+                //val file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
36
+                ManageApplication.instance?.let { context ->
37
+                    var currentRetry = 0
38
+                    var successful = false
39
+                    while(currentRetry<3&&!successful){
40
+                        val file = context.getExternalFilesDir(null)
41
+                        val name = file.toString()
42
+                        val downloader = Downloader.Builder().build()
43
+                        val request = Download.Request.Builder()
44
+                            .url(Const.URL1 + url)
45
+                            .into(File(name, "$appName.apk"))
46
+                            .build()
47
+                        LogShow("开始下载")
48
+                        val execute = downloader.newCall(request).execute()
49
+                        successful = execute.isSuccessful()
50
+                        LogShow("下载结果" + successful)
51
+                        if (successful) {
52
+                            withContext(Dispatchers.Main) {
53
+                                ManageApplication.installPackage(File(name, "$appName.apk").toString())
54
+                            }
55
+                        } else {
56
+                            currentRetry++
57
+                        }
58
+                        LogShow("下载后,开始安装")
59
+                    }
60
+                }
61
+            } catch (e: Exception) {
62
+
63
+            } finally {
64
+                startNextDownload()
65
+            }
66
+        }
67
+    }
68
+}

+ 16
- 3
app/src/main/res/layout/layout_notice_dialog.xml Wyświetl plik

@@ -20,12 +20,25 @@
20 20
         android:gravity="center_horizontal"
21 21
         android:orientation="vertical"
22 22
         android:padding="15dp">
23
-
24
-        <TextView
23
+        <androidx.core.widget.NestedScrollView
25 24
             android:layout_width="match_parent"
26 25
             android:layout_height="0dp"
27
-            android:layout_weight="1" />
26
+            android:layout_weight="1">
27
+            <androidx.appcompat.widget.LinearLayoutCompat
28
+                android:layout_width="match_parent"
29
+                android:layout_height="match_parent">
30
+                <TextView
31
+                    android:id="@+id/dialog_title_tv"
32
+                    android:layout_width="match_parent"
33
+                    android:layout_height="wrap_content"
34
+                    android:layout_weight="1" />
28 35
 
36
+                <TextView
37
+                    android:id="@+id/dialog_content_tv"
38
+                    android:layout_width="match_parent"
39
+                    android:layout_height="wrap_content" />
40
+            </androidx.appcompat.widget.LinearLayoutCompat>
41
+        </androidx.core.widget.NestedScrollView>
29 42
         <Button
30 43
             android:id="@+id/dialog_close_btn"
31 44
             android:layout_width="350dp"

+ 3
- 4
corelib/src/main/java/com/xhly/corelib/network/ResponseInterceptor.kt Wyświetl plik

@@ -21,11 +21,10 @@ class ResponseInterceptor : Interceptor {
21 21
         var request = chain.request()
22 22
         return try {
23 23
             val requestBuilder: Request.Builder = request.newBuilder()
24
-            //TODO 需要替换token
25
-         /*   requestBuilder.header("Authorization", "Bearer ${Const.token}")
26
-            requestBuilder.header("Accept-Language", "zh-Hans")*/
24
+            //TODO 需要替换userid和机型
27 25
             requestBuilder.header("XH-UserId","1")
28
-            requestBuilder.header("XH-DeviceModel", Build.MODEL)
26
+           /* requestBuilder.header("XH-DeviceModel", Build.MODEL)*/
27
+            requestBuilder.header("XH-DeviceModel", "CPN-W09")
29 28
             request = requestBuilder.build()
30 29
             val originalResponse = chain.proceed(request)
31 30
             val responseBody = originalResponse.body

+ 2
- 2
corelib/src/main/java/com/xhly/corelib/utils/baseUtils.kt Wyświetl plik

@@ -65,7 +65,7 @@ fun String.hourToCurrentDate(formatType: String = "HH:mm:ss"): Date {
65 65
     }
66 66
 }
67 67
 
68
-fun String.hourToCurrentDateLong(formatType: String = "HH:mm:ss"): Long {
68
+fun String.hourToCurrentDateLong(formatType: String = "HH:mm"): Long {
69 69
     try {
70 70
         val simpleDateFormat = SimpleDateFormat(formatType)
71 71
         val parse = simpleDateFormat.parse(this)
@@ -104,7 +104,7 @@ fun Any.LogTag(): String {
104 104
 }
105 105
 
106 106
 fun Any.LogShow(s: String) {
107
-    LogUtils.d(LogTag(), s)
107
+    LogUtils.e(LogTag(), s)
108 108
 }
109 109
 
110 110
 fun Double.getScaleSize(

Ładowanie…
Anuluj
Zapisz