Home | History | Annotate | Download | only in datamodel
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      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 package com.android.messaging.datamodel;
     17 
     18 import android.content.Context;
     19 import android.database.Cursor;
     20 import android.net.Uri;
     21 
     22 import com.android.messaging.util.Assert;
     23 import com.google.common.annotations.VisibleForTesting;
     24 
     25 /**
     26  * Holds parameters and data (such as content URI) for performing queries on the content provider.
     27  * This class could then be used to perform a query using either a BoundCursorLoader or querying
     28  * on the content resolver directly.
     29  *
     30  * This class is used for cases where the way to load a cursor is not fixed. For example,
     31  * when using ContactUtil to query for phone numbers, the ContactPickerFragment wants to use
     32  * a CursorLoader to asynchronously load the data and tie in nicely with its data binding
     33  * paradigm, whereas ContactRecipientAdapter wants to synchronously perform the query on the
     34  * worker thread.
     35  */
     36 public class CursorQueryData {
     37     protected final Uri mUri;
     38     protected final String[] mProjection;
     39     protected final String mSelection;
     40     protected final String[] mSelectionArgs;
     41     protected final String mSortOrder;
     42     protected final Context mContext;
     43 
     44     public CursorQueryData(final Context context, final Uri uri, final String[] projection,
     45             final String selection, final String[] selectionArgs, final String sortOrder) {
     46         mContext = context;
     47         mUri = uri;
     48         mProjection = projection;
     49         mSelection = selection;
     50         mSelectionArgs = selectionArgs;
     51         mSortOrder = sortOrder;
     52     }
     53 
     54     public BoundCursorLoader createBoundCursorLoader(final String bindingId) {
     55         return new BoundCursorLoader(bindingId, mContext, mUri, mProjection, mSelection,
     56                 mSelectionArgs, mSortOrder);
     57     }
     58 
     59     public Cursor performSynchronousQuery() {
     60         Assert.isNotMainThread();
     61         if (mUri == null) {
     62             // See {@link #getEmptyQueryData}
     63             return null;
     64         } else {
     65             return mContext.getContentResolver().query(mUri, mProjection, mSelection,
     66                     mSelectionArgs, mSortOrder);
     67         }
     68     }
     69 
     70     @VisibleForTesting
     71     public Uri getUri() {
     72         return mUri;
     73     }
     74 
     75     /**
     76      * Representation of an invalid query. {@link #performSynchronousQuery} will return
     77      * a null Cursor.
     78      */
     79     public static CursorQueryData getEmptyQueryData() {
     80         return new CursorQueryData(null, null, null, null, null, null);
     81     }
     82 }
     83