1/* 2 * Copyright (C) 2011 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 */ 16package com.android.contacts.list; 17 18import com.google.android.collect.Lists; 19 20import android.content.Context; 21import android.content.CursorLoader; 22import android.database.Cursor; 23import android.database.MatrixCursor; 24import android.database.MergeCursor; 25import android.os.Bundle; 26import android.provider.ContactsContract.Profile; 27 28import java.util.List; 29 30/** 31 * A loader for use in the default contact list, which will also query for the user's profile 32 * if configured to do so. 33 */ 34public class ProfileAndContactsLoader extends CursorLoader { 35 36 private boolean mLoadProfile; 37 private String[] mProjection; 38 39 public ProfileAndContactsLoader(Context context) { 40 super(context); 41 } 42 43 public void setLoadProfile(boolean flag) { 44 mLoadProfile = flag; 45 } 46 47 public void setProjection(String[] projection) { 48 super.setProjection(projection); 49 mProjection = projection; 50 } 51 52 @Override 53 public Cursor loadInBackground() { 54 // First load the profile, if enabled. 55 List<Cursor> cursors = Lists.newArrayList(); 56 if (mLoadProfile) { 57 cursors.add(loadProfile()); 58 } 59 final Cursor contactsCursor = super.loadInBackground(); 60 cursors.add(contactsCursor); 61 return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) { 62 @Override 63 public Bundle getExtras() { 64 // Need to get the extras from the contacts cursor. 65 return contactsCursor.getExtras(); 66 } 67 }; 68 } 69 70 /** 71 * Loads the profile into a MatrixCursor. 72 */ 73 private MatrixCursor loadProfile() { 74 Cursor cursor = getContext().getContentResolver().query(Profile.CONTENT_URI, mProjection, 75 null, null, null); 76 try { 77 MatrixCursor matrix = new MatrixCursor(mProjection); 78 Object[] row = new Object[mProjection.length]; 79 while (cursor.moveToNext()) { 80 for (int i = 0; i < row.length; i++) { 81 row[i] = cursor.getString(i); 82 } 83 matrix.addRow(row); 84 } 85 return matrix; 86 } finally { 87 cursor.close(); 88 } 89 } 90} 91