Преглед изворни кода

1.Const添加新常量。

2.AppUtils增加获得应用版本和版本名称。
3.ManageAccessibilityService增加Event事件记录应用打开
master
wangwanlei пре 10 месеци
родитељ
комит
5a4f823a08

+ 12
- 2
app/src/main/java/com/xhly/manageapp/ManageApplication.kt Прегледај датотеку

@@ -8,7 +8,7 @@ import com.xhly.corelib.utils.ModelNameUtils
8 8
 
9 9
 class ManageApplication : MultiDexApplication() {
10 10
     companion object {
11
-        var csdkManager: CSDKManager? = null
11
+        private var csdkManager: CSDKManager? = null
12 12
 
13 13
         /**
14 14
          * 加入可安装应用白名单
@@ -53,7 +53,7 @@ class ManageApplication : MultiDexApplication() {
53 53
          */
54 54
         fun setDisplayBlacklist(pkgList: ArrayList<String>) {
55 55
             if (ModelNameUtils.IS_LianxX505f()) {
56
-                csdkManager?.displayBlacklistV3=pkgList
56
+                csdkManager?.displayBlacklistV3 = pkgList
57 57
             }
58 58
         }
59 59
 
@@ -184,6 +184,16 @@ class ManageApplication : MultiDexApplication() {
184 184
                 csdkManager?.setRuntimePermissions(enableFlag)
185 185
             }
186 186
         }
187
+
188
+        /**
189
+         * "设备Mac地址", "设备SN号", "获取设备型号信息", "获取设备IMEI信息" 1,2,3,4
190
+         */
191
+        fun getDeviceInfo(type: Int = 2): String? {
192
+            if (ModelNameUtils.IS_LianxX505f()) {
193
+                return csdkManager?.getDeviceInfo(type)
194
+            }
195
+            return "0"
196
+        }
187 197
     }
188 198
 
189 199
     override fun onCreate() {

+ 8
- 0
app/src/main/java/com/xhly/manageapp/bean/AppTimeManageBean.kt Прегледај датотеку

@@ -0,0 +1,8 @@
1
+package com.xhly.manageapp.bean
2
+
3
+//用力处理APP的时间管控,包括包名和可使用的时间段
4
+class AppTimeManageBean {
5
+    private var pkgName = ""
6
+    private var startTime = 0L
7
+    private var endTime = 0L
8
+}

+ 3
- 0
app/src/main/java/com/xhly/manageapp/network/LogService.kt Прегледај датотеку

@@ -20,4 +20,7 @@ interface LogService {
20 20
     @POST(UriAdress.LOGAPPSTARTADD)
21 21
     suspend fun postLogAppStartAdd(@Body logApp: LogAppStartBean)
22 22
 
23
+    @POST("/appgroup/list_app")
24
+    suspend fun postListApp()
25
+
23 26
 }

+ 15
- 8
app/src/main/java/com/xhly/manageapp/service/ManageAccessibilityService.kt Прегледај датотеку

@@ -2,18 +2,25 @@ package com.xhly.manageapp.service
2 2
 
3 3
 import android.accessibilityservice.AccessibilityService
4 4
 import android.view.accessibility.AccessibilityEvent
5
+import com.xhly.corelib.Const
6
+import com.xhly.corelib.eventbus.UIEvent
5 7
 import com.xhly.corelib.utils.LogShow
6 8
 
7 9
 class ManageAccessibilityService : AccessibilityService() {
8 10
     override fun onAccessibilityEvent(event: AccessibilityEvent?) {
9
-       event?.let {
10
-           if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == it.eventType) {
11
-               LogShow(it.packageName.toString() +"|||" + it.className.toString())
12
-               if (it.className.toString().equals("com.example.mytestxf.MainActivity")){
13
-                   performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
14
-               }
15
-           }
16
-       }
11
+        event?.let {
12
+            if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == it.eventType) {
13
+                LogShow(it.packageName.toString() + "|||" + it.className.toString())
14
+                if (it.className.toString().equals("com.example.mytestxf.MainActivity")) {
15
+                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
16
+                }
17
+                if (!it.packageName.toString().isNullOrBlank()) {
18
+                    val uiEvent = UIEvent(Const.LOGAPPSTART)
19
+                    uiEvent.setMessage(it.packageName.toString())
20
+                    uiEvent.post()
21
+                }
22
+            }
23
+        }
17 24
     }
18 25
 
19 26
     override fun onInterrupt() {

+ 37
- 2
app/src/main/java/com/xhly/manageapp/service/websocket/AppSocketService.kt Прегледај датотеку

@@ -10,16 +10,19 @@ import android.content.Intent
10 10
 import android.os.Build
11 11
 import android.widget.Toast
12 12
 import com.xhly.corelib.Const
13
+import com.xhly.corelib.bean.AppInfo
13 14
 import com.xhly.corelib.eventbus.UIEvent
14 15
 import com.xhly.corelib.network.RetrofitService
16
+import com.xhly.corelib.utils.AppUtils
15 17
 import com.xhly.corelib.utils.LogShow
16 18
 import com.xhly.corelib.utils.SharedPreferencesUtils
19
+import com.xhly.manageapp.ManageApplication
20
+import com.xhly.manageapp.bean.LogAppStartBean
17 21
 import com.xhly.manageapp.bean.LogdOperateBean
18 22
 import com.xhly.manageapp.network.LogService
19 23
 import com.xhly.websocket.service.LongConnService
20 24
 import com.xhly.websocket.utils.SocketPushUtils.dealPushMessage
21 25
 import kotlinx.coroutines.CoroutineScope
22
-import kotlinx.coroutines.GlobalScope
23 26
 import kotlinx.coroutines.MainScope
24 27
 import kotlinx.coroutines.cancel
25 28
 import kotlinx.coroutines.launch
@@ -32,12 +35,17 @@ open class AppSocketService : LongConnService() {
32 35
     var CHANNEL_NAME = "通知"
33 36
     var logService = RetrofitService.create<LogService>()
34 37
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
38
+    private var appList = arrayListOf<AppInfo>()
39
+
40
+    //标记上次打开的包名,当连续打开同一包名时,不认为打开多次
41
+    private var lastPkgName = ""
35 42
 
36 43
     /*  private var csdkManager: CSDKManager? = null*/
37 44
     val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }
38 45
 
39 46
     companion object {
40 47
         fun openWebSocket(context: Context) {
48
+            LogShow("打开广播")
41 49
             context.startService(Intent(context, AppSocketService::class.java))
42 50
         }
43 51
 
@@ -49,9 +57,11 @@ open class AppSocketService : LongConnService() {
49 57
     override fun onCreate() {
50 58
         super.onCreate()
51 59
         EventBus.getDefault().register(this)
52
-        connectUrl = "wss://api.stsotc.com/api/ws"
60
+        connectUrl = "wss://mcwstest.xhkjedu.com/ws"
53 61
         /*   csdkManager = CSDKManager(this)*/
54 62
         /* startNotification()*/
63
+        LogShow("已经创建")
64
+        appList = AppUtils.GetAppList(this) as ArrayList<AppInfo>
55 65
     }
56 66
 
57 67
     override fun processTextMessage(text: String) {
@@ -88,6 +98,26 @@ open class AppSocketService : LongConnService() {
88 98
                 //csdkManager?.launchFactoryReset()
89 99
             }
90 100
 
101
+            Const.LOGAPPSTART -> {
102
+                val message = uiEvent.message
103
+                val filter = appList.filter { it.packageName == message }
104
+                if (filter.isNotEmpty()) {
105
+                    val appInfo = filter[0]
106
+                    serviceScope.launch {
107
+                        val logStartBean = LogAppStartBean()
108
+                        ManageApplication.getDeviceInfo()?.let {
109
+                            logStartBean.sn = it
110
+                        }
111
+                        logStartBean.appName = appInfo.name
112
+                        logStartBean.appPackage = appInfo.packageName
113
+                        logStartBean.versionName = appInfo.versionName
114
+                        logStartBean.versionNum = appInfo.versionNum
115
+                    LogShow("当前应用"+logStartBean.appName+"|||"+logStartBean.versionName+"|||"+logStartBean.versionNum)
116
+                    //logService.postLogAppStartAdd(logStartBean)
117
+                    }
118
+                }
119
+            }
120
+
91 121
             "10086" -> {
92 122
                 serviceScope.launch {
93 123
                     logService.postLogdoperateAdd(LogdOperateBean())
@@ -122,6 +152,11 @@ open class AppSocketService : LongConnService() {
122 152
         stopForeground(Service.STOP_FOREGROUND_REMOVE)
123 153
         /*  val intent=Intent(this,ManageService::class.java)
124 154
           startService(intent)*/
155
+        val intent = Intent()
156
+        intent.setPackage("com.xhly.manageapp")
157
+        intent.setAction("test")
158
+        sendBroadcast(intent)
159
+        LogShow("已经销毁")
125 160
     }
126 161
 
127 162
     private fun startNotification() {

+ 14
- 13
app/src/main/java/com/xhly/manageapp/ui/MainActivity.kt Прегледај датотеку

@@ -28,7 +28,6 @@ import com.xhly.corelib.utils.AppUtils
28 28
 import com.xhly.corelib.utils.LogShow
29 29
 import com.xhly.manageapp.AppAdapter
30 30
 import com.xhly.manageapp.R
31
-import com.xhly.manageapp.bean.LogdOperateBean
32 31
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
33 32
 import com.xhly.manageapp.databinding.ActivityMainBinding
34 33
 import com.xhly.manageapp.network.LogService
@@ -51,7 +50,7 @@ class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
51 50
         if (user.isNullOrBlank()) {
52 51
             startIntentActivity(LoginActivity().javaClass)
53 52
         }
54
-        val getAppList1 = AppUtils.GetAppList1(this)
53
+        val getAppList1 = AppUtils.GetAppList(this)
55 54
         dataList.addAll(getAppList1)
56 55
         dataList.forEach {
57 56
             LogShow("数据" + it.packageName + "|||")
@@ -71,22 +70,24 @@ class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
71 70
          使用情况权限
72 71
          val permissionIntent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
73 72
           startActivity(permissionIntent)*/
74
-        AppSocketService.openWebSocket(this)
75 73
         // csdkManagerUtils.csdkManager.enableAccessibility("com.xhly.manageapp","com.xhly.manageapp.service.TestService",true)
76 74
     }
77 75
 
76
+    override fun onNewIntent(intent: Intent?) {
77
+        super.onNewIntent(intent)
78
+    }
78 79
     override fun initView() {
79
-        var logService = RetrofitService.create<LogService>()
80 80
         mBinding.loginoutIv.setOnClickListener {
81
-           /* showEditDialog()*/
82
-            lifecycleScope.launch {
83
-                logService.postLogdoperateAdd(LogdOperateBean())
84
-            }
81
+            showEditDialog()
85 82
         }
86 83
         mBinding.stoptv.setOnClickListener {
87 84
             spUtils.setParam(Const.DISABLEAPP, false)
88 85
         }
89 86
         onBackPressedDispatcher.addCallback {
87
+          lifecycleScope.launch {
88
+              var logService = RetrofitService.create<LogService>()
89
+              logService.postListApp()
90
+          }
90 91
             //屏蔽返回
91 92
         }
92 93
     }
@@ -96,6 +97,9 @@ class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
96 97
         super.onResume()
97 98
         getAppInfo()
98 99
         useStrategy()
100
+        if (!isServiceON(this,AppSocketService::class.java.name)){
101
+            AppSocketService.openWebSocket(this)
102
+        }
99 103
         /*      lifecycleScope.launch(Dispatchers.IO) {
100 104
                   delay(20000)
101 105
                   withContext(Dispatchers.Main) {
@@ -147,7 +151,7 @@ class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
147 151
         when (uiEvent.getEvent()) {
148 152
             "update" -> {
149 153
                 dataList.clear()
150
-                val getAppList1 = AppUtils.GetAppList1(this)
154
+                val getAppList1 = AppUtils.GetAppList(this)
151 155
                 dataList.addAll(getAppList1)
152 156
                 adapter.notifyDataSetChanged()
153 157
                 dataList.forEach {
@@ -295,10 +299,7 @@ class MainActivity : BaseActivity<CommonBaseViewModel, ActivityMainBinding>() {
295 299
                     titleTv.text = getString(R.string.exit)
296 300
                     enterTv.setOnClickListener {
297 301
                         dialog.dismiss()
298
-                        val intent = Intent(Settings.ACTION_DEVICE_INFO_SETTINGS)
299
-                        startActivity(intent)
300
-
301
-                        //loginOut()
302
+                        loginOut()
302 303
                     }
303 304
                     cancelTv.setOnClickListener {
304 305
                         dialog.dismiss()

+ 2
- 1
corelib/src/main/java/com/xhly/corelib/Const.kt Прегледај датотеку

@@ -35,5 +35,6 @@ object Const {
35 35
     //恢复出厂
36 36
     const val CODE2006 = 2006
37 37
 
38
-
38
+    //应用启动日志
39
+    const val LOGAPPSTART="LOGAPPSTART"
39 40
 }

+ 9
- 1
corelib/src/main/java/com/xhly/corelib/utils/AppUtils.java Прегледај датотеку

@@ -5,6 +5,7 @@ import static com.xhly.corelib.utils.BaseUtilsKt.LogShow;
5 5
 import android.content.ComponentName;
6 6
 import android.content.Context;
7 7
 import android.content.Intent;
8
+import android.content.pm.PackageInfo;
8 9
 import android.content.pm.PackageManager;
9 10
 import android.content.pm.ResolveInfo;
10 11
 
@@ -15,7 +16,7 @@ import java.util.ArrayList;
15 16
 import java.util.List;
16 17
 
17 18
 public class AppUtils {
18
-    public static List<AppInfo> GetAppList1(Context context) {
19
+    public static List<AppInfo> GetAppList(Context context) {
19 20
         List<AppInfo> list = new ArrayList<>();
20 21
         PackageManager pm = context.getPackageManager();
21 22
         Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
@@ -31,6 +32,13 @@ public class AppUtils {
31 32
             mInfo.setIco(info.activityInfo.applicationInfo.loadIcon(pm));
32 33
             mInfo.setName(info.activityInfo.applicationInfo.loadLabel(pm).toString());
33 34
             mInfo.setPackageName(packName);
35
+            try {
36
+                PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packName, PackageManager.GET_META_DATA);
37
+                 mInfo.setVersionName(packageInfo.versionName);
38
+                 mInfo.setVersionNum(packageInfo.versionCode);
39
+            } catch (PackageManager.NameNotFoundException e) {
40
+                throw new RuntimeException(e);
41
+            }
34 42
             // 为应用程序的启动Activity 准备Intent
35 43
             Intent launchIntent = new Intent();
36 44
             launchIntent.setComponent(new ComponentName(packName,

+ 3
- 1
websocket/src/main/java/com/xhly/websocket/utils/SocketPushUtils.kt Прегледај датотеку

@@ -9,7 +9,9 @@ object SocketPushUtils {
9 9
     fun dealPushMessage(content: String) {
10 10
         try {
11 11
             //没用泛型是因为如果带有泛型的类型,可能会遇到解析异常的问题。需要使用TypeToken
12
-            var socketData = GsonUtils.parseJsonWithGson(content, WebSocketData::class.java)
12
+          /*  var socketData = GsonUtils.parseJsonWithGson(content, WebSocketData::class.java)*/
13
+            LogShow("数据$content")
14
+            var socketData=WebSocketData()
13 15
             socketData.code.let { code ->
14 16
                 when (code) {
15 17
                     1001 -> {

Loading…
Откажи
Сачувај