Merge "Minor fixes in Account"
diff --git a/src/com/android/mail/providers/ListParams.java b/src/com/android/mail/providers/ListParams.java
new file mode 100644
index 0000000..f472917
--- /dev/null
+++ b/src/com/android/mail/providers/ListParams.java
@@ -0,0 +1,126 @@
+/**
+ * Copyright (c) 2012, Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.mail.providers;
+
+import com.android.mail.utils.LogUtils;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+public class ListParams implements Parcelable {
+    private static final String LIMIT_KEY = "limit";
+    private static final String USE_NETWORK_KEY = "use-network";
+
+    public static final int NO_LIMIT = -1;
+
+    private static final String LOG_TAG = new LogUtils().getLogTag();
+
+    // The maximum number of results to be created by this search
+    public final int mLimit;
+
+    public final boolean mUseNetwork;
+
+    public ListParams(int limit, boolean useNetwork) {
+        mLimit = limit;
+        mUseNetwork = useNetwork;
+    }
+
+    /**
+     * Supports Parcelable
+     */
+    public static final Parcelable.Creator<ListParams> CREATOR
+        = new Parcelable.Creator<ListParams>() {
+        @Override
+        public ListParams createFromParcel(Parcel in) {
+            return new ListParams(in);
+        }
+
+        @Override
+        public ListParams[] newArray(int size) {
+            return new ListParams[size];
+        }
+    };
+
+    /**
+     * Supports Parcelable
+     */
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeInt(mLimit);
+        dest.writeInt(mUseNetwork ? 1 : 0);
+    }
+
+    /**
+     * Supports Parcelable
+     */
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    /**
+     * Supports Parcelable
+     */
+    public ListParams(Parcel in) {
+        mLimit = in.readInt();
+        mUseNetwork = in.readInt() != 0;
+    }
+
+    /**
+     * Return a serialized String for this ListParams.
+     */
+    public synchronized String serialize() {
+        JSONObject json = new JSONObject();
+        try {
+            json.put(LIMIT_KEY, mLimit);
+            json.put(USE_NETWORK_KEY, mUseNetwork);
+        } catch (JSONException e) {
+            LogUtils.wtf(LOG_TAG, e, "Could not serialize ListParams");
+        }
+        return json.toString();
+    }
+
+    /**
+     * Create a new instance of an ListParams object using a serialized instance created previously
+     * using {@link #serialize()}. This returns null if the serialized instance was invalid or does
+     * not represent a valid parameter object.
+     *
+     * @param serializedParams
+     * @return
+     */
+    public static ListParams newinstance(String serializedParams) {
+        // This method is a wrapper to check for errors and exceptions and return back a null
+        // in cases something breaks.
+        JSONObject json = null;
+        try {
+            json = new JSONObject(serializedParams);
+            final int limit = json.getInt(LIMIT_KEY);
+            final boolean useNetwork = json.getBoolean(USE_NETWORK_KEY);
+            return new ListParams(limit, useNetwork);
+        } catch (JSONException e) {
+            LogUtils.wtf(LOG_TAG, e, "Could not create an params object from this input: \""
+                    + serializedParams);
+            return null;
+        }
+    }
+
+
+
+}
\ No newline at end of file
diff --git a/src/com/android/mail/providers/UIProvider.java b/src/com/android/mail/providers/UIProvider.java
index a0f303c..52b7fa7 100644
--- a/src/com/android/mail/providers/UIProvider.java
+++ b/src/com/android/mail/providers/UIProvider.java
@@ -77,6 +77,13 @@
     public static final String ACCOUNT_TYPE =
             "vnd.android.cursor.item/vnd.com.android.mail.account";
 
+    /**
+     * Query parameter key that can be used to control the behavior of list queries.  The value
+     * must be a serialized {@link ListParams} object.  UIProvider implementations are not
+     * required to respect this query parameter
+     */
+    public static final String LIST_PARAMS_QUERY_PARAMETER = "listParams";
+
     public static final String[] ACCOUNTS_PROJECTION = {
             BaseColumns._ID,
             AccountColumns.NAME,
@@ -565,8 +572,6 @@
     }
 
     public static final class ConversationFlags {
-        public static final int READ = 1<<0;
-        public static final int STARRED = 1<<1;
         public static final int REPLIED = 1<<2;
         public static final int FORWARDED = 1<<3;
         public static final int CALENDAR_INVITE = 1<<4;
diff --git a/src/com/android/mail/ui/ConversationViewFragment.java b/src/com/android/mail/ui/ConversationViewFragment.java
index 17a3a7f..9d69926 100644
--- a/src/com/android/mail/ui/ConversationViewFragment.java
+++ b/src/com/android/mail/ui/ConversationViewFragment.java
@@ -48,6 +48,7 @@
 import com.android.mail.browse.MessageHeaderView;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.Conversation;
+import com.android.mail.providers.ListParams;
 import com.android.mail.providers.Message;
 import com.android.mail.providers.UIProvider;
 import com.android.mail.utils.LogUtils;
@@ -263,6 +264,7 @@
     }
 
     private static class MessageLoader extends CursorLoader {
+        private boolean mDeliveredFirstResults = false;
 
         public MessageLoader(Context c, Uri uri) {
             super(c, uri, UIProvider.MESSAGE_PROJECTION, null, null, null);
@@ -273,6 +275,27 @@
             return new MessageCursor(super.loadInBackground());
 
         }
+
+        @Override
+        public void deliverResult(Cursor result) {
+            // We want to deliver these results, and then we want to make sure that any subsequent
+            // queries do not hit the network
+            super.deliverResult(result);
+
+            if (!mDeliveredFirstResults) {
+                mDeliveredFirstResults = true;
+                Uri uri = getUri();
+
+                // Create a ListParams that tells the provider to not hit the network
+                final ListParams listParams =
+                        new ListParams(ListParams.NO_LIMIT, false /* useNetwork */);
+
+                // Build the new uri with this additional parameter
+                uri = uri.buildUpon().appendQueryParameter(
+                        UIProvider.LIST_PARAMS_QUERY_PARAMETER, listParams.serialize()).build();
+                setUri(uri);
+            }
+        }
     }
 
     private static class MessageCursor extends CursorWrapper {
diff --git a/src/com/android/mail/ui/TwoPaneController.java b/src/com/android/mail/ui/TwoPaneController.java
index 49e281d..eb8f4a9 100644
--- a/src/com/android/mail/ui/TwoPaneController.java
+++ b/src/com/android/mail/ui/TwoPaneController.java
@@ -21,6 +21,7 @@
 import com.android.mail.R;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.Conversation;
+import com.android.mail.providers.Folder;
 import com.android.mail.utils.LogUtils;
 
 import android.app.Fragment;
@@ -88,6 +89,9 @@
         // stack.
         resetActionBarIcon();
         attachFolderList(folderListFragment);
+        if (getCurrentListContext() != null) {
+            folderListFragment.selectFolder(getCurrentListContext().folder);
+        }
     }
 
     @Override
@@ -132,6 +136,14 @@
     }
 
     @Override
+    public void onFolderChanged(Folder folder) {
+        super.onFolderChanged(folder);
+        if (mFolderListFragment != null) {
+            mFolderListFragment.selectFolder(folder);
+        }
+    }
+
+    @Override
     public void onViewModeChanged(int newMode) {
         super.onViewModeChanged(newMode);
         if (newMode != ViewMode.CONVERSATION) {