From 140175823f1c9763fb67a8d3ad135b7c94741421 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 24 Jul 2022 17:16:22 +0530 Subject: Fix some StrictMode violations and start logging them (#2031) --- app/src/main/java/app/passwordstore/Application.kt | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'app/src/main/java') diff --git a/app/src/main/java/app/passwordstore/Application.kt b/app/src/main/java/app/passwordstore/Application.kt index 05bffbfb..e1436d29 100644 --- a/app/src/main/java/app/passwordstore/Application.kt +++ b/app/src/main/java/app/passwordstore/Application.kt @@ -5,6 +5,8 @@ package app.passwordstore import android.content.SharedPreferences +import android.os.Build +import android.os.StrictMode import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY 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 io.sentry.Sentry import io.sentry.protocol.User +import java.util.concurrent.Executors import javax.inject.Inject import logcat.AndroidLogcatLogger import logcat.LogPriority.DEBUG +import logcat.LogPriority.VERBOSE import logcat.LogcatLogger +import logcat.logcat @Suppress("Unused") @HiltAndroidApp @@ -47,6 +52,7 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere prefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) ) { LogcatLogger.install(AndroidLogcatLogger(DEBUG)) + setVmPolicy() } prefs.registerOnSharedPreferenceChangeListener(this) 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() { AppCompatDelegate.setDefaultNightMode( when (prefs.getString(PreferenceKeys.APP_THEME) ?: getString(R.string.app_theme_def)) { -- cgit v1.2.3