Skip to content

Commit e0caf14

Browse files
committed
1、增加权限判断
2、隐藏部分类不让外部访问
1 parent 907ae30 commit e0caf14

File tree

11 files changed

+144
-15
lines changed

11 files changed

+144
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ Gradle:
77
88
maven { url 'https://jitpack.io' }
99
10-
implementation 'com.github.Lbyao:AndroidLogUtils:0.0.1'
10+
implementation 'com.github.Lbyao:AndroidLogUtils:0.1.3'
11+
1112
```

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ dependencies {
4141
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4242
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4343

44-
// implementation 'com.github.Lbyao:AndroidLogUtils:0.1.1'
44+
// implementation 'com.github.Lbyao:AndroidLogUtils:0.1.2'
4545
implementation project(':utils')
4646
}

app/src/main/java/com/liubuyao/androidlogutils/MainActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
55
import android.widget.TextView
66
import androidx.appcompat.widget.AppCompatButton
7-
import kotlinx.coroutines.GlobalScope
7+
import com.liubuyao.utils.CrashUtils
88

99
/**
1010
* @author lby
1111
*/
1212
class MainActivity : AppCompatActivity() {
1313

1414
val list = mutableListOf<String>()
15+
var isOpen = true
1516

1617
override fun onCreate(savedInstanceState: Bundle?) {
1718
super.onCreate(savedInstanceState)
@@ -21,5 +22,10 @@ class MainActivity : AppCompatActivity() {
2122
findViewById<TextView>(R.id.tvText).text = list[2]
2223
}
2324

25+
findViewById<AppCompatButton>(R.id.btnOpen).setOnClickListener {
26+
isOpen = !isOpen
27+
CrashUtils.openCrashSaveFile(isOpen)
28+
}
29+
2430
}
2531
}

app/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414
<androidx.appcompat.widget.AppCompatButton
1515
android:id="@+id/btnTest"
16+
android:text="崩溃"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"/>
19+
20+
<androidx.appcompat.widget.AppCompatButton
21+
android:id="@+id/btnOpen"
22+
android:text="保存文件开关"
1623
android:layout_width="wrap_content"
1724
android:layout_height="wrap_content"/>
1825

utils/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,6 @@ afterEvaluate {
6363
}
6464

6565
dependencies {
66+
implementation 'androidx.core:core-ktx:1.8.0'
6667
// implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
6768
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.liubuyao.utils
22

3+
import android.Manifest
34
import java.io.File
45

56
/**
67
* 常量
78
*
89
* @author lby
910
*/
10-
object Constants {
11+
internal object Constants {
1112

1213
/**
1314
* “/”-->斜杠分割线
1415
*/
1516
val separtor = File.separatorChar
1617

18+
val WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE
19+
1720
}

utils/src/main/java/com/liubuyao/utils/CrashUtils.kt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ package com.liubuyao.utils
55
*/
66
object CrashUtils {
77

8-
val DEFAULT = Thread.getDefaultUncaughtExceptionHandler()
8+
private val DEFAULT = Thread.getDefaultUncaughtExceptionHandler()
9+
private var isOpen = true
910

1011
/**
1112
* 初始化
@@ -42,20 +43,36 @@ object CrashUtils {
4243
onCrashListener: OnCrashListener?
4344
): Thread.UncaughtExceptionHandler {
4445
return Thread.UncaughtExceptionHandler { thread, throwable ->
45-
val crashDateTime = MyUtils.getNowDateByCrash()
46-
val throwableInfoToString = MyUtils.getThrowableInfoToString(throwable)
47-
val phoneInfo = MyUtils.getPhoneInfo(thread)
48-
MyUtils.writeToFile(
49-
"${path}crash_log_${crashDateTime}.txt",
50-
phoneInfo + throwableInfoToString,
51-
false
52-
)
46+
val isGranted = MyUtils.isGrantedPermission(Constants.WRITE_EXTERNAL_STORAGE)
47+
if (isGranted && isOpen) {
48+
val crashDateTime = MyUtils.getNowDateByCrash()
49+
val throwableInfoToString = MyUtils.getThrowableInfoToString(throwable)
50+
val phoneInfo = MyUtils.getPhoneInfo(thread)
51+
MyUtils.writeToFile(
52+
"${path}crash_log_${crashDateTime}.txt",
53+
phoneInfo + throwableInfoToString,
54+
false
55+
)
56+
} else {
57+
if (isOpen) {
58+
MyUtils.showToast("无存储权限,异常未写入文件!!")
59+
} else {
60+
MyUtils.showToast("关闭写入文件!!")
61+
}
62+
}
5363
onCrashListener?.onCrash(thread, throwable)
5464
//需要用默认的接收器处理,否则会卡死一段时间
5565
DEFAULT?.uncaughtException(thread, throwable)
5666
}
5767
}
5868

69+
/**
70+
* 设置是否保存到文件
71+
*/
72+
fun openCrashSaveFile(open: Boolean) {
73+
isOpen = open
74+
}
75+
5976
interface OnCrashListener {
6077
fun onCrash(thread: Thread, throwable: Throwable)
6178
}

utils/src/main/java/com/liubuyao/utils/MyUtils.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
package com.liubuyao.utils
22

3+
import android.app.Application
4+
35
/**
46
* 工具类的中间类直接调用方法通过该类处理,适当的解耦Util直接的联系
57
* ,如果后续更换,不用修改调用该功能的类
68
*
79
* @author lby
810
*/
9-
object MyUtils {
11+
internal object MyUtils {
12+
13+
/**
14+
* 获取Application
15+
*/
16+
fun getApp(): Application {
17+
return AppUtils.getApp()
18+
}
1019

1120
/**
1221
* 获取sd卡是否可用
@@ -76,11 +85,41 @@ object MyUtils {
7685
return str.toString()
7786
}
7887

88+
/**
89+
* 是否是空白字符串
90+
*/
7991
fun isSpace(string: String?): Boolean {
8092
return StringUtils.isSpace(string)
8193
}
8294

95+
/**
96+
* 写入文件
97+
*/
8398
fun writeToFile(pathFile: String, content: String, append: Boolean) {
8499
FileUtils.writeToFile(pathFile, content, append)
85100
}
101+
102+
/**
103+
* 是否获取到权限
104+
* @param permissions 要查询的多个权限
105+
*
106+
* @return 有一个没获取到权限就会返回false
107+
*/
108+
fun isGrantedPermission(vararg permissions: String): Boolean {
109+
return PermissionsUtils.getIsGrantedPermission(*permissions)
110+
}
111+
112+
/**
113+
* 是否获取到权限
114+
* @param permission 要查询的权限
115+
*
116+
* @return true 有 false 无
117+
*/
118+
fun isGrantedPermission(permission: String): Boolean {
119+
return PermissionsUtils.getIsGrantedPermission(permission)
120+
}
121+
122+
fun showToast(content: String) {
123+
ToastUtils.showToast(content)
124+
}
86125
}

utils/src/main/java/com/liubuyao/utils/PathUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object PathUtils {
3030
return if (!MyUtils.getSDCardCanUsed()) {
3131
return ""
3232
} else {
33-
val externalCacheDir = AppUtils.getApp().externalCacheDir ?: return ""
33+
val externalCacheDir = MyUtils.getApp().externalCacheDir ?: return ""
3434
getAbsolutePathByFile(externalCacheDir)
3535
}
3636
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.liubuyao.utils
2+
3+
import android.content.pm.PackageManager
4+
import androidx.core.content.ContextCompat
5+
6+
/**
7+
* @author lby
8+
*/
9+
object PermissionsUtils {
10+
11+
/**
12+
* 是否获取到权限
13+
*
14+
* @return true 有权限 false 无
15+
*/
16+
fun getIsGrantedPermission(vararg permissions: String): Boolean {
17+
for (permission in permissions) {
18+
if (!getIsGrantedPermission(permission)) {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
25+
/**
26+
* 是否获取到权限
27+
*
28+
* @return true 有权限 false 无
29+
*/
30+
fun getIsGrantedPermission(permission: String): Boolean {
31+
return ContextCompat.checkSelfPermission(
32+
MyUtils.getApp(),
33+
permission
34+
) == PackageManager.PERMISSION_GRANTED
35+
}
36+
37+
}

0 commit comments

Comments
 (0)