33import android .annotation .SuppressLint ;
44import android .app .Activity ;
55import android .content .Context ;
6+ import android .content .ContextWrapper ;
67import android .content .res .Resources ;
78import android .graphics .Color ;
89import android .graphics .Point ;
@@ -221,8 +222,10 @@ public static View setStatusBarColor(@NonNull final Activity activity,
221222 public static void setStatusBarColor (@ NonNull final View fakeStatusBar ,
222223 @ ColorInt final int color ) {
223224 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .KITKAT ) return ;
225+ Activity activity = getActivityByView (fakeStatusBar );
226+ if (activity == null ) return ;
227+ transparentStatusBar (activity );
224228 fakeStatusBar .setVisibility (View .VISIBLE );
225- transparentStatusBar ((Activity ) fakeStatusBar .getContext ());
226229 ViewGroup .LayoutParams layoutParams = fakeStatusBar .getLayoutParams ();
227230 layoutParams .width = ViewGroup .LayoutParams .MATCH_PARENT ;
228231 layoutParams .height = getStatusBarHeight ();
@@ -236,23 +239,20 @@ public static void setStatusBarColor(@NonNull final View fakeStatusBar,
236239 */
237240 public static void setStatusBarCustom (@ NonNull final View fakeStatusBar ) {
238241 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .KITKAT ) return ;
239- Context context = fakeStatusBar .getContext ();
240- if (context instanceof Activity ) {
241- fakeStatusBar .setVisibility (View .VISIBLE );
242- transparentStatusBar ((Activity ) context );
243- ViewGroup .LayoutParams layoutParams = fakeStatusBar .getLayoutParams ();
244- if (layoutParams == null ) {
245- layoutParams = new ViewGroup .LayoutParams (
246- ViewGroup .LayoutParams .MATCH_PARENT ,
247- getStatusBarHeight ()
248- );
249- fakeStatusBar .setLayoutParams (layoutParams );
250- } else {
251- layoutParams .width = ViewGroup .LayoutParams .MATCH_PARENT ;
252- layoutParams .height = getStatusBarHeight ();
253- }
242+ Activity activity = getActivityByView (fakeStatusBar );
243+ if (activity == null ) return ;
244+ transparentStatusBar (activity );
245+ fakeStatusBar .setVisibility (View .VISIBLE );
246+ ViewGroup .LayoutParams layoutParams = fakeStatusBar .getLayoutParams ();
247+ if (layoutParams == null ) {
248+ layoutParams = new ViewGroup .LayoutParams (
249+ ViewGroup .LayoutParams .MATCH_PARENT ,
250+ getStatusBarHeight ()
251+ );
252+ fakeStatusBar .setLayoutParams (layoutParams );
254253 } else {
255- Log .e ("BarUtils" , "fakeStatusBar's context is not instance of Activity." );
254+ layoutParams .width = ViewGroup .LayoutParams .MATCH_PARENT ;
255+ layoutParams .height = getStatusBarHeight ();
256256 }
257257 }
258258
@@ -284,20 +284,18 @@ public static void setStatusBarColor4Drawer(@NonNull final DrawerLayout drawer,
284284 @ ColorInt final int color ,
285285 final boolean isTop ) {
286286 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .KITKAT ) return ;
287+ Activity activity = getActivityByView (fakeStatusBar );
288+ if (activity == null ) return ;
289+ transparentStatusBar (activity );
287290 drawer .setFitsSystemWindows (false );
288- Context context = fakeStatusBar .getContext ();
289- if (context instanceof Activity ) {
290- final Activity activity = (Activity ) context ;
291- transparentStatusBar (activity );
292- setStatusBarColor (fakeStatusBar , color );
293- for (int i = 0 , count = drawer .getChildCount (); i < count ; i ++) {
294- drawer .getChildAt (i ).setFitsSystemWindows (false );
295- }
296- if (isTop ) {
297- hideStatusBarView (activity );
298- } else {
299- setStatusBarColor (activity , color , false );
300- }
291+ setStatusBarColor (fakeStatusBar , color );
292+ for (int i = 0 , count = drawer .getChildCount (); i < count ; i ++) {
293+ drawer .getChildAt (i ).setFitsSystemWindows (false );
294+ }
295+ if (isTop ) {
296+ hideStatusBarView (activity );
297+ } else {
298+ setStatusBarColor (activity , color , false );
301299 }
302300 }
303301
@@ -580,4 +578,16 @@ public static boolean isSupportNavBar() {
580578 boolean back = KeyCharacterMap .deviceHasKey (KeyEvent .KEYCODE_BACK );
581579 return !menu && !back ;
582580 }
581+
582+ private static Activity getActivityByView (@ NonNull final View view ) {
583+ Context context = view .getContext ();
584+ while (context instanceof ContextWrapper ) {
585+ if (context instanceof Activity ) {
586+ return (Activity ) context ;
587+ }
588+ context = ((ContextWrapper ) context ).getBaseContext ();
589+ }
590+ Log .e ("BarUtils" , "the view's Context is not an Activity." );
591+ return null ;
592+ }
583593}
0 commit comments