mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2026-10-22 23:42:14 +03:00
Fix MaterialAlertDialogBuilder crash with unthemed application context
This commit is contained in:
1 parent
2d49ea0435
commit
0c7c553ed4
2 files changed
+19
-4
No files matched your search
@@ -9,11 +9,22 @@ package org.moire.ultrasonic.util
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.view.ContextThemeWrapper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import java.lang.ref.WeakReference
|
||||
import org.moire.ultrasonic.R
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* MaterialAlertDialogBuilder requires a context whose theme inherits from Theme.AppCompat
|
||||
* (it resolves the colorPrimary attribute in ThemeEnforcement). An application or service
|
||||
* context carries no app theme, so it must be wrapped in a ContextThemeWrapper.
|
||||
*/
|
||||
private fun themedContext(context: Context): Context {
|
||||
if (context is Activity || context is ContextThemeWrapper) return context
|
||||
return ContextThemeWrapper(context, UiUtil.getStyleFromSettings(context))
|
||||
}
|
||||
|
||||
/*
|
||||
* InfoDialog can be used to show some information to the user. Typically it cannot be cancelled,
|
||||
* only dismissed via OK.
|
||||
@@ -25,7 +36,8 @@ open class InfoDialog(
|
||||
private val finishActivityOnClose: Boolean = false
|
||||
) {
|
||||
private val activityRef: WeakReference<Activity?> = WeakReference(activity)
|
||||
open var builder: MaterialAlertDialogBuilder = Builder(activityRef.get() ?: context, message)
|
||||
open var builder: MaterialAlertDialogBuilder =
|
||||
Builder(themedContext(activityRef.get() ?: context), message)
|
||||
|
||||
fun show() {
|
||||
builder.setOnCancelListener {
|
||||
@@ -74,7 +86,8 @@ class ErrorDialog(
|
||||
finishActivityOnClose: Boolean = false
|
||||
) : InfoDialog(context, message, activity, finishActivityOnClose) {
|
||||
|
||||
override var builder: MaterialAlertDialogBuilder = Builder(activity ?: context, message)
|
||||
override var builder: MaterialAlertDialogBuilder =
|
||||
Builder(themedContext(activity ?: context), message)
|
||||
|
||||
class Builder(context: Context) : MaterialAlertDialogBuilder(context) {
|
||||
constructor(context: Context, message: CharSequence?) : this(context) {
|
||||
@@ -101,7 +114,8 @@ class ConfirmationDialog(
|
||||
activity: Activity? = null,
|
||||
finishActivityOnClose: Boolean = false
|
||||
) : InfoDialog(context, message, activity, finishActivityOnClose) {
|
||||
override var builder: MaterialAlertDialogBuilder = Builder(activity ?: context, message)
|
||||
override var builder: MaterialAlertDialogBuilder =
|
||||
Builder(themedContext(activity ?: context), message)
|
||||
|
||||
class Builder(context: Context) : MaterialAlertDialogBuilder(context) {
|
||||
constructor(context: Context, message: CharSequence?) : this(context) {
|
||||
|
||||
@@ -43,7 +43,8 @@ object UiUtil {
|
||||
context.setTheme(R.style.UltrasonicTheme_Base)
|
||||
}
|
||||
|
||||
private fun getStyleFromSettings(context: Context): Int = when (Settings.theme.lowercase()) {
|
||||
@JvmStatic
|
||||
fun getStyleFromSettings(context: Context): Int = when (Settings.theme.lowercase()) {
|
||||
context.getString(R.string.setting_key_theme_dark) -> {
|
||||
R.style.UltrasonicTheme_Dark
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user