blob: 18c64f3c40ebf7f9875d0ae9f333fab063da1e74 [file] [log] [blame]
Daniel Lehmann4cd94412010-04-08 16:44:36 -07001/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
Daniel Lehmannc2687c32010-04-19 18:20:44 -070017package android.app.patterns;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070018
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070019import android.content.Context;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070020import android.database.ContentObserver;
21import android.os.Handler;
22
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070023public abstract class Loader<D> {
24 private int mId;
25 private OnLoadCompleteListener<D> mListener;
26 private Context mContext;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070027
28 protected final class ForceLoadContentObserver extends ContentObserver {
29 public ForceLoadContentObserver() {
30 super(new Handler());
31 }
32
33 @Override
34 public boolean deliverSelfNotifications() {
35 return true;
36 }
37
38 @Override
39 public void onChange(boolean selfChange) {
40 forceLoad();
41 }
42 }
43
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070044 public interface OnLoadCompleteListener<D> {
Daniel Lehmannc2687c32010-04-19 18:20:44 -070045 /**
46 * Called on the thread that created the Loader when the load is complete.
47 *
48 * @param loader the loader that completed the load
49 * @param data the result of the load
50 */
Dmitri Plotnikov86053952010-04-29 10:22:01 -070051 public void onLoadComplete(Loader<D> loader, D data);
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070052 }
53
Daniel Lehmannc2687c32010-04-19 18:20:44 -070054 /**
55 * Sends the result of the load to the register listener.
56 *
57 * @param data the result of the load
58 */
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070059 protected void deliverResult(D data) {
60 if (mListener != null) {
Daniel Lehmannc2687c32010-04-19 18:20:44 -070061 mListener.onLoadComplete(this, data);
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070062 }
63 }
64
Daniel Lehmannc2687c32010-04-19 18:20:44 -070065 /**
66 * Stores away the application context associated with context. Since Loaders can be used
67 * across multiple activities it's dangerous to store the context directly.
68 *
69 * @param context used to retrieve the application context.
70 */
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070071 public Loader(Context context) {
72 mContext = context.getApplicationContext();
73 }
74
75 /**
Daniel Lehmannc2687c32010-04-19 18:20:44 -070076 * @return an application context retrieved from the Context passed to the constructor.
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070077 */
78 public Context getContext() {
79 return mContext;
80 }
81
Daniel Lehmann4cd94412010-04-08 16:44:36 -070082 /**
Daniel Lehmannc2687c32010-04-19 18:20:44 -070083 * @return the ID of this loader
84 */
85 public int getId() {
86 return mId;
87 }
88
89 /**
Daniel Lehmann4cd94412010-04-08 16:44:36 -070090 * Registers a class that will receive callbacks when a load is complete. The callbacks will
91 * be called on the UI thread so it's safe to pass the results to widgets.
Dmitri Plotnikov86053952010-04-29 10:22:01 -070092 *
Daniel Lehmann4cd94412010-04-08 16:44:36 -070093 * Must be called from the UI thread
94 */
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070095 public void registerListener(int id, OnLoadCompleteListener<D> listener) {
Daniel Lehmannc2687c32010-04-19 18:20:44 -070096 if (mListener != null) {
97 throw new IllegalStateException("There is already a listener registered");
98 }
Daniel Lehmanncbcc4492010-04-12 18:03:54 -070099 mListener = listener;
100 mId = id;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700101 }
102
103 /**
104 * Must be called from the UI thread
105 */
Daniel Lehmanncbcc4492010-04-12 18:03:54 -0700106 public void unregisterListener(OnLoadCompleteListener<D> listener) {
107 if (mListener == null) {
108 throw new IllegalStateException("No listener register");
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700109 }
Daniel Lehmanncbcc4492010-04-12 18:03:54 -0700110 if (mListener != listener) {
111 throw new IllegalArgumentException("Attempting to unregister the wrong listener");
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700112 }
Daniel Lehmanncbcc4492010-04-12 18:03:54 -0700113 mListener = null;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700114 }
115
116 /**
117 * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
118 * will be called on the UI thread. If a previous load has been completed and is still valid
119 * the result may be passed to the callbacks immediately. The loader will monitor the source of
120 * the data set and may deliver future callbacks if the source changes. Calling
Dmitri Plotnikov86053952010-04-29 10:22:01 -0700121 * {@link #stopLoading} will stop the delivery of callbacks.
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700122 *
123 * Must be called from the UI thread
124 */
125 public abstract void startLoading();
126
127 /**
128 * Force an asynchronous load. Unlike {@link #startLoading()} this will ignore a previously
129 * loaded data set and load a new one.
130 */
131 public abstract void forceLoad();
132
133 /**
Daniel Lehmannc2687c32010-04-19 18:20:44 -0700134 * Stops delivery of updates until the next time {@link #startLoading()} is called
135 *
136 * Must be called from the UI thread
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700137 */
138 public abstract void stopLoading();
139
140 /**
Daniel Lehmannc2687c32010-04-19 18:20:44 -0700141 * Destroys the loader and frees it's resources, making it unusable.
142 *
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700143 * Must be called from the UI thread
144 */
145 public abstract void destroy();
146}