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 startActNoParam(context: Context) { val intent = Intent(context, T::class.java) context.startActivity(intent) } inline fun startAct(context: Context, bundle: Bundle) { val intent = Intent(context, T::class.java) intent.putExtra("param", bundle) context.startActivity(intent) } //传参 inline fun 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 = 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 { val hashMap = HashMap() 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 ) } } }