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