|
@@ -0,0 +1,332 @@
|
|
1
|
+package com.xhly.manageapp.ui
|
|
2
|
+
|
|
3
|
+import BaseActivity
|
|
4
|
+import android.app.ActivityManager
|
|
5
|
+import android.app.usage.UsageEvents
|
|
6
|
+import android.app.usage.UsageStatsManager
|
|
7
|
+import android.content.Context
|
|
8
|
+import android.content.Intent
|
|
9
|
+import android.content.IntentFilter
|
|
10
|
+import android.graphics.Color
|
|
11
|
+import android.provider.Settings
|
|
12
|
+import android.view.View
|
|
13
|
+import android.widget.Button
|
|
14
|
+import android.widget.EditText
|
|
15
|
+import android.widget.TextView
|
|
16
|
+import android.widget.Toast
|
|
17
|
+import androidx.activity.addCallback
|
|
18
|
+import androidx.recyclerview.widget.GridLayoutManager
|
|
19
|
+import com.kongzue.dialogx.dialogs.CustomDialog
|
|
20
|
+import com.kongzue.dialogx.interfaces.OnBindView
|
|
21
|
+import com.xhly.corelib.Const
|
|
22
|
+import com.xhly.corelib.base.viewmodel.CommonBaseViewModel
|
|
23
|
+import com.xhly.corelib.bean.AppInfo
|
|
24
|
+import com.xhly.corelib.eventbus.UIEvent
|
|
25
|
+import com.xhly.corelib.utils.AppUtils
|
|
26
|
+import com.xhly.corelib.utils.LogShow
|
|
27
|
+import com.xhly.manageapp.AppAdapter
|
|
28
|
+import com.xhly.manageapp.ManageApplication
|
|
29
|
+import com.xhly.manageapp.R
|
|
30
|
+import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
|
|
31
|
+import com.xhly.manageapp.databinding.ActivityMainBinding
|
|
32
|
+import com.xhly.manageapp.service.TestService
|
|
33
|
+import com.xhly.manageapp.ui.login.activity.LoginActivity
|
|
34
|
+import com.xhly.websocket.utils.WebSocketUtils
|
|
35
|
+import java.util.Calendar
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
|
|
39
|
+ lateinit var adapter: AppAdapter
|
|
40
|
+ /*private var csdkManager = CSDKManager(this)*/
|
|
41
|
+ private var mMyInstallReceiver: AppInstallReceiver? = null
|
|
42
|
+ private var dataList: ArrayList<AppInfo> = arrayListOf()
|
|
43
|
+ override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
|
|
44
|
+
|
|
45
|
+ override fun initData() {
|
|
46
|
+ var user = spUtils.get("user")
|
|
47
|
+ if (user.isNullOrBlank()) {
|
|
48
|
+ startIntentActivity(LoginActivity().javaClass)
|
|
49
|
+ }
|
|
50
|
+ val getAppList1 = AppUtils.GetAppList1(this)
|
|
51
|
+ dataList.addAll(getAppList1)
|
|
52
|
+ dataList.forEach {
|
|
53
|
+ LogShow("数据" + it.packageName + "|||")
|
|
54
|
+ }
|
|
55
|
+ adapter = AppAdapter(this, dataList)
|
|
56
|
+ mBinding.rv.layoutManager = GridLayoutManager(this, 6, GridLayoutManager.VERTICAL, false)
|
|
57
|
+ mBinding.rv.adapter = adapter
|
|
58
|
+ // checkAccessibility(this)
|
|
59
|
+ mBinding.btn.setOnClickListener {
|
|
60
|
+ startIntentActivity(ManageActivity().javaClass)
|
|
61
|
+ }
|
|
62
|
+ mBinding.resetbtn.setOnClickListener {
|
|
63
|
+ UIEvent("2005").post()
|
|
64
|
+ }
|
|
65
|
+ registerAppInstallReceiver()
|
|
66
|
+ /*
|
|
67
|
+ 使用情况权限
|
|
68
|
+ val permissionIntent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
|
|
69
|
+ startActivity(permissionIntent)*/
|
|
70
|
+ WebSocketUtils.openWebSocket(this)
|
|
71
|
+ // csdkManagerUtils.csdkManager.enableAccessibility("com.xhly.manageapp","com.xhly.manageapp.service.TestService",true)
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ override fun initView() {
|
|
75
|
+ mBinding.loginoutIv.setOnClickListener {
|
|
76
|
+ showEditDialog()
|
|
77
|
+ }
|
|
78
|
+ mBinding.stoptv.setOnClickListener {
|
|
79
|
+ spUtils.setParam(Const.DISABLEAPP, false)
|
|
80
|
+ }
|
|
81
|
+ onBackPressedDispatcher.addCallback {
|
|
82
|
+ //屏蔽返回
|
|
83
|
+ }
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+ override fun onResume() {
|
|
88
|
+ super.onResume()
|
|
89
|
+ getAppInfo()
|
|
90
|
+ useStrategy()
|
|
91
|
+ /* lifecycleScope.launch(Dispatchers.IO) {
|
|
92
|
+ delay(20000)
|
|
93
|
+ withContext(Dispatchers.Main) {
|
|
94
|
+ Toast("冻结应用")
|
|
95
|
+ *//*csdkManager.setPackageEnabled("com.android.calendar", true)*//*
|
|
96
|
+ *//* ManageApplication.killApplicationProcess("com.android.calendar")*//*
|
|
97
|
+ }
|
|
98
|
+ }*/
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ private fun useStrategy() {
|
|
102
|
+ //获得sputils中的数据设置状态,以及策略
|
|
103
|
+ try {
|
|
104
|
+ var param = spUtils.getParam(Const.DISABLEAPP, false)
|
|
105
|
+ if (param as Boolean) {
|
|
106
|
+ mBinding.stoptv.visibility = View.VISIBLE
|
|
107
|
+ } else {
|
|
108
|
+ mBinding.stoptv.visibility = View.GONE
|
|
109
|
+ }
|
|
110
|
+ changeWhiteList()
|
|
111
|
+ } catch (e: Exception) {
|
|
112
|
+
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ private fun changeWhiteList() {
|
|
117
|
+/* //获得当前白名单
|
|
118
|
+ val packageWhiteList = csdkManager.installPackageWhiteList
|
|
119
|
+ LogShow("当前白名单$packageWhiteList")
|
|
120
|
+ //获得从网络获取的白名单
|
|
121
|
+ val jsonArray = spUtils.getFromJson(Const.WHITELISTAPP, ArrayList<String>().javaClass)
|
|
122
|
+ val removeList = arrayListOf<String>()
|
|
123
|
+ //移除白名单外的应用,并设置新的白名单
|
|
124
|
+ if (jsonArray != null) {
|
|
125
|
+ val whiteList = jsonArray as ArrayList<String>
|
|
126
|
+ packageWhiteList.forEach { pkg ->
|
|
127
|
+ val filter = whiteList.filter { it == pkg }
|
|
128
|
+ if (filter.isEmpty()) {
|
|
129
|
+ removeList.add(pkg)
|
|
130
|
+ }
|
|
131
|
+ }
|
|
132
|
+ ManageApplication.addInstallPackageWhiteList(whiteList)
|
|
133
|
+ }
|
|
134
|
+ ManageApplication.removeInstallPackageWhiteList(removeList)*/
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ override fun onUiEvent(uiEvent: UIEvent) {
|
|
138
|
+ super.onUiEvent(uiEvent)
|
|
139
|
+ when (uiEvent.getEvent()) {
|
|
140
|
+ "update" -> {
|
|
141
|
+ dataList.clear()
|
|
142
|
+ val getAppList1 = AppUtils.GetAppList1(this)
|
|
143
|
+ dataList.addAll(getAppList1)
|
|
144
|
+ adapter.notifyDataSetChanged()
|
|
145
|
+ dataList.forEach {
|
|
146
|
+ LogShow("app包名" + it.packageName)
|
|
147
|
+ }
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ "策略更新" -> {
|
|
151
|
+
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ private fun getAppInfo() {
|
|
157
|
+ //统计使用时长
|
|
158
|
+ var instance = Calendar.getInstance()
|
|
159
|
+ instance.set(Calendar.HOUR_OF_DAY, 0)
|
|
160
|
+ instance.set(Calendar.MINUTE, 0)
|
|
161
|
+ instance.set(Calendar.SECOND, 1)
|
|
162
|
+ var timeInMillis = instance.timeInMillis
|
|
163
|
+ val usageManager = getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager
|
|
164
|
+ if (usageManager != null) {
|
|
165
|
+ val intervalType = UsageStatsManager.INTERVAL_DAILY
|
|
166
|
+ val endTime = System.currentTimeMillis()
|
|
167
|
+ val startTime = timeInMillis
|
|
168
|
+ //queryUsageStats查询使用时长,时间范围设置后,返回的数据时间范围不一定和设置的时间范围一致。
|
|
169
|
+ /* val applicationList = usageManager.queryUsageStats(intervalType, startTime, endTime)
|
|
170
|
+ for (usageStats in applicationList) {
|
|
171
|
+ dataList.filter { it.packageName.equals(usageStats.packageName) }.forEach {
|
|
172
|
+ LogShow("应用时长统计"+it.name+"使用时间"+millisecondsToHMS(usageStats.totalTimeInForeground))
|
|
173
|
+ }
|
|
174
|
+ if (usageStats.packageName.equals("com.xhly.manageapp")){
|
|
175
|
+ LogShow("应用时长统计XhlyManageApp"+"使用时间"+millisecondsToHMS(usageStats.totalTimeInForeground))
|
|
176
|
+ }
|
|
177
|
+ }*/
|
|
178
|
+
|
|
179
|
+ //queryEvents查询使用时长,时间范围设置后,返回的数据时间范围和设置的一致,但需要自己判断时长
|
|
180
|
+ var queryEvents = usageManager.queryEvents(startTime, endTime)
|
|
181
|
+ var event = UsageEvents.Event()
|
|
182
|
+
|
|
183
|
+ var totalTime = 0L
|
|
184
|
+ var totalStartTime = 0L
|
|
185
|
+ while (queryEvents.hasNextEvent()) {
|
|
186
|
+ queryEvents.getNextEvent(event)
|
|
187
|
+ when (event.eventType) {
|
|
188
|
+ UsageEvents.Event.ACTIVITY_RESUMED -> {
|
|
189
|
+ var first =
|
|
190
|
+ dataList.firstOrNull { event.packageName.equals(it.packageName) }
|
|
191
|
+ first?.let {
|
|
192
|
+ it.startTime = event.timeStamp
|
|
193
|
+ }
|
|
194
|
+ if (event.packageName.equals("com.xhly.manageapp")) {
|
|
195
|
+ totalStartTime = event.timeStamp
|
|
196
|
+ }
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ UsageEvents.Event.ACTIVITY_PAUSED -> {
|
|
200
|
+ var first =
|
|
201
|
+ dataList.firstOrNull { event.packageName.equals(it.packageName) }
|
|
202
|
+ first?.let {
|
|
203
|
+ if (it.startTime == 0L) {
|
|
204
|
+ it.startTime = instance.timeInMillis
|
|
205
|
+ }
|
|
206
|
+ it.foregroundTime += (event.timeStamp - it.startTime)
|
|
207
|
+ }
|
|
208
|
+ if (event.packageName.equals("com.xhly.manageapp")) {
|
|
209
|
+ totalTime += (event.timeStamp - totalStartTime)
|
|
210
|
+ }
|
|
211
|
+ }
|
|
212
|
+ }
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ dataList.filter { it.foregroundTime > 0L }.forEach {
|
|
216
|
+ LogShow("events应用时长统计" + it.name + "使用时间" + millisecondsToHMS(it.foregroundTime))
|
|
217
|
+ }
|
|
218
|
+ if (totalTime > 0L) {
|
|
219
|
+ LogShow("events应用时长统计com.xhly.manageapp使用时间" + millisecondsToHMS(totalTime))
|
|
220
|
+ } else {
|
|
221
|
+ LogShow("events应用时长统计com.xhly.manageapp使用时间无法获取")
|
|
222
|
+ }
|
|
223
|
+ }
|
|
224
|
+ }
|
|
225
|
+
|
|
226
|
+ fun checkAccessibility(context: Context): Boolean {
|
|
227
|
+ // 判断辅助功能是否开启
|
|
228
|
+ if (!isServiceON(this, TestService::class.java.name)) {
|
|
229
|
+ // 引导至辅助功能设置页面
|
|
230
|
+ context.startActivity(
|
|
231
|
+ Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
|
|
232
|
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
233
|
+ )
|
|
234
|
+ Toast.makeText(context, "请先开启 \"Activity 栈\" 的辅助功能", Toast.LENGTH_LONG).show()
|
|
235
|
+ return false
|
|
236
|
+ }
|
|
237
|
+ return true
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ fun millisecondsToHMS(milliseconds: Long): String {
|
|
241
|
+ val seconds = milliseconds / 1000
|
|
242
|
+ val hours = seconds / 3600
|
|
243
|
+ val minutes = (seconds % 3600) / 60
|
|
244
|
+ val remainingSeconds = seconds % 60
|
|
245
|
+
|
|
246
|
+ return String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds)
|
|
247
|
+ }
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+ fun isServiceON(context: Context, className: String?): Boolean {
|
|
251
|
+ val activityManager = context.getSystemService(ACTIVITY_SERVICE) as ActivityManager
|
|
252
|
+ val runningServices = activityManager.getRunningServices(100)
|
|
253
|
+ if (runningServices.size < 0) {
|
|
254
|
+ return false
|
|
255
|
+ }
|
|
256
|
+ for (i in runningServices.indices) {
|
|
257
|
+ val service = runningServices[i].service
|
|
258
|
+ if (service.className.contains(className!!)) {
|
|
259
|
+ return true
|
|
260
|
+ }
|
|
261
|
+ }
|
|
262
|
+ return false
|
|
263
|
+ }
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+ private fun registerAppInstallReceiver() {
|
|
267
|
+ val intentFilter = IntentFilter();
|
|
268
|
+ intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
|
|
269
|
+ intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED)
|
|
270
|
+ intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED)
|
|
271
|
+ intentFilter.addDataScheme("package")
|
|
272
|
+ mMyInstallReceiver = AppInstallReceiver()
|
|
273
|
+ registerReceiver(mMyInstallReceiver, intentFilter);
|
|
274
|
+ }
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+ private fun showEditDialog() {
|
|
278
|
+ CustomDialog
|
|
279
|
+ .build()
|
|
280
|
+ .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_edit_dialog) {
|
|
281
|
+ override fun onBind(dialog: CustomDialog, v: View) {
|
|
282
|
+ var titleTv = v.findViewById<TextView>(R.id.dialog_title_tv)
|
|
283
|
+ val enterTv = v.findViewById<TextView>(R.id.dialog_enter_tv)
|
|
284
|
+ val cancelTv = v.findViewById<TextView>(R.id.dialog_cancel_tv)
|
|
285
|
+ val editText = v.findViewById<EditText>(R.id.dialog_et)
|
|
286
|
+ editText.hint = getString(R.string.inputcode)
|
|
287
|
+ titleTv.text = getString(R.string.exit)
|
|
288
|
+ enterTv.setOnClickListener {
|
|
289
|
+ dialog.dismiss()
|
|
290
|
+ val intent = Intent(Settings.ACTION_DEVICE_INFO_SETTINGS)
|
|
291
|
+ startActivity(intent)
|
|
292
|
+
|
|
293
|
+ //loginOut()
|
|
294
|
+ }
|
|
295
|
+ cancelTv.setOnClickListener {
|
|
296
|
+ dialog.dismiss()
|
|
297
|
+ showNoticeDialog()
|
|
298
|
+ }
|
|
299
|
+ }
|
|
300
|
+ }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))
|
|
301
|
+ .setAlign(CustomDialog.ALIGN.CENTER).show()
|
|
302
|
+ }
|
|
303
|
+
|
|
304
|
+ private fun showNoticeDialog(){
|
|
305
|
+ CustomDialog
|
|
306
|
+ .build()
|
|
307
|
+ .setCustomView(object : OnBindView<CustomDialog>(R.layout.layout_notice_dialog) {
|
|
308
|
+ override fun onBind(dialog: CustomDialog, v: View) {
|
|
309
|
+ var closeBtn = v.findViewById<Button>(R.id.dialog_close_btn)
|
|
310
|
+ closeBtn.setOnClickListener {
|
|
311
|
+ dialog.dismiss()
|
|
312
|
+
|
|
313
|
+ }
|
|
314
|
+ }
|
|
315
|
+ }).setCancelable(false).setMaskColor(Color.parseColor("#66000000"))
|
|
316
|
+ .setAlign(CustomDialog.ALIGN.CENTER).show()
|
|
317
|
+ }
|
|
318
|
+
|
|
319
|
+ private fun loginOut() {
|
|
320
|
+ startIntentActivity(LoginActivity().javaClass)
|
|
321
|
+ finish()
|
|
322
|
+ }
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+ override fun onDestroy() {
|
|
326
|
+ super.onDestroy()
|
|
327
|
+ mMyInstallReceiver?.let {
|
|
328
|
+ unregisterReceiver(it)
|
|
329
|
+ }
|
|
330
|
+
|
|
331
|
+ }
|
|
332
|
+}
|