18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
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
package com.example.basecore.util
 
import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.core.content.ContextCompat
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import java.io.File
import java.io.FileInputStream
import java.math.BigInteger
import java.security.MessageDigest
 
 
inline fun <reified T> startActNoParam(context: Context) {
    val intent = Intent(context, T::class.java)
    context.startActivity(intent)
}
 
 
inline fun <reified T> startAct(context: Context, bundle: Bundle) {
    val intent = Intent(context, T::class.java)
    intent.putExtra("param", bundle)
    context.startActivity(intent)
}
 
 
//传参
inline fun <reified T> startAct(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    context.startActivity(intent)
}
 
 
/**
 * The absolute width of the available display size in pixels
 */
val Context.screenWidth
    get() = resources.displayMetrics.widthPixels
 
/**
 * The absolute height of the available display size in pixels
 */
val Context.screenHeight
    get() = resources.displayMetrics.heightPixels
 
 
fun Context.dp2px(dp: Int): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}
 
 
fun Context.dp2px(dp: Float): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}
 
fun Context.px2dp(px: Int): Int {
    val scale = resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}
 
fun View.dp2px(dp: Int): Int {
    val scale = resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}
 
fun View.px2dp(px: Int): Int {
    val scale = resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}
 
/**
 * Sets the [text] on the clipboard
 */
 
fun Context.copyToClipboard(text: String, label: String = "KTX") {
 
    var clipboardManager = ContextCompat.getSystemService(this, ClipboardManager::class.java)
    val clipData = ClipData.newPlainText(label, text)
//    clipboardManager?.primaryClip = clipData
}
 
fun isShowInput(context: Context, edt: EditText): Boolean {
    val inputMethodManager: InputMethodManager =
        context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
 
    return inputMethodManager.isActive(edt)
 
}
 
// 构建输入框文字变化流
fun EditText.textChangeFlow(): Flow<CharSequence> = callbackFlow {
    // 构建输入框监听器
    val watcher = object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}
        override fun beforeTextChanged( s: CharSequence?, start: Int, count: Int, after: Int ) { }
        // 在文本变化后向流发射数据
        override fun onTextChanged( s: CharSequence?, start: Int, before: Int, count: Int ) {
            s?.apply {
                trySend(s)
            }
        }
    }
    addTextChangedListener(watcher) // 设置输入框监听器
    awaitClose { removeTextChangedListener(watcher) } // 阻塞以保证流一直运行
}
 
 
 
/**
 * 文件md5
 */
fun getFileMD5(file: File): String {
    if (!file.isFile) {
        return ""
    }
    val digest: MessageDigest?
    val ins = FileInputStream(file)
    val buffer = ByteArray(1024)
    var len: Int
    try {
        digest = MessageDigest.getInstance("MD5")
        while (ins.read(buffer, 0, 1024).also { len = it } != -1) {
            digest.update(buffer, 0, len)
        }
        ins.close()
    } catch (e: java.lang.Exception) {
        e.printStackTrace()
        return ""
    }
    val bigInt = BigInteger(1, digest.digest())
    return bigInt.toString(16)
}
 
 
fun convertDp2px(context: Context, dp: Int): Int {
    val scale = context.resources.displayMetrics.density
    return (dp * scale + 0.5f).toInt()
}
 
fun convertPx2dp(context: Context, px: Int): Int {
    val scale = context.resources.displayMetrics.density
    return (px / scale + 0.5f).toInt()
}
 
 
/**
 * 版本 1.0.0
 */
fun getVersionName(context: Context): String {
    val manager = context.applicationContext.packageManager
    return try {
        val info = manager.getPackageInfo(context.applicationContext.packageName, 0)
        info!!.versionName
    } catch (e: Exception) {
        ""
    }
}
 
 
 
 
/**
 * 接口添加公共参数
 */
fun addParam(): HashMap<String, Any> {
    val hashMap = HashMap<String, Any>()
    hashMap["s"] = "android"
    hashMap["timestamp"] = System.currentTimeMillis()
    hashMap["m"] = Build.MODEL
    hashMap["sv"] = Build.VERSION.RELEASE
 
    hashMap["b"] = Build.BRAND
//    hashMap["v"] = getVersionName()
    hashMap["access_token"] = SPUtils.getString("token", "")
 
    return hashMap
}
 
 
fun hideKeyboard(activity: Activity) {
    val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    if (imm.isActive) {
        if (activity.currentFocus!!.windowToken != null) {
            imm.hideSoftInputFromWindow(
                activity.currentFocus!!.windowToken,
                InputMethodManager.HIDE_NOT_ALWAYS
            )
        }
    }
}