Browse Source

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

master
wangwanlei 9 months ago
parent
commit
c5e66ec9a3

+ 65
- 0
app/src/main/java/com/xhly/manageapp/bean/EventLog.kt View File

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

45
 
45
 
46
     var comm: String = ""
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 View File

19
             val packageName = intent.data!!.schemeSpecificPart
19
             val packageName = intent.data!!.schemeSpecificPart
20
             Log.i(TAG, "--------安装成功$packageName")
20
             Log.i(TAG, "--------安装成功$packageName")
21
             Toast.makeText(context, "安装成功$packageName", Toast.LENGTH_LONG).show()
21
             Toast.makeText(context, "安装成功$packageName", Toast.LENGTH_LONG).show()
22
-            UIEvent(Const.CODE2002.toString()).post()
22
+            UIEvent(Const.APPINSTALL).setMessage(packageName).post()
23
         } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REPLACED)) {
23
         } else if (TextUtils.equals(intent.action, Intent.ACTION_PACKAGE_REPLACED)) {
24
             val packageName = intent.data!!.schemeSpecificPart
24
             val packageName = intent.data!!.schemeSpecificPart
25
             Log.i(TAG, "--------替换成功$packageName")
25
             Log.i(TAG, "--------替换成功$packageName")
28
             val packageName = intent.data!!.schemeSpecificPart
28
             val packageName = intent.data!!.schemeSpecificPart
29
             Log.i(TAG, "--------卸载成功$packageName")
29
             Log.i(TAG, "--------卸载成功$packageName")
30
             Toast.makeText(context, "卸载成功$packageName", Toast.LENGTH_LONG).show()
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 View File

3
 import android.app.Notification
3
 import android.app.Notification
4
 import android.app.NotificationChannel
4
 import android.app.NotificationChannel
5
 import android.app.NotificationManager
5
 import android.app.NotificationManager
6
-import android.app.Service
7
 import android.content.ComponentName
6
 import android.content.ComponentName
8
 import android.content.Context
7
 import android.content.Context
9
 import android.content.Intent
8
 import android.content.Intent
19
 import com.xhly.corelib.utils.MD5Utils
18
 import com.xhly.corelib.utils.MD5Utils
20
 import com.xhly.corelib.utils.SharedPreferencesUtils
19
 import com.xhly.corelib.utils.SharedPreferencesUtils
21
 import com.xhly.manageapp.ManageApplication
20
 import com.xhly.manageapp.ManageApplication
21
+import com.xhly.manageapp.bean.EventLog
22
 import com.xhly.manageapp.bean.log.LogAppStartBean
22
 import com.xhly.manageapp.bean.log.LogAppStartBean
23
 import com.xhly.manageapp.bean.log.LogdOperateBean
23
 import com.xhly.manageapp.bean.log.LogdOperateBean
24
 import com.xhly.manageapp.network.LogService
24
 import com.xhly.manageapp.network.LogService
42
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
42
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
43
     private var appList = arrayListOf<AppInfo>()
43
     private var appList = arrayListOf<AppInfo>()
44
 
44
 
45
-    //标记上次打开的包名,当连续打开同一包名时,不认为打开多次
46
-    private var lastPkgName = ""
47
-
48
     /*  private var csdkManager: CSDKManager? = null*/
45
     /*  private var csdkManager: CSDKManager? = null*/
49
     val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }
46
     val spUtils by lazy { SharedPreferencesUtils.getInstance(this) }
50
 
47
 
51
     companion object {
48
     companion object {
49
+        //标记上次打开的包名,当连续打开同一包名时,不认为打开多次
50
+        var lastPkgName = ""
52
         fun openWebSocket(context: Context) {
51
         fun openWebSocket(context: Context) {
53
             LogShow("打开广播")
52
             LogShow("打开广播")
54
             context.startService(Intent(context, AppSocketService::class.java))
53
             context.startService(Intent(context, AppSocketService::class.java))
62
     override fun onCreate() {
61
     override fun onCreate() {
63
         super.onCreate()
62
         super.onCreate()
64
         EventBus.getDefault().register(this)
63
         EventBus.getDefault().register(this)
65
-        connectUrl = "wss://mcwstest.xhkjedu.com/ws"
64
+        connectUrl = Const.WSURL
66
         /*   csdkManager = CSDKManager(this)*/
65
         /*   csdkManager = CSDKManager(this)*/
67
         /* startNotification()*/
66
         /* startNotification()*/
68
         LogShow("已经创建")
67
         LogShow("已经创建")
78
     override fun onConnectedSuccess() {
77
     override fun onConnectedSuccess() {
79
         super.onConnectedSuccess()
78
         super.onConnectedSuccess()
80
         UIEvent(Const.UPDATE_CONNECTED_SUCCESS).post()
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
         val currentTimeSeconds = System.currentTimeMillis() / 1000
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
         WebSocketHelper.getInstance().sendSocketString(GsonUtils.parseClassToJson(webData))
89
         WebSocketHelper.getInstance().sendSocketString(GsonUtils.parseClassToJson(webData))
90
     }
90
     }
91
 
91
 
115
 
115
 
116
             Const.LOGAPPSTART -> {
116
             Const.LOGAPPSTART -> {
117
                 val message = uiEvent.message
117
                 val message = uiEvent.message
118
-                if (lastPkgName==message){
118
+                if (lastPkgName == message) {
119
                     return
119
                     return
120
                 }
120
                 }
121
                 val filter = appList.filter { it.packageName == message }
121
                 val filter = appList.filter { it.packageName == message }
122
                 if (filter.isNotEmpty()) {
122
                 if (filter.isNotEmpty()) {
123
                     val appInfo = filter[0]
123
                     val appInfo = filter[0]
124
-                    lastPkgName=message
124
+                    lastPkgName = message
125
                     serviceScope.launch {
125
                     serviceScope.launch {
126
                         val logStartBean = LogAppStartBean()
126
                         val logStartBean = LogAppStartBean()
127
                         ManageApplication.getDeviceInfo()?.let {
127
                         ManageApplication.getDeviceInfo()?.let {
131
                         logStartBean.appPackage = appInfo.packageName
131
                         logStartBean.appPackage = appInfo.packageName
132
                         logStartBean.versionName = appInfo.versionName
132
                         logStartBean.versionName = appInfo.versionName
133
                         logStartBean.versionNum = appInfo.versionNum
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
             "10086" -> {
171
             "10086" -> {
141
                 serviceScope.launch {
172
                 serviceScope.launch {
142
                     logService.postLogdoperateAdd(LogdOperateBean())
173
                     logService.postLogdoperateAdd(LogdOperateBean())
164
         }
195
         }
165
     }
196
     }
166
 
197
 
198
+    //用来发送事件日志
199
+    private fun eventLog(operateBean: LogdOperateBean) {
200
+        serviceScope.launch {
201
+            logService.postLogdoperateAdd(operateBean)
202
+        }
203
+    }
204
+
167
     override fun onDestroy() {
205
     override fun onDestroy() {
168
         super.onDestroy()
206
         super.onDestroy()
169
         EventBus.getDefault().unregister(this)
207
         EventBus.getDefault().unregister(this)
170
         serviceScope.cancel()
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
         val intent = Intent()
210
         val intent = Intent()
175
         intent.setPackage("com.xhly.manageapp")
211
         intent.setPackage("com.xhly.manageapp")
176
         intent.setAction("test")
212
         intent.setAction("test")

+ 11
- 2
corelib/src/main/java/com/xhly/corelib/Const.kt View File

5
     //接口地址
5
     //接口地址
6
     const val URL1: String = "https://mcapitest.xhkjedu.com/"
6
     const val URL1: String = "https://mcapitest.xhkjedu.com/"
7
 
7
 
8
+    //接口地址
9
+    const val WSURL: String = "wss://mcwstest.xhkjedu.com/ws"
10
+
8
     //标记app禁用
11
     //标记app禁用
9
     const val DISABLEAPP = "DISABLEAPP"
12
     const val DISABLEAPP = "DISABLEAPP"
10
 
13
 
11
     //作为存储白名单应用的key
14
     //作为存储白名单应用的key
12
-    const val WHITELISTAPP="WHITELISTAPP"
15
+    const val WHITELISTAPP = "WHITELISTAPP"
13
 
16
 
14
     //401退出登录
17
     //401退出登录
15
     const val LOGIN_OUT_401 = "LOGIN_OUT_401"
18
     const val LOGIN_OUT_401 = "LOGIN_OUT_401"
36
     const val CODE2006 = 2006
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
 }

Loading…
Cancel
Save