/*
 * Copyright 2017-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.android.support.exopackage;

import android.app.Activity;
import android.app.Fragment;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import java.util.ArrayList;
import java.util.List;

/** Support and Utility Functions for Exopackage, including hotswap helper functions */
public class ExoHelper {
  private static final List<OnModulesChangedCallback> sCallbacks =
      new ArrayList<OnModulesChangedCallback>();

  /**
   * Restart the given activity. The same intent will be used to launch the new activity, preserving
   * actions, extras, etc. Must be run on the main thread.
   *
   * @param activity the activity to restart
   */
  public static void restartActivity(Activity activity) {
    assertIsUiThread();
    final Intent intent = activity.getIntent();
    activity.startActivity(intent);
    activity.finish();
  }

  /**
   * Create an Intent to launch the component with the given classname. This is a simple wrapper
   * around Intent#setComponent with the given params.
   *
   * @param context the current context
   * @param className the name of the class targeted by the Intent
   * @return an Intent suitable for launching the component with the given className
   */
  public static Intent intentForClassName(Context context, String className) {
    final Intent intent = new Intent();
    intent.setComponent(new ComponentName(context, className));
    return intent;
  }

  /**
   * Create a new Fragment instance. This should be preferred to Fragment#instantiate() due to
   * caching of the lookup class.
   *
   * @param className the name of the fragment subclass to instantiate
   * @param args optional args to pass to the fragment, or null if no args
   * @return a new instance of the given Fragment class
   * @throws RuntimeException if the given className cannot be found or instantiated as a fragment
   */
  public static Fragment createFragment(String className, Bundle args) {
    try {
      final Fragment instance = (Fragment) Class.forName(className).newInstance();
      instance.setArguments(args);
      return instance;
    } catch (IllegalAccessException | InstantiationException e) {
      throw new RuntimeException("Could not instantiate " + className, e);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException(className + " could not be found", e);
    } catch (ClassCastException e) {
      throw new RuntimeException("Fragment is not assignable from " + className, e);
    }
  }

  /**
   * Add a callback which will fire whenever code definitions have changed in the app. This callback
   * will be executed on the main thread.
   *
   * @param callback the callback to add
   */
  public static void addOnModulesChangedCallback(OnModulesChangedCallback callback) {
    synchronized (sCallbacks) {
      sCallbacks.add(callback);
    }
  }

  /**
   * Remove a previously added callback
   *
   * @param callback the callback to remove
   */
  public static void removeOnModulesChangedCallback(OnModulesChangedCallback callback) {
    synchronized (sCallbacks) {
      sCallbacks.remove(callback);
    }
  }

  /**
   * Trigger all callbacks which have registered to receive notifications when the module
   * definitions have changed on disk
   */
  static void triggerCallbacks() {
    synchronized (sCallbacks) {
      for (OnModulesChangedCallback mCallback : sCallbacks) {
        mCallback.onModulesChanged();
      }
    }
  }

  private static void assertIsUiThread() {
    if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
      throw new RuntimeException("This method must be called from the UI thread");
    }
  }
}

@@ -709,24 +709,140 @@
 p.Fragment;%0A
+import android.app.Fragment.SavedState;%0Aimport android.app.FragmentManager;%0Aimport android.app.FragmentTransaction;%0A
 import andro
@@ -978,24 +978,50 @@
 .os.Looper;%0A
+import android.view.View;%0A
 import java.
@@ -3324,32 +3324,1707 @@
  e);%0A    %7D%0A  %7D%0A%0A
+  /**%0A   * Refresh the given fragment in place, preserving construction args, and any saved state. The new%0A   * fragment will be constructed via a fresh class lookup. This method must be called from the UI%0A   * thread%0A   *%0A   * @param fragment the fragment to refresh%0A   */%0A  public static void refreshFragment(Fragment fragment) %7B%0A    refreshFragment(fragment, true);%0A  %7D%0A%0A  /**%0A   * Try refreshing the given Fragment in place, preserving construction args. The new Fragment will%0A   * be constructed via a fresh class lookup. This method must be called from the UI thread%0A   *%0A   * @param fragment the fragment to refresh%0A   * @param preserveState if true, attempt to save the Fragment's current state and restore it to%0A   *     the new instance of the fragment%0A   */%0A  public static void refreshFragment(Fragment fragment, boolean preserveState) %7B%0A    assertIsUiThread();%0A    FragmentManager manager = fragment.getFragmentManager();%0A    final Bundle args = fragment.getArguments();%0A    Fragment replacement = createFragment(fragment.getClass().getName(), args);%0A%0A    String tag = fragment.getTag();%0A%0A    if (preserveState) %7B%0A      // Restore any state that's possible%0A      final SavedState savedState = manager.saveFragmentInstanceState(fragment);%0A      replacement.setInitialSavedState(savedState);%0A    %7D%0A%0A    int containerViewId = ((View) fragment.getView().getParent()).getId();%0A    final FragmentTransaction transaction = manager.beginTransaction();%0A    transaction.remove(fragment);%0A    if (tag != null) %7B%0A      transaction.add(containerViewId, replacement, tag);%0A    %7D else %7B%0A      transaction.add(containerViewId, replacement);%0A    %7D%0A    transaction.commit();%0A  %7D%0A%0A
   /**%0A   * Add a

Add a helper function to refresh a given Fragment in place