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.CursorWrapper;
     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  * This loader also loads the "suggestion" cursor, which can be accessed with:
     29  * {@code ((JoinContactLoaderResult) result).suggestionCursor }
     30  */
     31 public class JoinContactLoader extends CursorLoader {
     32 
     33     private String[] mProjection;
     34     private Uri mSuggestionUri;
     35 
     36     /**
     37      * Actual returned class.  It's guaranteed that this loader always returns an instance of this
     38      * class.  This class is needed to tie the lifecycle of the second cursor to that of the
     39      * primary one.
     40      *
     41      * Note we can't change the result type of this loader itself, because CursorLoader
     42      * extends AsyncTaskLoader<Cursor>, not AsyncTaskLoader<? extends Cursor>
     43      */
     44     public static class JoinContactLoaderResult extends CursorWrapper {
     45         public final Cursor suggestionCursor;
     46 
     47         public JoinContactLoaderResult(Cursor baseCursor, Cursor suggestionCursor) {
     48             super(baseCursor);
     49             this.suggestionCursor = suggestionCursor;
     50         }
     51 
     52         @Override
     53         public void close() {
     54             try {
     55                 suggestionCursor.close();
     56             } finally {
     57                 super.close();
     58             }
     59         }
     60     }
     61 
     62     public JoinContactLoader(Context context) {
     63         super(context, null, null, null, null, null);
     64     }
     65 
     66     public void setSuggestionUri(Uri uri) {
     67         this.mSuggestionUri = uri;
     68     }
     69 
     70     @Override
     71     public void setProjection(String[] projection) {
     72         super.setProjection(projection);
     73         this.mProjection = projection;
     74     }
     75 
     76     @Override
     77     public Cursor loadInBackground() {
     78         // First execute the suggestions query, then call super.loadInBackground
     79         // to load the entire list
     80         final Cursor suggestionsCursor = getContext().getContentResolver()
     81                 .query(mSuggestionUri, mProjection, null, null, null);
     82         return new JoinContactLoaderResult(super.loadInBackground(), suggestionsCursor);
     83     }
     84 }