Browse Source

1.增加socket消息处理。

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

+ 15
- 10
app/src/main/java/com/xhly/manageapp/AppAdapter.kt View File

3
 import android.annotation.SuppressLint
3
 import android.annotation.SuppressLint
4
 import android.app.ActivityManager
4
 import android.app.ActivityManager
5
 import android.content.Context
5
 import android.content.Context
6
+import android.content.Intent
7
+import android.provider.Settings
6
 import android.view.LayoutInflater
8
 import android.view.LayoutInflater
7
 import android.view.ViewGroup
9
 import android.view.ViewGroup
8
 import android.widget.Toast
10
 import android.widget.Toast
61
             holder.mBinding.itemCv.setImageDrawable(appInfo.ico)
63
             holder.mBinding.itemCv.setImageDrawable(appInfo.ico)
62
             holder.mBinding.itemCv.setText(appInfo.name)
64
             holder.mBinding.itemCv.setText(appInfo.name)
63
             holder.mBinding.itemCv.setOnClickListener {
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 View File

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 View File

16
 import android.widget.TextView
16
 import android.widget.TextView
17
 import android.widget.Toast
17
 import android.widget.Toast
18
 import androidx.activity.addCallback
18
 import androidx.activity.addCallback
19
+import androidx.lifecycle.viewModelScope
19
 import androidx.recyclerview.widget.GridLayoutManager
20
 import androidx.recyclerview.widget.GridLayoutManager
20
 import androidx.work.PeriodicWorkRequest
21
 import androidx.work.PeriodicWorkRequest
21
 import androidx.work.WorkManager
22
 import androidx.work.WorkManager
27
 import com.xhly.corelib.eventbus.UIEvent
28
 import com.xhly.corelib.eventbus.UIEvent
28
 import com.xhly.corelib.utils.AppUtils
29
 import com.xhly.corelib.utils.AppUtils
29
 import com.xhly.corelib.utils.CustomOSUtils
30
 import com.xhly.corelib.utils.CustomOSUtils
31
+import com.xhly.corelib.utils.GsonUtils
30
 import com.xhly.corelib.utils.LogShow
32
 import com.xhly.corelib.utils.LogShow
31
 import com.xhly.corelib.utils.SharedPreferencesUtils
33
 import com.xhly.corelib.utils.SharedPreferencesUtils
32
 import com.xhly.corelib.utils.SystemUtil
34
 import com.xhly.corelib.utils.SystemUtil
34
 import com.xhly.manageapp.ManageApplication
36
 import com.xhly.manageapp.ManageApplication
35
 import com.xhly.manageapp.R
37
 import com.xhly.manageapp.R
36
 import com.xhly.manageapp.bean.EventLog
38
 import com.xhly.manageapp.bean.EventLog
39
+import com.xhly.manageapp.bean.SocketMsgBean
37
 import com.xhly.manageapp.bean.UpdateBean
40
 import com.xhly.manageapp.bean.UpdateBean
38
 import com.xhly.manageapp.bean.log.LogdOperateBean
41
 import com.xhly.manageapp.bean.log.LogdOperateBean
39
 import com.xhly.manageapp.bean.strategy.StrategyBean
42
 import com.xhly.manageapp.bean.strategy.StrategyBean
45
 import com.xhly.manageapp.ui.ManageActivity
48
 import com.xhly.manageapp.ui.ManageActivity
46
 import com.xhly.manageapp.ui.login.activity.LoginActivity
49
 import com.xhly.manageapp.ui.login.activity.LoginActivity
47
 import com.xhly.manageapp.ui.main.viewmodel.MainViewModel
50
 import com.xhly.manageapp.ui.main.viewmodel.MainViewModel
51
+import com.xhly.manageapp.utils.AppDownLoadUtils
48
 import com.xhly.manageapp.utils.PadInfoUtils
52
 import com.xhly.manageapp.utils.PadInfoUtils
49
 import com.xhly.manageapp.utils.StrategyUtils
53
 import com.xhly.manageapp.utils.StrategyUtils
50
 import com.xhly.manageapp.workmanager.TimingWorker
54
 import com.xhly.manageapp.workmanager.TimingWorker
55
+import kotlinx.coroutines.launch
51
 import java.util.Calendar
56
 import java.util.Calendar
52
 import java.util.concurrent.TimeUnit
57
 import java.util.concurrent.TimeUnit
53
 
58
 
54
 
59
 
55
 class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
60
 class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
56
-    lateinit var adapter: AppAdapter
61
+    var adapter: AppAdapter? = null
57
 
62
 
58
     private var mMyInstallReceiver: AppInstallReceiver? = null
63
     private var mMyInstallReceiver: AppInstallReceiver? = null
59
     private var dataList: ArrayList<AppInfo> = arrayListOf()
64
     private var dataList: ArrayList<AppInfo> = arrayListOf()
84
           startActivity(permissionIntent)*/
89
           startActivity(permissionIntent)*/
85
         // csdkManagerUtils.csdkManager.enableAccessibility("com.xhly.manageapp","com.xhly.manageapp.service.TestService",true)
90
         // csdkManagerUtils.csdkManager.enableAccessibility("com.xhly.manageapp","com.xhly.manageapp.service.TestService",true)
86
         viewModel.getAppList()
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
         viewModel.strategyData.observe(this) {
127
         viewModel.strategyData.observe(this) {
93
             spUtils.saveJson(Const.STRATEGYBEANKEY, it)
128
             spUtils.saveJson(Const.STRATEGYBEANKEY, it)
94
             StrategyUtils.setStrategy(it)
129
             StrategyUtils.setStrategy(it)
137
         }
172
         }
138
         onBackPressedDispatcher.addCallback {
173
         onBackPressedDispatcher.addCallback {
139
             //屏蔽返回
174
             //屏蔽返回
140
-            viewModel.getAppList()
175
+            /*    viewModel.getAppList()
176
+                viewModel.getForceAppList()*/
141
         }
177
         }
142
     }
178
     }
143
 
179
 
151
         updatePadInfo()
187
         updatePadInfo()
152
         updateMcApp()
188
         updateMcApp()
153
         getLocation()
189
         getLocation()
190
+        ManageApplication.lockTaskPackages().forEach {
191
+            LogShow("获得的姓名是"+it)
192
+        }
154
         try {
193
         try {
155
             var s =
194
             var s =
156
                 "厂商" + CustomOSUtils.getSystemBrand() + ",型号" + CustomOSUtils.getSystemDevice() + ",系统版本" + CustomOSUtils.getSystemDisplay()
195
                 "厂商" + CustomOSUtils.getSystemBrand() + ",型号" + CustomOSUtils.getSystemDevice() + ",系统版本" + CustomOSUtils.getSystemDisplay()
298
         super.onUiEvent(uiEvent)
337
         super.onUiEvent(uiEvent)
299
         when (uiEvent.getEvent()) {
338
         when (uiEvent.getEvent()) {
300
             "update" -> {
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
             Const.CODE2001.toString() -> {
343
             Const.CODE2001.toString() -> {
323
             }
356
             }
324
 
357
 
325
             Const.CODE2004.toString() -> {
358
             Const.CODE2004.toString() -> {
359
+                //TODO 更新策略是否需要通过应用接口处理应用
326
                 //请求接口更新策略
360
                 //请求接口更新策略
327
                 viewModel.uploadAppStrategy()
361
                 viewModel.uploadAppStrategy()
328
                 viewModel.updateStrategy()
362
                 viewModel.updateStrategy()
355
             }
389
             }
356
 
390
 
357
             Const.APPINSTALL -> {
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
             Const.SDINSTALL -> {
399
             Const.SDINSTALL -> {
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
     fun checkAccessibility(context: Context): Boolean {
482
     fun checkAccessibility(context: Context): Boolean {
435
         // 判断辅助功能是否开启
483
         // 判断辅助功能是否开启
436
         if (!isServiceON(this, ManageAccessibilityService::class.java.name)) {
484
         if (!isServiceON(this, ManageAccessibilityService::class.java.name)) {
509
             .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_notice_dialog) {
557
             .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_notice_dialog) {
510
                 override fun onBind(dialog: CustomDialog, v: View) {
558
                 override fun onBind(dialog: CustomDialog, v: View) {
511
                     val closeBtn = v.findViewById<Button>(R.id.dialog_close_btn)
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
                     closeBtn.setOnClickListener {
573
                     closeBtn.setOnClickListener {
513
                         dialog.dismiss()
574
                         dialog.dismiss()
514
-
515
                     }
575
                     }
516
                 }
576
                 }
517
             }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))
577
             }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))

+ 4
- 2
app/src/main/java/com/xhly/manageapp/ui/main/viewmodel/MainViewModel.kt View File

56
 
56
 
57
     fun getForceAppList(){
57
     fun getForceAppList(){
58
         launchUI {
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 View File

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 View File

20
         android:gravity="center_horizontal"
20
         android:gravity="center_horizontal"
21
         android:orientation="vertical"
21
         android:orientation="vertical"
22
         android:padding="15dp">
22
         android:padding="15dp">
23
-
24
-        <TextView
23
+        <androidx.core.widget.NestedScrollView
25
             android:layout_width="match_parent"
24
             android:layout_width="match_parent"
26
             android:layout_height="0dp"
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
         <Button
42
         <Button
30
             android:id="@+id/dialog_close_btn"
43
             android:id="@+id/dialog_close_btn"
31
             android:layout_width="350dp"
44
             android:layout_width="350dp"

+ 3
- 4
corelib/src/main/java/com/xhly/corelib/network/ResponseInterceptor.kt View File

21
         var request = chain.request()
21
         var request = chain.request()
22
         return try {
22
         return try {
23
             val requestBuilder: Request.Builder = request.newBuilder()
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
             requestBuilder.header("XH-UserId","1")
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
             request = requestBuilder.build()
28
             request = requestBuilder.build()
30
             val originalResponse = chain.proceed(request)
29
             val originalResponse = chain.proceed(request)
31
             val responseBody = originalResponse.body
30
             val responseBody = originalResponse.body

+ 2
- 2
corelib/src/main/java/com/xhly/corelib/utils/baseUtils.kt View File

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
     try {
69
     try {
70
         val simpleDateFormat = SimpleDateFormat(formatType)
70
         val simpleDateFormat = SimpleDateFormat(formatType)
71
         val parse = simpleDateFormat.parse(this)
71
         val parse = simpleDateFormat.parse(this)
104
 }
104
 }
105
 
105
 
106
 fun Any.LogShow(s: String) {
106
 fun Any.LogShow(s: String) {
107
-    LogUtils.d(LogTag(), s)
107
+    LogUtils.e(LogTag(), s)
108
 }
108
 }
109
 
109
 
110
 fun Double.getScaleSize(
110
 fun Double.getScaleSize(

Loading…
Cancel
Save