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 boolean mLoadSuggestionsAndAllContacts; 31 private String[] mProjection; 32 private Uri mSuggestionUri; 33 private MatrixCursor mSuggestionsCursor; 34 35 public JoinContactLoader(Context context) { 36 super(context, null, null, null, null, null); 37 } 38 39 public void setLoadSuggestionsAndAllContacts(boolean flag) { 40 mLoadSuggestionsAndAllContacts = flag; 41 } 42 43 public void setSuggestionUri(Uri uri) { 44 this.mSuggestionUri = uri; 45 } 46 47 @Override 48 public void setProjection(String[] projection) { 49 super.setProjection(projection); 50 this.mProjection = projection; 51 } 52 53 public Cursor getSuggestionsCursor() { 54 return mSuggestionsCursor; 55 } 56 57 @Override 58 public Cursor loadInBackground() { 59 // First execute the suggestions query, then call super.loadInBackground 60 // to load the entire list 61 mSuggestionsCursor = loadSuggestions(); 62 if (!mLoadSuggestionsAndAllContacts && mSuggestionsCursor.getCount() != 0) { 63 // In case we only need suggestions, send "0" as the search query, which 64 // will always return an empty cursor (but we can still register to 65 // listen for changes on it). 66 setSelection("0"); 67 setSelectionArgs(null); 68 } 69 return super.loadInBackground(); 70 } 71 72 /** 73 * Loads join suggestions into a MatrixCursor. 74 */ 75 private MatrixCursor loadSuggestions() { 76 Cursor cursor = getContext().getContentResolver().query(mSuggestionUri, mProjection, 77 null, null, null); 78 try { 79 MatrixCursor matrix = new MatrixCursor(mProjection); 80 Object[] row = new Object[mProjection.length]; 81 while (cursor.moveToNext()) { 82 for (int i = 0; i < row.length; i++) { 83 row[i] = cursor.getString(i); 84 } 85 matrix.addRow(row); 86 } 87 return matrix; 88 } finally { 89 cursor.close(); 90 } 91 } 92 }