Home | History | Annotate | Download | only in providers
      1 /**
      2  * Copyright (c) 2012, Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.mail.providers;
     18 
     19 import com.android.mail.utils.LogTag;
     20 import com.android.mail.utils.LogUtils;
     21 
     22 import android.os.Parcel;
     23 import android.os.Parcelable;
     24 
     25 import org.json.JSONException;
     26 import org.json.JSONObject;
     27 
     28 public class ListParams implements Parcelable {
     29     private static final String LIMIT_KEY = "limit";
     30     private static final String USE_NETWORK_KEY = "use-network";
     31 
     32     public static final int NO_LIMIT = -1;
     33 
     34     private static final String LOG_TAG = LogTag.getLogTag();
     35 
     36     // The maximum number of results to be created by this search
     37     public final int mLimit;
     38 
     39     public final boolean mUseNetwork;
     40 
     41     public ListParams(int limit, boolean useNetwork) {
     42         mLimit = limit;
     43         mUseNetwork = useNetwork;
     44     }
     45 
     46     /**
     47      * Supports Parcelable
     48      */
     49     public static final Parcelable.Creator<ListParams> CREATOR
     50         = new Parcelable.Creator<ListParams>() {
     51         @Override
     52         public ListParams createFromParcel(Parcel in) {
     53             return new ListParams(in);
     54         }
     55 
     56         @Override
     57         public ListParams[] newArray(int size) {
     58             return new ListParams[size];
     59         }
     60     };
     61 
     62     /**
     63      * Supports Parcelable
     64      */
     65     @Override
     66     public void writeToParcel(Parcel dest, int flags) {
     67         dest.writeInt(mLimit);
     68         dest.writeInt(mUseNetwork ? 1 : 0);
     69     }
     70 
     71     /**
     72      * Supports Parcelable
     73      */
     74     @Override
     75     public int describeContents() {
     76         return 0;
     77     }
     78 
     79     /**
     80      * Supports Parcelable
     81      */
     82     public ListParams(Parcel in) {
     83         mLimit = in.readInt();
     84         mUseNetwork = in.readInt() != 0;
     85     }
     86 
     87     /**
     88      * Return a serialized String for this ListParams.
     89      */
     90     public synchronized String serialize() {
     91         JSONObject json = new JSONObject();
     92         try {
     93             json.put(LIMIT_KEY, mLimit);
     94             json.put(USE_NETWORK_KEY, mUseNetwork);
     95         } catch (JSONException e) {
     96             LogUtils.wtf(LOG_TAG, e, "Could not serialize ListParams");
     97         }
     98         return json.toString();
     99     }
    100 
    101     /**
    102      * Create a new instance of an ListParams object using a serialized instance created previously
    103      * using {@link #serialize()}. This returns null if the serialized instance was invalid or does
    104      * not represent a valid parameter object.
    105      *
    106      * @param serializedParams
    107      * @return
    108      */
    109     public static ListParams newinstance(String serializedParams) {
    110         // This method is a wrapper to check for errors and exceptions and return back a null
    111         // in cases something breaks.
    112         JSONObject json = null;
    113         try {
    114             json = new JSONObject(serializedParams);
    115             final int limit = json.getInt(LIMIT_KEY);
    116             final boolean useNetwork = json.getBoolean(USE_NETWORK_KEY);
    117             return new ListParams(limit, useNetwork);
    118         } catch (JSONException e) {
    119             LogUtils.wtf(LOG_TAG, e, "Could not create an params object from this input: \""
    120                     + serializedParams);
    121             return null;
    122         }
    123     }
    124 
    125 
    126 
    127 }