|
@@ -0,0 +1,109 @@
|
|
1
|
+package com.xhly.manageapp.workmanager
|
|
2
|
+
|
|
3
|
+import android.app.usage.UsageEvents
|
|
4
|
+import android.app.usage.UsageStatsManager
|
|
5
|
+import android.content.Context
|
|
6
|
+import androidx.appcompat.app.AppCompatActivity
|
|
7
|
+import androidx.work.Worker
|
|
8
|
+import androidx.work.WorkerParameters
|
|
9
|
+import com.xhly.corelib.network.RetrofitService
|
|
10
|
+import com.xhly.corelib.utils.AppUtils
|
|
11
|
+import com.xhly.corelib.utils.LogShow
|
|
12
|
+import com.xhly.corelib.utils.toFormat
|
|
13
|
+import com.xhly.manageapp.ManageApplication
|
|
14
|
+import com.xhly.manageapp.bean.log.LogAppStartBean
|
|
15
|
+import com.xhly.manageapp.network.log.LogService
|
|
16
|
+import kotlinx.coroutines.CoroutineScope
|
|
17
|
+import kotlinx.coroutines.MainScope
|
|
18
|
+import kotlinx.coroutines.cancel
|
|
19
|
+import kotlinx.coroutines.launch
|
|
20
|
+import java.util.Calendar
|
|
21
|
+
|
|
22
|
+/**
|
|
23
|
+ * 用来记录应用使用时长的定时任务
|
|
24
|
+ */
|
|
25
|
+class TimingWorker(context: Context, workerParams: WorkerParameters) :
|
|
26
|
+ Worker(context, workerParams) {
|
|
27
|
+ private val serviceScope = CoroutineScope(MainScope().coroutineContext)
|
|
28
|
+ override fun doWork(): Result {
|
|
29
|
+ val result: Result = Result.success()
|
|
30
|
+ val logService = RetrofitService.create<LogService>()
|
|
31
|
+ serviceScope.launch {
|
|
32
|
+ val appInfo = getAppInfo()
|
|
33
|
+ if (appInfo.size > 0) {
|
|
34
|
+ logService.postLogAppStartAdd(appInfo)
|
|
35
|
+ }
|
|
36
|
+ }
|
|
37
|
+ return result
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ private fun getAppInfo(): ArrayList<LogAppStartBean> {
|
|
41
|
+ //统计使用时长
|
|
42
|
+ val dataList = AppUtils.GetAppList(applicationContext)
|
|
43
|
+ val instance = Calendar.getInstance()
|
|
44
|
+ instance.set(Calendar.MILLISECOND, 0)
|
|
45
|
+ val timeInMillis = instance.timeInMillis - 1000 * 60 * 5
|
|
46
|
+ val usageManager =
|
|
47
|
+ applicationContext.getSystemService(AppCompatActivity.USAGE_STATS_SERVICE) as UsageStatsManager
|
|
48
|
+ val startList = arrayListOf<LogAppStartBean>()
|
|
49
|
+ var sn = ManageApplication.getDeviceInfo()
|
|
50
|
+ if (usageManager != null) {
|
|
51
|
+ val endTime = System.currentTimeMillis()
|
|
52
|
+ val startTime = timeInMillis
|
|
53
|
+ //queryEvents查询使用时长,时间范围设置后,返回的数据时间范围和设置的一致,但需要自己判断时长
|
|
54
|
+ val queryEvents = usageManager.queryEvents(startTime, endTime)
|
|
55
|
+ val event = UsageEvents.Event()
|
|
56
|
+
|
|
57
|
+ while (queryEvents.hasNextEvent()) {
|
|
58
|
+ queryEvents.getNextEvent(event)
|
|
59
|
+ when (event.eventType) {
|
|
60
|
+ UsageEvents.Event.ACTIVITY_RESUMED -> {
|
|
61
|
+ val appStartBean = LogAppStartBean()
|
|
62
|
+ val first =
|
|
63
|
+ dataList.firstOrNull { event.packageName.equals(it.packageName) }
|
|
64
|
+ first?.let {
|
|
65
|
+ sn?.let {
|
|
66
|
+ appStartBean.sn=sn
|
|
67
|
+ }
|
|
68
|
+ appStartBean.appName = first.name
|
|
69
|
+ appStartBean.appPackage = first.packageName
|
|
70
|
+ appStartBean.starttime = event.timeStamp.toFormat()
|
|
71
|
+ appStartBean.ltype = 1
|
|
72
|
+ appStartBean.versionName = first.versionName
|
|
73
|
+ appStartBean.versionNum = first.versionNum
|
|
74
|
+ startList.add(appStartBean)
|
|
75
|
+ }
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ UsageEvents.Event.ACTIVITY_PAUSED -> {
|
|
79
|
+ val appStartBean = LogAppStartBean()
|
|
80
|
+ var first =
|
|
81
|
+ dataList.firstOrNull { event.packageName.equals(it.packageName) }
|
|
82
|
+ first?.let {
|
|
83
|
+ sn?.let {
|
|
84
|
+ appStartBean.sn=sn
|
|
85
|
+ }
|
|
86
|
+ appStartBean.appName = first.name
|
|
87
|
+ appStartBean.appPackage = first.packageName
|
|
88
|
+ appStartBean.starttime = event.timeStamp.toFormat()
|
|
89
|
+ appStartBean.ltype = 2
|
|
90
|
+ appStartBean.versionName = first.versionName
|
|
91
|
+ appStartBean.versionNum = first.versionNum
|
|
92
|
+ startList.add(appStartBean)
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ return startList
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ override fun onStopped() {
|
|
102
|
+ super.onStopped()
|
|
103
|
+ try {
|
|
104
|
+ serviceScope.cancel()
|
|
105
|
+ } catch (e: Exception) {
|
|
106
|
+
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+}
|