18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.xw.yunwei.push
 
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.google.android.material.tabs.TabLayout
import com.xw.common.net.UpdateManager
import com.xw.common.utils.KeyPreferences
import com.xw.common.utils.LogUtils
import com.xw.common.utils.MMKVManager
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.MyApp
import com.xw.yunwei.databinding.DialogProtocolBinding
import com.xw.yunwei.model.SwitchTabMain
import com.xw.yunwei.model.SwitchTabTask
import com.xw.yunwei.net.UserRepository
import com.xw.yunwei.ui.MainActivity
import com.xw.yunwei.ui.task.TaskVM
import com.xw.yunwei.ui.task.accident.detail.AccidentDetailActivity
import com.xw.yunwei.ui.task.normal.NormalTaskDetailActivity
import com.xw.yunwei.ui.user.mine.protocol.ProtocolActivity
import com.xw.yunwei.ui.user.mine.scheduling.SchedulingActivity
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import kotlin.getValue
 
object MessageHandler : KoinComponent {
    val userRepository: UserRepository by inject()
 
    fun handlePushEvent(content: String?, isPush: Boolean = false) {
        try {
            handlePushEvent(JSONObject(content ?: ""), isPush)
        } catch (e: Exception) {
            ToastUtilCommon.showError("未获取到消息详情")
            LogUtils.e("zmc-MessageHandler","handlePushEvent content = $content error = $e")
        }
    }
 
    fun handlePushEvent(map: Map<String?, String?>?, isPush: Boolean = false) {
        map?.let {
            handlePushEvent(map.toJSONObject(), isPush)
        }
    }
 
    @OptIn(DelicateCoroutinesApi::class)
    fun handlePushEvent(jsob: JSONObject, isPush: Boolean) {
        LogUtils.i("zmc-MessageHandler","handlePushEvent $jsob isPush = $isPush")
        TaskVM.needRefresh.value = true
        /**
         * content:消息内容
         * strategy:推送策略 1=新增事故,2=事故即将超时提示,3=事故超时提示,4=排班信息变动,5=事故状态变更,6=新增任务,7=任务过期提示,8=版本更新提示,9=变更协议政策内容,10=变更协议内容并需用户授权,11=打卡成功通知, 12=强制升级
         * type:类型,事故需要,根据不同的id查询不同的事故详情 无-null 1=车辆检测 2=车辆充电 3=车辆整备 4=整备洗车 5=车辆维修 6=其他常规任务 7=事故任务
         * id:页面id(事故为事故id),无-null或0
         */
        val content = jsob.optString("content")
        val strategy = jsob.optInt("strategy")
        val type = jsob.optInt("type")
        val id = jsob.optString("id")
        GlobalScope.launch {
            val currentShowActivity = MyApp.myApp.getCurrentShowActivity()
            LogUtils.i(
                "zmc-MessageHandler",
                "handlePushEvent ${currentShowActivity.javaClass.name}"
            )
            handleMsg(currentShowActivity, strategy, id, isPush)
        }
    }
 
    private fun handleMsg(currentShowActivity: Activity, strategy: Int, id: String, isPush: Boolean) {
        when (strategy) {
            1, 2, 3, 5 -> { // 1=新增事故,2=事故即将超时提示,3=事故超时提示,5=事故状态变更 点击都跳事故详情
                if(strategy == 5 && isPush) return
                currentShowActivity.startActivity(
                    Intent(currentShowActivity, AccidentDetailActivity::class.java).apply {
                        putExtra("taskId", id.toLong())
                    }
                )
            }
 
            4 -> { // 排班信息变动
                currentShowActivity.startActivity(
                    Intent(currentShowActivity, SchedulingActivity::class.java)
                )
            }
 
            6, 7 -> { // 6=新增任务,7=任务过期提示  点击都跳任务详情
                if (isPush) return
                if (strategy == 6 && id == "0") { // 多条任务 跳转任务中心待处理页面
                    EventBus.getDefault().post(SwitchTabMain(1))
                    EventBus.getDefault().post(SwitchTabTask(2))
                    currentShowActivity.startActivity(
                        Intent(
                            currentShowActivity,
                            MainActivity::class.java
                        )
                    )
                } else {
                    currentShowActivity.startActivity(
                        Intent(currentShowActivity, NormalTaskDetailActivity::class.java).apply {
                            putExtra("taskId", id.toLong())
                        }
                    )
                }
            }
 
            8, 12 -> { // 升级
                if(strategy == 8 && isPush) return
                showUpdateDialog(currentShowActivity)
            }
 
            9, 10 -> { // 变更协议内容
                if (strategy == 9 && isPush) return
                if (strategy == 9) {
                    currentShowActivity.startActivity(
                        Intent(currentShowActivity, ProtocolActivity::class.java)
                    )
                } else {
                    MMKVManager.getInstance().saveBoolean(KeyPreferences.NEED_READ, true)
                    showProtocolDialog(currentShowActivity, true)
                }
            }
        }
    }
 
    fun Map<String?, String?>.toJSONObject(): JSONObject {
        return JSONObject().apply {
            this@toJSONObject.forEach { (key, value) ->
                if (key != null) {
                    put(key, value ?: JSONObject.NULL)
                }
            }
        }
    }
 
    fun showUpdateDialog(currentShowActivity: Activity) {
        (currentShowActivity as AppCompatActivity).lifecycleScope.launch {
            userRepository.getVersion()?.let {
                Handler(Looper.getMainLooper()).post {
                    UpdateManager.instance!!
                        .setContext(currentShowActivity)
                        .apkUrl(it.androidFile ?: "")
                        .updateContent(it.updateDescription ?: "")
                        .updateVersionCode(it.versionCode ?: 1)
                        .updateVersionName(it.version ?: "1.0")
                        .updateForceUpdateFlag(it.forceUpdateFlag ?: 0)
                        .show()
                }
            }
        }
    }
 
    fun showProtocolDialog(currentShowActivity: Activity, forced: Boolean) {
        Handler(Looper.getMainLooper()).post {
            MaterialDialog(currentShowActivity).cornerRadius(15f).cancelable(!forced).show {
                customView(
                    com.xw.yunwei.R.layout.dialog_protocol,
                    noVerticalPadding = true,
                    dialogWrapContent = true
                )
                DataBindingUtil.bind<DialogProtocolBinding>(getCustomView())?.let { bind ->
//                    val tab1 = bind.protocolLayout.tabLayout.newTab().apply { text = "用户协议" }
//                    val tab2 = bind.protocolLayout.tabLayout.newTab().apply { text = "隐私条款" }
 
                    val tab1 = bind.protocolLayout.tabLayout.newTab().apply { text = "小鹏Robotaxi...隐私政策" }
                    val tab2 = bind.protocolLayout.tabLayout.newTab().apply { text = "第三方共享信息清单" }
                    val tab3 = bind.protocolLayout.tabLayout.newTab().apply { text = "个人信息收集清单" }
                    val tab4 = bind.protocolLayout.tabLayout.newTab().apply { text = "权限调用" }
 
                    bind.protocolLayout.tabLayout.addTab(tab1, true)
                    bind.protocolLayout.tabLayout.addTab(tab2, false)
                    bind.protocolLayout.tabLayout.addTab(tab3, false)
                    bind.protocolLayout.tabLayout.addTab(tab4, false)
 
                    // bind.btnLeft.text = if (forced) "取消并退出" else "关闭"
                    bind.btnLeft.text = "关闭"
                    bind.btnLeft.setOnClickListener {
                        dismiss()
                        if (forced) {
                            currentShowActivity.finishAffinity()
                            android.os.Process.killProcess(android.os.Process.myPid())
                        }
                    }
                    bind.btnRight.setOnClickListener {
                        MMKVManager.getInstance().saveBoolean(KeyPreferences.NEED_READ, false)
                        dismiss()
                    }
                    initWeb(bind.protocolLayout.web)
 
                    val getProtocol: (Int) -> Unit = { type ->
                        (currentShowActivity as AppCompatActivity).lifecycleScope.launch {
                            val data = userRepository.getProtocol(type)
                            Handler(Looper.getMainLooper()).post {
                                setContent(bind.protocolLayout.web, data?.content ?: "")
                            }
                        }
                    }
 
                    getProtocol(8)
                    bind.protocolLayout.tabLayout.addOnTabSelectedListener(object :
                        TabLayout.OnTabSelectedListener {
                        override fun onTabSelected(tab: TabLayout.Tab?) {
                            tab?.let {
                                when (it.text) {
                                    "用户协议" -> getProtocol(1)
                                    "隐私条款" -> getProtocol(2)
                                    "小鹏Robotaxi...隐私政策" -> getProtocol(8)
                                    "第三方共享信息清单" -> getProtocol(9)
                                    "个人信息收集清单" -> getProtocol(10)
                                    "权限调用" -> getProtocol(11)
                                    else -> {}
                                }
                            }
                        }
 
                        override fun onTabUnselected(tab: TabLayout.Tab?) {}
                        override fun onTabReselected(tab: TabLayout.Tab?) {}
                    })
                }
            }
        }
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    private fun initWeb(webView: WebView) {
        //声明WebSettings子类
        val webSettings = webView.settings
        webSettings.domStorageEnabled = true //设置适应Html5
 
        //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript
        webSettings.javaScriptEnabled = true //允许js调用
        webSettings.allowFileAccess = true //在File域下,能够执行任意的JavaScript代码,同源策略跨域访问能够对私有目录文件进行访问等
        webSettings.allowContentAccess = true //是否允许在WebView中访问内容URL(Content Url),默认允许
 
        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放,默认为true。是下面那个的前提。
        webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false,则该WebView不可缩放
        webSettings.displayZoomControls = false //隐藏原生的缩放控件
 
        //其他细节操作
        webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存
        webSettings.allowFileAccess = true //设置可以访问文件
        webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
        webSettings.loadsImagesAutomatically = true //支持自动加载图片
        webSettings.defaultTextEncodingName = "utf-8" //设置编码格式
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    private fun setContent(webView: WebView, content: String?) {
        content?.let {
            // 加载并显示HTML代码
            webView.loadDataWithBaseURL(
                null,
                content,
                "text/html",
                "utf-8",
                null
            )
        }
    }
 
}