lmw
2024-09-23 f23be5d1086538d541281b84d8b093f95c545c47
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
package com.future.driver.ui.pub
 
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.Signature
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import androidx.core.app.ActivityCompat
import androidx.core.os.bundleOf
import cn.sinata.xldutils.utils.SPUtils
import com.future.driver.R
import com.future.driver.base.*
import com.future.driver.bean.AgreeBean
import com.future.driver.netUtls.Api
import com.future.driver.netUtls.callNet
import com.future.driver.netUtls.getMapByAny
import com.future.driver.netUtls.getToken
import com.future.driver.ui.main.MainActivity
import com.future.driver.utils.Cache.CacheKey
import com.future.driver.utils.view.UserRuleDialog
import org.jetbrains.anko.startActivity
import java.io.ByteArrayInputStream
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
import java.util.regex.Matcher
import java.util.regex.Pattern
 
class StartActivity : MyBaseActivity() {
    override fun setContentView() {
        setContentView(R.layout.activity_start)
    }
 
    override fun initView() {
        showHeader(false)
        showTitle(false)
        val publicKey = getSignInfo(this)
        Log.e("mmp", "公钥:$publicKey")
    }
 
    @SuppressLint("SourceLockedOrientationActivity")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkPrivacy()
//        setRxpermiss()
    }
 
    fun subString(sub: String?): String? {
        val pp: Pattern = Pattern.compile("\\s*|\t|\r|\n")
        val mm: Matcher = pp.matcher(sub)
        return mm.replaceAll("")
 
    }
 
    protected fun getSignInfo(mContext: Context): String? {
        var signcode = ""
        try {
            val packageInfo: PackageInfo = mContext.packageManager.getPackageInfo(
                "com.future.driver", PackageManager.GET_SIGNATURES
            )
            val signs: Array<Signature> = packageInfo.signatures
            val sign: Signature = signs[0]
            signcode = parseSignature(sign.toByteArray())
            signcode = signcode.toLowerCase()
        } catch (e: Exception) {
        }
        return signcode
    }
 
    protected fun parseSignature(signature: ByteArray): String {
        var sign = ""
        try {
            val certFactory = CertificateFactory
                .getInstance("X.509")
            val cert = certFactory
                .generateCertificate(ByteArrayInputStream(signature)) as X509Certificate
            val pubKey = cert.publicKey.toString()
            var ss = subString(pubKey)
            ss = ss!!.replace(",", "")
            ss = ss.toLowerCase()
            val aa = ss.indexOf("modulus")
            val bb = ss.indexOf("publicexponent")
            sign = ss.substring(aa + 8, bb)
        } catch (e: CertificateException) {
 
        }
        return sign
    }
 
    private fun callLast() {
        MyApplication.getInstance()?.initSdk()
        object :CountDownTimer(2000,1000){
            override fun onTick(millisUntilFinished: Long) {
            }
 
            override fun onFinish() {
                if (getToken().isNullOrEmpty()){
                    startActivity<LoginActivity>()
                }else{
                    startActivity<MainActivity>()
                }
            }
        }.start()
    }
 
    @SuppressLint("CheckResult")
    private fun setRxpermiss() {
        ActivityCompat.requestPermissions(this, Const.PERMISSIONS_LIST, 101);
    }
 
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 101) {
            var b = grantResults.size == Const.PERMISSIONS_LIST.size
            if (b) {
                callLast()
            } else {
                callLast()
            }
        }
    }
 
    override fun setOnclick() {
    }
 
    private var privacy = "" //隐私协议
    private var user = "" //用户协议
    fun checkPrivacy() {
        var isAgreen = CacheKey.getKeyStr("isAgreen")
        if (isAgreen.isEmpty()) {
            callys {
                privacy = it
                showPrivacyDialog()
            }
            callyh {
                user = it
                showPrivacyDialog()
            }
        }else
            callLast()
    }
 
    private fun showPrivacyDialog(){
        if (privacy.isNotEmpty() && user.isNotEmpty()) {
            val userRuleDialog = UserRuleDialog()
            userRuleDialog.arguments = bundleOf("privacy" to privacy, "user" to user)
            userRuleDialog.setDismissCallback(object : UserRuleDialog.OnDismiss {
                override fun onDismiss() {
                    if (CacheKey.getKeyStr("isAgreen").isNullOrEmpty())
                        finish()
                    else
                        callLast()
                }
            })
            val ft = supportFragmentManager.beginTransaction()
            ft.add(userRuleDialog, "rule")
            ft.commitAllowingStateLoss()
        }
    }
 
    fun callys(ok: (s: String) -> Unit) {
        var map = getMapByAny()
        map["type"] = 1
        callNet(false,Api.queryByType, map) {
            var data = gson.fromJson<AgreeBean>(it, AgreeBean::class.java)
            ok(data.data.content)
        }
    }
 
    fun callyh(ok: (s: String) -> Unit) {
        var map = getMapByAny()
        map["type"] = 2
        callNet(false,Api.queryByType, map) {
            var data = gson.fromJson<AgreeBean>(it, AgreeBean::class.java)
            ok(data.data.content)
        }
    }
 
    private fun setWebview(webView: WebView, url: String) {
        val settings = webView.settings
        settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        settings.defaultTextEncodingName = "utf-8" //设置编码格式
        settings.useWideViewPort = true //设置此属性,可任意比例缩放
        settings.loadWithOverviewMode = true // 页面支持缩放:
        settings.builtInZoomControls = true
        settings.useWideViewPort = false //将图片调整到适合webview的大小
        settings.setSupportZoom(false) //支持缩放
        settings.supportMultipleWindows() //多窗口
        settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存
        settings.allowFileAccess = true //设置可以访问文件
        settings.setNeedInitialFocus(true) //当webview调用requestFocus时为webview设置节点
        settings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
        settings.loadsImagesAutomatically = true //支持自动加载图片
        settings.javaScriptEnabled = true
        settings.domStorageEnabled = true
        webView.isScrollContainer = false
        webView.isScrollbarFadingEnabled = false
        webView.scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY
        webView.addJavascriptInterface(InnerJsInterface(), "HTMLOUT")
        if (url!!.startsWith("http")) {
            webView.loadUrl(url)
        } else {
            val sHead =
                "<html><head><meta name=\"viewport\" content=\"width=device-width, " + "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />" + "<style>img{max-width:100% !important;height:auto !important;}</style>" + "<style>body{max-width:100% !important;}</style>" + "</head><body>"
            webView.loadDataWithBaseURL(null, sHead + url, "text/html", "utf-8", null)
        }
    }
 
}