13f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey/*
23f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Copyright (C) 2009 The Android Open Source Project
33f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
43f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
53f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * you may not use this file except in compliance with the License.
63f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * You may obtain a copy of the License at
73f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
83f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
93f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
103f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
113f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
123f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * See the License for the specific language governing permissions and
143f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * limitations under the License.
153f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey */
163f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey
173f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkeypackage com.android.contacts.util;
183f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
193f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkeyimport android.content.AsyncQueryHandler;
203f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkeyimport android.content.Context;
213f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkeyimport android.database.Cursor;
223f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
233f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkeyimport java.lang.ref.WeakReference;
243f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
253f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey/**
263f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * Slightly more abstract {@link AsyncQueryHandler} that helps keep a
273f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * {@link WeakReference} back to a listener. Will properly close any
288579e25faeedbb1d00ee295da4c74e45bd782573Fred Quintana * {@link Cursor} if the listener ceases to exist.
293f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey * <p>
303f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * This pattern can be used to perform background queries without leaking
313f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * {@link Context} objects.
323f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey *
333f0b7b87cd41b5a9cd631b6fcf29ea5025905e18Jeff Sharkey * @hide pending API council review
343f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey */
353f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkeypublic class NotifyingAsyncQueryHandler extends AsyncQueryHandler {
365f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    private WeakReference<AsyncQueryListener> mListener;
373f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
383f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    /**
395f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar     * Interface to listen for completed query operations.
403f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey     */
415f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    public interface AsyncQueryListener {
425f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar        void onQueryComplete(int token, Object cookie, Cursor cursor);
433f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    }
443f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
455f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    public NotifyingAsyncQueryHandler(Context context, AsyncQueryListener listener) {
463f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey        super(context.getContentResolver());
475f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar        setQueryListener(listener);
485f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    }
495f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar
505f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    /**
515f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar     * Assign the given {@link AsyncQueryListener} to receive query events from
525f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar     * asynchronous calls. Will replace any existing listener.
535f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar     */
545f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar    public void setQueryListener(AsyncQueryListener listener) {
555f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar        mListener = new WeakReference<AsyncQueryListener>(listener);
563f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    }
573f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey
583f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    /** {@inheritDoc} */
593f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    @Override
603f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
615f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar        final AsyncQueryListener listener = mListener.get();
623f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey        if (listener != null) {
633f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey            listener.onQueryComplete(token, cookie, cursor);
645f4af705cef8c914d5875983900e5cf5a5524b68Evan Millar        } else if (cursor != null) {
653f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey            cursor.close();
663f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey        }
673f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey    }
683f17759408b4cec77ab43ee76cc75805577ca34eJeff Sharkey}
69