content: package top.niunaijun.blackbox.utils.compat;
  
  import android.content.Context;
  import android.content.ContextWrapper;
  import android.util.*;
  
  import black.android.app.BRContextImpl;
  import black.android.app.BRContextImplKitkat;
  import black.android.content.AttributionSourceStateContext;
  import black.android.content.BRAttributionSource;
  import black.android.content.BRAttributionSourceState;
  import black.android.content.BRContentResolver;
  import top.niunaijun.blackbox.BlackBoxCore;
  import top.niunaijun.blackbox.app.BActivityThread;
  import top.niunaijun.blackbox.utils.Slog;
  
  /**
   * updated by alex5402 on 3/31/21.
   * * ∧＿∧
   * (`･ω･∥
   * 丶　つ０
   * しーＪ
   * TFNQw5HgWUS33Ke1eNmSFTwoQySGU7XNsK (USDT TRC20)
   */
  public class ContextCompat {
      public static final String TAG = "ContextCompat";
  
      public static void fixAttributionSourceState(Object obj, int uid) {
          Object mAttributionSourceState;
          if (obj != null && BRAttributionSource.get(obj)._check_mAttributionSourceState() != null) {
              mAttributionSourceState = BRAttributionSource.get(obj).mAttributionSourceState();
  
              AttributionSourceStateContext attributionSourceStateContext = BRAttributionSourceState.get(mAttributionSourceState);
              attributionSourceStateContext._set_packageName(BlackBoxCore.getHostPkg());
              attributionSourceStateContext._set_uid(uid);
              fixAttributionSourceState(BRAttributionSource.get(obj).getNext(), uid);
          }
      }
  
      public static void fix(Context context) {
          try {
              // Check if context is null
              if (context == null) {
                  Slog.w(TAG, "Context is null, skipping ContextCompat.fix");
                  return;
              }
              
              int deep = 0;
              while (context instanceof ContextWrapper) {
                  context = ((ContextWrapper) context).getBaseContext();
                  deep++;
                  if (deep >= 10) {
                      return;
                  }
              }
              
              // Check if context is still null after unwrapping
              if (context == null) {
                  Slog.w(TAG, "Base context is null after unwrapping, skipping ContextCompat.fix");
                  return;
              }
              
              BRContextImpl.get(context)._set_mPackageManager(null);
              try {
                  context.getPackageManager();
              } catch (Throwable e) {
                  e.printStackTrace();
              }
  
              BRContextImpl.get(context)._set_mBasePackageName(BlackBoxCore.getHostPkg());
              BRContextImplKitkat.get(context)._set_mOpPackageName(BlackBoxCore.getHostPkg());
              
              try {
                  BRContentResolver.get(context.getContentResolver())._set_mPackageName(BlackBoxCore.getHostPkg());
              } catch (Exception e) {
                  Slog.w(TAG, "Failed to fix content resolver: " + e.getMessage());
              }
  
              if (BuildCompat.isS()) {
                  try {
                      fixAttributionSourceState(BRContextImpl.get(context).getAttributionSource(), BlackBoxCore.getBUid());
                  } catch (Exception e) {
                      Slog.w(TAG, "Failed to fix attribution source state: " + e.getMessage());
                  }
              }
          } catch (Exception e) {
              Slog.e(TAG, "Error in ContextCompat.fix: " + e.getMessage());
              e.printStackTrace();
          }
      }
  }
  