lmw
2024-06-07 4264d859bb9f6cbc4c069a9350c917f1b23009dd
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
package com.future.dispatch.base
 
import android.app.Activity
import android.app.Application
import android.os.*
import android.util.Log
import androidx.annotation.RequiresApi
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.api.TagAliasCallback
import cn.sinata.xldutils.BaseApplication
import com.future.dispatch.utils.sysErr
import com.amap.api.location.AMapLocation
import com.future.dispatch.utils.Cache.CacheKey
import com.xuexiang.xui.XUI
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.*
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManager
import javax.net.ssl.X509TrustManager
 
/**
 * Created by Administrator on 2018/1/17.
 */
class MyApplication : BaseApplication(), Application.ActivityLifecycleCallbacks {
    //设置debug模式
    var isDebug = false
    var heartHandler: Handler? = null
    var currentTime = System.currentTimeMillis()
 
 
    override fun setSharedPreferencesName(): String {
        return "MyApplication"
    }
 
    @RequiresApi(api = Build.VERSION_CODES.DONUT)
    override fun onCreate() {
        super.onCreate()
        appContext = this
        handleSSLHandshake()
        //如果是主进程
        XUI.init(this)
        registerActivityLifecycleCallbacks(this)
        initJpush()
    }
 
    public fun initJpush() {
        if (CacheKey.getUserInfo().pushOrder ==  1){
            JPushInterface.setDebugMode(true)
            JPushInterface.init(this)
            setAlisa()
        }
    }
 
    public fun setAlisa() {
        if (CacheKey.getUserId().isNotEmpty()){
            var alisa = "DISPATCH"+ CacheKey.getUserId()
            sysErr(alisa)
            mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_ALIAS, alisa))
        }
    }
 
 
    private val MSG_SET_ALIAS = 1001
    private val mHandler: Handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                MSG_SET_ALIAS -> {
                    Log.d(TAG, "Set alias in handler.")
                    // 调用 JPush 接口来设置别名。
                    JPushInterface.setAliasAndTags(
                        applicationContext,
                        msg.obj as String,
                        null,
                        mAliasCallback
                    )
                }
                else -> Log.i(TAG, "Unhandled msg - " + msg.what)
            }
        }
    }
 
    private val mAliasCallback =
        TagAliasCallback { code, alias, tags ->
            val logs: String
            when (code) {
                0 -> {
                    logs = "Set tag and alias success"
                    Log.i("TAG", logs)
                }
                6002 -> {
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s."
                    Log.i("TAG", logs)
                    // 延迟 60 秒来调用 Handler 设置别名
                    var alisa = "DISPATCH"+ CacheKey.getUserInfo().id.toString()
                    Handler().postDelayed({
                        JPushInterface.setAliasAndTags(this, alisa,null,null)
                    },60*1000)
                }
                else -> {
                    logs = "Failed with errorCode = $code"
                    Log.e("TAG", logs)
                }
            }
        }
 
 
    companion object {
        private const val TAG = "MyApplication"
        public var appContext: MyApplication? = null
 
        public var isLogin : Boolean = true
        fun getInstance(): MyApplication? {
            if (appContext == null) {
                appContext = MyApplication()
            }
            return appContext
        }
 
 
        fun getLocation(): AMapLocation {
             if (aMapLocation == null) {
                var map = AMapLocation("")
                 map.latitude = -1.0
                 map.longitude = -1.0
                 return map
            } else {
                 return aMapLocation!!
            }
        }
 
        var aMapLocation: AMapLocation? = null
        var currentOrderId: String = ""
        var currentOrderType: String = ""
        var testHeartNunm = 0
        var testReceiveNum = 0
        fun handleSSLHandshake() {
            try {
                val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
                    override fun getAcceptedIssuers(): Array<X509Certificate?> {
                        return arrayOfNulls(0)
                    }
 
                    override fun checkClientTrusted(
                        certs: Array<X509Certificate>,
                        authType: String
                    ) {
 
                    }
 
                    override fun checkServerTrusted(
                        certs: Array<X509Certificate>,
                        authType: String
                    ) {
 
                    }
                })
                val sc = SSLContext.getInstance("TLS")
                // trustAllCerts信任所有的证书
                sc.init(null, trustAllCerts, SecureRandom())
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.socketFactory)
                HttpsURLConnection.setDefaultHostnameVerifier { hostname, session -> true }
            } catch (ignored: Exception) {
 
            }
        }
    }
 
    private val activities = ArrayList<Activity?>()
    private var showNum = 0
 
    override fun onActivityPaused(activity: Activity?) {
 
    }
 
    override fun onActivityResumed(activity: Activity?) {
    }
 
    override fun onActivityStarted(activity: Activity?) {
        showNum++
    }
 
    override fun onActivityDestroyed(activity: Activity?) {
        activities.remove(activity)
    }
 
    override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
    }
 
    override fun onActivityStopped(activity: Activity?) {
        showNum--
 
    }
 
    override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        activities.add(activity)
    }
 
 
}