Home | History | Annotate | Download | only in list
      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  */
     16 package com.android.contacts.common.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.database.MergeCursor;
     23 import android.os.Bundle;
     24 import android.provider.ContactsContract.Profile;
     25 
     26 import com.google.common.collect.Lists;
     27 
     28 import 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  */
     34 public 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         // ContactsCursor.loadInBackground() can return null; MergeCursor
     60         // correctly handles null cursors.
     61         Cursor cursor = null;
     62         try {
     63             cursor = super.loadInBackground();
     64         } catch (NullPointerException | SecurityException e) {
     65             // Ignore NPEs and SecurityExceptions thrown by providers
     66         }
     67         final Cursor contactsCursor = cursor;
     68         cursors.add(contactsCursor);
     69         return new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) {
     70             @Override
     71             public Bundle getExtras() {
     72                 // Need to get the extras from the contacts cursor.
     73                 return contactsCursor == null ? new Bundle() : contactsCursor.getExtras();
     74             }
     75         };
     76     }
     77 
     78     /**
     79      * Loads the profile into a MatrixCursor. On failure returns null, which
     80      * matches the behavior of CursorLoader.loadInBackground().
     81      *
     82      * @return MatrixCursor containing profile or null on query failure.
     83      */
     84     private MatrixCursor loadProfile() {
     85         Cursor cursor = getContext().getContentResolver().query(Profile.CONTENT_URI, mProjection,
     86                 null, null, null);
     87         if (cursor == null) {
     88             return null;
     89         }
     90         try {
     91             MatrixCursor matrix = new MatrixCursor(mProjection);
     92             Object[] row = new Object[mProjection.length];
     93             while (cursor.moveToNext()) {
     94                 for (int i = 0; i < row.length; i++) {
     95                     row[i] = cursor.getString(i);
     96                 }
     97                 matrix.addRow(row);
     98             }
     99             return matrix;
    100         } finally {
    101             cursor.close();
    102         }
    103     }
    104 }
    105