1bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng/* 2bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * Copyright (C) 2012 The Android Open Source Project 3bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * 4bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License"); 5bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * you may not use this file except in compliance with the License. 6bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * You may obtain a copy of the License at 7bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * 8bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * http://www.apache.org/licenses/LICENSE-2.0 9bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * 10bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * Unless required by applicable law or agreed to in writing, software 11bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS, 12bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * See the License for the specific language governing permissions and 14bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * limitations under the License 15bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng */ 16bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 17bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengpackage com.android.contacts.common.database; 18bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 19bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengimport android.content.AsyncQueryHandler; 20bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengimport android.content.ContentResolver; 21bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengimport android.database.Cursor; 22bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengimport android.net.Uri; 23bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 24bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng/** 25bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * An {@AsyncQueryHandler} that will never return a null cursor. 26bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * <p> 27bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * Instead, will return a {@link Cursor} with 0 records. 28bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng */ 29bf1f360793c466dae12f8bc386161353355f2b28Chiao Chengpublic abstract class NoNullCursorAsyncQueryHandler extends AsyncQueryHandler { 30bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 31bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng public NoNullCursorAsyncQueryHandler(ContentResolver cr) { 32bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng super(cr); 33bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 34bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 35bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng @Override 36bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng public void startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, 37bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng String[] selectionArgs, String orderBy) { 38bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng final CookieWithProjection projectionCookie = new CookieWithProjection(cookie, projection); 39bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng super.startQuery(token, projectionCookie, uri, projection, selection, selectionArgs, 40bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng orderBy); 41bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 42bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 43bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng @Override 44bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng protected final void onQueryComplete(int token, Object cookie, Cursor cursor) { 45bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng CookieWithProjection projectionCookie = (CookieWithProjection) cookie; 46bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 47bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng super.onQueryComplete(token, projectionCookie.originalCookie, cursor); 48bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 49bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng if (cursor == null) { 50bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng cursor = new EmptyCursor(projectionCookie.projection); 51bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 52bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng onNotNullableQueryComplete(token, projectionCookie.originalCookie, cursor); 53bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 54bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 55bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng protected abstract void onNotNullableQueryComplete(int token, Object cookie, Cursor cursor); 56bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 57bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng /** 58bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng * Class to add projection to an existing cookie. 59bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng */ 60bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng private static class CookieWithProjection { 61bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng public final Object originalCookie; 62bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng public final String[] projection; 63bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng 64bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng public CookieWithProjection(Object cookie, String[] projection) { 65bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng this.originalCookie = cookie; 66bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng this.projection = projection; 67bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 68bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng } 69bf1f360793c466dae12f8bc386161353355f2b28Chiao Cheng} 70