1 /* 2 * Copyright (C) 2009 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 17 package com.android.contacts.util; 18 19 import android.content.AsyncQueryHandler; 20 import android.content.Context; 21 import android.content.EntityIterator; 22 import android.database.Cursor; 23 24 import java.lang.ref.WeakReference; 25 26 /** 27 * Slightly more abstract {@link AsyncQueryHandler} that helps keep a 28 * {@link WeakReference} back to a listener. Will properly close any 29 * {@link Cursor} or {@link EntityIterator} if the listener ceases to exist. 30 * <p> 31 * This pattern can be used to perform background queries without leaking 32 * {@link Context} objects. 33 * 34 * @hide pending API council review 35 */ 36 public class NotifyingAsyncQueryHandler extends AsyncQueryHandler { 37 private WeakReference<AsyncQueryListener> mListener; 38 39 /** 40 * Interface to listen for completed query operations. 41 */ 42 public interface AsyncQueryListener { onQueryComplete(int token, Object cookie, Cursor cursor)43 void onQueryComplete(int token, Object cookie, Cursor cursor); onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator)44 void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator); 45 } 46 NotifyingAsyncQueryHandler(Context context, AsyncQueryListener listener)47 public NotifyingAsyncQueryHandler(Context context, AsyncQueryListener listener) { 48 super(context.getContentResolver()); 49 setQueryListener(listener); 50 } 51 52 /** 53 * Assign the given {@link AsyncQueryListener} to receive query events from 54 * asynchronous calls. Will replace any existing listener. 55 */ setQueryListener(AsyncQueryListener listener)56 public void setQueryListener(AsyncQueryListener listener) { 57 mListener = new WeakReference<AsyncQueryListener>(listener); 58 } 59 60 /** {@inheritDoc} */ 61 @Override onQueryComplete(int token, Object cookie, Cursor cursor)62 protected void onQueryComplete(int token, Object cookie, Cursor cursor) { 63 final AsyncQueryListener listener = mListener.get(); 64 if (listener != null) { 65 listener.onQueryComplete(token, cookie, cursor); 66 } else if (cursor != null) { 67 cursor.close(); 68 } 69 } 70 71 /** {@inheritDoc} */ 72 @Override onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator)73 protected void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) { 74 final AsyncQueryListener listener = mListener.get(); 75 if (listener != null) { 76 listener.onQueryEntitiesComplete(token, cookie, iterator); 77 } else if (iterator != null) { 78 iterator.close(); 79 } 80 } 81 } 82