Skip to content

Commit

Permalink
Backport home-assistant#1138 Sentry Filtering and home-assistant#1143
Browse files Browse the repository at this point in the history
…Add ability to opt out of crash reporting.
  • Loading branch information
anyuta1166 committed Mar 10, 2022
1 parent 394edb6 commit 9365aba
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 7 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ android {
buildTypes {
named("debug").configure {
applicationIdSuffix = ".debug"
manifestPlaceholders["sentryEnabled"] = "false"
}
named("release").configure {
isDebuggable = false
isJniDebuggable = false
isZipAlignEnabled = true
signingConfig = signingConfigs.getByName("release")
manifestPlaceholders["sentryEnabled"] = "true"
}
}
flavorDimensions("version")
Expand Down
5 changes: 1 addition & 4 deletions app/src/full/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

<application
android:name=".HomeAssistantApplication" >
<meta-data android:name="io.sentry.auto-init" android:value="${sentryEnabled}" />
<meta-data android:name="io.sentry.auto-init" android:value="false" />
<meta-data android:name="io.sentry.release" android:value="${sentryRelease}" />
<meta-data android:name="io.sentry.session-tracking.enable" android:value="true" />
<meta-data android:name="io.sentry.ndk.enable" android:value="false" />
<meta-data android:name="io.sentry.dsn" android:value="https://2d646f40f9574e0b9579e301a69bb030@o427061.ingest.sentry.io/5372876" />

<receiver
android:name=".notifications.NotificationActionReceiver"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.homeassistant.companion.android

import android.content.Context
import io.sentry.android.core.SentryAndroid
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.net.ssl.SSLException
import javax.net.ssl.SSLHandshakeException
import javax.net.ssl.SSLProtocolException

fun initCrashReporting(context: Context, enabled: Boolean) {
// Don't init on debug builds or when disabled
if (BuildConfig.DEBUG || !enabled)
return

SentryAndroid.init(context) { options ->
options.isEnableSessionTracking = true
options.isEnableNdk = false
options.dsn = "https://2d646f40f9574e0b9579e301a69bb030@o427061.ingest.sentry.io/5372876"
options.setBeforeSend { event, hint ->
return@setBeforeSend when (hint) {
is ConnectException,
is SocketTimeoutException,
is SSLException,
is SSLHandshakeException,
is SSLProtocolException,
is UnknownHostException -> null
else -> event
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ import io.homeassistant.companion.android.common.dagger.Graph
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.migrations.Migrations
import io.homeassistant.companion.android.sensors.SensorReceiver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch

open class HomeAssistantApplication : Application(), GraphComponentAccessor {

lateinit var graph: Graph
private val ioScope: CoroutineScope = CoroutineScope(Dispatchers.IO + Job())

override fun onCreate() {
super.onCreate()

graph = Graph(this, 0)

ioScope.launch {
initCrashReporting(applicationContext, graph.appComponent.integrationUseCase().isCrashReporting())
}

Migrations(this).migrate()

val sensorReceiver = SensorReceiver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ class SettingsFragment : PreferenceFragmentCompat(), SettingsView {
return@setOnPreferenceClickListener true
}

if (BuildConfig.FLAVOR == "full") {
findPreference<SwitchPreference>("crash_reporting")?.let {
it.isVisible = true
it.setOnPreferenceChangeListener { _, newValue ->
val checked = newValue as Boolean

true
}
}
}

findPreference<Preference>("version")?.let {
it.isCopyingEnabled = true
it.summary = BuildConfig.VERSION_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SettingsPresenterImpl @Inject constructor(
return@runBlocking when (key) {
"fullscreen" -> integrationUseCase.isFullScreenEnabled()
"app_lock" -> authenticationUseCase.isLockEnabled()
"crash_reporting" -> integrationUseCase.isCrashReporting()
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand All @@ -44,6 +45,7 @@ class SettingsPresenterImpl @Inject constructor(
when (key) {
"fullscreen" -> integrationUseCase.setFullScreenEnabled(value)
"app_lock" -> authenticationUseCase.setLockEnabled(value)
"crash_reporting" -> integrationUseCase.setCrashReporting(value)
else -> throw IllegalArgumentException("No boolean found by this key: $key")
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_android_debug_bridge.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/colorAccent"
android:pathData="M15,9A1,1 0 0,1 14,8A1,1 0 0,1 15,7A1,1 0 0,1 16,8A1,1 0 0,1 15,9M9,9A1,1 0 0,1 8,8A1,1 0 0,1 9,7A1,1 0 0,1 10,8A1,1 0 0,1 9,9M16.12,4.37L18.22,2.27L17.4,1.44L15.09,3.75C14.16,3.28 13.11,3 12,3C10.88,3 9.84,3.28 8.91,3.75L6.6,1.44L5.78,2.27L7.88,4.37C6.14,5.64 5,7.68 5,10V11H19V10C19,7.68 17.86,5.64 16.12,4.37M5,16C5,19.86 8.13,23 12,23A7,7 0 0,0 19,16V12H5V16Z" />
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<string name="connect_to_home_internet">Make sure your phone is connected
to your home internet.</string>
<string name="connected_to_home_assistant">Connected to Home Assistant</string>
<string name="crash_reporting">Crash Reporting</string>
<string name="crash_reporting_summary">Help the developers fix bugs and crashes by leaving this enabled. If the application crashes this will automatically generate and send a report. If you notice a crash also create an issue on Github!</string>
<string name="create_template">Create Template</string>
<string name="developer_tools">Developer Tools</string>
<string name="device_class">Device Class</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@
android:icon="@drawable/app_icon"
android:title="@string/application_version"
android:summary="1.0.0 (1)"/>
<SwitchPreference
android:key="crash_reporting"
android:icon="@drawable/ic_android_debug_bridge"
app:isPreferenceVisible="false"
android:title="@string/crash_reporting"
android:summary="@string/crash_reporting_summary" />
</PreferenceCategory>

</androidx.preference.PreferenceScreen>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.homeassistant.companion.android

import android.content.Context

fun initCrashReporting(context: Context, enabled: Boolean) {
// Noop
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object Config {
}

object Misc {
const val sentry = "io.sentry:sentry-android:2.2.2"
const val sentry = "io.sentry:sentry-android:3.1.0"
const val jackson = "com.fasterxml.jackson.module:jackson-module-kotlin:2.10.1"
const val threeTenBp = "org.threeten:threetenbp:1.4.0"
const val javaxInject = "javax.inject:javax.inject:1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class IntegrationRepositoryImpl @Inject constructor(
private const val PREF_SENSORS_REGISTERED = "sensors_registered"

private const val PREF_MINIMUM_ACCURACY = "minimum_accuracy"
private const val PREF_CRASH_REPORTING_DISABLED = "crash_reporting"
}

override suspend fun registerDevice(deviceRegistration: DeviceRegistration) {
Expand Down Expand Up @@ -294,6 +295,14 @@ class IntegrationRepositoryImpl @Inject constructor(
return localStorage.getInt(PREF_MINIMUM_ACCURACY) ?: 5000
}

override suspend fun setCrashReporting(crashReportingEnabled: Boolean) {
localStorage.putBoolean(PREF_CRASH_REPORTING_DISABLED, !crashReportingEnabled)
}

override suspend fun isCrashReporting(): Boolean {
return !localStorage.getBoolean(PREF_CRASH_REPORTING_DISABLED)
}

override suspend fun sessionTimeOut(value: Int) {
localStorage.putInt(PREF_SESSION_TIMEOUT, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface IntegrationRepository {
suspend fun minimumAccuracy(value: Int)
suspend fun getMinimumAccuracy(): Int

suspend fun setCrashReporting(crashReportingEnabled: Boolean)
suspend fun isCrashReporting(): Boolean

suspend fun sessionTimeOut(value: Int)
suspend fun getSessionTimeOut(): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ interface IntegrationUseCase {
suspend fun minimumAccuracy(value: Int)
suspend fun getMinimumAccuracy(): Int

suspend fun setCrashReporting(crashReportingEnabled: Boolean)
suspend fun isCrashReporting(): Boolean

suspend fun setFullScreenEnabled(enabled: Boolean)
suspend fun isFullScreenEnabled(): Boolean

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class IntegrationUseCaseImpl @Inject constructor(
return integrationRepository.getMinimumAccuracy()
}

override suspend fun setCrashReporting(crashReportingEnabled: Boolean) {
return integrationRepository.setCrashReporting(crashReportingEnabled)
}

override suspend fun isCrashReporting(): Boolean {
return integrationRepository.isCrashReporting()
}

override suspend fun sessionTimeOut(value: Int) {
return integrationRepository.sessionTimeOut(value)
}
Expand Down

0 comments on commit 9365aba

Please sign in to comment.