// Path: lib/src/main/java/mortar/lib/util/MortarUtil.java
// public class MortarUtil {
// 
//   public static View createScreen(Context context, Blueprint screen) {
//     MortarScope scope = Mortar.getScope(context);
//     MortarScope newChildScope = scope.requireChild(screen);
// 
//     Context childContext = newChildScope.createContext(context);
//     return Layouts.createView(childContext, screen);
//   }
// 
//   public static int getLayoutId(Blueprint screen) {
//     Class<? extends Blueprint> screenType = screen.getClass();
//     Layout layoutAnnotation = screenType.getAnnotation(Layout.class);
//     if (layoutAnnotation == null) {
//       throw new IllegalArgumentException(
//           String.format("@%s annotation not found on class %s", Layout.class.getSimpleName(),
//               screenType.getName()));
//     }
// 
//     return layoutAnnotation.value();
//   }
// 
//   public static void destroyScreen(View view) {
//     destroyScreen(view.getContext().getApplicationContext(), view);
//   }
// 
//   public static void destroyScreen(Context parentContext, View view) {
//     MortarScope parentScope = Mortar.getScope(parentContext);
//     MortarScope oldChildScope = Mortar.getScope(view.getContext());
//     parentScope.destroyChild(oldChildScope);
//   }
// 
//   public static Activity getActivity(Context context) {
//     Context currentContext = context;
//     while (currentContext instanceof ContextWrapper) {
//       currentContext = ((ContextWrapper) currentContext).getBaseContext();
// 
//       if (currentContext instanceof Activity) {
//         return (Activity) currentContext;
//       } else if (currentContext instanceof Application) {
//         return null;
//       }
//     }
// 
//     return null;
//   }
// 
// }


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import flow.Flow;
import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;
import mortar.lib.util.MortarUtil;
package mortar.lib.screen;




/**
 * A conductor that can swap subviews within a container view.
 * <p/>
 *
 * @param <S> the type of the screens that serve as a {@link Blueprint} for subview. Must
 * be annotated with {@link flow.Layout}, suitable for use with {@link flow.Layouts#createView}.
 */
public abstract class AbstractScreenConductor<S extends Blueprint> implements CanShowScreen<S> {

  private final Context mContext;
  private final ViewGroup mContainer;

  /**
   * @param container the container used to host child views. Typically this is a {@link
   * android.widget.FrameLayout} under the action bar.
   */
  public AbstractScreenConductor(Context context, ViewGroup container) {
    this.mContext = context;
    this.mContainer = container;
  }

  public void showScreen(S screen, Flow.Direction direction) {
    MortarScope parentScope = Mortar.getScope(mContext);
    MortarScope newChildScope = parentScope.requireChild(screen);

    View oldChild = getChildView();

    if (oldChild != null) {
      MortarScope oldChildScope = Mortar.getScope(oldChild.getContext());
      if (oldChildScope.getName().equals(screen.getMortarScopeName())) {
        // If it's already showing, short circuit.
        return;
      }

      parentScope.destroyChild(oldChildScope);
    }

    // Create the new child.
    Context childContext = newChildScope.createContext(mContext);
    View newView = inflateScreen(childContext, screen, mContainer);
    animateAndAddScreen(childContext, direction, oldChild, newView, mContainer);
  }

  protected View inflateScreen(Context context, S screen, ViewGroup container) {
// Path: lib/src/main/java/mortar/lib/util/MortarUtil.java
// public class MortarUtil {
// 
//   public static View createScreen(Context context, Blueprint screen) {
//     MortarScope scope = Mortar.getScope(context);
//     MortarScope newChildScope = scope.requireChild(screen);
// 
//     Context childContext = newChildScope.createContext(context);
//     return Layouts.createView(childContext, screen);
//   }
// 
//   public static int getLayoutId(Blueprint screen) {
//     Class<? extends Blueprint> screenType = screen.getClass();
//     Layout layoutAnnotation = screenType.getAnnotation(Layout.class);
//     if (layoutAnnotation == null) {
//       throw new IllegalArgumentException(
//           String.format("@%s annotation not found on class %s", Layout.class.getSimpleName(),
//               screenType.getName()));
//     }
// 
//     return layoutAnnotation.value();
//   }
// 
//   public static void destroyScreen(View view) {
//     destroyScreen(view.getContext().getApplicationContext(), view);
//   }
// 
//   public static void destroyScreen(Context parentContext, View view) {
//     MortarScope parentScope = Mortar.getScope(parentContext);
//     MortarScope oldChildScope = Mortar.getScope(view.getContext());
//     parentScope.destroyChild(oldChildScope);
//   }
// 
//   public static Activity getActivity(Context context) {
//     Context currentContext = context;
//     while (currentContext instanceof ContextWrapper) {
//       currentContext = ((ContextWrapper) currentContext).getBaseContext();
// 
//       if (currentContext instanceof Activity) {
//         return (Activity) currentContext;
//       } else if (currentContext instanceof Application) {
//         return null;
//       }
//     }
// 
//     return null;
//   }
// 
// }


// Path: lib/src/main/java/mortar/lib/screen/AbstractScreenConductor.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import flow.Flow;
import mortar.Blueprint;
import mortar.Mortar;
import mortar.MortarScope;
import mortar.lib.util.MortarUtil;

package mortar.lib.screen;




/**
 * A conductor that can swap subviews within a container view.
 * <p/>
 *
 * @param <S> the type of the screens that serve as a {@link Blueprint} for subview. Must
 * be annotated with {@link flow.Layout}, suitable for use with {@link flow.Layouts#createView}.
 */
public abstract class AbstractScreenConductor<S extends Blueprint> implements CanShowScreen<S> {

  private final Context mContext;
  private final ViewGroup mContainer;

  /**
   * @param container the container used to host child views. Typically this is a {@link
   * android.widget.FrameLayout} under the action bar.
   */
  public AbstractScreenConductor(Context context, ViewGroup container) {
    this.mContext = context;
    this.mContainer = container;
  }

  public void showScreen(S screen, Flow.Direction direction) {
    MortarScope parentScope = Mortar.getScope(mContext);
    MortarScope newChildScope = parentScope.requireChild(screen);

    View oldChild = getChildView();

    if (oldChild != null) {
      MortarScope oldChildScope = Mortar.getScope(oldChild.getContext());
      if (oldChildScope.getName().equals(screen.getMortarScopeName())) {
        // If it's already showing, short circuit.
        return;
      }

      parentScope.destroyChild(oldChildScope);
    }

    // Create the new child.
    Context childContext = newChildScope.createContext(mContext);
    View newView = inflateScreen(childContext, screen, mContainer);
    animateAndAddScreen(childContext, direction, oldChild, newView, mContainer);
  }

  protected View inflateScreen(Context context, S screen, ViewGroup container) {
    int layoutId = MortarUtil.getLayoutId(screen);