From fe1986d3646d8a51887a09e2da76136e63c4135b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?U=C4=9Furcan=20Durak?= Date: Sun, 4 Apr 2021 02:15:28 +0300 Subject: [PATCH] Fix: fix blur problem when using Hilt annotation @AndroidEntryPoint in Fragment --- .../java/io/alterac/blurkit/BlurLayout.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java index 7d03c9e..aabf802 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java @@ -2,6 +2,7 @@ import android.app.Activity; import android.content.Context; +import android.content.ContextWrapper; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -301,7 +302,7 @@ private Bitmap blur() { private View getActivityView() { Activity activity; try { - activity = (Activity) getContext(); + activity = getActivity(); } catch (ClassCastException e) { return null; } @@ -309,6 +310,20 @@ private View getActivityView() { return activity.getWindow().getDecorView().findViewById(android.R.id.content); } + /** + * Casts context to Activity if context is a View or not. + */ + private Activity getActivity() { + Context context = this.getContext(); + while (context instanceof ContextWrapper) { + if (context instanceof Activity) { + return (Activity) context; + } + context = ((ContextWrapper) context).getBaseContext(); + } + return null; + } + /** * Returns the position in screen. Left abstract to allow for specific implementations such as * caching behavior.