New fragment stuff: back stack, and layout integration.

You can now have fragment transactions pushed on to a local back
stack, which will automatically be popped when the user pressed
back in the activity.

Fragments can also now be inserted into layouts.

Change-Id: Id1c9ae3fbc54f696cd8bb5ca5957bec4d3eabf18
diff --git a/api/current.xml b/api/current.xml
index 396e205..347fc01 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -20484,6 +20484,17 @@
 <parameter name="exitAnim" type="int">
 </parameter>
 </method>
+<method name="popBackStack"
+ return="boolean"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="registerForContextMenu"
  return="void"
  abstract="false"
@@ -24938,6 +24949,21 @@
  visibility="public"
 >
 </method>
+<method name="onInflate"
+ return="void"
+ abstract="false"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+<parameter name="activity" type="android.app.Activity">
+</parameter>
+<parameter name="attrs" type="android.util.AttributeSet">
+</parameter>
+</method>
 <method name="onLowMemory"
  return="void"
  abstract="false"
@@ -24960,17 +24986,6 @@
  visibility="public"
 >
 </method>
-<method name="onRestart"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
 <method name="onRestoreInstanceState"
  return="void"
  abstract="false"
@@ -25081,6 +25096,17 @@
 <parameter name="containerViewId" type="int">
 </parameter>
 </method>
+<method name="addToBackStack"
+ return="android.app.FragmentTransaction"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="commit"
  return="void"
  abstract="true"
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 15bf242..6ffc451 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -29,6 +29,7 @@
 import android.content.pm.ActivityInfo;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -39,7 +40,6 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
-import android.os.Looper;
 import android.os.RemoteException;
 import android.text.Selection;
 import android.text.SpannableStringBuilder;
@@ -52,6 +52,7 @@
 import android.util.SparseArray;
 import android.view.ContextMenu;
 import android.view.ContextThemeWrapper;
+import android.view.InflateException;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -69,6 +70,7 @@
 import android.view.accessibility.AccessibilityEvent;
 import android.widget.AdapterView;
 
+import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.HashMap;
 
@@ -652,9 +654,19 @@
 
     final FragmentManager mFragments = new FragmentManager();
     
-    private final class FragmentTransactionImpl implements FragmentTransaction {
+    private final Object[] sConstructorArgs = new Object[0];
+
+    private static final Class[] sConstructorSignature = new Class[] { };
+
+    private static final HashMap<String, Constructor> sConstructorMap =
+            new HashMap<String, Constructor>();
+    
+    private final class FragmentTransactionImpl implements FragmentTransaction,
+            Runnable, BackStackState {
         ArrayList<Fragment> mAdded;
         ArrayList<Fragment> mRemoved;
+        boolean mAddToBackStack;
+        boolean mCommitted;
         
         public FragmentTransaction add(Fragment fragment, int containerViewId) {
             return add(fragment, null, containerViewId);
@@ -692,7 +704,18 @@
             return this;
         }
 
+        public FragmentTransaction addToBackStack() {
+            mAddToBackStack = true;
+            return this;
+        }
+
         public void commit() {
+            if (mCommitted) throw new IllegalStateException("commit already called");
+            mCommitted = true;
+            mHandler.post(this);
+        }
+        
+        public void run() {
             if (mRemoved != null) {
                 for (int i=mRemoved.size()-1; i>=0; i--) {
                     mFragments.removeFragment(mRemoved.get(i));
@@ -700,13 +723,28 @@
             }
             if (mAdded != null) {
                 for (int i=mAdded.size()-1; i>=0; i--) {
-                    mFragments.addFragment(mAdded.get(i));
+                    mFragments.addFragment(mAdded.get(i), false);
                 }
             }
-            if (mFragments != null) {
-                mFragments.moveToState(mFragments.mCurState);
+            mFragments.moveToState(mFragments.mCurState, true);
+            if (mAddToBackStack) {
+                mFragments.addBackStackState(this);
             }
         }
+        
+        public void popFromBackStack() {
+            if (mAdded != null) {
+                for (int i=mAdded.size()-1; i>=0; i--) {
+                    mFragments.removeFragment(mAdded.get(i));
+                }
+            }
+            if (mRemoved != null) {
+                for (int i=mRemoved.size()-1; i>=0; i--) {
+                    mFragments.addFragment(mRemoved.get(i), false);
+                }
+            }
+            mFragments.moveToState(mFragments.mCurState, true);
+        }
     }
     
     private static final class ManagedCursor {
@@ -859,6 +897,7 @@
     protected void onCreate(Bundle savedInstanceState) {
         mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
                 com.android.internal.R.styleable.Window_windowNoDisplay, false);
+        mFragments.dispatchCreate(savedInstanceState);
         mCalled = true;
     }
 
@@ -1969,12 +2008,22 @@
     }
     
     /**
+     * Pop the last fragment transition from the local activity's fragment
+     * back stack.  If there is nothing to pop, false is returned.
+     */
+    public boolean popBackStack() {
+        return mFragments.popBackStackState(mHandler);
+    }
+    
+    /**
      * Called when the activity has detected the user's press of the back
      * key.  The default implementation simply finishes the current activity,
      * but you can override this to do whatever you want.
      */
     public void onBackPressed() {
-        finish();
+        if (!popBackStack()) {
+            finish();
+        }
     }
     
     /**
@@ -3776,15 +3825,62 @@
     }
 
     /**
-     * Stub implementation of {@link android.view.LayoutInflater.Factory#onCreateView} used when
-     * inflating with the LayoutInflater returned by {@link #getSystemService}.  This
-     * implementation simply returns null for all view names.
+     * Standard implementation of
+     * {@link android.view.LayoutInflater.Factory#onCreateView} used when
+     * inflating with the LayoutInflater returned by {@link #getSystemService}.
+     * This implementation handles <fragment> tags to embed fragments inside
+     * of the activity.
      *
      * @see android.view.LayoutInflater#createView
      * @see android.view.Window#getLayoutInflater
      */
     public View onCreateView(String name, Context context, AttributeSet attrs) {
-        return null;
+        if (!"fragment".equals(name)) {
+            return null;
+        }
+        
+        TypedArray a = 
+            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
+        String fname = a.getString(com.android.internal.R.styleable.Fragment_name);
+        int id = a.getInt(com.android.internal.R.styleable.Fragment_id, 0);
+        String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);
+        a.recycle();
+        
+        Constructor constructor = sConstructorMap.get(fname);
+        Class clazz = null;
+
+        try {
+            if (constructor == null) {
+                // Class not found in the cache, see if it's real, and try to add it
+                clazz = getClassLoader().loadClass(fname);
+                constructor = clazz.getConstructor(sConstructorSignature);
+                sConstructorMap.put(fname, constructor);
+            }
+            Fragment fragment = (Fragment)constructor.newInstance(sConstructorArgs);
+            fragment.onInflate(this, attrs);
+            mFragments.addFragment(fragment, true);
+            if (fragment.mView == null) {
+                throw new IllegalStateException("Fragment " + fname
+                        + " did not create a view.");
+            }
+            return fragment.mView;
+
+        } catch (NoSuchMethodException e) {
+            InflateException ie = new InflateException(attrs.getPositionDescription()
+                    + ": Error inflating class " + fname);
+            ie.initCause(e);
+            throw ie;
+
+        } catch (ClassNotFoundException e) {
+            // If loadClass fails, we should propagate the exception.
+            throw new RuntimeException(e);
+        } catch (Exception e) {
+            InflateException ie = new InflateException(attrs.getPositionDescription()
+                    + ": Error inflating class "
+                    + (clazz == null ? "<unknown>" : clazz.getName()));
+            ie.initCause(e);
+            throw new RuntimeException(ie);
+        }
     }
 
     // ------------------ Internal API ------------------
@@ -3814,6 +3910,7 @@
         
         mWindow = PolicyManager.makeNewWindow(this);
         mWindow.setCallback(this);
+        mWindow.getLayoutInflater().setFactory(this);
         if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
             mWindow.setSoftInputMode(info.softInputMode);
         }
@@ -3847,7 +3944,6 @@
 
     final void performCreate(Bundle icicle) {
         onCreate(icicle);
-        mFragments.dispatchCreate(icicle);
     }
     
     final void performStart() {
diff --git a/core/java/android/app/Fragment.java b/core/java/android/app/Fragment.java
index c0dc869..a9ef654 100644
--- a/core/java/android/app/Fragment.java
+++ b/core/java/android/app/Fragment.java
@@ -17,8 +17,10 @@
 package android.app;
 
 import android.content.ComponentCallbacks;
+import android.content.Context;
 import android.content.res.Configuration;
 import android.os.Bundle;
+import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -59,6 +61,10 @@
         return mActivity;
     }
     
+    public void onInflate(Activity activity, AttributeSet attrs) {
+        mCalled = true;
+    }
+    
     public void onAttach(Activity activity) {
         mCalled = true;
     }
@@ -78,10 +84,6 @@
         mCalled = true;
     }
     
-    public void onRestart() {
-        mCalled = true;
-    }
-    
     public void onResume() {
         mCalled = true;
     }
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java
index d5e49cf..aa99a92 100644
--- a/core/java/android/app/FragmentManager.java
+++ b/core/java/android/app/FragmentManager.java
@@ -17,15 +17,21 @@
 package android.app;
 
 import android.os.Bundle;
+import android.os.Handler;
 import android.view.ViewGroup;
 
 import java.util.ArrayList;
 
+interface BackStackState {
+    public void popFromBackStack();
+}
+
 /**
  * Container for fragments associated with an activity.
  */
 class FragmentManager {
-    final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
+    ArrayList<Fragment> mFragments;
+    ArrayList<BackStackState> mBackStack;
     
     int mCurState = Fragment.INITIALIZING;
     Activity mActivity;
@@ -130,20 +136,32 @@
         f.mState = newState;
     }
     
-    void moveToState(int newState) {
+    void moveToState(int newState, boolean always) {
         if (mActivity == null && newState != Fragment.INITIALIZING) {
             throw new IllegalStateException("No activity");
         }
         
+        if (!always && mCurState == newState) {
+            return;
+        }
+        
         mCurState = newState;
-        for (int i=0; i<mFragments.size(); i++) {
-            Fragment f = mFragments.get(i);
-            moveToState(f, newState);
+        if (mFragments != null) {
+            for (int i=0; i<mFragments.size(); i++) {
+                Fragment f = mFragments.get(i);
+                moveToState(f, newState);
+            }
         }
     }
     
-    public void addFragment(Fragment fragment) {
+    public void addFragment(Fragment fragment, boolean moveToStateNow) {
+        if (mFragments == null) {
+            mFragments = new ArrayList<Fragment>();
+        }
         mFragments.add(fragment);
+        if (moveToStateNow) {
+            moveToState(fragment, mCurState);
+        }
     }
     
     public void removeFragment(Fragment fragment) {
@@ -151,33 +169,57 @@
         moveToState(fragment, Fragment.INITIALIZING);
     }
     
+    public void addBackStackState(BackStackState state) {
+        if (mBackStack == null) {
+            mBackStack = new ArrayList<BackStackState>();
+        }
+        mBackStack.add(state);
+    }
+    
+    public boolean popBackStackState(Handler handler) {
+        if (mBackStack == null) {
+            return false;
+        }
+        int last = mBackStack.size()-1;
+        if (last < 0) {
+            return false;
+        }
+        final BackStackState bss = mBackStack.remove(last);
+        handler.post(new Runnable() {
+            public void run() {
+                bss.popFromBackStack();
+            }
+        });
+        return true;
+    }
+    
     public void attachActivity(Activity activity) {
         if (mActivity != null) throw new IllegalStateException();
         mActivity = activity;
     }
     
     public void dispatchCreate(Bundle state) {
-        moveToState(Fragment.CREATED);
+        moveToState(Fragment.CREATED, false);
     }
     
     public void dispatchStart() {
-        moveToState(Fragment.STARTED);
+        moveToState(Fragment.STARTED, false);
     }
     
     public void dispatchResume() {
-        moveToState(Fragment.RESUMED);
+        moveToState(Fragment.RESUMED, false);
     }
     
     public void dispatchPause() {
-        moveToState(Fragment.STARTED);
+        moveToState(Fragment.STARTED, false);
     }
     
     public void dispatchStop() {
-        moveToState(Fragment.CREATED);
+        moveToState(Fragment.CREATED, false);
     }
     
     public void dispatchDestroy() {
-        moveToState(Fragment.INITIALIZING);
+        moveToState(Fragment.INITIALIZING, false);
         mActivity = null;
     }
 }
diff --git a/core/java/android/app/FragmentTransaction.java b/core/java/android/app/FragmentTransaction.java
index f97e510..8ca8364 100644
--- a/core/java/android/app/FragmentTransaction.java
+++ b/core/java/android/app/FragmentTransaction.java
@@ -7,5 +7,6 @@
     public FragmentTransaction add(Fragment fragment, int containerViewId);
     public FragmentTransaction add(Fragment fragment, String name, int containerViewId);
     public FragmentTransaction remove(Fragment fragment);
+    public FragmentTransaction addToBackStack();
     public void commit();
 }
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index d75a1a6..1449b02 100755
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -3530,6 +3530,39 @@
     <!-- =============================== -->
     <eat-comment />
     
+    <!-- ============================= -->
+    <!-- View package class attributes -->
+    <!-- ============================= -->
+    <eat-comment />
+
+    <!-- Attributes that can be used with <code>&lt;fragment&gt;</code>
+         tags inside of the layout of an Activity.  This instantiates
+         the given {@link android.app.Fragment} and inserts its content
+         view into the current location in the layout. -->
+    <declare-styleable name="Fragment">
+        <!-- Supply the name of the fragment class to instantiate. -->
+        <attr name="name" />
+        
+        <!-- Supply an identifier name for the top-level view, to later retrieve it
+             with {@link android.view.View#findViewById View.findViewById()} or
+             {@link android.app.Activity#findViewById Activity.findViewById()}.
+             This must be a
+             resource reference; typically you set this using the
+             <code>@+</code> syntax to create a new ID resources.
+             For example: <code>android:id="@+id/my_id"</code> which
+             allows you to later retrieve the view
+             with <code>findViewById(R.id.my_id)</code>. -->
+        <attr name="id" />
+
+        <!-- Supply a tag for the top-level view containing a String, to be retrieved
+             later with {@link android.view.View#getTag View.getTag()} or
+             searched for with {@link android.view.View#findViewWithTag
+             View.findViewWithTag()}.  It is generally preferable to use
+             IDs (through the android:id attribute) instead of tags because
+             they are faster and allow for compile-time type checking. -->
+        <attr name="tag" />
+    </declare-styleable>
+
     <!-- Use <code>device-admin</code> as the root tag of the XML resource that
          describes a
          {@link android.app.admin.DeviceAdminReceiver}, which is