Skip to content

Commit

Permalink
Merge pull request #52 from zeoflow/status-bar-to-utils
Browse files Browse the repository at this point in the history
Status Bar package changed
  • Loading branch information
teogor authored Apr 9, 2021
2 parents d046626 + 166ef08 commit a7dc711
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package com.zeoflow.app;
package com.zeoflow.utils;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
Expand All @@ -29,29 +29,28 @@
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.drawerlayout.widget.DrawerLayout;

import com.zeoflow.flow.kit.R;
import com.zeoflow.annotation.ColorInt;
import com.zeoflow.annotation.IntRange;
import com.zeoflow.annotation.NonNull;
import com.zeoflow.flow.kit.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@SuppressWarnings({"unused", "RedundantSuppression"})
public class StatusBarUtil
{

public static final int DEFAULT_STATUS_BAR_ALPHA = 112;
private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.zf_cr_statusbarutil_fake_status_bar_view;
private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.zf_cr_statusbarutil_translucent_view;
private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.zf_flow_kit_statusbarutil_fake_status_bar_view;
private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.zf_flow_kit_statusbarutil_translucent_view;
private static final int TAG_KEY_HAVE_SET_OFFSET = -123;


public static void setColor(Activity activity, @ColorInt int color)
{
setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA);
}


public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
Expand Down Expand Up @@ -79,13 +78,11 @@ public static void setColor(Activity activity, @ColorInt int color, @IntRange(fr
}
}


public static void setColorForSwipeBack(Activity activity, int color)
{
setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA);
}


public static void setColorForSwipeBack(Activity activity, @ColorInt int color,
@IntRange(from = 0, to = 255) int statusBarAlpha)
{
Expand All @@ -105,18 +102,16 @@ public static void setColorForSwipeBack(Activity activity, @ColorInt int color,
setTransparentForWindow(activity);
}


public static void setColorNoTranslucent(Activity activity, @ColorInt int color)
{
setColor(activity, color, 0);
}


@Deprecated
public static void setColorDiff(Activity activity, @ColorInt int color)
{
transparentStatusBar(activity);
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
ViewGroup contentView = activity.findViewById(android.R.id.content);

View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID);
if (fakeStatusBarView != null)
Expand All @@ -133,7 +128,6 @@ public static void setColorDiff(Activity activity, @ColorInt int color)
setRootView(activity);
}


public static void setTranslucent(Activity activity)
{
setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
Expand Down Expand Up @@ -188,7 +182,6 @@ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawe
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}


ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID);
if (fakeStatusBarView != null)
Expand All @@ -206,15 +199,14 @@ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawe
if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null)
{
contentLayout.getChildAt(1)
.setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
.setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
}

setDrawerLayoutProperty(drawerLayout, contentLayout);
addTranslucentView(activity, statusBarAlpha);
}


private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout)
{
ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
Expand All @@ -224,7 +216,6 @@ private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup
drawer.setFitsSystemWindows(false);
}


@Deprecated
public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color)
{
Expand Down Expand Up @@ -253,21 +244,18 @@ public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout d
setDrawerLayoutProperty(drawerLayout, contentLayout);
}


public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout)
{
setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA);
}


public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout,
@IntRange(from = 0, to = 255) int statusBarAlpha)
{
setTransparentForDrawerLayout(activity, drawerLayout);
addTranslucentView(activity, statusBarAlpha);
}


public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
Expand All @@ -287,11 +275,9 @@ public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout
contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0);
}


setDrawerLayoutProperty(drawerLayout, contentLayout);
}


@Deprecated
public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout)
{
Expand All @@ -308,19 +294,16 @@ public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLa
drawerLayout.setFitsSystemWindows(false);
}


public static void setTransparentForImageView(Activity activity, View needOffsetView)
{
setTranslucentForImageView(activity, 0, needOffsetView);
}


public static void setTranslucentForImageView(Activity activity, View needOffsetView)
{
setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
}


public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
View needOffsetView)
{
Expand All @@ -335,31 +318,27 @@ public static void setTranslucentForImageView(Activity activity, @IntRange(from
}
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams();
layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity),
layoutParams.rightMargin, layoutParams.bottomMargin);
layoutParams.rightMargin, layoutParams.bottomMargin);
needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true);
}
}


public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView)
{
setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView);
}


public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView)
{
setTranslucentForImageViewInFragment(activity, 0, needOffsetView);
}


public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
View needOffsetView)
{
setTranslucentForImageView(activity, statusBarAlpha, needOffsetView);
}


public static void hideFakeStatusBarView(Activity activity)
{
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
Expand Down Expand Up @@ -397,7 +376,6 @@ public static void setDarkMode(Activity activity)
}
}


private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon)
{
Class<? extends Window> clazz = activity.getWindow().getClass();
Expand All @@ -414,7 +392,7 @@ private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean
}
}


@SuppressWarnings("JavaReflectionMemberAccess")
private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon)
{
try
Expand All @@ -435,13 +413,12 @@ private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolea
}
meizuFlags.setInt(lp, value);
activity.getWindow().setAttributes(lp);
} catch (Exception e)
} catch (Exception ignored)
{

// ignored :)
}
}


@TargetApi(Build.VERSION_CODES.KITKAT)
private static void clearPreviousSetting(Activity activity)
{
Expand All @@ -455,10 +432,9 @@ private static void clearPreviousSetting(Activity activity)
}
}


private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha)
{
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
ViewGroup contentView = activity.findViewById(android.R.id.content);
View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID);
if (fakeTranslucentView != null)
{
Expand All @@ -473,29 +449,26 @@ private static void addTranslucentView(Activity activity, @IntRange(from = 0, to
}
}


private static View createStatusBarView(Activity activity, @ColorInt int color)
{
return createStatusBarView(activity, color, 0);
}


private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha)
{

View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(calculateStatusColor(color, alpha));
statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID);
return statusBarView;
}


private static void setRootView(Activity activity)
{
ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
ViewGroup parent = activity.findViewById(android.R.id.content);
for (int i = 0, count = parent.getChildCount(); i < count; i++)
{
View childView = parent.getChildAt(i);
Expand All @@ -507,23 +480,21 @@ private static void setRootView(Activity activity)
}
}


private static void setTransparentForWindow(Activity activity)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
activity.getWindow()
.getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
.getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
} else
{
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}


@TargetApi(Build.VERSION_CODES.KITKAT)
private static void transparentStatusBar(Activity activity)
{
Expand All @@ -539,28 +510,25 @@ private static void transparentStatusBar(Activity activity)
}
}


private static View createTranslucentStatusBarView(Activity activity, int alpha)
{

View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0));
statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID);
return statusBarView;
}


private static int getStatusBarHeight(Context context)
{

int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}


private static int calculateStatusColor(@ColorInt int color, int alpha)
{
if (alpha == 0)
Expand All @@ -576,4 +544,5 @@ private static int calculateStatusColor(@ColorInt int color, int alpha)
blue = (int) (blue * a + 0.5);
return 0xff << 24 | red << 16 | green << 8 | blue;
}

}
4 changes: 2 additions & 2 deletions flow-kit/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<resources>
<item name="zf_cr_statusbarutil_fake_status_bar_view" type="id" />
<item name="zf_cr_statusbarutil_translucent_view" type="id" />
<item name="zf_flow_kit_statusbarutil_fake_status_bar_view" type="id" />
<item name="zf_flow_kit_statusbarutil_translucent_view" type="id" />
</resources>

0 comments on commit a7dc711

Please sign in to comment.