riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
package cn.flightfeather.thirdappmodule.util
 
import android.content.ClipData
import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.util.TypedValue
import android.util.TypedValue.COMPLEX_UNIT_DIP
import android.util.TypedValue.COMPLEX_UNIT_SP
import java.io.File
import java.util.regex.Pattern
import kotlin.math.round
 
/**
 * 拓展类
 * Created by riku
 * Date: 2019/5/20
 */
 
private val metrics = Resources.getSystem().displayMetrics
 
val Float.dp: Float
    get() = TypedValue.applyDimension(COMPLEX_UNIT_DIP, this, metrics)
 
val Int.dp: Int
    get() = TypedValue.applyDimension(COMPLEX_UNIT_DIP, this.toFloat(), metrics).toInt()
 
val Float.sp: Float
    get() = TypedValue.applyDimension(COMPLEX_UNIT_SP, this, metrics)
 
val Int.sp: Int
    get() = TypedValue.applyDimension(COMPLEX_UNIT_SP, this.toFloat(), metrics).toInt()
 
val Number.px: Number
    get() = this
 
val Number.px2dp: Int
    get() = (this.toFloat() / metrics.density).toInt()
 
val Number.px2sp: Int
    get() = (this.toFloat() / metrics.scaledDensity).toInt()
 
/**
 * 拓展颜色转String
 */
fun Context.colorIdToString(colorId: Int): String {
    val stringBuffer = StringBuffer()
    val color = ContextCompat.getColor(this, colorId)
    stringBuffer.append("#")
    stringBuffer.append(Integer.toHexString(Color.red(color)))
    stringBuffer.append(Integer.toHexString(Color.green(color)))
    stringBuffer.append(Integer.toHexString(Color.blue(color)))
    return stringBuffer.toString()
}
 
/**
 * 拓展复制到粘粘版
 */
fun Context.copy(string: String) {
    val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE)
            as android.content.ClipboardManager
    val clip = ClipData.newPlainText("", string)
    clipboardManager.primaryClip = clip
}
 
/**
 * 拓展获取版本号
 */
fun Context.getVersionName(): String {
    val manager = packageManager.getPackageInfo(packageName, 0)
    return manager.versionName
}
 
 
/**
 * 拓展列表到文本转化
 */
fun ArrayList<String>.toSplitString(): String {
    var result = ""
    this.forEach {
        result = "$result/$it"
    }
    return result
}
 
/**
 * 拓展String版本号对比
 */
fun String.compareVersion(v2: String?): String? {
    if (v2 == null || v2.isEmpty()) return null
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    var s1: String = p.matcher(this).replaceAll("").trim()
    var s2: String = p.matcher(v2).replaceAll("").trim()
 
    val cha: Int = s1.length - s2.length
    val buffer = StringBuffer()
    var i = 0
    while (i < Math.abs(cha)) {
        buffer.append("0")
        ++i
    }
 
    if (cha > 0) {
        buffer.insert(0, s2)
        s2 = buffer.toString()
    } else if (cha < 0) {
        buffer.insert(0, s1)
        s1 = buffer.toString()
    }
 
    val s1Int = s1.toInt()
    val s2Int = s2.toInt()
 
    return if (s1Int > s2Int) this
    else v2
}
 
const val KB = 1024L
const val MB = 1024 * KB
const val GB = 1024 * MB
/**
 * 拓展获取文件大小
 */
fun File.size(): String {
    return when (val bytes = this.length()) {
        in 0..KB -> "$bytes B"
        in KB..MB -> "${round(bytes.toDouble().div(KB.toDouble()).times(100)).div(100)} K"
        in MB..GB -> "${round(bytes.toDouble().div(MB.toDouble()).times(100)).div(100)} M"
        else -> if (bytes >= GB) {
            "${round(bytes.toDouble().div(GB.toDouble()).times(100)).div(100)} G"
        } else {
            ""
        }
    }
}