content: package com.sixbugs.bujuan.utils;
  
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.content.Context;
  import android.content.res.Resources;
  import android.graphics.Color;
  import android.os.Build;
  import android.view.Gravity;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.LinearLayout;
  
  import androidx.annotation.NonNull;
  import androidx.fragment.app.Fragment;
  import androidx.fragment.app.FragmentManager;
  import androidx.fragment.app.FragmentTransaction;
  
  /**
   * @author lzx
   * @date 2018/3/26
   */
  
  public class ActivityUtils  {
  
      public static boolean activityIsAlive(Context context) {
          if (context == null) {
              return false;
          }
          if (context instanceof Activity) {
              Activity activity = (Activity) context;
              return activityIsAlive(activity);
          } else {
              return true;
          }
      }
  
      public static boolean activityIsAlive(Activity activity) {
          if (activity == null) {
              return false;
          }
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return !(activity.isDestroyed() || activity.isFinishing());
          } else {
              return !activity.isFinishing();
          }
      }
  
      public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                               @NonNull Fragment fragment, int frameId) {
          checkNotNull(fragmentManager);
          checkNotNull(fragment);
          FragmentTransaction transaction = fragmentManager.beginTransaction();
          transaction.add(frameId, fragment);
          transaction.commit();
      }
  
      public static void makeStatusBarTransparent(Activity activity) {
          if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
              return;
          }
          Window window = activity.getWindow();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
              window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
              int option = window.getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
              window.getDecorView().setSystemUiVisibility(option);
              window.setStatusBarColor(Color.TRANSPARENT);
          } else {
              window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
          }
      }
  
      public static <T> T checkNotNull(T reference) {
          if (reference == null) {
              throw new NullPointerException();
          } else {
              return reference;
          }
      }
  
      @TargetApi(19)
      public static void setStatusBarColor(Activity activity, int color) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
              activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
  
              ViewGroup decorViewGroup = (ViewGroup)activity.getWindow().getDecorView();
              //获取自己布局的根视图
              View rootView = ((ViewGroup) (decorViewGroup.findViewById(android.R.id.content))).getChildAt(0);
              //预留状态栏位置
              rootView.setFitsSystemWindows(true);
  
              //添加状态栏高度的视图布局，并填充颜色
              View statusBarTintView = new View(activity);
              LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                      ActivityUtils.getInternalDimensionSize(activity.getResources(), "status_bar_height"));
              params.gravity = Gravity.TOP;
              statusBarTintView.setLayoutParams(params);
              statusBarTintView.setBackgroundColor(color);
              decorViewGroup.addView(statusBarTintView);
          }
      }
  
      public static int getInternalDimensionSize(Resources res, String key) {
          int result = 0;
          int resourceId = res.getIdentifier(key, "dimen", "android");
          if (resourceId > 0) {
              result = res.getDimensionPixelSize(resourceId);
          }
          return result;
      }
  
  }
  