1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.widget;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.ContentObserver;
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.Cursor;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.DataSetObserver;
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler;
24c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.util.Log;
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View;
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.ViewGroup;
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.BaseAdapter;
29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.Filter;
30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.FilterQueryProvider;
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.Filterable;
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
33c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport static android.support.annotation.RestrictTo.Scope.GROUP_ID;
34c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.widget.CursorAdapter}.
37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation.  See the framework SDK
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic abstract class CursorAdapter extends BaseAdapter implements Filterable,
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        CursorFilter.CursorFilterClient {
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
48c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected boolean mDataValid;
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
54c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected boolean mAutoRequery;
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
60c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected Cursor mCursor;
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
66c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected Context mContext;
68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
72c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected int mRowIDColumn;
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
78c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected ChangeObserver mChangeObserver;
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
84c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected DataSetObserver mDataSetObserver;
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
90c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected CursorFilter mCursorFilter;
92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
95cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
96c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viverette    @RestrictTo(GROUP_ID)
97cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected FilterQueryProvider mFilterQueryProvider;
98cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If set the adapter will call requery() on the cursor whenever a content change
101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * notification is delivered. Implies {@link #FLAG_REGISTER_CONTENT_OBSERVER}.
102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @deprecated This option is discouraged, as it results in Cursor queries
104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * being performed on the application's UI thread and thus can cause poor
105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * responsiveness or even Application Not Responding errors.  As an alternative,
106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Deprecated
109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int FLAG_AUTO_REQUERY = 0x01;
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If set the adapter will register a content observer on the cursor and will call
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #onContentChanged()} when a notification comes in.  Be careful when
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * using this flag: you will need to unset the current Cursor from the adapter
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to avoid leaks due to its registered observers.  This flag is not needed
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * when using a CursorAdapter with a
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link android.content.CursorLoader}.
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static final int FLAG_REGISTER_CONTENT_OBSERVER = 0x02;
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Constructor that always enables auto-requery.
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @deprecated This option is discouraged, as it results in Cursor queries
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * being performed on the application's UI thread and thus can cause poor
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * responsiveness or even Application Not Responding errors.  As an alternative,
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The cursor from which to get the data.
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context The context
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Deprecated
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CursorAdapter(Context context, Cursor c) {
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        init(context, c, FLAG_AUTO_REQUERY);
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Constructor that allows control over auto-requery.  It is recommended
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * you not use this, but instead {@link #CursorAdapter(Context, Cursor, int)}.
140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * When using this constructor, {@link #FLAG_REGISTER_CONTENT_OBSERVER}
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * will always be set.
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The cursor from which to get the data.
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context The context
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param autoRequery If true the adapter will call requery() on the
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *                    cursor whenever it changes so the most recent
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *                    data is always displayed.  Using true here is discouraged.
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CursorAdapter(Context context, Cursor c, boolean autoRequery) {
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        init(context, c, autoRequery ? FLAG_AUTO_REQUERY : FLAG_REGISTER_CONTENT_OBSERVER);
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Recommended constructor.
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The cursor from which to get the data.
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context The context
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param flags Flags used to determine the behavior of the adapter; may
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * be any combination of {@link #FLAG_AUTO_REQUERY} and
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #FLAG_REGISTER_CONTENT_OBSERVER}.
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CursorAdapter(Context context, Cursor c, int flags) {
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        init(context, c, flags);
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @deprecated Don't use this, use the normal constructor.  This will
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * be removed in the future.
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Deprecated
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void init(Context context, Cursor c, boolean autoRequery) {
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        init(context, c, autoRequery ? FLAG_AUTO_REQUERY : FLAG_REGISTER_CONTENT_OBSERVER);
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    void init(Context context, Cursor c, int flags) {
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if ((flags & FLAG_AUTO_REQUERY) == FLAG_AUTO_REQUERY) {
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            flags |= FLAG_REGISTER_CONTENT_OBSERVER;
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAutoRequery = true;
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mAutoRequery = false;
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean cursorPresent = c != null;
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCursor = c;
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mDataValid = cursorPresent;
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mContext = context;
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if ((flags & FLAG_REGISTER_CONTENT_OBSERVER) == FLAG_REGISTER_CONTENT_OBSERVER) {
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mChangeObserver = new ChangeObserver();
189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataSetObserver = new MyDataSetObserver();
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mChangeObserver = null;
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataSetObserver = null;
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (cursorPresent) {
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mChangeObserver != null) c.registerContentObserver(mChangeObserver);
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mDataSetObserver != null) c.registerDataSetObserver(mDataSetObserver);
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the cursor.
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return the cursor.
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
20515375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Cursor getCursor() {
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCursor;
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.ListAdapter#getCount()
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
21315375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getCount() {
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDataValid && mCursor != null) {
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return mCursor.getCount();
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return 0;
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2214d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.ListAdapter#getItem(int)
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
22515375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Object getItem(int position) {
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDataValid && mCursor != null) {
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mCursor.moveToPosition(position);
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return mCursor;
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.ListAdapter#getItemId(int)
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
23815375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public long getItemId(int position) {
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDataValid && mCursor != null) {
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mCursor.moveToPosition(position)) {
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return mCursor.getLong(mRowIDColumn);
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                return 0;
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return 0;
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2504d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean hasStableIds() {
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return true;
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
25915375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public View getView(int position, View convertView, ViewGroup parent) {
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mDataValid) {
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("this should only be called when the cursor is valid");
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (!mCursor.moveToPosition(position)) {
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("couldn't move cursor to position " + position);
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        View v;
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (convertView == null) {
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v = newView(mContext, mCursor, parent);
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v = convertView;
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        bindView(v, mContext, mCursor);
274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return v;
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public View getDropDownView(int position, View convertView, ViewGroup parent) {
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mDataValid) {
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mCursor.moveToPosition(position);
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            View v;
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (convertView == null) {
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                v = newDropDownView(mContext, mCursor, parent);
284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            } else {
285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                v = convertView;
286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            bindView(v, mContext, mCursor);
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return v;
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
2934d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Makes a new view to hold the data pointed to by cursor.
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context Interface to application's global information
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor The cursor from which to get the data. The cursor is already
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * moved to the correct position.
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param parent The parent to which the new view is attached to
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return the newly created view.
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract View newView(Context context, Cursor cursor, ViewGroup parent);
303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Makes a new drop down view to hold the data pointed to by cursor.
306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context Interface to application's global information
307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor The cursor from which to get the data. The cursor is already
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * moved to the correct position.
309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param parent The parent to which the new view is attached to
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return the newly created view.
311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return newView(context, cursor, parent);
314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Bind an existing view to the data pointed to by cursor
318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param view Existing view, returned earlier by newView
319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context Interface to application's global information
320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor The cursor from which to get the data. The cursor is already
321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * moved to the correct position.
322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public abstract void bindView(View view, Context context, Cursor cursor);
3244d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas
325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * closed.
3284d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas     *
329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor The new cursor to be used
330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
33115375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void changeCursor(Cursor cursor) {
333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        Cursor old = swapCursor(cursor);
334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (old != null) {
335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            old.close();
336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Swap in a new Cursor, returning the old Cursor.  Unlike
341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * closed.
343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param newCursor The new cursor to be used.
345c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas     * @return Returns the previously set Cursor, or null if there was not one.
346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If the given new Cursor is the same instance is the previously set
347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Cursor, null is also returned.
348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Cursor swapCursor(Cursor newCursor) {
350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (newCursor == mCursor) {
351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return null;
352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        Cursor oldCursor = mCursor;
354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (oldCursor != null) {
355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver);
356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver);
357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCursor = newCursor;
359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (newCursor != null) {
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver);
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver);
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mRowIDColumn = newCursor.getColumnIndexOrThrow("_id");
363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataValid = true;
364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // notify the observers about the new cursor
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            notifyDataSetChanged();
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mRowIDColumn = -1;
368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataValid = false;
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // notify the observers about the lack of a data set
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            notifyDataSetInvalidated();
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return oldCursor;
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Converts the cursor into a CharSequence. Subclasses should override this
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * method to convert their results. The default implementation returns an
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * empty String for null values or the default String representation of
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the value.</p>
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor the cursor to convert to a CharSequence
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a CharSequence representing the value
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
38415375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CharSequence convertToString(Cursor cursor) {
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return cursor == null ? "" : cursor.toString();
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Runs a query with the specified constraint. This query is requested
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * by the filter attached to this adapter.
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The query is provided by a
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link android.widget.FilterQueryProvider}.
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If no provider is specified, the current cursor is not filtered and returned.
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * After this method returns the resulting cursor is passed to {@link #changeCursor(Cursor)}
398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * and the previous cursor is closed.
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This method is always executed on a background thread, not on the
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * application's main thread (or UI thread.)
4024d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas     *
403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Contract: when constraint is null or empty, the original results,
404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * prior to any filtering, must be returned.
405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param constraint the constraint with which the query must be filtered
407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a Cursor representing the results of the new query
409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getFilter()
411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getFilterQueryProvider()
412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setFilterQueryProvider(android.widget.FilterQueryProvider)
413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
41415375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mFilterQueryProvider != null) {
417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return mFilterQueryProvider.runQuery(constraint);
418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCursor;
421cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
42315375aa6fd54b036f97f99229aefab2822c8a1c9Aurimas Liutikas    @Override
424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Filter getFilter() {
425cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCursorFilter == null) {
426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mCursorFilter = new CursorFilter(this);
427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCursorFilter;
429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
431cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
432cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the query filter provider used for filtering. When the
433cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * provider is null, no filtering occurs.
434cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return the current filter query provider or null if it does not exist
436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setFilterQueryProvider(android.widget.FilterQueryProvider)
438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #runQueryOnBackgroundThread(CharSequence)
439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
440cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public FilterQueryProvider getFilterQueryProvider() {
441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mFilterQueryProvider;
442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
445cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sets the query filter provider used to filter the current Cursor.
446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The provider's
447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link android.widget.FilterQueryProvider#runQuery(CharSequence)}
448cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * method is invoked when filtering is requested by a client of
449cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * this adapter.
450cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
451cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param filterQueryProvider the filter query provider or null to remove it
452cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
453cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getFilterQueryProvider()
454cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #runQueryOnBackgroundThread(CharSequence)
455cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
456cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setFilterQueryProvider(FilterQueryProvider filterQueryProvider) {
457cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mFilterQueryProvider = filterQueryProvider;
458cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
459cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
460cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
461cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called when the {@link ContentObserver} on the cursor receives a change notification.
462cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The default implementation provides the auto-requery logic, but may be overridden by
463cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * sub classes.
4644d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas     *
465cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see ContentObserver#onChange(boolean)
466cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
467cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void onContentChanged() {
468cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
4694f55b122ba30dac3af0ebc9a48f1a43c55025d42Joe Onorato            if (false) Log.v("Cursor", "Auto requerying " + mCursor + " due to update");
470cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataValid = mCursor.requery();
471cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
472cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private class ChangeObserver extends ContentObserver {
4754d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas        ChangeObserver() {
476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            super(new Handler());
477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
478cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public boolean deliverSelfNotifications() {
481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return true;
482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onChange(boolean selfChange) {
486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            onContentChanged();
487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private class MyDataSetObserver extends DataSetObserver {
491540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas        MyDataSetObserver() {
492540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas        }
493540222c3675801eaa141ace1c164c4d3499b4721Aurimas Liutikas
494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onChanged() {
496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataValid = true;
497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            notifyDataSetChanged();
498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onInvalidated() {
502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mDataValid = false;
503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            notifyDataSetInvalidated();
504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
508