Skip to content

Commit 0bacbb8

Browse files
committed
1、增加provider
2、完成CrashUtils,引用库自动初始化 3、增加CrashUtils需要的工具类方法
1 parent add731a commit 0bacbb8

File tree

14 files changed

+559
-9
lines changed

14 files changed

+559
-9
lines changed

utils/build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,4 @@ afterEvaluate {
6464

6565
dependencies {
6666

67-
implementation 'androidx.core:core-ktx:1.7.0'
68-
implementation 'androidx.appcompat:appcompat:1.3.0'
69-
implementation 'com.google.android.material:material:1.4.0'
70-
testImplementation 'junit:junit:4.13.2'
71-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
72-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
7367
}

utils/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.liubuyao.utils">
44

5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:supportsRtl="true">
10+
<!-- 声明Provider应用启动时初始化library -->
11+
<provider
12+
android:authorities="${applicationId}.SuperUtilsProvider"
13+
android:name=".provider.SuperUtilsProvider"
14+
android:exported="false"/>
15+
</application>
516
</manifest>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.liubuyao.utils
2+
3+
import android.app.Application
4+
import android.content.Context
5+
import android.os.Build
6+
7+
/**
8+
* 用于初始化context全局调用使用
9+
*
10+
* @author lby
11+
*/
12+
object AppUtils {
13+
14+
@Volatile
15+
var application: Application? = null
16+
17+
fun init(context: Context?) {
18+
if (context == null) {
19+
throw NullPointerException("未初始化Library")
20+
}
21+
application = context.applicationContext as Application?
22+
}
23+
24+
fun getApp(): Application {
25+
if (application == null) {
26+
throw NullPointerException("未初始化Library")
27+
}
28+
return application!!
29+
}
30+
31+
fun getPackageName(): String {
32+
return getApp().packageName
33+
}
34+
35+
fun getVersionName(): String {
36+
return getVersionName(getPackageName())
37+
}
38+
39+
fun getVersionName(packageName: String?): String {
40+
if (packageName.isNullOrEmpty()) {
41+
return ""
42+
}
43+
val pm = getApp().packageManager
44+
val packageInfo = pm.getPackageInfo(packageName, 0)
45+
return if (packageInfo == null) "" else packageInfo.packageName
46+
}
47+
48+
fun getVersionCode(): Int {
49+
return getVersionCode(getPackageName())
50+
}
51+
52+
fun getVersionCode(packageName: String?): Int {
53+
if (packageName.isNullOrEmpty()) {
54+
return -1
55+
}
56+
val pm = getApp().packageManager
57+
val packageInfo = pm.getPackageInfo(packageName, 0)
58+
return packageInfo?.versionCode ?: -1
59+
}
60+
61+
fun getSDKVersionName(): String {
62+
return Build.VERSION.RELEASE
63+
}
64+
65+
fun getSDKVersionCode(): Int {
66+
return Build.VERSION.SDK_INT
67+
}
68+
69+
/**
70+
* 制造商
71+
*/
72+
fun getManufacturer(): String {
73+
return Build.MANUFACTURER
74+
}
75+
76+
/**
77+
* 手机厂商
78+
*/
79+
fun getBrand(): String {
80+
return Build.BRAND
81+
}
82+
83+
/**
84+
* 型号
85+
*/
86+
fun getModel(): String {
87+
return Build.MODEL
88+
}
89+
90+
/**
91+
* 获取ABI
92+
*/
93+
fun getABI(): String {
94+
return Build.CPU_ABI
95+
}
96+
97+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.liubuyao.utils
2+
3+
import java.io.File
4+
5+
/**
6+
* 常量
7+
*
8+
* @author lby
9+
*/
10+
object Constants {
11+
12+
/**
13+
* “/”-->斜杠分割线
14+
*/
15+
val separtor = File.separatorChar
16+
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.liubuyao.utils
2+
3+
/**
4+
* crash日志保存工具类,自动初始化
5+
*/
6+
object CrashUtils {
7+
8+
/**
9+
* 初始化
10+
*/
11+
fun init(path: String) {
12+
init(path, null)
13+
}
14+
15+
/**
16+
* 初始化
17+
*/
18+
fun init(path: String, onCrashListener: OnCrashListener?) {
19+
val realPath = if (path.isNotEmpty()) {
20+
""
21+
} else if (path.endsWith(Constants.separtor)) {
22+
path
23+
} else {
24+
path + Constants.separtor
25+
}
26+
Thread.setDefaultUncaughtExceptionHandler(
27+
getUncaughtExceptionHandler(
28+
realPath,
29+
onCrashListener
30+
)
31+
)
32+
}
33+
34+
35+
/**
36+
* 获取到异常崩溃接收handle
37+
*/
38+
private fun getUncaughtExceptionHandler(
39+
path: String,
40+
onCrashListener: OnCrashListener?
41+
): Thread.UncaughtExceptionHandler {
42+
return Thread.UncaughtExceptionHandler { thread, throwable ->
43+
val crashDateTime = MyUtils.getNowDateByCrash()
44+
val throwableInfoToString = MyUtils.getThrowableInfoToString(throwable)
45+
val phoneInfo = MyUtils.getPhoneInfo()
46+
MyUtils.writeToFile("$path$crashDateTime.txt",phoneInfo+throwableInfoToString,false)
47+
onCrashListener?.onCrash(thread, throwable)
48+
}
49+
}
50+
51+
interface OnCrashListener {
52+
fun onCrash(thread: Thread, throwable: Throwable)
53+
}
54+
55+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.liubuyao.utils
2+
3+
import java.text.SimpleDateFormat
4+
import java.util.*
5+
6+
/**
7+
* 日期工具类
8+
*
9+
* @author lby
10+
*/
11+
object DateUtils {
12+
/**
13+
* 保存用,中间没有空格
14+
*/
15+
const val FORMAT_YMD_HMS_SAVE = "yyyy_MM_dd-HH_mm_ss"
16+
17+
/**
18+
* 正常格式的日期格式用年月日用-分隔,时分秒用:分隔
19+
*/
20+
const val FORMAT_YMD_HMS_upload = "yyyy-MM-dd HH:mm:ss"
21+
22+
/**
23+
* 正常格式的日期格式用年月日用-分隔
24+
*/
25+
const val FORMAT_YMD_upload = "yyyy-MM-dd"
26+
27+
/**
28+
* 正常格式的日期格式用年月日用/分隔,时分秒用:分隔
29+
*/
30+
const val FORMAT_YMD_HMS_upload2 = "yyyy/MM/dd HH:mm:ss"
31+
32+
/**
33+
* 正常格式的日期格式用年月日用/分隔
34+
*/
35+
const val FORMAT_YMD_upload2 = "yyyy/MM/dd"
36+
37+
/**
38+
* @return string
39+
*/
40+
fun getNowDateToString(): String {
41+
return formatDate(Date(), FORMAT_YMD_HMS_upload)
42+
}
43+
44+
/**
45+
* @param format 格式化的样式
46+
* @return string
47+
*/
48+
fun getNowDateToString(format: String): String {
49+
return formatDate(Date(), format)
50+
}
51+
52+
/**
53+
* @return Date
54+
*/
55+
fun getNowDate() = Date()
56+
57+
/**
58+
* 格式化日期
59+
* @return string
60+
*/
61+
fun formatDate(date: Date, format: String): String {
62+
return SimpleDateFormat(format, Locale.getDefault()).format(date)
63+
}
64+
65+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.liubuyao.utils
2+
3+
import android.os.Build
4+
import java.io.File
5+
import java.io.IOException
6+
import java.nio.file.Files
7+
import java.nio.file.StandardOpenOption
8+
9+
object FileUtils {
10+
11+
/**
12+
* 通过路径获取文件
13+
* @return 文件
14+
*/
15+
fun getFile(filePath: String?): File? {
16+
return if (MyUtils.isSpace(filePath)) null else File(filePath)
17+
}
18+
19+
/**
20+
* 创建文件
21+
* @return 是否成功
22+
*/
23+
fun createFile(filePath: String?): Boolean {
24+
return createFile(getFile(filePath))
25+
}
26+
27+
/**
28+
* 创建文件
29+
* @return 是否成功
30+
*/
31+
fun createFile(file: File?): Boolean {
32+
if (file == null) return false
33+
if (file.exists()) return file.isFile
34+
return file.createNewFile()
35+
}
36+
37+
/**
38+
* 写文本到文件内
39+
*
40+
* @return 是否成功
41+
*/
42+
fun writeToFile(filePath: String?, content: String?, append: Boolean): Boolean {
43+
return writeToFile(getFile(filePath), content, append)
44+
}
45+
46+
/**
47+
* 写文本到文件内
48+
*
49+
* @return 是否成功
50+
*/
51+
fun writeToFile(file: File?, content: String?, append: Boolean = false): Boolean {
52+
if (file == null || content == null) return false
53+
if (!createFile(file)) {
54+
return false
55+
}
56+
57+
try {
58+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
59+
if (append) {
60+
Files.write(file.toPath(), content.toByteArray(), StandardOpenOption.APPEND)
61+
} else {
62+
Files.write(file.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
63+
}
64+
} else {
65+
file.bufferedWriter().use {
66+
if (append) {
67+
it.append(content)
68+
} else {
69+
it.write(content)
70+
}
71+
}
72+
}
73+
return true
74+
} catch (e: IOException) {
75+
return false
76+
}
77+
}
78+
79+
80+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.liubuyao.utils;
1+
package com.liubuyao.utils
22

33
/**
4+
* 日志工具类
45
*
56
* @author lby
67
*/
7-
public class LogUtils {
8+
object LogUtils {
89

9-
}
10+
}

0 commit comments

Comments
 (0)