Skip to content

Commit 099db3e

Browse files
committed
Fixed a crash when a dialog gets opened from a web view on android P
1 parent d29d7d0 commit 099db3e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

viewpump/src/main/java/dev/b3nedikt/viewpump/ViewPumpAppCompatDelegate.kt

+34
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package androidx.appcompat.app
44

5+
import android.annotation.SuppressLint
56
import android.content.Context
67
import android.os.Build
78
import android.util.AttributeSet
@@ -10,6 +11,9 @@ import android.view.LayoutInflater
1011
import android.view.View
1112
import android.webkit.WebView
1213
import android.widget.SearchView
14+
import androidx.appcompat.widget.AlertDialogLayout
15+
import androidx.appcompat.widget.ButtonBarLayout
16+
import androidx.appcompat.widget.DialogTitle
1317
import androidx.core.view.LayoutInflaterCompat
1418
import dev.b3nedikt.viewpump.InflateRequest
1519
import dev.b3nedikt.viewpump.InflateResult
@@ -87,6 +91,13 @@ class ViewPumpAppCompatDelegate @JvmOverloads constructor(
8791
view = createCustomWebView(view, context, attrs)
8892
}
8993

94+
// On Android P normally inflated dialog views crash when used in dialogs
95+
// opened from web views, we therefor replace them with their newer versions
96+
// from androidx
97+
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
98+
view = createDialogWidgetView(name, view, attrs)
99+
}
100+
90101
// The framework SearchView needs to be inflated manually,
91102
// as it is not inflated by the AppCompatViewInflater
92103
if (name == "SearchView") {
@@ -155,4 +166,27 @@ class ViewPumpAppCompatDelegate @JvmOverloads constructor(
155166

156167
private fun createWebViewContext(context: Context) =
157168
super.attachBaseContext2(WebViewContextWrapper(context))
169+
170+
@SuppressLint("RestrictedApi")
171+
private fun createDialogWidgetView(
172+
name: String,
173+
view: View?,
174+
attrs: AttributeSet
175+
): View? {
176+
177+
val wrappedContext = baseDelegate.attachBaseContext2(baseContext)
178+
179+
return when (name) {
180+
"com.android.internal.widget.AlertDialogLayout" ->
181+
AlertDialogLayout(wrappedContext, attrs)
182+
183+
"com.android.internal.widget.DialogTitle" ->
184+
DialogTitle(wrappedContext, attrs)
185+
186+
"com.android.internal.widget.ButtonBarLayout" ->
187+
ButtonBarLayout(wrappedContext, attrs)
188+
189+
else -> view
190+
}
191+
}
158192
}

0 commit comments

Comments
 (0)