| author | Da Risk <da_risk@geekorum.com> |
| Mon, 20 May 2019 16:35:57 -0700 | |
| changeset 163 | 07a90afdb9db |
| parent 155 | 13bf2b0f3940 |
| child 278 | 834acfd87281 |
| permissions | -rw-r--r-- |
| 131 | 1 |
/* |
2 |
* Geekttrss is a RSS feed reader application on the Android Platform. |
|
3 |
* |
|
|
137
5464f07a306c
Update copyright headers for 2019
Da Risk <da_risk@geekorum.com>
parents:
136
diff
changeset
|
4 |
* Copyright (C) 2017-2019 by Frederic-Charles Barthelery. |
| 131 | 5 |
* |
6 |
* This file is part of Geekttrss. |
|
7 |
* |
|
8 |
* Geekttrss is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* Geekttrss is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with Geekttrss. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
|
21 |
package com.geekorum.ttrss.debugtools |
|
22 |
||
23 |
import android.app.Application |
|
24 |
import android.content.ContentResolver |
|
25 |
import android.os.Build |
|
26 |
import android.os.StrictMode |
|
|
139
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
27 |
import android.os.StrictMode.allowThreadDiskReads |
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
28 |
import android.os.strictmode.Violation |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
29 |
import androidx.annotation.RequiresApi |
| 131 | 30 |
import com.geekorum.geekdroid.dagger.AppInitializer |
31 |
import com.geekorum.geekdroid.dagger.AppInitializersModule |
|
32 |
import com.geekorum.ttrss.BuildConfig |
|
33 |
import dagger.Binds |
|
34 |
import dagger.Module |
|
35 |
import dagger.multibindings.IntoSet |
|
36 |
import kotlinx.coroutines.Dispatchers |
|
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
37 |
import timber.log.Timber |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
38 |
import java.util.concurrent.Executors |
| 131 | 39 |
import javax.inject.Inject |
|
154
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
40 |
import kotlin.jvm.internal.Reflection |
| 131 | 41 |
|
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
42 |
private const val TAG = "StrictMode" |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
43 |
|
| 131 | 44 |
/** |
45 |
* Configure StrictMode policies |
|
46 |
*/ |
|
|
163
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
47 |
class StrictModeInitializer @Inject constructor() : AppInitializer {
|
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
48 |
private val listenerExecutor by lazy { Executors.newSingleThreadExecutor() }
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
49 |
private val shouldBeFatal: Boolean = (BuildConfig.DEBUG) |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
50 |
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
51 |
@delegate:RequiresApi(Build.VERSION_CODES.P) |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
52 |
private val violationListener: ViolationListener by lazy { ViolationListener() }
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
53 |
|
| 131 | 54 |
override fun initialize(app: Application) {
|
55 |
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder() |
|
56 |
.detectActivityLeaks() |
|
57 |
.detectCleartextNetwork() |
|
58 |
.detectFileUriExposure() |
|
59 |
.detectLeakedClosableObjects() |
|
60 |
.detectLeakedRegistrationObjects() |
|
61 |
.detectLeakedSqlLiteObjects() |
|
|
143
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
62 |
.penaltyLog() |
| 131 | 63 |
.apply {
|
64 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
65 |
detectContentUriWithoutPermission() |
|
|
143
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
66 |
// because crashlytics don't tag its socket |
|
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
67 |
if (BuildConfig.FLAVOR != "google" || !shouldBeFatal) {
|
|
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
68 |
detectUntaggedSockets() |
|
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
69 |
} |
| 131 | 70 |
} |
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
71 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
72 |
// appcompat use nonsdk |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
73 |
// detectNonSdkApiUsage() |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
74 |
} |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
75 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
76 |
penaltyListener(listenerExecutor, violationListener) |
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
77 |
} |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
78 |
if (shouldBeFatal) {
|
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
79 |
penaltyDeath() |
| 131 | 80 |
} |
81 |
} |
|
82 |
.build()) |
|
83 |
||
84 |
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder() |
|
85 |
.detectAll() |
|
|
143
f52f2e9f99ee
StrictMode: Don't detect untaggedSocket if running google flavor and fatal
Da Risk <da_risk@geekorum.com>
parents:
139
diff
changeset
|
86 |
.penaltyLog() |
| 131 | 87 |
.apply {
|
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
88 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
89 |
penaltyListener(listenerExecutor, violationListener) |
|
136
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
90 |
} |
|
83d9f96bfead
StrictMode: log error with Timber if possible
Da Risk <da_risk@geekorum.com>
parents:
131
diff
changeset
|
91 |
if (shouldBeFatal) {
|
| 131 | 92 |
penaltyDeath() |
93 |
} |
|
94 |
} |
|
95 |
.build()) |
|
96 |
} |
|
97 |
||
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
98 |
@RequiresApi(Build.VERSION_CODES.P) |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
99 |
private inner class ViolationListener : StrictMode.OnThreadViolationListener, StrictMode.OnVmViolationListener {
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
100 |
override fun onThreadViolation(v: Violation) = onViolation(v) |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
101 |
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
102 |
override fun onVmViolation(v: Violation) = onViolation(v) |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
103 |
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
104 |
private fun onViolation(v: Violation) {
|
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
105 |
Timber.tag(TAG).e(v, "StrictMode violation") |
|
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
106 |
} |
| 131 | 107 |
} |
|
155
13bf2b0f3940
StrictMode: relax strictmode. Don't be fatal if running on FirebaseTestDevice
Da Risk <da_risk@geekorum.com>
parents:
154
diff
changeset
|
108 |
|
| 131 | 109 |
} |
110 |
||
111 |
||
|
154
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
112 |
@Module(includes = [AppInitializersModule::class, KotlinInitializerModule::class]) |
| 131 | 113 |
abstract class StrictModeModule {
|
114 |
@Binds |
|
115 |
@IntoSet |
|
116 |
abstract fun bindStrictModeInitializer(strictModeInitializer: StrictModeInitializer): AppInitializer |
|
117 |
} |
|
118 |
||
119 |
||
|
154
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
120 |
/** |
|
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
121 |
* Initialize some kotlin functionality eagerly to avoid a DiskReadViolation out of our control |
|
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
122 |
*/ |
|
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
123 |
class KotlinInitializer @Inject constructor() : AppInitializer {
|
| 131 | 124 |
override fun initialize(app: Application) {
|
125 |
// load Dispatchers.Main at application start |
|
|
139
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
126 |
withStrictMode(allowThreadDiskReads()) {
|
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
127 |
Dispatchers.Main |
|
154
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
128 |
val k = Reflection.getOrCreateKotlinClass(Object::class.java) |
|
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
129 |
Timber.d("initialize kotlin Klass with class $k")
|
|
139
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
130 |
} |
| 131 | 131 |
} |
132 |
} |
|
133 |
||
134 |
@Module |
|
|
154
f5be33a9e354
StrictMode: initialize kotlin Klass in KotlinInitializer to avoid disk read violation
Da Risk <da_risk@geekorum.com>
parents:
143
diff
changeset
|
135 |
abstract class KotlinInitializerModule {
|
| 131 | 136 |
@Binds |
137 |
@IntoSet |
|
|
163
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
138 |
abstract fun bindKotlinInitializer(kotlinInitializer: KotlinInitializer): AppInitializer |
| 131 | 139 |
|
140 |
} |
|
|
139
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
141 |
|
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
142 |
/** |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
143 |
* Run [block] then restore [originalThreadPolicy] |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
144 |
* Allows to write code like |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
145 |
* ``` |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
146 |
* withStrictMode(allowThreadDiskReads()) {
|
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
147 |
* //read disk data |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
148 |
* } |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
149 |
* |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
150 |
* ``` |
|
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
151 |
*/ |
|
163
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
152 |
inline fun <R> withStrictMode(originalThreadPolicy: StrictMode.ThreadPolicy, block: () -> R): R {
|
|
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
153 |
try {
|
|
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
154 |
return block() |
|
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
155 |
} finally {
|
|
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
156 |
StrictMode.setThreadPolicy(originalThreadPolicy) |
|
07a90afdb9db
StrictMode: TinyrssAcountManager: start allowing disk read for a few methods
Da Risk <da_risk@geekorum.com>
parents:
155
diff
changeset
|
157 |
} |
|
139
74f1150de442
Add helper method to run with temporary strict mode
Da Risk <da_risk@geekorum.com>
parents:
137
diff
changeset
|
158 |
} |