-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...s/test-app/src/main/kotlin/com/google/firebase/testing/crashlytics/PreFirebaseProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.crashlytics | ||
|
||
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 | ||
} |