diff --git a/emailcommon/src/com/android/emailcommon/utility/Utility.java b/emailcommon/src/com/android/emailcommon/utility/Utility.java index f85895e5a..77e4253a3 100644 --- a/emailcommon/src/com/android/emailcommon/utility/Utility.java +++ b/emailcommon/src/com/android/emailcommon/utility/Utility.java @@ -738,46 +738,6 @@ public class Utility { column, defaultValue, BLOB_GETTER); } - /** - * A class used to restore ListView state (e.g. scroll position) when changing adapter. - */ - public static class ListStateSaver implements Parcelable { - private final Parcelable mState; - - private ListStateSaver(Parcel p) { - mState = p.readParcelable(getClass().getClassLoader()); - } - - public ListStateSaver(AbsListView lv) { - mState = lv.onSaveInstanceState(); - } - - public void restore(AbsListView lv) { - lv.onRestoreInstanceState(mState); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeParcelable(mState, flags); - } - - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { - public ListStateSaver createFromParcel(Parcel in) { - return new ListStateSaver(in); - } - - public ListStateSaver[] newArray(int size) { - return new ListStateSaver[size]; - } - }; - } - public static boolean attachmentExists(Context context, Attachment attachment) { if (attachment == null) { return false; diff --git a/src/com/android/email/activity/MailboxListFragment.java b/src/com/android/email/activity/MailboxListFragment.java index 2932c81e6..8abab7175 100644 --- a/src/com/android/email/activity/MailboxListFragment.java +++ b/src/com/android/email/activity/MailboxListFragment.java @@ -39,6 +39,7 @@ import android.database.Cursor; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; +import android.os.Parcelable; import android.util.Log; import android.view.DragEvent; import android.view.LayoutInflater; @@ -46,8 +47,8 @@ import android.view.View; import android.view.View.OnDragListener; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.ListView; import android.widget.AdapterView.OnItemClickListener; +import android.widget.ListView; import java.security.InvalidParameterException; @@ -121,9 +122,10 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList // True if we are currently scrolling under the drag item private boolean mTargetScrolling; - private Utility.ListStateSaver mSavedListState; + private Parcelable mSavedListState; - private MailboxesAdapter.Callback mMailboxesAdapterCallback = new MailboxesAdapter.Callback() { + private final MailboxesAdapter.Callback mMailboxesAdapterCallback = + new MailboxesAdapter.Callback() { @Override public void onBind(MailboxListItem listItem) { listItem.setDropTargetBackground(mDragInProgress, mDragItemMailboxId); @@ -343,7 +345,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList } mResumed = false; super.onPause(); - mSavedListState = new Utility.ListStateSaver(getListView()); + mSavedListState = getListView().onSaveInstanceState(); } /** @@ -375,7 +377,7 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList } super.onSaveInstanceState(outState); outState.putLong(BUNDLE_KEY_SELECTED_MAILBOX_ID, mSelectedMailboxId); - outState.putParcelable(BUNDLE_LIST_STATE, new Utility.ListStateSaver(getListView())); + outState.putParcelable(BUNDLE_LIST_STATE, getListView().onSaveInstanceState()); } private void restoreInstanceState(Bundle savedInstanceState) { @@ -442,14 +444,14 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList // Save list view state (primarily scroll position) final ListView lv = getListView(); - final Utility.ListStateSaver lss; + final Parcelable listState; if (!mSaveListState) { - lss = null; // Don't preserve list state + listState = null; // Don't preserve list state } else if (mSavedListState != null) { - lss = mSavedListState; + listState = mSavedListState; mSavedListState = null; } else { - lss = new Utility.ListStateSaver(lv); + listState = lv.onSaveInstanceState(); } if (cursor.getCount() == 0) { @@ -469,8 +471,8 @@ public class MailboxListFragment extends ListFragment implements OnItemClickList } // Restore the state - if (lss != null) { - lss.restore(lv); + if (listState != null) { + lv.onRestoreInstanceState(listState); } // Clear this for next reload triggered by content changed events. diff --git a/src/com/android/email/activity/MessageListFragment.java b/src/com/android/email/activity/MessageListFragment.java index 4ec94ab62..582ac7040 100644 --- a/src/com/android/email/activity/MessageListFragment.java +++ b/src/com/android/email/activity/MessageListFragment.java @@ -30,7 +30,6 @@ import com.android.emailcommon.provider.EmailContent.Mailbox; import com.android.emailcommon.provider.EmailContent.Message; import com.android.emailcommon.utility.EmailAsyncTask; import com.android.emailcommon.utility.Utility; -import com.android.emailcommon.utility.Utility.ListStateSaver; import android.app.Activity; import android.app.ListFragment; @@ -157,7 +156,7 @@ public class MessageListFragment extends ListFragment private ActionMode mSelectionMode; private SelectionModeCallback mLastSelectionModeCallback; - private Utility.ListStateSaver mSavedListState; + private Parcelable mSavedListState; private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker(); @@ -307,7 +306,7 @@ public class MessageListFragment extends ListFragment } mResumed = false; super.onStop(); - mSavedListState = new Utility.ListStateSaver(getListView()); + mSavedListState = getListView().onSaveInstanceState(); } @Override @@ -335,7 +334,7 @@ public class MessageListFragment extends ListFragment } super.onSaveInstanceState(outState); mListAdapter.onSaveInstanceState(outState); - outState.putParcelable(BUNDLE_LIST_STATE, new Utility.ListStateSaver(getListView())); + outState.putParcelable(BUNDLE_LIST_STATE, getListView().onSaveInstanceState()); outState.putLong(BUNDLE_KEY_SELECTED_MESSAGE_ID, mSelectedMessageId); } @@ -1169,14 +1168,14 @@ public class MessageListFragment extends ListFragment // Save list view state (primarily scroll position) final ListView lv = getListView(); - final Utility.ListStateSaver lss; + final Parcelable listState; if (mMailboxChanging) { - lss = null; // Don't preserve list state + listState = null; // Don't preserve list state } else if (mSavedListState != null) { - lss = mSavedListState; + listState = mSavedListState; mSavedListState = null; } else { - lss = new Utility.ListStateSaver(lv); + listState = lv.onSaveInstanceState(); } // If this is a search mailbox, set the query; otherwise, clear it @@ -1206,8 +1205,8 @@ public class MessageListFragment extends ListFragment // Restore the state -- this step has to be the last, because Some of the // "post processing" seems to reset the scroll position. - if (lss != null) { - lss.restore(lv); + if (listState != null) { + lv.onRestoreInstanceState(listState); } resetNewMessageCount(mActivity, mMailboxId, getAccountId()); @@ -1385,14 +1384,14 @@ public class MessageListFragment extends ListFragment * transitions. */ public static class State implements Parcelable { - private final ListStateSaver mListState; + private final Parcelable mListState; private State(Parcel p) { mListState = p.readParcelable(getClass().getClassLoader()); } private State(MessageListFragment messageListFragment) { - mListState = new Utility.ListStateSaver(messageListFragment.getListView()); + mListState = messageListFragment.getListView().onSaveInstanceState(); } public void restore(MessageListFragment messageListFragment) { diff --git a/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java b/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java index ce0aff1d1..4d428bb75 100644 --- a/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java +++ b/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java @@ -32,16 +32,12 @@ import android.database.CursorWrapper; import android.database.MatrixCursor; import android.graphics.drawable.Drawable; import android.net.Uri; -import android.os.Bundle; import android.os.Environment; -import android.os.Parcel; -import android.os.Parcelable; import android.test.AndroidTestCase; import android.test.MoreAsserts; import android.test.suitebuilder.annotation.SmallTest; import android.text.SpannableStringBuilder; import android.text.TextUtils; -import android.widget.ListView; import android.widget.TextView; import java.io.File; @@ -325,29 +321,6 @@ public class UtilityUnitTests extends AndroidTestCase { assertTrue(Utility.isPortFieldValid(view)); } - public void testListStateSaver() { - final String BUNDLE_KEY = "a"; - - Bundle b = new Bundle(); - // Create a list view, save the state. - // (Use blocks to make sure we won't use the same instance later.) - { - final MockListView lv1 = new MockListView(getContext()); - lv1.mCustomData = 1; - - final Utility.ListStateSaver lss1 = new Utility.ListStateSaver(lv1); - b.putParcelable(BUNDLE_KEY, lss1); - } - - // Restore the state into a new list view. - { - final Utility.ListStateSaver lss2 = b.getParcelable(BUNDLE_KEY); - final MockListView lv2 = new MockListView(getContext()); - lss2.restore(lv2); - assertEquals(1, lv2.mCustomData); - } - } - public void testToPrimitiveLongArray() { assertEquals(0, Utility.toPrimitiveLongArray(createLongCollection()).length); @@ -469,59 +442,4 @@ public class UtilityUnitTests extends AndroidTestCase { s2 = null; assertTrue(Utility.areStringsEqual(s1, s2)); } - - /** - * A {@link ListView} used by {@link #testListStateSaver}. - */ - private static class MockListView extends ListView { - public int mCustomData; - - public MockListView(Context context) { - super(context); - } - - @Override - public Parcelable onSaveInstanceState() { - SavedState ss = new SavedState(super.onSaveInstanceState()); - ss.mCustomData = mCustomData; - return ss; - } - - @Override - public void onRestoreInstanceState(Parcelable state) { - SavedState ss = (SavedState) state; - mCustomData = ss.mCustomData; - } - - static class SavedState extends BaseSavedState { - public int mCustomData; - - SavedState(Parcelable superState) { - super(superState); - } - - private SavedState(Parcel in) { - super(in); - in.writeInt(mCustomData); - } - - @Override - public void writeToParcel(Parcel out, int flags) { - super.writeToParcel(out, flags); - mCustomData = out.readInt(); - } - - @SuppressWarnings({"hiding", "unused"}) - public static final Parcelable.Creator CREATOR - = new Parcelable.Creator() { - public SavedState createFromParcel(Parcel in) { - return new SavedState(in); - } - - public SavedState[] newArray(int size) { - return new SavedState[size]; - } - }; - } - } }