Fixe some stuff.

Addresses these bugs:

3061847 - With no headers, PreferenceActivity crashes
2888426 - minor typo in DevicePolicyManagerService.ActiveAdmin.writeToXml()
3159155 - IllegalStateException:"Can not perform this action after
    onSaveInstanceState" while dismissing a DialogFragment
3155995 - PopupWindow.showAtLocation does not respect LayoutParams

Also tweak the new fragment APIs to use abstract classes instead of
interfaces as base classes.

Change-Id: I9c0b4337fe0e304b737b5f7c2762762372bb3020
diff --git a/api/current.xml b/api/current.xml
index 5cad414..ef49855 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -28065,13 +28065,22 @@
 </parameter>
 </method>
 </class>
-<interface name="FragmentManager"
+<class name="FragmentManager"
+ extends="java.lang.Object"
  abstract="true"
  static="false"
  final="false"
  deprecated="not deprecated"
  visibility="public"
 >
+<constructor name="FragmentManager"
+ type="android.app.FragmentManager"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</constructor>
 <method name="addOnBackStackChangedListener"
  return="void"
  abstract="true"
@@ -28262,7 +28271,7 @@
  visibility="public"
 >
 </field>
-</interface>
+</class>
 <interface name="FragmentManager.BackStackEntry"
  abstract="true"
  static="true"
@@ -28323,13 +28332,22 @@
 >
 </method>
 </interface>
-<interface name="FragmentTransaction"
+<class name="FragmentTransaction"
+ extends="java.lang.Object"
  abstract="true"
  static="false"
  final="false"
  deprecated="not deprecated"
  visibility="public"
 >
+<constructor name="FragmentTransaction"
+ type="android.app.FragmentTransaction"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</constructor>
 <method name="add"
  return="android.app.FragmentTransaction"
  abstract="true"
@@ -28401,6 +28419,17 @@
  visibility="public"
 >
 </method>
+<method name="commitAllowingStateLoss"
+ return="int"
+ abstract="true"
+ native="false"
+ synchronized="false"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</method>
 <method name="disallowAddToBackStack"
  return="android.app.FragmentTransaction"
  abstract="true"
@@ -28686,7 +28715,7 @@
  visibility="public"
 >
 </field>
-</interface>
+</class>
 <class name="Instrumentation"
  extends="java.lang.Object"
  abstract="false"
@@ -30281,13 +30310,22 @@
 </parameter>
 </method>
 </class>
-<interface name="LoaderManager"
+<class name="LoaderManager"
+ extends="java.lang.Object"
  abstract="true"
  static="false"
  final="false"
  deprecated="not deprecated"
  visibility="public"
 >
+<constructor name="LoaderManager"
+ type="android.app.LoaderManager"
+ static="false"
+ final="false"
+ deprecated="not deprecated"
+ visibility="public"
+>
+</constructor>
 <method name="getLoader"
  return="android.content.Loader&lt;D&gt;"
  abstract="true"
@@ -30348,7 +30386,7 @@
 <parameter name="id" type="int">
 </parameter>
 </method>
-</interface>
+</class>
 <interface name="LoaderManager.LoaderCallbacks"
  abstract="true"
  static="true"
@@ -30392,7 +30430,7 @@
  abstract="true"
  static="false"
  final="false"
- deprecated="not deprecated"
+ deprecated="deprecated"
  visibility="public"
 >
 <implements name="android.content.Loader.OnLoadCompleteListener">
diff --git a/core/java/android/app/BackStackRecord.java b/core/java/android/app/BackStackRecord.java
index c75777d..e9b6869 100644
--- a/core/java/android/app/BackStackRecord.java
+++ b/core/java/android/app/BackStackRecord.java
@@ -155,7 +155,7 @@
 /**
  * @hide Entry of an operation on the fragment back stack.
  */
-final class BackStackRecord implements FragmentTransaction,
+final class BackStackRecord extends FragmentTransaction implements
         FragmentManager.BackStackEntry, Runnable {
     static final String TAG = "BackStackEntry";
 
@@ -417,6 +417,14 @@
     }
 
     public int commit() {
+        return commitInternal(false);
+    }
+
+    public int commitAllowingStateLoss() {
+        return commitInternal(true);
+    }
+    
+    int commitInternal(boolean allowStateLoss) {
         if (mCommitted) throw new IllegalStateException("commit already called");
         if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Commit: " + this);
         mCommitted = true;
@@ -425,10 +433,10 @@
         } else {
             mIndex = -1;
         }
-        mManager.enqueueAction(this);
+        mManager.enqueueAction(this, allowStateLoss);
         return mIndex;
     }
-
+    
     public void run() {
         if (FragmentManagerImpl.DEBUG) Log.v(TAG, "Run: " + this);
 
diff --git a/core/java/android/app/DialogFragment.java b/core/java/android/app/DialogFragment.java
index 8e2389b..cbecc21 100644
--- a/core/java/android/app/DialogFragment.java
+++ b/core/java/android/app/DialogFragment.java
@@ -256,6 +256,10 @@
      * the fragment.
      */
     public void dismiss() {
+        dismissInternal(false);
+    }
+
+    void dismissInternal(boolean allowStateLoss) {
         if (mDialog != null) {
             mDialog.dismiss();
             mDialog = null;
@@ -271,7 +275,7 @@
             ft.commit();
         }
     }
-
+    
     public Dialog getDialog() {
         return mDialog;
     }
@@ -353,7 +357,11 @@
 
     public void onDismiss(DialogInterface dialog) {
         if (!mRemoved) {
-            dismiss();
+            // Note: we need to use allowStateLoss, because the dialog
+            // dispatches this asynchronously so we can receive the call
+            // after the activity is paused.  Worst case, when the user comes
+            // back to the activity they see the dialog again.
+            dismissInternal(true);
         }
     }
 
diff --git a/core/java/android/app/FragmentManager.java b/core/java/android/app/FragmentManager.java
index 512ca16..11de6a6 100644
--- a/core/java/android/app/FragmentManager.java
+++ b/core/java/android/app/FragmentManager.java
@@ -40,7 +40,7 @@
  * Interface for interacting with {@link Fragment} objects inside of an
  * {@link Activity}
  */
-public interface FragmentManager {
+public abstract class FragmentManager {
     /**
      * Representation of an entry on the fragment back stack, as created
      * with {@link FragmentTransaction#addToBackStack(String)
@@ -96,7 +96,7 @@
      * in the state, and if changes are made after the state is saved then they
      * will be lost.</p>
      */
-    public FragmentTransaction openTransaction();
+    public abstract FragmentTransaction openTransaction();
 
     /**
      * Finds a fragment that was identified by the given id either when inflated
@@ -106,7 +106,7 @@
      * on the back stack associated with this ID are searched.
      * @return The fragment if found or null otherwise.
      */
-    public Fragment findFragmentById(int id);
+    public abstract Fragment findFragmentById(int id);
 
     /**
      * Finds a fragment that was identified by the given tag either when inflated
@@ -116,7 +116,7 @@
      * on the back stack are searched.
      * @return The fragment if found or null otherwise.
      */
-    public Fragment findFragmentByTag(String tag);
+    public abstract Fragment findFragmentByTag(String tag);
 
     /**
      * Flag for {@link #popBackStack(String, int)}
@@ -132,7 +132,7 @@
      * Pop the top state off the back stack.  Returns true if there was one
      * to pop, else false.
      */
-    public boolean popBackStack();
+    public abstract boolean popBackStack();
 
     /**
      * Pop the last fragment transition from the manager's fragment
@@ -143,7 +143,7 @@
      * the named state itself is popped. If null, only the top state is popped.
      * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
      */
-    public boolean popBackStack(String name, int flags);
+    public abstract boolean popBackStack(String name, int flags);
 
     /**
      * Pop all back stack states up to the one with the given identifier.
@@ -155,29 +155,29 @@
      * the named state itself is popped.
      * @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}.
      */
-    public boolean popBackStack(int id, int flags);
+    public abstract boolean popBackStack(int id, int flags);
 
     /**
      * Return the number of entries currently in the back stack.
      */
-    public int countBackStackEntries();
+    public abstract int countBackStackEntries();
 
     /**
      * Return the BackStackEntry at index <var>index</var> in the back stack;
      * entries start index 0 being the bottom of the stack.
      */
-    public BackStackEntry getBackStackEntry(int index);
+    public abstract BackStackEntry getBackStackEntry(int index);
 
     /**
      * Add a new listener for changes to the fragment back stack.
      */
-    public void addOnBackStackChangedListener(OnBackStackChangedListener listener);
+    public abstract void addOnBackStackChangedListener(OnBackStackChangedListener listener);
 
     /**
      * Remove a listener that was previously added with
      * {@link #addOnBackStackChangedListener(OnBackStackChangedListener)}.
      */
-    public void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
+    public abstract void removeOnBackStackChangedListener(OnBackStackChangedListener listener);
 
     /**
      * Put a reference to a fragment in a Bundle.  This Bundle can be
@@ -189,7 +189,7 @@
      * @param key The name of the entry in the bundle.
      * @param fragment The Fragment whose reference is to be stored.
      */
-    public void putFragment(Bundle bundle, String key, Fragment fragment);
+    public abstract void putFragment(Bundle bundle, String key, Fragment fragment);
 
     /**
      * Retrieve the current Fragment instance for a reference previously
@@ -200,7 +200,7 @@
      * @return Returns the current Fragment instance that is associated with
      * the given reference.
      */
-    public Fragment getFragment(Bundle bundle, String key);
+    public abstract Fragment getFragment(Bundle bundle, String key);
 
     /**
      * Print the FragmentManager's state into the given stream.
@@ -210,7 +210,7 @@
      * @param writer A PrintWriter to which the dump is to be set.
      * @param args additional arguments to the dump request.
      */
-    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
+    public abstract void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args);
 }
 
 final class FragmentManagerState implements Parcelable {
@@ -252,7 +252,7 @@
 /**
  * Container for fragments associated with an activity.
  */
-final class FragmentManagerImpl implements FragmentManager {
+final class FragmentManagerImpl extends FragmentManager {
     static final boolean DEBUG = true;
     static final String TAG = "FragmentManager";
     
@@ -849,8 +849,8 @@
         return null;
     }
     
-    public void enqueueAction(Runnable action) {
-        if (mStateSaved) {
+    public void enqueueAction(Runnable action, boolean allowStateLoss) {
+        if (!allowStateLoss && mStateSaved) {
             throw new IllegalStateException(
                     "Can not perform this action after onSaveInstanceState");
         }
@@ -991,7 +991,7 @@
                     bss.popFromBackStack(true);
                     reportBackStackChanged();
                 }
-            });
+            }, false);
         } else {
             int index = -1;
             if (name != null || id >= 0) {
@@ -1042,7 +1042,7 @@
                     }
                     reportBackStackChanged();
                 }
-            });
+            }, false);
         }
         return true;
     }
diff --git a/core/java/android/app/FragmentTransaction.java b/core/java/android/app/FragmentTransaction.java
index 19da763..dc4acbe 100644
--- a/core/java/android/app/FragmentTransaction.java
+++ b/core/java/android/app/FragmentTransaction.java
@@ -3,16 +3,16 @@
 /**
  * API for performing a set of Fragment operations.
  */
-public interface FragmentTransaction {
+public abstract class FragmentTransaction {
     /**
      * Calls {@link #add(int, Fragment, String)} with a 0 containerViewId.
      */
-    public FragmentTransaction add(Fragment fragment, String tag);
+    public abstract FragmentTransaction add(Fragment fragment, String tag);
     
     /**
      * Calls {@link #add(int, Fragment, String)} with a null tag.
      */
-    public FragmentTransaction add(int containerViewId, Fragment fragment);
+    public abstract FragmentTransaction add(int containerViewId, Fragment fragment);
     
     /**
      * Add a fragment to the activity state.  This fragment may optionally
@@ -29,12 +29,12 @@
      * 
      * @return Returns the same FragmentTransaction instance.
      */
-    public FragmentTransaction add(int containerViewId, Fragment fragment, String tag);
+    public abstract FragmentTransaction add(int containerViewId, Fragment fragment, String tag);
     
     /**
      * Calls {@link #replace(int, Fragment, String)} with a null tag.
      */
-    public FragmentTransaction replace(int containerViewId, Fragment fragment);
+    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment);
     
     /**
      * Replace an existing fragment that was added to a container.  This is
@@ -52,7 +52,7 @@
      * 
      * @return Returns the same FragmentTransaction instance.
      */
-    public FragmentTransaction replace(int containerViewId, Fragment fragment, String tag);
+    public abstract FragmentTransaction replace(int containerViewId, Fragment fragment, String tag);
     
     /**
      * Remove an existing fragment.  If it was added to a container, its view
@@ -62,7 +62,7 @@
      * 
      * @return Returns the same FragmentTransaction instance.
      */
-    public FragmentTransaction remove(Fragment fragment);
+    public abstract FragmentTransaction remove(Fragment fragment);
     
     /**
      * Hides an existing fragment.  This is only relevant for fragments whose
@@ -73,7 +73,7 @@
      * 
      * @return Returns the same FragmentTransaction instance.
      */
-    public FragmentTransaction hide(Fragment fragment);
+    public abstract FragmentTransaction hide(Fragment fragment);
     
     /**
      * Hides a previously hidden fragment.  This is only relevant for fragments whose
@@ -84,55 +84,55 @@
      * 
      * @return Returns the same FragmentTransaction instance.
      */
-    public FragmentTransaction show(Fragment fragment);
+    public abstract FragmentTransaction show(Fragment fragment);
 
     /**
      * @return <code>true</code> if this transaction contains no operations,
      * <code>false</code> otherwise.
      */
-    public boolean isEmpty();
+    public abstract boolean isEmpty();
     
     /**
      * Bit mask that is set for all enter transitions.
      */
-    public final int TRANSIT_ENTER_MASK = 0x1000;
+    public static final int TRANSIT_ENTER_MASK = 0x1000;
     
     /**
      * Bit mask that is set for all exit transitions.
      */
-    public final int TRANSIT_EXIT_MASK = 0x2000;
+    public static final int TRANSIT_EXIT_MASK = 0x2000;
     
     /** Not set up for a transition. */
-    public final int TRANSIT_UNSET = -1;
+    public static final int TRANSIT_UNSET = -1;
     /** No animation for transition. */
-    public final int TRANSIT_NONE = 0;
+    public static final int TRANSIT_NONE = 0;
     /** Fragment is being added onto the stack */
-    public final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
+    public static final int TRANSIT_FRAGMENT_OPEN = 1 | TRANSIT_ENTER_MASK;
     /** Fragment is being removed from the stack */
-    public final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
+    public static final int TRANSIT_FRAGMENT_CLOSE = 2 | TRANSIT_EXIT_MASK;
     /** Fragment is being added in a 'next' operation*/
-    public final int TRANSIT_FRAGMENT_NEXT = 3 | TRANSIT_ENTER_MASK;
+    public static final int TRANSIT_FRAGMENT_NEXT = 3 | TRANSIT_ENTER_MASK;
     /** Fragment is being removed in a 'previous' operation */
-    public final int TRANSIT_FRAGMENT_PREV = 4 | TRANSIT_EXIT_MASK;
+    public static final int TRANSIT_FRAGMENT_PREV = 4 | TRANSIT_EXIT_MASK;
 
     /**
      * Set specific animation resources to run for the fragments that are
      * entering and exiting in this transaction.
      */
-    public FragmentTransaction setCustomAnimations(int enter, int exit);
+    public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
     
     /**
      * Select a standard transition animation for this transaction.  May be
      * one of {@link #TRANSIT_NONE}, {@link #TRANSIT_FRAGMENT_OPEN},
      * or {@link #TRANSIT_FRAGMENT_CLOSE}
      */
-    public FragmentTransaction setTransition(int transit);
+    public abstract FragmentTransaction setTransition(int transit);
 
     /**
      * Set a custom style resource that will be used for resolving transit
      * animations.
      */
-    public FragmentTransaction setTransitionStyle(int styleRes);
+    public abstract FragmentTransaction setTransitionStyle(int styleRes);
     
     /**
      * Add this transaction to the back stack.  This means that the transaction
@@ -141,7 +141,7 @@
      *
      * @param name An optional name for this back stack state, or null.
      */
-    public FragmentTransaction addToBackStack(String name);
+    public abstract FragmentTransaction addToBackStack(String name);
 
     /**
      * Returns true if this FragmentTransaction is allowed to be added to the back
@@ -150,14 +150,14 @@
      *
      * @return True if {@link #addToBackStack(String)} is permitted on this transaction.
      */
-    public boolean isAddToBackStackAllowed();
+    public abstract boolean isAddToBackStackAllowed();
 
     /**
      * Disallow calls to {@link #addToBackStack(String)}. Any future calls to
      * addToBackStack will throw {@link IllegalStateException}. If addToBackStack
      * has already been called, this method will throw IllegalStateException.
      */
-    public FragmentTransaction disallowAddToBackStack();
+    public abstract FragmentTransaction disallowAddToBackStack();
 
     /**
      * Set the full title to show as a bread crumb when this transaction
@@ -165,14 +165,14 @@
      *
      * @param res A string resource containing the title.
      */
-    public FragmentTransaction setBreadCrumbTitle(int res);
+    public abstract FragmentTransaction setBreadCrumbTitle(int res);
 
     /**
      * Like {@link #setBreadCrumbTitle(int)} but taking a raw string; this
      * method is <em>not</em> recommended, as the string can not be changed
      * later if the locale changes.
      */
-    public FragmentTransaction setBreadCrumbTitle(CharSequence text);
+    public abstract FragmentTransaction setBreadCrumbTitle(CharSequence text);
 
     /**
      * Set the short title to show as a bread crumb when this transaction
@@ -180,23 +180,39 @@
      *
      * @param res A string resource containing the title.
      */
-    public FragmentTransaction setBreadCrumbShortTitle(int res);
+    public abstract FragmentTransaction setBreadCrumbShortTitle(int res);
 
     /**
      * Like {@link #setBreadCrumbShortTitle(int)} but taking a raw string; this
      * method is <em>not</em> recommended, as the string can not be changed
      * later if the locale changes.
      */
-    public FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
+    public abstract FragmentTransaction setBreadCrumbShortTitle(CharSequence text);
 
     /**
-     * Schedules a commit of this transaction.  Note that the commit does
+     * Schedules a commit of this transaction.  The commit does
      * not happen immediately; it will be scheduled as work on the main thread
      * to be done the next time that thread is ready.
      *
+     * <p class="note">A transaction can only be committed with this method
+     * prior to its containing activity saving its state.  If the commit is
+     * attempted after that point, an exception will be thrown.  This is
+     * because the state after the commit can be lost if the activity needs to
+     * be restored from its state.  See {@link #commitAllowingStateLoss()} for
+     * situations where it may be okay to lose the commit.</p>
+     * 
      * @return Returns the identifier of this transaction's back stack entry,
      * if {@link #addToBackStack(String)} had been called.  Otherwise, returns
      * a negative number.
      */
-    public int commit();
+    public abstract int commit();
+
+    /**
+     * Like {@link #commit} but allows the commit to be executed after an
+     * activity's state is saved.  This is dangerous because the commit can
+     * be lost if the activity needs to later be restored from its state, so
+     * this should only be used for cases where it is okay for the UI state
+     * to change unexpectedly on the user.
+     */
+    public abstract int commitAllowingStateLoss();
 }
diff --git a/core/java/android/app/LoaderManager.java b/core/java/android/app/LoaderManager.java
index 4d4ea9a..7ae4b95 100644
--- a/core/java/android/app/LoaderManager.java
+++ b/core/java/android/app/LoaderManager.java
@@ -25,7 +25,7 @@
  * Interface associated with an {@link Activity} or {@link Fragment} for managing
  * one or more {@link android.content.Loader} instances associated with it.
  */
-public interface LoaderManager {
+public abstract class LoaderManager {
     /**
      * Callback interface for a client to interact with the manager.
      */
@@ -66,7 +66,7 @@
      * be called immediately (inside of this function), so you must be prepared
      * for this to happen.
      */
-    public <D> Loader<D> initLoader(int id, Bundle args,
+    public abstract <D> Loader<D> initLoader(int id, Bundle args,
             LoaderManager.LoaderCallbacks<D> callback);
 
     /**
@@ -77,22 +77,22 @@
      * its work. The callback will be delivered before the old loader
      * is destroyed.
      */
-    public <D> Loader<D> restartLoader(int id, Bundle args,
+    public abstract <D> Loader<D> restartLoader(int id, Bundle args,
             LoaderManager.LoaderCallbacks<D> callback);
 
     /**
      * Stops and removes the loader with the given ID.
      */
-    public void stopLoader(int id);
+    public abstract void stopLoader(int id);
 
     /**
      * Return the Loader with the given id or null if no matching Loader
      * is found.
      */
-    public <D> Loader<D> getLoader(int id);
+    public abstract <D> Loader<D> getLoader(int id);
 }
 
-class LoaderManagerImpl implements LoaderManager {
+class LoaderManagerImpl extends LoaderManager {
     static final String TAG = "LoaderManagerImpl";
     static final boolean DEBUG = true;
 
diff --git a/core/java/android/app/LoaderManagingFragment.java b/core/java/android/app/LoaderManagingFragment.java
index af71170..f0f5856 100644
--- a/core/java/android/app/LoaderManagingFragment.java
+++ b/core/java/android/app/LoaderManagingFragment.java
@@ -26,7 +26,10 @@
  *
  * @param <D> The type of data returned by the Loader. If you're using multiple Loaders with
  * different return types use Object and case the results.
+ * 
+ * @deprecated This was an old design, it will be removed before Honeycomb ships.
  */
+@Deprecated
 public abstract class LoaderManagingFragment<D> extends Fragment
         implements Loader.OnLoadCompleteListener<D> {
     private boolean mStarted = false;
diff --git a/core/java/android/preference/PreferenceActivity.java b/core/java/android/preference/PreferenceActivity.java
index c1ee0cf..0ce69ad 100644
--- a/core/java/android/preference/PreferenceActivity.java
+++ b/core/java/android/preference/PreferenceActivity.java
@@ -216,8 +216,6 @@
                         Header mappedHeader = findBestMatchingHeader(mCurHeader, mHeaders);
                         if (mappedHeader != null) {
                             setSelectedHeader(mappedHeader);
-                        } else {
-                            switchToHeader(null);
                         }
                     }
                 } break;
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 4b7c071..95678c6 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -787,6 +787,8 @@
         p.gravity = gravity;
         p.x = x;
         p.y = y;
+        if (mHeightMode < 0) p.height = mLastHeight = mHeightMode;
+        if (mWidthMode < 0) p.width = mLastWidth = mWidthMode;
         invokePopup(p);
     }
 
diff --git a/services/java/com/android/server/DevicePolicyManagerService.java b/services/java/com/android/server/DevicePolicyManagerService.java
index 081bd30..68aa8e3 100644
--- a/services/java/com/android/server/DevicePolicyManagerService.java
+++ b/services/java/com/android/server/DevicePolicyManagerService.java
@@ -175,7 +175,7 @@
                     out.endTag(null, "min-password-nonletter");
                 }
             }
-            if (maximumTimeToUnlock != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
+            if (maximumTimeToUnlock != 0) {
                 out.startTag(null, "max-time-to-unlock");
                 out.attribute(null, "value", Long.toString(maximumTimeToUnlock));
                 out.endTag(null, "max-time-to-unlock");