blob: 2223e9c1e4f3cdb02fc975e737a4a04e4fbdc25a [file] [log] [blame]
Jeff Sharkey3f177592009-05-18 15:23:12 -07001package com.android.contacts;
2
3import android.content.AsyncQueryHandler;
4import android.content.Context;
5import android.database.Cursor;
6
7import java.lang.ref.WeakReference;
8
9/**
10 * Slightly more abstract {@link android.content.AsyncQueryHandler} that helps
11 * keep a {@link WeakReference} back to a callback interface. Will properly
12 * close the completed query if the listener ceases to exist.
13 * <p>
14 * Using this pattern will help keep you from leaking a {@link Context}.
15 */
16public class NotifyingAsyncQueryHandler extends AsyncQueryHandler {
17 private final WeakReference<QueryCompleteListener> mListener;
18
19 /**
20 * Interface to listen for completed queries.
21 */
22 public static interface QueryCompleteListener {
23 public void onQueryComplete(int token, Object cookie, Cursor cursor);
24 }
25
26 public NotifyingAsyncQueryHandler(Context context, QueryCompleteListener listener) {
27 super(context.getContentResolver());
28 mListener = new WeakReference<QueryCompleteListener>(listener);
29 }
30
31 /** {@inheritDoc} */
32 @Override
33 protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
34 final QueryCompleteListener listener = mListener.get();
35 if (listener != null) {
36 listener.onQueryComplete(token, cookie, cursor);
37 } else {
38 cursor.close();
39 }
40 }
41}