Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2010 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.contacts.list;
     17 
     18 import android.content.Context;
     19 import android.content.CursorLoader;
     20 import android.database.Cursor;
     21 import android.database.MatrixCursor;
     22 import android.net.Uri;
     23 
     24 /**
     25  * A specialized loader for the Join Contacts UI.  It executes two queries:
     26  * join suggestions and (optionally) the full contact list.
     27  */
     28 public class JoinContactLoader extends CursorLoader {
     29 
     30     private String[] mProjection;
     31     private Uri mSuggestionUri;
     32     private MatrixCursor mSuggestionsCursor;
     33 
     34     public JoinContactLoader(Context context) {
     35         super(context, null, null, null, null, null);
     36     }
     37 
     38     public void setSuggestionUri(Uri uri) {
     39         this.mSuggestionUri = uri;
     40     }
     41 
     42     @Override
     43     public void setProjection(String[] projection) {
     44         super.setProjection(projection);
     45         this.mProjection = projection;
     46     }
     47 
     48     public Cursor getSuggestionsCursor() {
     49         return mSuggestionsCursor;
     50     }
     51 
     52     @Override
     53     public Cursor loadInBackground() {
     54         // First execute the suggestions query, then call super.loadInBackground
     55         // to load the entire list
     56         mSuggestionsCursor = loadSuggestions();
     57         return super.loadInBackground();
     58     }
     59 
     60     /**
     61      * Loads join suggestions into a MatrixCursor.
     62      */
     63     private MatrixCursor loadSuggestions() {
     64         Cursor cursor = getContext().getContentResolver().query(mSuggestionUri, mProjection,
     65                 null, null, null);
     66         try {
     67             MatrixCursor matrix = new MatrixCursor(mProjection);
     68             Object[] row = new Object[mProjection.length];
     69             while (cursor.moveToNext()) {
     70                 for (int i = 0; i < row.length; i++) {
     71                     row[i] = cursor.getString(i);
     72                 }
     73                 matrix.addRow(row);
     74             }
     75             return matrix;
     76         } finally {
     77             cursor.close();
     78         }
     79     }
     80 }