Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8eb2e244 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Various PreferenceActivity and related improvement.

This is all about making the preferences implementation better.

Well, mostly all about that.

Change-Id: I8efa98cb5680f3ccfa3ed694a1586de3fb3a9e11
parent 5060309f
Loading
Loading
Loading
Loading
+54 −14
Original line number Diff line number Diff line
@@ -27628,6 +27628,17 @@
 visibility="public"
>
</method>
<method name="onDestroyOptionsMenu"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="onDestroyView"
 return="void"
 abstract="false"
@@ -145083,6 +145094,23 @@
<parameter name="key" type="java.lang.CharSequence">
</parameter>
</method>
<method name="finishPreferencePanel"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="caller" type="android.app.Fragment">
</parameter>
<parameter name="resultCode" type="int">
</parameter>
<parameter name="resultData" type="android.content.Intent">
</parameter>
</method>
<method name="getPreferenceManager"
 return="android.preference.PreferenceManager"
 abstract="false"
@@ -145311,7 +145339,7 @@
<parameter name="push" type="boolean">
</parameter>
</method>
<method name="startPreferenceFragment"
<method name="startPreferencePanel"
 return="void"
 abstract="false"
 native="false"
@@ -145321,9 +145349,17 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="fragment" type="android.app.Fragment">
<parameter name="fragmentClass" type="java.lang.String">
</parameter>
<parameter name="ft" type="android.app.FragmentTransaction">
<parameter name="args" type="android.os.Bundle">
</parameter>
<parameter name="titleRes" type="int">
</parameter>
<parameter name="titleText" type="java.lang.CharSequence">
</parameter>
<parameter name="resultTo" type="android.app.Fragment">
</parameter>
<parameter name="resultRequestCode" type="int">
</parameter>
</method>
<method name="startWithFragment"
@@ -145340,6 +145376,10 @@
</parameter>
<parameter name="args" type="android.os.Bundle">
</parameter>
<parameter name="resultTo" type="android.app.Fragment">
</parameter>
<parameter name="resultRequestCode" type="int">
</parameter>
</method>
<method name="switchToHeader"
 return="void"
@@ -347602,7 +347642,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="toCopyIn" type="E[]">
<parameter name="array" type="E[]">
</parameter>
</constructor>
<method name="add"
@@ -347630,7 +347670,7 @@
>
<parameter name="index" type="int">
</parameter>
<parameter name="element" type="E">
<parameter name="e" type="E">
</parameter>
</method>
<method name="addAll"
@@ -347758,7 +347798,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="o" type="java.lang.Object">
<parameter name="e" type="E">
</parameter>
<parameter name="index" type="int">
</parameter>
</method>
<method name="indexOf"
@@ -347771,9 +347813,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="e" type="E">
</parameter>
<parameter name="index" type="int">
<parameter name="o" type="java.lang.Object">
</parameter>
</method>
<method name="isEmpty"
@@ -347808,7 +347848,9 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="o" type="java.lang.Object">
<parameter name="e" type="E">
</parameter>
<parameter name="index" type="int">
</parameter>
</method>
<method name="lastIndexOf"
@@ -347821,9 +347863,7 @@
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="e" type="E">
</parameter>
<parameter name="index" type="int">
<parameter name="o" type="java.lang.Object">
</parameter>
</method>
<method name="listIterator"
@@ -347914,7 +347954,7 @@
>
<parameter name="index" type="int">
</parameter>
<parameter name="element" type="E">
<parameter name="e" type="E">
</parameter>
</method>
<method name="size"
+10 −0
Original line number Diff line number Diff line
@@ -1026,6 +1026,16 @@ public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener
    public void onPrepareOptionsMenu(Menu menu) {
    }

    /**
     * Called when this fragment's option menu items are no longer being
     * included in the overall options menu.  Receiving this call means that
     * the menu needed to be rebuilt, but this fragment's items were not
     * included in the newly built menu (its {@link #onCreateOptionsMenu(Menu, MenuInflater)}
     * was not called).
     */
    public void onDestroyOptionsMenu() {
    }
    
    /**
     * This hook is called whenever an item in your options menu is selected.
     * The default implementation simply returns false to have the normal
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.app;

import android.animation.LayoutTransition;
import android.app.FragmentManager.BackStackEntry;
import android.content.Context;
import android.util.AttributeSet;
@@ -69,6 +70,7 @@ public class FragmentBreadCrumbs extends ViewGroup
        addView(mContainer);
        a.getFragmentManager().addOnBackStackChangedListener(this);
        updateCrumbs();
        setLayoutTransition(new LayoutTransition());
    }

    /**
+18 −0
Original line number Diff line number Diff line
@@ -268,6 +268,7 @@ final class FragmentManagerImpl implements FragmentManager {
    ArrayList<Fragment> mAdded;
    ArrayList<Integer> mAvailIndices;
    ArrayList<BackStackRecord> mBackStack;
    ArrayList<Fragment> mCreatedMenus;
    
    // Must be accessed while locked.
    ArrayList<BackStackRecord> mBackStackIndices;
@@ -1325,15 +1326,32 @@ final class FragmentManagerImpl implements FragmentManager {
    
    public boolean dispatchCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        boolean show = false;
        ArrayList<Fragment> newMenus = null;
        if (mActive != null) {
            for (int i=0; i<mAdded.size(); i++) {
                Fragment f = mAdded.get(i);
                if (f != null && !f.mHidden && f.mHasMenu) {
                    show = true;
                    f.onCreateOptionsMenu(menu, inflater);
                    if (newMenus == null) {
                        newMenus = new ArrayList<Fragment>();
                    }
                    newMenus.add(f);
                }
            }
        }
        
        if (mCreatedMenus != null) {
            for (int i=0; i<mCreatedMenus.size(); i++) {
                Fragment f = mCreatedMenus.get(i);
                if (newMenus == null || !newMenus.contains(f)) {
                    f.onDestroyOptionsMenu();
                }
            }
        }
        
        mCreatedMenus = newMenus;
        
        return show;
    }
    
+83 −23
Original line number Diff line number Diff line
@@ -825,7 +825,7 @@ public abstract class PreferenceActivity extends ListActivity implements

    /**
     * Called when the user selects an item in the header list.  The default
     * implementation will call either {@link #startWithFragment(String, Bundle)}
     * implementation will call either {@link #startWithFragment(String, Bundle, Fragment, int)}
     * or {@link #switchToHeader(Header)} as appropriate.
     *
     * @param header The header that was selected.
@@ -834,7 +834,7 @@ public abstract class PreferenceActivity extends ListActivity implements
    public void onHeaderClick(Header header, int position) {
        if (header.fragment != null) {
            if (mSinglePane) {
                startWithFragment(header.fragment, header.fragmentArguments);
                startWithFragment(header.fragment, header.fragmentArguments, null, 0);
            } else {
                switchToHeader(header);
            }
@@ -852,13 +852,18 @@ public abstract class PreferenceActivity extends ListActivity implements
     * @param fragmentName The name of the fragment to display.
     * @param args Optional arguments to supply to the fragment.
     */
    public void startWithFragment(String fragmentName, Bundle args) {
    public void startWithFragment(String fragmentName, Bundle args,
            Fragment resultTo, int resultRequestCode) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(this, getClass());
        intent.putExtra(EXTRA_SHOW_FRAGMENT, fragmentName);
        intent.putExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS, args);
        intent.putExtra(EXTRA_NO_HEADERS, true);
        if (resultTo == null) {
            startActivity(intent);
        } else {
            resultTo.startActivityForResult(intent, resultRequestCode);
        }
    }

    /**
@@ -923,10 +928,16 @@ public abstract class PreferenceActivity extends ListActivity implements
     * @param header The new header to display.
     */
    public void switchToHeader(Header header) {
        if (mCurHeader == header) {
            // This is the header we are currently displaying.  Just make sure
            // to pop the stack up to its root state.
            getFragmentManager().popBackStack(BACK_STACK_PREFS, POP_BACK_STACK_INCLUSIVE);
        } else {
            int direction = mHeaders.indexOf(header) - mHeaders.indexOf(mCurHeader);
            switchToHeaderInner(header.fragment, header.fragmentArguments, direction);
            setSelectedHeader(header);
        }
    }

    Header findBestMatchingHeader(Header cur, ArrayList<Header> from) {
        ArrayList<Header> matches = new ArrayList<Header>();
@@ -982,7 +993,7 @@ public abstract class PreferenceActivity extends ListActivity implements
     */
    public void startPreferenceFragment(Fragment fragment, boolean push) {
        FragmentTransaction transaction = getFragmentManager().openTransaction();
        startPreferenceFragment(fragment, transaction);
        transaction.replace(com.android.internal.R.id.prefs, fragment);
        if (push) {
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.addToBackStack(BACK_STACK_PREFS);
@@ -993,25 +1004,74 @@ public abstract class PreferenceActivity extends ListActivity implements
    }

    /**
     * Start a new fragment.
     * Start a new fragment containing a preference panel.  If the prefences
     * are being displayed in multi-pane mode, the given fragment class will
     * be instantiated and placed in the appropriate pane.  If running in
     * single-pane mode, a new activity will be launched in which to show the
     * fragment.
     * 
     * @param fragment The fragment to start
     * @param ft The FragmentTransaction in which to perform this operation.
     * Will not be added to the back stack or committed for you; you use do that.
     * @param fragmentClass Full name of the class implementing the fragment.
     * @param args Any desired arguments to supply to the fragment.
     * @param titleRes Optional resource identifier of the title of this
     * fragment.
     * @param titleText Optional text of the title of this fragment.
     * @param resultTo Optional fragment that result data should be sent to.
     * If non-null, resultTo.onActivityResult() will be called when this
     * preference panel is done.  The launched panel must use
     * {@link #finishPreferencePanel(Fragment, int, Intent)} when done.
     * @param resultRequestCode If resultTo is non-null, this is the caller's
     * request code to be received with the resut.
     */
    public void startPreferenceFragment(Fragment fragment, FragmentTransaction ft) {
        ft.replace(com.android.internal.R.id.prefs, fragment);
    public void startPreferencePanel(String fragmentClass, Bundle args, int titleRes,
            CharSequence titleText, Fragment resultTo, int resultRequestCode) {
        if (mSinglePane) {
            startWithFragment(fragmentClass, args, resultTo, resultRequestCode);
        } else {
            Fragment f = Fragment.instantiate(this, fragmentClass, args);
            if (resultTo != null) {
                f.setTargetFragment(resultTo, resultRequestCode);
            }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        Fragment f = Fragment.instantiate(this, pref.getFragment(), pref.getExtras());
            FragmentTransaction transaction = getFragmentManager().openTransaction();
        startPreferenceFragment(f, transaction);
        transaction.setBreadCrumbTitle(pref.getTitle());
            transaction.replace(com.android.internal.R.id.prefs, f);
            if (titleRes != 0) {
                transaction.setBreadCrumbTitle(titleRes);
            } else if (titleText != null) {
                transaction.setBreadCrumbTitle(titleText);
            }
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.addToBackStack(BACK_STACK_PREFS);
            transaction.commit();
        }
    }
    
    /**
     * Called by a preference panel fragment to finish itself.
     * 
     * @param caller The fragment that is asking to be finished.
     * @param resultCode Optional result code to send back to the original
     * launching fragment.
     * @param resultData Optional result data to send back to the original
     * launching fragment.
     */
    public void finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) {
        if (mSinglePane) {
            setResult(resultCode, resultData);
            finish();
        } else {
            if (caller != null) {
                if (caller.getTargetFragment() != null) {
                    caller.getTargetFragment().onActivityResult(caller.getTargetRequestCode(),
                            resultCode, resultData);
                }
            }
            // XXX be smarter about popping the stack.
            onBackPressed();
        }
    }
    
    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        startPreferencePanel(pref.getFragment(), pref.getExtras(), 0, pref.getTitle(), null, 0);
        return true;
    }

Loading