GlobaExt.kt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package com.develop.base.ext
  2. import android.Manifest
  3. import android.app.Application
  4. import android.content.Context
  5. import android.content.pm.PackageManager
  6. import android.content.res.Resources
  7. import android.graphics.Bitmap
  8. import android.graphics.BitmapFactory
  9. import android.graphics.drawable.Drawable
  10. import android.net.wifi.WifiInfo
  11. import android.net.wifi.WifiManager
  12. import android.os.Build
  13. import android.text.TextUtils
  14. import android.util.Patterns
  15. import android.widget.ImageView
  16. import androidx.annotation.DimenRes
  17. import androidx.core.app.ActivityCompat
  18. import androidx.core.content.ContextCompat
  19. import com.bumptech.glide.Glide
  20. import com.bumptech.glide.load.engine.DiskCacheStrategy
  21. import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
  22. import com.develop.base.BuildConfig
  23. import com.develop.base.app.BaseApp
  24. import com.develop.base.util.MMkvUtils
  25. import kotlinx.serialization.decodeFromString
  26. import kotlinx.serialization.encodeToString
  27. import kotlinx.serialization.json.Json
  28. import okhttp3.MediaType.Companion.toMediaTypeOrNull
  29. import okhttp3.MultipartBody
  30. import okhttp3.RequestBody
  31. import okhttp3.RequestBody.Companion.toRequestBody
  32. import java.io.File
  33. import java.io.IOException
  34. import java.io.InputStream
  35. import java.net.URI
  36. import java.text.SimpleDateFormat
  37. import java.util.*
  38. import java.util.regex.Pattern
  39. fun globalApp(): Application = BaseApp.application
  40. fun globalRes(): Resources = globalApp().resources
  41. // ------------------------------Any扩展-------------------------------------
  42. fun Any.toJson(): String = Json.encodeToString(this)
  43. fun Any.dimenRes(@DimenRes res: Int): Int {
  44. return globalApp().resources.getDimensionPixelSize(res)
  45. }
  46. // ------------------------------String扩展-------------------------------------
  47. fun String.fromJson(): Any = Json.decodeFromString(this)
  48. fun String.getHost(): String {
  49. var hostUrl = this
  50. if (!(hostUrl.startsWith("http://") || hostUrl.startsWith("https://"))) {
  51. hostUrl = "http://$this"
  52. }
  53. var returnVal = ""
  54. try {
  55. val uri = URI(hostUrl)
  56. returnVal = uri.host
  57. } catch (e: Exception) {
  58. }
  59. if (returnVal.endsWith(".html") || returnVal.endsWith(".htm")) {
  60. returnVal = ""
  61. }
  62. return "http://$returnVal"
  63. }
  64. fun String.isHttpUrl(): Boolean = Patterns.WEB_URL.matcher(this).matches()
  65. fun String.assetsUri2Bitmap(): Bitmap? {
  66. val path = this
  67. var image: Bitmap? = null
  68. val am = globalApp().resources.assets
  69. try {
  70. val stream: InputStream = am.open(path)
  71. image = BitmapFactory.decodeStream(stream)
  72. stream.close()
  73. } catch (e: IOException) {
  74. e.printStackTrace()
  75. }
  76. return image
  77. }
  78. fun String.jsonSpiltField(fieldName: String): String {
  79. if (fieldName.isEmpty()) {
  80. return ""
  81. }
  82. val regex = "(?<=(\"$fieldName\":\")).*?(?=(\"))"
  83. val pattern = Pattern.compile(regex)
  84. val matcher = pattern.matcher(this)
  85. while (matcher.find()) {
  86. if (!TextUtils.isEmpty(matcher.group().trim { it <= ' ' })) {
  87. return matcher.group().trim { it <= ' ' }
  88. }
  89. }
  90. return ""
  91. }
  92. fun String.createFolder() {
  93. val file = File(this)
  94. if (!file.exists()) {
  95. file.mkdirs()
  96. }
  97. }
  98. fun String.deleteFile(): Boolean {
  99. val file = File(this)
  100. if (!file.exists()) {
  101. return false
  102. }
  103. return file.delete()
  104. }
  105. fun timeStamp2Time(timeSeconds: Long, format: String = "yyyy/MM/dd HH:mm"): String? {
  106. val date = Date(timeSeconds * 1000)
  107. return SimpleDateFormat(format, Locale.getDefault()).format(date)
  108. }
  109. fun String.toCacheFolder(): String = globalApp().externalCacheDir.toString() + "/" + this
  110. // ------------------------------Int扩展-------------------------------------
  111. fun Int.resId2Drawable(): Drawable? = ContextCompat.getDrawable(globalApp(), this)
  112. fun Int.resId2Dimension(): Float = BaseApp.application.resources.getDimension(this)
  113. // ------------------------------RequestBody扩展-------------------------------------
  114. fun Map<String, String>.genMultipartBody(): RequestBody {
  115. val builder = MultipartBody.Builder()
  116. builder.setType(MultipartBody.FORM)//表单类型
  117. this.forEach {
  118. builder.addFormDataPart(it.key, it.value)
  119. }
  120. return builder.build()
  121. }
  122. fun Map<String, String>.genRequestBody(): Map<String, RequestBody> {
  123. val requestBodyMap: MutableMap<String, RequestBody> = HashMap()
  124. for (key in this.keys) {
  125. val requestBody: RequestBody =
  126. (if (this[key] == null) "" else this[key] ?: ""
  127. ).toRequestBody("text/plain".toMediaTypeOrNull())
  128. requestBodyMap[key] = requestBody
  129. }
  130. return requestBodyMap
  131. }
  132. fun GlobalApp(): Application {
  133. return BaseApp.application
  134. }
  135. fun getWifiMacAddress(context: Context): String {
  136. // 检查是否具有读取Wi-Fi状态的权限
  137. if (ActivityCompat.checkSelfPermission(
  138. context,
  139. Manifest.permission.ACCESS_WIFI_STATE
  140. ) != PackageManager.PERMISSION_GRANTED
  141. ) {
  142. // 无权限,请求权限
  143. // 这里需要您自行实现权限请求的逻辑
  144. return ""
  145. }
  146. val wifiManager =
  147. context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
  148. val wifiInfo: WifiInfo? = wifiManager.connectionInfo
  149. return if (wifiInfo != null) wifiInfo.macAddress else ""
  150. }
  151. /**
  152. * 获取SN
  153. * @return
  154. */
  155. fun getBrandNum(): String {
  156. return getSN().substring(0, 3)
  157. }
  158. /**
  159. * 获取SN
  160. * @return
  161. */
  162. fun getSN(): String {
  163. var serial: String
  164. if (BuildConfig.DEBUG) {
  165. // return "045A21030020123010190001"
  166. // return "000A30150020123010190001"
  167. // return "002A30150020123010190001"
  168. // return "000A10390020123010190001"
  169. // return "010D10390020123010190001"
  170. // return "011A30150020123010190001"
  171. // return "007D20020020123010190001"
  172. // return "036I21060020123010190003"
  173. // return "030A10390020123010190001"
  174. // return "017A20060020123010190001"
  175. // return "010D10390020123010190001"
  176. // return "000A10390020123010190009"
  177. return "032D21060020123010190001"
  178. // return "030A10390020123010190002"
  179. } else {
  180. //通过反射获取sn号
  181. try {
  182. val c = Class.forName("android.os.SystemProperties")
  183. val get = c.getMethod("get", String::class.java)
  184. serial = get.invoke(c, "ro.serialno") as String
  185. if (serial != "" && serial != "unknown") return serial
  186. //9.0及以上无法获取到sn,此方法为补充,能够获取到多数高版本手机 sn
  187. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) serial = Build.getSerial()
  188. } catch (e: java.lang.Exception) {
  189. serial = "unknown"
  190. }
  191. return serial
  192. }
  193. }
  194. fun ImageView.load(data: Any?) {
  195. Glide.with(this.context).load(data)
  196. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  197. .into(this)
  198. }
  199. fun ImageView.load(data: Any?, width: Int, height: Int) {
  200. Glide.with(this.context)
  201. .asDrawable()
  202. .load(data)
  203. .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
  204. .skipMemoryCache(false)
  205. .override(width, height)
  206. .dontAnimate()
  207. .into(this)
  208. }
  209. fun ImageView.load(data: Any?, error: Int) {
  210. Glide.with(this.context).load(data)
  211. .error(error)
  212. .transition(DrawableTransitionOptions.withCrossFade())
  213. .into(this)
  214. }
  215. fun isScreenWidth(): Int {
  216. return MMkvUtils.getInt("screen_width", 0)
  217. }
  218. fun isScreenHeight(): Int {
  219. return MMkvUtils.getInt("screen_height", 0)
  220. }
  221. fun setScreenWidth(width: Int) {
  222. MMkvUtils.save("screen_width", width)
  223. }
  224. fun setScreenHeight(height: Int) {
  225. MMkvUtils.save("screen_height", height)
  226. }
  227. fun isNightTheme(): Boolean {
  228. return MMkvUtils.getBool("nightTheme", false)
  229. }
  230. fun isFrLanguage(): Boolean {
  231. return MMkvUtils.getString("CURRENT_LANGUAGE") == "FR"
  232. }
  233. fun setNightTheme(isNight: Boolean) {
  234. MMkvUtils.save("nightTheme", isNight)
  235. }
  236. fun setIsBrand036I(is036I: Boolean) {
  237. MMkvUtils.save("is036I", is036I)
  238. }
  239. fun isBrand036I(): Boolean {
  240. return MMkvUtils.getBool("is036I", false)
  241. }
  242. fun setIsBrand011A(is011A: Boolean) {
  243. MMkvUtils.save("is011A", is011A)
  244. }
  245. fun isBrand011A(): Boolean {
  246. return MMkvUtils.getBool("is011A", false)
  247. }
  248. fun setIsBrand054A(is054A: Boolean) {
  249. MMkvUtils.save("is054A", is054A)
  250. }
  251. fun isBrand054A(): Boolean {
  252. return MMkvUtils.getBool("is054A", false)
  253. }