Skip to content

Commit 86a04e4

Browse files
author
taonce
committed
add other util
1 parent 2dc2e67 commit 86a04e4

File tree

7 files changed

+322
-10
lines changed

7 files changed

+322
-10
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.taonce.utilmodule">
44

55
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
67

78
<application
89
android:allowBackup="true"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.taonce.utilmodule
2+
3+
import android.content.Context
4+
import android.util.TypedValue
5+
6+
/**
7+
* Author: taoyongxiang
8+
* Date: 2019/4/1
9+
* Desc: 单位转换
10+
*/
11+
12+
fun Context.dp2px(dp: Float): Float = TypedValue.applyDimension(
13+
TypedValue.COMPLEX_UNIT_DIP,
14+
dp,
15+
this.resources.displayMetrics
16+
)
17+
18+
fun Context.sp2dp(sp: Float): Float = sp / (this.resources.displayMetrics.density)
19+
20+
fun Context.sp2px(sp: Float): Float = TypedValue.applyDimension(
21+
TypedValue.COMPLEX_UNIT_SP,
22+
sp,
23+
this.resources.displayMetrics
24+
)
25+
26+
fun Context.px2sp(px: Float): Float = px / this.resources.displayMetrics.scaledDensity

app/src/main/java/com/taonce/utilmodule/LogUtil.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,37 @@ import android.util.Log
99
* Date: 2019/3/28
1010
* Desc: 常用的日志打印类
1111
*/
12+
const val isShow: Boolean = true
1213

1314
fun showDebug(
1415
tag: String = "taonce",
1516
msg: String
1617
) {
17-
Log.d(tag, msg)
18+
if (isShow) {
19+
Log.d(tag, msg)
20+
}
1821
}
1922

2023
fun showError(
2124
tag: String = "taonce",
2225
msg: String
2326
) {
24-
Log.e(tag, msg)
27+
if (isShow)
28+
Log.e(tag, msg)
2529
}
2630

2731
fun showInfo(
2832
tag: String = "taonce",
2933
msg: String
3034
) {
31-
Log.i(tag, msg)
35+
if (isShow)
36+
Log.i(tag, msg)
3237
}
3338

3439
fun showWarning(
3540
tag: String = "taonce",
3641
msg: String
3742
) {
38-
Log.w(tag, msg)
43+
if (isShow)
44+
Log.w(tag, msg)
3945
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.taonce.utilmodule
2+
3+
import android.content.Context
4+
import android.net.ConnectivityManager
5+
import android.net.NetworkCapabilities
6+
import android.net.NetworkInfo
7+
import android.os.Build
8+
9+
10+
/**
11+
* Author: taoyongxiang
12+
* Date: 2019/4/1
13+
* Desc: 网络相关工具类
14+
*/
15+
16+
/**
17+
* 判断网络是否连接,需要`ACCESS_NETWORK_STATE`权限
18+
*/
19+
fun Context.isNetConnected(): Boolean {
20+
val connectivityManager: ConnectivityManager? =
21+
this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
22+
connectivityManager?.let {
23+
val netInfo: NetworkInfo = it.activeNetworkInfo
24+
if (netInfo.isConnected) {
25+
if (netInfo.detailedState == NetworkInfo.DetailedState.CONNECTED) {
26+
return true
27+
}
28+
}
29+
}
30+
return false
31+
}
32+
33+
/**
34+
* 判断是否为 `wifi` 连接
35+
*/
36+
fun Context.isWifi(): Boolean {
37+
val connectivityManager: ConnectivityManager =
38+
this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
39+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
40+
connectivityManager.activeNetworkInfo.subtype == NetworkCapabilities.TRANSPORT_WIFI
41+
} else {
42+
connectivityManager.activeNetworkInfo.subtype == ConnectivityManager.TYPE_WIFI
43+
}
44+
}

app/src/main/java/com/taonce/utilmodule/PhoneUtil.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ fun Context.getDeviceImei(): String {
5959
) {
6060
imei = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
6161
manager.imei
62-
} else {
63-
""
64-
}
62+
} else ""
6563
}
6664
return imei
6765
}
@@ -80,9 +78,7 @@ fun Context.getDeviceMeid(): String {
8078
) {
8179
meid = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
8280
manager.meid
83-
} else {
84-
""
85-
}
81+
} else ""
8682
}
8783
return meid
8884
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.taonce.utilmodule
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
6+
7+
/**
8+
* Author: taoyongxiang
9+
* Date: 2019/4/1
10+
* Desc: SP 文件的工具类
11+
*/
12+
13+
// SP文件名
14+
const val fileName = "share_preference"
15+
16+
/**
17+
* 向SP中写入数据
18+
* [key]-[value] 键值对
19+
*/
20+
fun Context.putSP(key: String, value: Any) {
21+
val sp: SharedPreferences = this.getSharedPreferences(fileName, Context.MODE_PRIVATE)
22+
when (value) {
23+
is String -> sp.edit().putString(key, value).apply()
24+
is Int -> sp.edit().putInt(key, value).apply()
25+
is Long -> sp.edit().putLong(key, value).apply()
26+
is Boolean -> sp.edit().putBoolean(key, value).apply()
27+
is Float -> sp.edit().putFloat(key, value).apply()
28+
else -> toast("类型错误")
29+
}
30+
}
31+
32+
/**
33+
* 向SP中取出数据
34+
* [key]-[value] 键值对
35+
*/
36+
fun Context.getSP(key: String, defaultValue: Any): Any? {
37+
val sp: SharedPreferences = this.getSharedPreferences(fileName, Context.MODE_PRIVATE)
38+
when (defaultValue) {
39+
is String -> return sp.getString(key, defaultValue)
40+
is Int -> return sp.getInt(key, defaultValue)
41+
is Long -> return sp.getLong(key, defaultValue)
42+
is Boolean -> return sp.getBoolean(key, defaultValue)
43+
is Float -> return sp.getFloat(key, defaultValue)
44+
else -> toast("类型错误")
45+
}
46+
return null
47+
}
48+
49+
/**
50+
* 移除[key]对应的[getSP]
51+
*/
52+
fun Context.remove(key: String) {
53+
val sp: SharedPreferences = this.getSharedPreferences(fileName, Context.MODE_PRIVATE)
54+
sp.edit().remove(key).apply()
55+
}
56+
57+
/**
58+
* 清除[SharedPreferences]的所有数据
59+
*/
60+
fun Context.clear() {
61+
val sp: SharedPreferences = this.getSharedPreferences(fileName, Context.MODE_PRIVATE)
62+
sp.edit().clear().apply()
63+
}
64+
65+
/**
66+
* 检测[SharedPreferences]是否包含某个键值对
67+
*/
68+
fun Context.contains(key: String): Boolean {
69+
val sp: SharedPreferences = this.getSharedPreferences(fileName, Context.MODE_PRIVATE)
70+
return sp.contains(key)
71+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package com.taonce.utilmodule
2+
3+
import java.lang.StringBuilder
4+
import java.security.MessageDigest
5+
import java.security.NoSuchAlgorithmException
6+
import java.util.*
7+
import java.util.regex.Pattern
8+
9+
10+
/**
11+
* Author: taoyongxiang
12+
* Date: 2019/4/1
13+
* Desc: 字符串相关工具
14+
*/
15+
16+
/**
17+
* 判断字符串是否都是数字
18+
*/
19+
fun String.isNumAll(): Boolean {
20+
val pattern: Pattern = Pattern.compile("[0-9]*")
21+
return pattern.matcher(this).matches()
22+
}
23+
24+
/**
25+
* 验证字符串是否符合手机号规则
26+
*/
27+
fun String.isPhoneNum(): Boolean {
28+
val rule = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$"
29+
return (!this.isEmpty() && this.matches(Regex(rule)))
30+
}
31+
32+
/**
33+
* 判断字符串是否都是字母
34+
*/
35+
fun String.isLetterAll(): Boolean {
36+
val pattern: Pattern = Pattern.compile("[a-z]*")
37+
return pattern.matcher(this).matches()
38+
}
39+
40+
/**
41+
* 判断车牌号是否规则
42+
*/
43+
fun String.isCarNum(): Boolean =
44+
(!this.isEmpty()
45+
&& this.matches(Regex("^[\u4e00-\u9fa5]{1}[A-H_J-N_P-Z]{1}[A-H_J-N_P-Z_0-9]{5,6}$")))
46+
47+
/**
48+
* 判断身份证号码是否规则
49+
*/
50+
fun String.isIdCard(): Boolean {
51+
val pattern: Pattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])")
52+
return pattern.matcher(this).matches()
53+
}
54+
55+
/**
56+
* 指定范围内的大写转小写
57+
* [startIndex] 开始的下标
58+
* [endIndex] 结束的下标,此下标对应的字母不转
59+
* @return ABCD [3,4] -> ABCd
60+
*/
61+
fun String.toLowerCase(startIndex: Int = 0, endIndex: Int = this.length): String {
62+
val subString: String = this.substring(startIndex, endIndex)
63+
return if (0 <= startIndex && startIndex <= this.length
64+
&& 0 <= endIndex && endIndex <= this.length
65+
&& startIndex <= endIndex
66+
&& !this.isEmpty()
67+
) this.replaceFirst(
68+
subString,
69+
subString.toLowerCase(Locale.getDefault())
70+
) else ""
71+
}
72+
73+
/**
74+
* 指定范围内的小写转大写
75+
* [startIndex] 开始的下标
76+
* [endIndex] 结束的下标,此下标对应的字母不转
77+
* @return abcd [3,4] -> abcD
78+
*/
79+
fun String.toUpperCase(startIndex: Int = 0, endIndex: Int = this.length): String {
80+
val subString: String = this.substring(startIndex, endIndex)
81+
return if (0 <= startIndex && startIndex <= this.length
82+
&& 0 <= endIndex && endIndex <= this.length
83+
&& startIndex <= endIndex
84+
&& !this.isEmpty()
85+
) this.replaceFirst(
86+
subString,
87+
subString.toLowerCase(Locale.getDefault())
88+
)
89+
else ""
90+
}
91+
92+
/**
93+
* MD5加密
94+
*/
95+
fun String.md5(): String {
96+
return if (this.isEmpty()) ""
97+
else {
98+
try {
99+
val md5: MessageDigest = MessageDigest.getInstance("MD5")
100+
val bytes: ByteArray = md5.digest(this.toByteArray())
101+
return with(StringBuilder()) {
102+
bytes.forEach {
103+
val temp: String = Integer.toHexString(it.toInt() and 0xff)
104+
if (temp.length == 1) {
105+
append("0").append(temp)
106+
} else {
107+
append(temp)
108+
}
109+
}
110+
toString()
111+
}
112+
} catch (e: NoSuchAlgorithmException) {
113+
""
114+
}
115+
}
116+
}
117+
118+
/**
119+
* SHA-1加密
120+
*/
121+
fun String.sha1(): String {
122+
return if (this.isEmpty()) ""
123+
else {
124+
try {
125+
val md5: MessageDigest = MessageDigest.getInstance("SHA-1")
126+
val bytes: ByteArray = md5.digest(this.toByteArray())
127+
return with(StringBuilder()) {
128+
bytes.forEach {
129+
val temp: String = Integer.toHexString(it.toInt() and 0xff)
130+
if (temp.length == 1) {
131+
append("0").append(temp)
132+
} else {
133+
append(temp)
134+
}
135+
}
136+
toString()
137+
}
138+
} catch (e: NoSuchAlgorithmException) {
139+
""
140+
}
141+
}
142+
}
143+
144+
/**
145+
* SHA-256加密
146+
*/
147+
fun String.sha256(): String {
148+
return if (this.isEmpty()) ""
149+
else {
150+
try {
151+
val md5: MessageDigest = MessageDigest.getInstance("SHA-256")
152+
val bytes: ByteArray = md5.digest(this.toByteArray())
153+
return with(StringBuilder()) {
154+
bytes.forEach {
155+
val temp: String = Integer.toHexString(it.toInt() and 0xff)
156+
if (temp.length == 1) {
157+
append("0").append(temp)
158+
} else {
159+
append(temp)
160+
}
161+
}
162+
toString()
163+
}
164+
} catch (e: NoSuchAlgorithmException) {
165+
""
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)