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)
|
}
|
|
|
}
|