Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Idea for exit cleanly on expected crash #6617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions firebase-sessions/test-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
android:exported="true"
android:label="@string/app_name"
android:name=".MainActivity"
android:process=":main"
android:theme="@style/Theme.Widget_test_app.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand All @@ -51,6 +50,15 @@
android:name="firebase_sessions_sessions_restart_timeout"
android:value="5" />

<provider
android:authorities="${applicationId}.preFirebase"
android:directBootAware="true"
android:exported="false"
android:initOrder="101"
android:name="com.google.firebase.testing.sessions.PreFirebaseProvider" />

<receiver android:name=".CrashBroadcastReceiver" />

<receiver
android:exported="false"
android:name="CrashWidgetProvider"
Expand All @@ -63,8 +71,6 @@
android:resource="@xml/homescreen_widget" />
</receiver>

<receiver android:name=".CrashBroadcastReceiver" />

<service
android:enabled="true"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class FirstFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
PreFirebaseProvider.expectedMessage = "i am expected"

binding.buttonCrash.setOnClickListener { throw RuntimeException("CRASHED") }
binding.buttonCrash.setOnClickListener { throw RuntimeException("so i am expected 123") }
binding.buttonNonFatal.setOnClickListener {
crashlytics.recordException(IllegalStateException())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.testing.sessions

import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import java.lang.Thread.UncaughtExceptionHandler
import kotlin.system.exitProcess

class PreFirebaseProvider : ContentProvider(), UncaughtExceptionHandler {
private var defaultUncaughtExceptionHandler: UncaughtExceptionHandler? = null

companion object {
/**
* Set an expected exception message. If the uncaught exception contains the given string, then
* the app will exit cleanly. Otherwise, it will propagate up to the default exception handler.
*/
var expectedMessage: String? = null
}

override fun onCreate(): Boolean {
defaultUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(this)

return false
}

/* Returns if the given exception contains the expectedMessage in its message. */
private fun isExpected(throwable: Throwable): Boolean =
expectedMessage?.run { throwable.message?.contains(this) } ?: false

override fun uncaughtException(thread: Thread, throwable: Throwable) {
if (isExpected(throwable)) {
// Exit cleanly
exitProcess(0)
} else {
// Propagate up to the default exception handler
defaultUncaughtExceptionHandler?.uncaughtException(thread, throwable)
}
}

override fun query(
uri: Uri,
projection: Array<out String>?,
selection: String?,
selectionArgs: Array<out String>?,
sortOrder: String?
): Cursor? = null

override fun getType(uri: Uri): String? = null

override fun insert(uri: Uri, values: ContentValues?): Uri? = null

override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0

override fun update(
uri: Uri,
values: ContentValues?,
selection: String?,
selectionArgs: Array<out String>?
): Int = 0
}
Loading