123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- package com.develop.base.ext
- import android.Manifest
- import android.app.Application
- import android.content.Context
- import android.content.pm.PackageManager
- import android.content.res.Resources
- import android.graphics.Bitmap
- import android.graphics.BitmapFactory
- import android.graphics.drawable.Drawable
- import android.net.wifi.WifiInfo
- import android.net.wifi.WifiManager
- import android.os.Build
- import android.text.TextUtils
- import android.util.Patterns
- import android.widget.ImageView
- import androidx.annotation.DimenRes
- import androidx.core.app.ActivityCompat
- import androidx.core.content.ContextCompat
- import com.bumptech.glide.Glide
- import com.bumptech.glide.load.engine.DiskCacheStrategy
- import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
- import com.develop.base.BuildConfig
- import com.develop.base.app.BaseApp
- import com.develop.base.util.MMkvUtils
- import kotlinx.serialization.decodeFromString
- import kotlinx.serialization.encodeToString
- import kotlinx.serialization.json.Json
- import okhttp3.MediaType.Companion.toMediaTypeOrNull
- import okhttp3.MultipartBody
- import okhttp3.RequestBody
- import okhttp3.RequestBody.Companion.toRequestBody
- import java.io.File
- import java.io.IOException
- import java.io.InputStream
- import java.net.URI
- import java.text.SimpleDateFormat
- import java.util.*
- import java.util.regex.Pattern
- fun globalApp(): Application = BaseApp.application
- fun globalRes(): Resources = globalApp().resources
- // ------------------------------Any扩展-------------------------------------
- fun Any.toJson(): String = Json.encodeToString(this)
- fun Any.dimenRes(@DimenRes res: Int): Int {
- return globalApp().resources.getDimensionPixelSize(res)
- }
- // ------------------------------String扩展-------------------------------------
- fun String.fromJson(): Any = Json.decodeFromString(this)
- fun String.getHost(): String {
- var hostUrl = this
- if (!(hostUrl.startsWith("http://") || hostUrl.startsWith("https://"))) {
- hostUrl = "http://$this"
- }
- var returnVal = ""
- try {
- val uri = URI(hostUrl)
- returnVal = uri.host
- } catch (e: Exception) {
- }
- if (returnVal.endsWith(".html") || returnVal.endsWith(".htm")) {
- returnVal = ""
- }
- return "http://$returnVal"
- }
- fun String.isHttpUrl(): Boolean = Patterns.WEB_URL.matcher(this).matches()
- fun String.assetsUri2Bitmap(): Bitmap? {
- val path = this
- var image: Bitmap? = null
- val am = globalApp().resources.assets
- try {
- val stream: InputStream = am.open(path)
- image = BitmapFactory.decodeStream(stream)
- stream.close()
- } catch (e: IOException) {
- e.printStackTrace()
- }
- return image
- }
- fun String.jsonSpiltField(fieldName: String): String {
- if (fieldName.isEmpty()) {
- return ""
- }
- val regex = "(?<=(\"$fieldName\":\")).*?(?=(\"))"
- val pattern = Pattern.compile(regex)
- val matcher = pattern.matcher(this)
- while (matcher.find()) {
- if (!TextUtils.isEmpty(matcher.group().trim { it <= ' ' })) {
- return matcher.group().trim { it <= ' ' }
- }
- }
- return ""
- }
- fun String.createFolder() {
- val file = File(this)
- if (!file.exists()) {
- file.mkdirs()
- }
- }
- fun String.deleteFile(): Boolean {
- val file = File(this)
- if (!file.exists()) {
- return false
- }
- return file.delete()
- }
- fun timeStamp2Time(timeSeconds: Long, format: String = "yyyy/MM/dd HH:mm"): String? {
- val date = Date(timeSeconds * 1000)
- return SimpleDateFormat(format, Locale.getDefault()).format(date)
- }
- fun String.toCacheFolder(): String = globalApp().externalCacheDir.toString() + "/" + this
- // ------------------------------Int扩展-------------------------------------
- fun Int.resId2Drawable(): Drawable? = ContextCompat.getDrawable(globalApp(), this)
- fun Int.resId2Dimension(): Float = BaseApp.application.resources.getDimension(this)
- // ------------------------------RequestBody扩展-------------------------------------
- fun Map<String, String>.genMultipartBody(): RequestBody {
- val builder = MultipartBody.Builder()
- builder.setType(MultipartBody.FORM)//表单类型
- this.forEach {
- builder.addFormDataPart(it.key, it.value)
- }
- return builder.build()
- }
- fun Map<String, String>.genRequestBody(): Map<String, RequestBody> {
- val requestBodyMap: MutableMap<String, RequestBody> = HashMap()
- for (key in this.keys) {
- val requestBody: RequestBody =
- (if (this[key] == null) "" else this[key] ?: ""
- ).toRequestBody("text/plain".toMediaTypeOrNull())
- requestBodyMap[key] = requestBody
- }
- return requestBodyMap
- }
- fun GlobalApp(): Application {
- return BaseApp.application
- }
- fun getWifiMacAddress(context: Context): String {
- // 检查是否具有读取Wi-Fi状态的权限
- if (ActivityCompat.checkSelfPermission(
- context,
- Manifest.permission.ACCESS_WIFI_STATE
- ) != PackageManager.PERMISSION_GRANTED
- ) {
- // 无权限,请求权限
- // 这里需要您自行实现权限请求的逻辑
- return ""
- }
- val wifiManager =
- context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
- val wifiInfo: WifiInfo? = wifiManager.connectionInfo
- return if (wifiInfo != null) wifiInfo.macAddress else ""
- }
- /**
- * 获取SN
- * @return
- */
- fun getBrandNum(): String {
- return getSN().substring(0, 3)
- }
- /**
- * 获取SN
- * @return
- */
- fun getSN(): String {
- var serial: String
- if (BuildConfig.DEBUG) {
- // return "045A21030020123010190001"
- // return "000A30150020123010190001"
- // return "002A30150020123010190001"
- // return "000A10390020123010190001"
- // return "010D10390020123010190001"
- // return "011A30150020123010190001"
- // return "007D20020020123010190001"
- // return "036I21060020123010190003"
- // return "030A10390020123010190001"
- // return "017A20060020123010190001"
- // return "010D10390020123010190001"
- // return "000A10390020123010190009"
- return "032D21060020123010190001"
- // return "030A10390020123010190002"
- } else {
- //通过反射获取sn号
- try {
- val c = Class.forName("android.os.SystemProperties")
- val get = c.getMethod("get", String::class.java)
- serial = get.invoke(c, "ro.serialno") as String
- if (serial != "" && serial != "unknown") return serial
- //9.0及以上无法获取到sn,此方法为补充,能够获取到多数高版本手机 sn
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) serial = Build.getSerial()
- } catch (e: java.lang.Exception) {
- serial = "unknown"
- }
- return serial
- }
- }
- fun ImageView.load(data: Any?) {
- Glide.with(this.context).load(data)
- .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
- .into(this)
- }
- fun ImageView.load(data: Any?, width: Int, height: Int) {
- Glide.with(this.context)
- .asDrawable()
- .load(data)
- .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
- .skipMemoryCache(false)
- .override(width, height)
- .dontAnimate()
- .into(this)
- }
- fun ImageView.load(data: Any?, error: Int) {
- Glide.with(this.context).load(data)
- .error(error)
- .transition(DrawableTransitionOptions.withCrossFade())
- .into(this)
- }
- fun isScreenWidth(): Int {
- return MMkvUtils.getInt("screen_width", 0)
- }
- fun isScreenHeight(): Int {
- return MMkvUtils.getInt("screen_height", 0)
- }
- fun setScreenWidth(width: Int) {
- MMkvUtils.save("screen_width", width)
- }
- fun setScreenHeight(height: Int) {
- MMkvUtils.save("screen_height", height)
- }
- fun isNightTheme(): Boolean {
- return MMkvUtils.getBool("nightTheme", false)
- }
- fun isFrLanguage(): Boolean {
- return MMkvUtils.getString("CURRENT_LANGUAGE") == "FR"
- }
- fun setNightTheme(isNight: Boolean) {
- MMkvUtils.save("nightTheme", isNight)
- }
- fun setIsBrand036I(is036I: Boolean) {
- MMkvUtils.save("is036I", is036I)
- }
- fun isBrand036I(): Boolean {
- return MMkvUtils.getBool("is036I", false)
- }
- fun setIsBrand011A(is011A: Boolean) {
- MMkvUtils.save("is011A", is011A)
- }
- fun isBrand011A(): Boolean {
- return MMkvUtils.getBool("is011A", false)
- }
- fun setIsBrand054A(is054A: Boolean) {
- MMkvUtils.save("is054A", is054A)
- }
- fun isBrand054A(): Boolean {
- return MMkvUtils.getBool("is054A", false)
- }
|