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.content;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.ContentObserver;
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.os.Handler;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.support.v4.util.DebugUtils;
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.FileDescriptor;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport java.io.PrintWriter;
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.content.Loader}.
29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation.  See the framework SDK
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class Loader<D> {
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    int mId;
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    OnLoadCompleteListener<D> mListener;
37220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    OnLoadCanceledListener<D> mOnLoadCanceledListener;
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    Context mContext;
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mStarted = false;
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mAbandoned = false;
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mReset = true;
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    boolean mContentChanged = false;
4380a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    boolean mProcessingChange = false;
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
457dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn    /**
467dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * An implementation of a ContentObserver that takes care of connecting
477dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * it to the Loader to have the loader re-load its data when the observer
487dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * is told it has changed.  You do not normally need to use this yourself;
49220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * it is used for you by {@link CursorLoader} to take care of executing
50220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * an update when the cursor's backing data changes.
517dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     */
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public final class ForceLoadContentObserver extends ContentObserver {
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public ForceLoadContentObserver() {
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            super(new Handler());
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public boolean deliverSelfNotifications() {
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return true;
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        @Override
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onChange(boolean selfChange) {
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            onContentChanged();
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
687dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn    /**
697dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * Interface that is implemented to discover when a Loader has finished
707dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * loading its data.  You do not normally need to implement this yourself;
717dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * it is used in the implementation of {@link android.support.v4.app.LoaderManager}
727dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * to find out when a Loader it is managing has completed so that this can
737dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * be reported to its client.  This interface should only be used if a
747dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     * Loader is not being used in conjunction with LoaderManager.
757dc96cc2410f551eefaa973ddc144146ad72d1ecDianne Hackborn     */
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public interface OnLoadCompleteListener<D> {
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Called on the thread that created the Loader when the load is complete.
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param loader the loader that completed the load
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param data the result of the load
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        public void onLoadComplete(Loader<D> loader, D data);
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
87220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Interface that is implemented to discover when a Loader has been canceled
88220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * before it finished loading its data.  You do not normally need to implement
89220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * this yourself; it is used in the implementation of {@link android.support.v4.app.LoaderManager}
90220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * to find out when a Loader it is managing has been canceled so that it
91220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * can schedule the next Loader.  This interface should only be used if a
92220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Loader is not being used in conjunction with LoaderManager.
93220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
94220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    public interface OnLoadCanceledListener<D> {
95220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        /**
96220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown         * Called on the thread that created the Loader when the load is canceled.
97220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown         *
98220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown         * @param loader the loader that canceled the load
99220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown         */
100220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        public void onLoadCanceled(Loader<D> loader);
101220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
102220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
103220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
104220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Stores away the application context associated with context.
105220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Since Loaders can be used across multiple activities it's dangerous to
106220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * store the context directly; always use {@link #getContext()} to retrieve
107220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * the Loader's Context, don't use the constructor argument directly.
108220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * The Context returned by {@link #getContext} is safe to use across
109220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Activity instances.
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context used to retrieve the application context.
112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Loader(Context context) {
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mContext = context.getApplicationContext();
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sends the result of the load to the registered listener. Should only be called by subclasses.
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Must be called from the process's main thread.
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param data the result of the load
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void deliverResult(D data) {
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mListener != null) {
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mListener.onLoadComplete(this, data);
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
131220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Informs the registered {@link OnLoadCanceledListener} that the load has been canceled.
132220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Should only be called by subclasses.
133220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
134220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Must be called from the process's main thread.
135220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
136220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    public void deliverCancellation() {
137220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        if (mOnLoadCanceledListener != null) {
138220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown            mOnLoadCanceledListener.onLoadCanceled(this);
139220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        }
140220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
141220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
142220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return an application context retrieved from the Context passed to the constructor.
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Context getContext() {
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mContext;
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return the ID of this loader
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getId() {
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mId;
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Registers a class that will receive callbacks when a load is complete.
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The callback will be called on the process's main thread so it's safe to
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * pass the results to widgets.
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void registerListener(int id, OnLoadCompleteListener<D> listener) {
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mListener != null) {
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("There is already a listener registered");
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mListener = listener;
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mId = id;
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Remove a listener that was previously added with {@link #registerListener}.
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Must be called from the process's main thread.
175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void unregisterListener(OnLoadCompleteListener<D> listener) {
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mListener == null) {
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalStateException("No listener register");
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mListener != listener) {
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            throw new IllegalArgumentException("Attempting to unregister the wrong listener");
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mListener = null;
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
187220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Registers a listener that will receive callbacks when a load is canceled.
188220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * The callback will be called on the process's main thread so it's safe to
189220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * pass the results to widgets.
190220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
191220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Must be called from the process's main thread.
192220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
193220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * @param listener The listener to register.
194220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
195220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    public void registerOnLoadCanceledListener(OnLoadCanceledListener<D> listener) {
196220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        if (mOnLoadCanceledListener != null) {
197220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown            throw new IllegalStateException("There is already a listener registered");
198220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        }
199220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        mOnLoadCanceledListener = listener;
200220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
201220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
202220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
203220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Unregisters a listener that was previously added with
204220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * {@link #registerOnLoadCanceledListener}.
205220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
206220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Must be called from the process's main thread.
207220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
208220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * @param listener The listener to unregister.
209220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
210220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    public void unregisterOnLoadCanceledListener(OnLoadCanceledListener<D> listener) {
211220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        if (mOnLoadCanceledListener == null) {
212220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown            throw new IllegalStateException("No listener register");
213220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        }
214220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        if (mOnLoadCanceledListener != listener) {
215220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown            throw new IllegalArgumentException("Attempting to unregister the wrong listener");
216220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        }
217220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        mOnLoadCanceledListener = null;
218220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
219220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
220220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return whether this load has been started.  That is, its {@link #startLoading()}
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * has been called and no calls to {@link #stopLoading()} or
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #reset()} have yet been made.
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean isStarted() {
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mStarted;
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return whether this loader has been abandoned.  In this state, the
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * loader <em>must not</em> report any new data, and <em>must</em> keep
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * its last reported data valid until it is finally reset.
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean isAbandoned() {
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mAbandoned;
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return whether this load has been reset.  That is, either the loader
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * has not yet been started for the first time, or its {@link #reset()}
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * has been called.
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean isReset() {
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mReset;
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
248220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * This function will normally be called for you automatically by
249220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * {@link android.support.v4.app.LoaderManager} when the associated fragment/activity
250220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * is being started.  When using a Loader with {@link android.support.v4.app.LoaderManager},
251220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * you <em>must not</em> call this method yourself, or you will conflict
252220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * with its management of the Loader.
253220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Starts an asynchronous load of the Loader's data. When the result
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is ready the callbacks will be called on the process's main thread.
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If a previous load has been completed and is still valid
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the result may be passed to the callbacks immediately.
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * The loader will monitor the source of
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the data set and may deliver future callbacks if the source changes.
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Calling {@link #stopLoading} will stop the delivery of callbacks.
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>This updates the Loader's internal state so that
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #isStarted()} and {@link #isReset()} will return the correct
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * values, and then calls the implementation's {@link #onStartLoading()}.
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public final void startLoading() {
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStarted = true;
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mReset = false;
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAbandoned = false;
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        onStartLoading();
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses must implement this to take care of loading their data,
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * as per {@link #startLoading()}.  This is not called by clients directly,
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * but as a result of a call to {@link #startLoading()}.
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void onStartLoading() {
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
284220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Attempt to cancel the current load task.
285220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Must be called on the main thread of the process.
286220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
287220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * <p>Cancellation is not an immediate operation, since the load is performed
288220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * in a background thread.  If there is currently a load in progress, this
289220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * method requests that the load be canceled, and notes this is the case;
290220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * once the background thread has completed its work its remaining state
291220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * will be cleared.  If another load request comes in during this time,
292220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * it will be held until the canceled load is complete.
293220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
294220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * @return Returns <tt>false</tt> if the task could not be canceled,
295220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * typically because it has already completed normally, or
296220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * because {@link #startLoading()} hasn't been called; returns
297220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * <tt>true</tt> otherwise.  When <tt>true</tt> is returned, the task
298220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * is still running and the {@link OnLoadCanceledListener} will be called
299220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * when the task completes.
300220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
301220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    public boolean cancelLoad() {
302220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        return onCancelLoad();
303220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
304220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
305220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
306220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * Subclasses must implement this to take care of requests to {@link #cancelLoad()}.
307220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * This will always be called from the process's main thread.
308220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
309220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * @return Returns <tt>false</tt> if the task could not be canceled,
310220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * typically because it has already completed normally, or
311220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * because {@link #startLoading()} hasn't been called; returns
312220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * <tt>true</tt> otherwise.  When <tt>true</tt> is returned, the task
313220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * is still running and the {@link OnLoadCanceledListener} will be called
314220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * when the task completes.
315220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     */
316220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    protected boolean onCancelLoad() {
317220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown        return false;
318220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    }
319220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown
320220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    /**
321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * loaded data set and load a new one.  This simply calls through to the
323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * implementation's {@link #onForceLoad()}.  You generally should only call this
324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * when the loader is started -- that is, {@link #isStarted()} returns true.
325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void forceLoad() {
329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        onForceLoad();
330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses must implement this to take care of requests to {@link #forceLoad()}.
334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This will always be called from the process's main thread.
335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void onForceLoad() {
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
340220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * This function will normally be called for you automatically by
341220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * {@link android.support.v4.app.LoaderManager} when the associated fragment/activity
342220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * is being stopped.  When using a Loader with {@link android.support.v4.app.LoaderManager},
343220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * you <em>must not</em> call this method yourself, or you will conflict
344220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * with its management of the Loader.
345220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
346220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * <p>Stops delivery of updates until the next time {@link #startLoading()} is called.
347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Implementations should <em>not</em> invalidate their data at this point --
348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * clients are still free to use the last data the loader reported.  They will,
349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * however, typically stop reporting new data if the data changes; they can
350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * still monitor for changes, but must not report them to the client until and
351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * if {@link #startLoading()} is later called.
352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>This updates the Loader's internal state so that
354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #isStarted()} will return the correct
355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * value, and then calls the implementation's {@link #onStopLoading()}.
356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void stopLoading() {
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStarted = false;
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        onStopLoading();
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses must implement this to take care of stopping their loader,
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * as per {@link #stopLoading()}.  This is not called by clients directly,
367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * but as a result of a call to {@link #stopLoading()}.
368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This will always be called from the process's main thread.
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void onStopLoading() {
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
374220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * This function will normally be called for you automatically by
375220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * {@link android.support.v4.app.LoaderManager} when restarting a Loader.  When using
376220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * a Loader with {@link android.support.v4.app.LoaderManager},
377220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * you <em>must not</em> call this method yourself, or you will conflict
378220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * with its management of the Loader.
379220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Tell the Loader that it is being abandoned.  This is called prior
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to {@link #reset} to have it retain its current data but not report
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * any new data.
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void abandon() {
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAbandoned = true;
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        onAbandon();
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses implement this to take care of being abandoned.  This is
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * an optional intermediate state prior to {@link #onReset()} -- it means that
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * the client is no longer interested in any new data from the loader,
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * so the loader must not report any further updates.  However, the
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * loader <em>must</em> keep its last reported data valid until the final
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #onReset()} happens.  You can retrieve the current abandoned
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * state with {@link #isAbandoned}.
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
398220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown    protected void onAbandon() {
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
402220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * This function will normally be called for you automatically by
403220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * {@link android.support.v4.app.LoaderManager} when destroying a Loader.  When using
404220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * a Loader with {@link android.support.v4.app.LoaderManager},
405220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * you <em>must not</em> call this method yourself, or you will conflict
406220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     * with its management of the Loader.
407220dc21ab5a2a5b3f8b6532105121750770a69f4Jeff Brown     *
408cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Resets the state of the Loader.  The Loader should at this point free
409cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * all of its resources, since it may never be called again; however, its
410cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #startLoading()} may later be called at which point it must be
411cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * able to start running again.
412cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
413cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>This updates the Loader's internal state so that
414cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #isStarted()} and {@link #isReset()} will return the correct
415cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * values, and then calls the implementation's {@link #onReset()}.
416cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
417cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
418cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
419cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void reset() {
420cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        onReset();
421cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mReset = true;
422cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStarted = false;
423cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mAbandoned = false;
424cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mContentChanged = false;
42580a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        mProcessingChange = false;
426cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
427cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
428cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
429cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Subclasses must implement this to take care of resetting their loader,
430cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * as per {@link #reset()}.  This is not called by clients directly,
431cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * but as a result of a call to {@link #reset()}.
432cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This will always be called from the process's main thread.
433cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
434cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected void onReset() {
435cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
436cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
437cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
438cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Take the current flag indicating whether the loader's content had
439cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * changed while it was stopped.  If it had, true is returned and the
440cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * flag is cleared.
441cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
442cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public boolean takeContentChanged() {
443cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean res = mContentChanged;
444cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mContentChanged = false;
44580a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        mProcessingChange |= res;
446cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return res;
447cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
44880a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn
44980a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    /**
45080a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * Commit that you have actually fully processed a content change that
45180a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * was returned by {@link #takeContentChanged}.  This is for use with
45280a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * {@link #rollbackContentChanged()} to handle situations where a load
45380a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * is cancelled.  Call this when you have completely processed a load
45480a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * without it being cancelled.
45580a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     */
45680a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    public void commitContentChanged() {
45780a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        mProcessingChange = false;
45880a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    }
45980a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn
46080a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    /**
46180a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * Report that you have abandoned the processing of a content change that
46280a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * was returned by {@link #takeContentChanged()} and would like to rollback
46380a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * to the state where there is again a pending content change.  This is
46480a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * to handle the case where a data load due to a content change has been
46580a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     * canceled before its data was delivered back to the loader.
46680a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn     */
46780a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    public void rollbackContentChanged() {
46880a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        if (mProcessingChange) {
4690bab42508734d3f701a1fbb1d24c96fabff96163Adam Powell            onContentChanged();
47080a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        }
47180a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn    }
47280a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn
473cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
474cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called when {@link ForceLoadContentObserver} detects a change.  The
475cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * default implementation checks to see if the loader is currently started;
476cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * if so, it simply calls {@link #forceLoad()}; otherwise, it sets a flag
477cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * so that {@link #takeContentChanged()} returns true.
478cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
479cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * <p>Must be called from the process's main thread.
480cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
481cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void onContentChanged() {
482cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mStarted) {
483cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            forceLoad();
484cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
485cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // This loader has been stopped, so we don't want to load
486cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // new data right now...  but keep track of it changing to
487cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            // refresh later if we start again.
488cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mContentChanged = true;
489cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
490cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
491cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
492cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
493cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * For debugging, converts an instance of the Loader's data class to
494cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * a string that can be printed.  Must handle a null data.
495cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
496cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public String dataToString(D data) {
497cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        StringBuilder sb = new StringBuilder(64);
498cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        DebugUtils.buildShortClassTag(data, sb);
499cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append("}");
500cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return sb.toString();
501cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
502cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
503cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
504cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public String toString() {
505cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        StringBuilder sb = new StringBuilder(64);
506cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        DebugUtils.buildShortClassTag(this, sb);
507cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append(" id=");
508cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append(mId);
509cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        sb.append("}");
510cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return sb.toString();
511cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
512cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
513cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
514cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Print the Loader's state into the given stream.
515cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
516cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param prefix Text to print at the front of each line.
517cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param fd The raw file descriptor that the dump is being sent to.
518cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param writer A PrintWriter to which the dump is to be set.
519cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param args Additional arguments to the dump request.
520cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
521cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
522cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        writer.print(prefix); writer.print("mId="); writer.print(mId);
523cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                writer.print(" mListener="); writer.println(mListener);
52480a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        if (mStarted || mContentChanged || mProcessingChange) {
52580a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn            writer.print(prefix); writer.print("mStarted="); writer.print(mStarted);
52680a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn                    writer.print(" mContentChanged="); writer.print(mContentChanged);
52780a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn                    writer.print(" mProcessingChange="); writer.println(mProcessingChange);
52880a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        }
52980a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        if (mAbandoned || mReset) {
53080a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn            writer.print(prefix); writer.print("mAbandoned="); writer.print(mAbandoned);
53180a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn                    writer.print(" mReset="); writer.println(mReset);
53280a0a3a33e3f6c27da4681a4f02eb2c6aae1fd40Dianne Hackborn        }
533cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
534cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}