From d725a94ccb65f76d130639bf74d8a2d44518055d Mon Sep 17 00:00:00 2001 From: lanxiaobin Date: Wed, 27 Jan 2021 00:53:59 +0800 Subject: [PATCH] fixed:this commit can help BlurLayout support using in Fragment because when in Fragment,the code ``` try { activity = (Activity) getContext(); } catch (ClassCastException e) { return null; } ``` will cause ClassCastException and return null --- .../java/io/alterac/blurkit/BlurLayout.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java index 7d03c9e..98031c5 100644 --- a/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java +++ b/blurkit/src/main/java/io/alterac/blurkit/BlurLayout.java @@ -299,16 +299,29 @@ private Bitmap blur() { * @return View reference for whole activity. */ private View getActivityView() { - Activity activity; - try { - activity = (Activity) getContext(); - } catch (ClassCastException e) { - return null; + Activity activity = getActivity(getContext()); + if (activity == null) { + return null } - return activity.getWindow().getDecorView().findViewById(android.R.id.content); } + /** + * Casts context to Activity + * @param context + * @return + */ + private Activity getActivity(Context context) { + if (context == null) { + return null; + } else if (context instanceof Activity) { + return (Activity) context; + } else if (context instanceof ContextWrapper) { + return getActivity(((ContextWrapper) context).getBaseContext()); + } + return null; + } + /** * Returns the position in screen. Left abstract to allow for specific implementations such as * caching behavior.