Parcourir la source

1.实现应用安装以及卸载时的事件提交。

master
wangwanlei il y a 9 mois
Parent
révision
c5e66ec9a3

+ 65
- 0
app/src/main/java/com/xhly/manageapp/bean/EventLog.kt Voir le fichier

@@ -0,0 +1,65 @@
1
+package com.xhly.manageapp.bean
2
+
3
+object EventLog {
4
+    /**
5
+     * 更换SIM卡
6
+     */
7
+    const val SIMEVENT = 1
8
+
9
+    /**
10
+     * 违规开启蓝牙
11
+     */
12
+    const val BLUETOOHEVENT = 2
13
+
14
+
15
+    /**
16
+     * USB连接电脑
17
+     */
18
+    const val USBCONNECTEVENT = 3
19
+
20
+    /**
21
+     *  USB断开电脑
22
+     */
23
+    const val USBDISCONNECTEVENT = 4
24
+
25
+    /**
26
+     * 插入SD卡
27
+     */
28
+    const val SDEMOUNTEDVENT = 5
29
+
30
+    /**
31
+     * 拔出SD卡
32
+     */
33
+    const val SDEJECTEVENT = 6
34
+
35
+
36
+    /**
37
+     * 账号登录
38
+     */
39
+    const val ACCOUNTLOGINEVENT = 7
40
+
41
+    /**
42
+     * 账号退出
43
+     */
44
+    const val ACCOUNTQUITEVENT = 8
45
+
46
+    /**
47
+     * 安装应用
48
+     */
49
+    const val APPINSTALLEVENT = 9
50
+
51
+    /**
52
+     * 卸载应用
53
+     */
54
+    const val APPUNINSTALLEVENT = 10
55
+
56
+    /**
57
+     * 启动应用
58
+     */
59
+    const val STARTAPPEVENT = 11
60
+
61
+    /**
62
+     * 首次使用
63
+     */
64
+    const val FIRSTEVENT = 12
65
+}

+ 5
- 0
app/src/main/java/com/xhly/manageapp/bean/log/LogdOperateBean.kt Voir le fichier

@@ -45,4 +45,9 @@ class LogdOperateBean {
45 45
 
46 46
     var comm: String = ""
47 47
 
48
+    /**
49
+     * 应用包名
50
+     */
51
+
52
+    var appPackage: String = ""
48 53
 }

+ 2
- 1
app/src/main/java/com/xhly/manageapp/broadcastreceiver/AppInstallReceiver.kt Voir le fichier

@@ -19,7 +19,7 @@ class AppInstallReceiver : BroadcastReceiver() {
19 19
             val packageName = intent.data!!.schemeSpecificPart
20 20
             Log.i(TAG, "--------安装成功$packageName")
21 21
             Toast.makeText(context, "安装成功$packageName", Toast.LENGTH_LONG).show()
22
-            UIEvent(Const.CODE2002.toString()).post()
22
+            UIEvent(Const.APPINSTALL).setMessage(packageName).post()
23 23
         } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REPLACED)) {
24 24
             val packageName = intent.data!!.schemeSpecificPart
25 25
             Log.i(TAG, "--------替换成功$packageName")
@@ -28,6 +28,7 @@ class AppInstallReceiver : BroadcastReceiver() {
28 28
             val packageName = intent.data!!.schemeSpecificPart
29 29
             Log.i(TAG, "--------卸载成功$packageName")
30 30
             Toast.makeText(context, "卸载成功$packageName", Toast.LENGTH_LONG).show()
31
+            UIEvent(Const.APPUNINSTALL).setMessage(packageName).post()
31 32
         }
32 33
     }
33 34
 }

+ 55
- 19
app/src/main/java/com/xhly/manageapp/service/websocket/AppSocketService.kt Voir le fichier

@@ -3,7 +3,6 @@ package com.xhly.manageapp.service.websocket
3 3
 import android.app.Notification
4 4
 import android.app.NotificationChannel
5 5
 import android.app.NotificationManager
6
-import android.app.Service
7 6
 import android.content.ComponentName
8 7
 import android.content.Context
9 8
 import android.content.Intent
@@ -19,6 +18,7 @@ import com.xhly.corelib.utils.LogShow
19 18
 import com.xhly.corelib.utils.MD5Utils
20 19
 import com.xhly.corelib.utils.SharedPreferencesUtils
21 20
 import com.xhly.manageapp.ManageApplication
21
+import com.xhly.manageapp.bean.EventLog
22 22
 import com.xhly.manageapp.bean.log.LogAppStartBean
23 23
 import com.xhly.manageapp.bean.log.LogdOperateBean
24 24
 import com.xhly.manageapp.network.LogService
@@ -42,13 +42,12 @@ open class AppSocketService : LongConnService() {
42 42
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
43 43
     private var appList = arrayListOf<AppInfo>()
44 44
 
45
-    //标记上次打开的包名,当连续打开同一包名时,不认为打开多次
46
-    private var lastPkgName = ""
47
-
48 45
     /*  private var csdkManager: CSDKManager? = null*/
49 46
     val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }
50 47
 
51 48
     companion object {
49
+        //标记上次打开的包名,当连续打开同一包名时,不认为打开多次
50
+        var lastPkgName = ""
52 51
         fun openWebSocket(context: Context) {
53 52
             LogShow("打开广播")
54 53
             context.startService(Intent(context, AppSocketService::class.java))
@@ -62,7 +61,7 @@ open class AppSocketService : LongConnService() {
62 61
     override fun onCreate() {
63 62
         super.onCreate()
64 63
         EventBus.getDefault().register(this)
65
-        connectUrl = "wss://mcwstest.xhkjedu.com/ws"
64
+        connectUrl = Const.WSURL
66 65
         /*   csdkManager = CSDKManager(this)*/
67 66
         /* startNotification()*/
68 67
         LogShow("已经创建")
@@ -78,14 +77,15 @@ open class AppSocketService : LongConnService() {
78 77
     override fun onConnectedSuccess() {
79 78
         super.onConnectedSuccess()
80 79
         UIEvent(Const.UPDATE_CONNECTED_SUCCESS).post()
81
-        val webData=WebSocketData()
82
-        webData.code=1002
83
-        webData.mid=UUID.randomUUID().toString()
80
+        val webData = WebSocketData()
81
+        webData.code = 1002
82
+        webData.mid = UUID.randomUUID().toString()
84 83
         val currentTimeSeconds = System.currentTimeMillis() / 1000
85
-        webData.timeunix=currentTimeSeconds
86
-        webData.fid=1
87
-        val jsMD5 = MD5Utils.getJsMD5(webData.code.toString()+"_"+webData.mid+"_"+webData.timeunix.toString())
88
-        webData.sign=MD5Utils.getJsMD5(jsMD5)
84
+        webData.timeunix = currentTimeSeconds
85
+        webData.fid = 1
86
+        val jsMD5 =
87
+            MD5Utils.getJsMD5(webData.code.toString() + "_" + webData.mid + "_" + webData.timeunix.toString())
88
+        webData.sign = MD5Utils.getJsMD5(jsMD5)
89 89
         WebSocketHelper.getInstance().sendSocketString(GsonUtils.parseClassToJson(webData))
90 90
     }
91 91
 
@@ -115,13 +115,13 @@ open class AppSocketService : LongConnService() {
115 115
 
116 116
             Const.LOGAPPSTART -> {
117 117
                 val message = uiEvent.message
118
-                if (lastPkgName==message){
118
+                if (lastPkgName == message) {
119 119
                     return
120 120
                 }
121 121
                 val filter = appList.filter { it.packageName == message }
122 122
                 if (filter.isNotEmpty()) {
123 123
                     val appInfo = filter[0]
124
-                    lastPkgName=message
124
+                    lastPkgName = message
125 125
                     serviceScope.launch {
126 126
                         val logStartBean = LogAppStartBean()
127 127
                         ManageApplication.getDeviceInfo()?.let {
@@ -131,12 +131,43 @@ open class AppSocketService : LongConnService() {
131 131
                         logStartBean.appPackage = appInfo.packageName
132 132
                         logStartBean.versionName = appInfo.versionName
133 133
                         logStartBean.versionNum = appInfo.versionNum
134
-                    LogShow("当前应用"+logStartBean.appName+"|||"+logStartBean.versionName+"|||"+logStartBean.versionNum)
135
-                    logService.postLogAppStartAdd(logStartBean)
134
+                        LogShow("当前应用" + logStartBean.appName + "|||" + logStartBean.versionName + "|||" + logStartBean.versionNum)
135
+                        logService.postLogAppStartAdd(logStartBean)
136
+                    }
137
+                    val data = LogdOperateBean()
138
+                    ManageApplication.getDeviceInfo()?.let {
139
+                        data.sn = it
136 140
                     }
141
+                    data.appPackage=message
142
+                    data.doEvent = EventLog.STARTAPPEVENT
143
+                    eventLog(data)
137 144
                 }
138 145
             }
139 146
 
147
+            Const.APPINSTALL -> {
148
+                val message = uiEvent.message
149
+                val data = LogdOperateBean()
150
+                ManageApplication.getDeviceInfo()?.let {
151
+                    data.sn = it
152
+                }
153
+                data.appPackage=message
154
+                data.comm="应用安装"
155
+                data.doEvent = EventLog.APPINSTALLEVENT
156
+                eventLog(data)
157
+            }
158
+
159
+            Const.APPUNINSTALL -> {
160
+                val message = uiEvent.message
161
+                val data = LogdOperateBean()
162
+                ManageApplication.getDeviceInfo()?.let {
163
+                    data.sn = it
164
+                }
165
+                data.appPackage=message
166
+                data.comm="应用卸载"
167
+                data.doEvent = EventLog.APPUNINSTALLEVENT
168
+                eventLog(data)
169
+            }
170
+
140 171
             "10086" -> {
141 172
                 serviceScope.launch {
142 173
                     logService.postLogdoperateAdd(LogdOperateBean())
@@ -164,13 +195,18 @@ open class AppSocketService : LongConnService() {
164 195
         }
165 196
     }
166 197
 
198
+    //用来发送事件日志
199
+    private fun eventLog(operateBean: LogdOperateBean) {
200
+        serviceScope.launch {
201
+            logService.postLogdoperateAdd(operateBean)
202
+        }
203
+    }
204
+
167 205
     override fun onDestroy() {
168 206
         super.onDestroy()
169 207
         EventBus.getDefault().unregister(this)
170 208
         serviceScope.cancel()
171
-        stopForeground(Service.STOP_FOREGROUND_REMOVE)
172
-        /*  val intent=Intent(this,ManageService::class.java)
173
-          startService(intent)*/
209
+        /* stopForeground(Service.STOP_FOREGROUND_REMOVE)*/
174 210
         val intent = Intent()
175 211
         intent.setPackage("com.xhly.manageapp")
176 212
         intent.setAction("test")

+ 11
- 2
corelib/src/main/java/com/xhly/corelib/Const.kt Voir le fichier

@@ -5,11 +5,14 @@ object Const {
5 5
     //接口地址
6 6
     const val URL1: String = "https://mcapitest.xhkjedu.com/"
7 7
 
8
+    //接口地址
9
+    const val WSURL: String = "wss://mcwstest.xhkjedu.com/ws"
10
+
8 11
     //标记app禁用
9 12
     const val DISABLEAPP = "DISABLEAPP"
10 13
 
11 14
     //作为存储白名单应用的key
12
-    const val WHITELISTAPP="WHITELISTAPP"
15
+    const val WHITELISTAPP = "WHITELISTAPP"
13 16
 
14 17
     //401退出登录
15 18
     const val LOGIN_OUT_401 = "LOGIN_OUT_401"
@@ -36,5 +39,11 @@ object Const {
36 39
     const val CODE2006 = 2006
37 40
 
38 41
     //应用启动日志
39
-    const val LOGAPPSTART="LOGAPPSTART"
42
+    const val LOGAPPSTART = "LOGAPPSTART"
43
+
44
+    //标记应用安装
45
+    const val APPINSTALL = "APPINSTALL"
46
+
47
+    //标记应用卸载
48
+    const val APPUNINSTALL = "APPUNINSTALL"
40 49
 }

Chargement…
Annuler
Enregistrer