content: package androidx.core.app;
  
  import android.app.Activity;
  import android.app.Application;
  import android.content.res.Configuration;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.Looper;
  import android.util.Log;
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  import java.util.List;
  
  final class ActivityRecreator {
      private static final String LOG_TAG = "ActivityRecreator";
      protected static final Class<?> activityThreadClass;
      private static final Handler mainHandler = new Handler(Looper.getMainLooper());
      protected static final Field mainThreadField = getMainThreadField();
      protected static final Method performStopActivity2ParamsMethod;
      protected static final Method performStopActivity3ParamsMethod;
      protected static final Method requestRelaunchActivityMethod;
      protected static final Field tokenField = getTokenField();
  
      private static final class LifecycleCheckCallbacks implements Application.ActivityLifecycleCallbacks {
          Object currentlyRecreatingToken;
          private Activity mActivity;
          private boolean mDestroyed = false;
          private final int mRecreatingHashCode;
          private boolean mStarted = false;
          private boolean mStopQueued = false;
  
          LifecycleCheckCallbacks(Activity aboutToRecreate) {
              this.mActivity = aboutToRecreate;
              this.mRecreatingHashCode = aboutToRecreate.hashCode();
          }
  
          public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
          }
  
          public void onActivityDestroyed(Activity activity) {
              if (this.mActivity == activity) {
                  this.mActivity = null;
                  this.mDestroyed = true;
              }
          }
  
          public void onActivityPaused(Activity activity) {
              if (this.mDestroyed && !this.mStopQueued && !this.mStarted && ActivityRecreator.queueOnStopIfNecessary(this.currentlyRecreatingToken, this.mRecreatingHashCode, activity)) {
                  this.mStopQueued = true;
                  this.currentlyRecreatingToken = null;
              }
          }
  
          public void onActivityResumed(Activity activity) {
          }
  
          public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
          }
  
          public void onActivityStarted(Activity activity) {
              if (this.mActivity == activity) {
                  this.mStarted = true;
              }
          }
  
          public void onActivityStopped(Activity activity) {
          }
      }
  
      static {
          Class<?> activityThreadClass2 = getActivityThreadClass();
          activityThreadClass = activityThreadClass2;
          performStopActivity3ParamsMethod = getPerformStopActivity3Params(activityThreadClass2);
          performStopActivity2ParamsMethod = getPerformStopActivity2Params(activityThreadClass2);
          requestRelaunchActivityMethod = getRequestRelaunchActivityMethod(activityThreadClass2);
      }
  
      private ActivityRecreator() {
      }
  
      private static Class<?> getActivityThreadClass() {
          try {
              return Class.forName("android.app.ActivityThread");
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static Field getMainThreadField() {
          try {
              Field declaredField = Activity.class.getDeclaredField("mMainThread");
              declaredField.setAccessible(true);
              return declaredField;
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static Method getPerformStopActivity2Params(Class<?> cls) {
          if (cls == null) {
              return null;
          }
          try {
              Method declaredMethod = cls.getDeclaredMethod("performStopActivity", new Class[]{IBinder.class, Boolean.TYPE});
              declaredMethod.setAccessible(true);
              return declaredMethod;
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static Method getPerformStopActivity3Params(Class<?> cls) {
          if (cls == null) {
              return null;
          }
          try {
              Method declaredMethod = cls.getDeclaredMethod("performStopActivity", new Class[]{IBinder.class, Boolean.TYPE, String.class});
              declaredMethod.setAccessible(true);
              return declaredMethod;
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static Method getRequestRelaunchActivityMethod(Class<?> cls) {
          if (!needsRelaunchCall() || cls == null) {
              return null;
          }
          try {
              Method declaredMethod = cls.getDeclaredMethod("requestRelaunchActivity", new Class[]{IBinder.class, List.class, List.class, Integer.TYPE, Boolean.TYPE, Configuration.class, Configuration.class, Boolean.TYPE, Boolean.TYPE});
              declaredMethod.setAccessible(true);
              return declaredMethod;
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static Field getTokenField() {
          try {
              Field declaredField = Activity.class.getDeclaredField("mToken");
              declaredField.setAccessible(true);
              return declaredField;
          } catch (Throwable th) {
              return null;
          }
      }
  
      private static boolean needsRelaunchCall() {
          return Build.VERSION.SDK_INT == 26 || Build.VERSION.SDK_INT == 27;
      }
  
      protected static boolean queueOnStopIfNecessary(Object currentlyRecreatingToken, int currentlyRecreatingHashCode, Activity activity) {
          try {
              final Object obj = tokenField.get(activity);
              if (obj == currentlyRecreatingToken) {
                  if (activity.hashCode() == currentlyRecreatingHashCode) {
                      final Object obj2 = mainThreadField.get(activity);
                      mainHandler.postAtFrontOfQueue(new Runnable() {
                          public void run() {
                              try {
                                  if (ActivityRecreator.performStopActivity3ParamsMethod != null) {
                                      ActivityRecreator.performStopActivity3ParamsMethod.invoke(obj2, new Object[]{obj, false, "AppCompat recreation"});
                                      return;
                                  }
                                  ActivityRecreator.performStopActivity2ParamsMethod.invoke(obj2, new Object[]{obj, false});
                              } catch (RuntimeException e) {
                                  if (e.getClass() == RuntimeException.class && e.getMessage() != null && e.getMessage().startsWith("Unable to stop")) {
                                      throw e;
                                  }
                              } catch (Throwable th) {
                                  Log.e(ActivityRecreator.LOG_TAG, "Exception while invoking performStopActivity", th);
                              }
                          }
                      });
                      return true;
                  }
              }
              return false;
          } catch (Throwable th) {
              Log.e(LOG_TAG, "Exception while fetching field values", th);
              return false;
          }
      }
  
      static boolean recreate(Activity activity) {
          Object obj;
          final Application application;
          final LifecycleCheckCallbacks lifecycleCheckCallbacks;
          if (Build.VERSION.SDK_INT >= 28) {
              activity.recreate();
              return true;
          } else if (needsRelaunchCall() && requestRelaunchActivityMethod == null) {
              return false;
          } else {
              if (performStopActivity2ParamsMethod == null && performStopActivity3ParamsMethod == null) {
                  return false;
              }
              try {
                  final Object obj2 = tokenField.get(activity);
                  if (obj2 == null || (obj = mainThreadField.get(activity)) == null) {
                      return false;
                  }
                  application = activity.getApplication();
                  lifecycleCheckCallbacks = new LifecycleCheckCallbacks(activity);
                  application.registerActivityLifecycleCallbacks(lifecycleCheckCallbacks);
                  Handler handler = mainHandler;
                  handler.post(new Runnable() {
                      public void run() {
                          LifecycleCheckCallbacks.this.currentlyRecreatingToken = obj2;
                      }
                  });
                  if (needsRelaunchCall()) {
                      requestRelaunchActivityMethod.invoke(obj, new Object[]{obj2, null, null, 0, false, null, null, false, false});
                  } else {
                      activity.recreate();
                  }
                  handler.post(new Runnable() {
                      public void run() {
                          application.unregisterActivityLifecycleCallbacks(lifecycleCheckCallbacks);
                      }
                  });
                  return true;
              } catch (Throwable th) {
                  return false;
              }
          }
      }
  }
  