Fix some StrictMode violations and start logging them (#2031)

This commit is contained in:
Harsh Shandilya
2022-07-24 17:16:22 +05:30
committed by GitHub
parent 9c5e9c8e43
commit 140175823f
2 changed files with 45 additions and 5 deletions

View File

@@ -5,6 +5,8 @@
package app.passwordstore package app.passwordstore
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build
import android.os.StrictMode
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
@@ -24,10 +26,13 @@ import com.google.android.material.color.DynamicColors
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import io.sentry.Sentry import io.sentry.Sentry
import io.sentry.protocol.User import io.sentry.protocol.User
import java.util.concurrent.Executors
import javax.inject.Inject import javax.inject.Inject
import logcat.AndroidLogcatLogger import logcat.AndroidLogcatLogger
import logcat.LogPriority.DEBUG import logcat.LogPriority.DEBUG
import logcat.LogPriority.VERBOSE
import logcat.LogcatLogger import logcat.LogcatLogger
import logcat.logcat
@Suppress("Unused") @Suppress("Unused")
@HiltAndroidApp @HiltAndroidApp
@@ -47,6 +52,7 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere
prefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) prefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false)
) { ) {
LogcatLogger.install(AndroidLogcatLogger(DEBUG)) LogcatLogger.install(AndroidLogcatLogger(DEBUG))
setVmPolicy()
} }
prefs.registerOnSharedPreferenceChangeListener(this) prefs.registerOnSharedPreferenceChangeListener(this)
setNightMode() setNightMode()
@@ -75,6 +81,44 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere
} }
} }
private fun setVmPolicy() {
val builder =
StrictMode.VmPolicy.Builder()
.detectActivityLeaks()
.detectCleartextNetwork()
.detectFileUriExposure()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects()
.detectLeakedSqlLiteObjects()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.detectContentUriWithoutPermission()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
builder.detectCredentialProtectedWhileLocked().detectImplicitDirectBoot()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
builder.detectNonSdkApiUsage()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.detectIncorrectContextUse().detectUnsafeIntentLaunch()
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
builder.penaltyListener(Executors.newSingleThreadExecutor()) { violation ->
logcat(VERBOSE) { violation.stackTraceToString() }
}
} else {
builder.penaltyLog()
}
val policy = builder.build()
StrictMode.setVmPolicy(policy)
}
private fun setNightMode() { private fun setNightMode() {
AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.setDefaultNightMode(
when (prefs.getString(PreferenceKeys.APP_THEME) ?: getString(R.string.app_theme_def)) { when (prefs.getString(PreferenceKeys.APP_THEME) ?: getString(R.string.app_theme_def)) {

View File

@@ -4,7 +4,6 @@ import app.passwordstore.gradle.flavors.FlavorDimensions
import app.passwordstore.gradle.flavors.ProductFlavors import app.passwordstore.gradle.flavors.ProductFlavors
import com.android.build.api.variant.ApplicationAndroidComponentsExtension import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import io.sentry.android.gradle.SentryPlugin import io.sentry.android.gradle.SentryPlugin
import io.sentry.android.gradle.extensions.InstrumentationFeature
import io.sentry.android.gradle.extensions.SentryPluginExtension import io.sentry.android.gradle.extensions.SentryPluginExtension
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
@@ -32,10 +31,7 @@ class SentryPlugin : Plugin<Project> {
) )
ignoredBuildTypes.set(setOf("debug")) ignoredBuildTypes.set(setOf("debug"))
ignoredFlavors.set(setOf(ProductFlavors.FREE)) ignoredFlavors.set(setOf(ProductFlavors.FREE))
tracingInstrumentation { tracingInstrumentation { enabled.set(false) }
enabled.set(true)
features.set(setOf(InstrumentationFeature.FILE_IO))
}
autoInstallation.enabled.set(false) autoInstallation.enabled.set(false)
} }
} }