Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 17 | package android.app.patterns; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 18 | |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 20 | import android.database.ContentObserver; |
| 21 | import android.os.Handler; |
| 22 | |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 23 | public abstract class Loader<D> { |
| 24 | private int mId; |
| 25 | private OnLoadCompleteListener<D> mListener; |
| 26 | private Context mContext; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 27 | |
| 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 Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 44 | public interface OnLoadCompleteListener<D> { |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 45 | /** |
| 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 Plotnikov | 8605395 | 2010-04-29 10:22:01 -0700 | [diff] [blame] | 51 | public void onLoadComplete(Loader<D> loader, D data); |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Sends the result of the load to the register listener. |
| 56 | * |
| 57 | * @param data the result of the load |
| 58 | */ |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 59 | protected void deliverResult(D data) { |
| 60 | if (mListener != null) { |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 61 | mListener.onLoadComplete(this, data); |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 65 | /** |
| 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 Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 71 | public Loader(Context context) { |
| 72 | mContext = context.getApplicationContext(); |
| 73 | } |
| 74 | |
| 75 | /** |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 76 | * @return an application context retrieved from the Context passed to the constructor. |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 77 | */ |
| 78 | public Context getContext() { |
| 79 | return mContext; |
| 80 | } |
| 81 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 82 | /** |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 83 | * @return the ID of this loader |
| 84 | */ |
| 85 | public int getId() { |
| 86 | return mId; |
| 87 | } |
| 88 | |
| 89 | /** |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 90 | * 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 Plotnikov | 8605395 | 2010-04-29 10:22:01 -0700 | [diff] [blame] | 92 | * |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 93 | * Must be called from the UI thread |
| 94 | */ |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 95 | public void registerListener(int id, OnLoadCompleteListener<D> listener) { |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 96 | if (mListener != null) { |
| 97 | throw new IllegalStateException("There is already a listener registered"); |
| 98 | } |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 99 | mListener = listener; |
| 100 | mId = id; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Must be called from the UI thread |
| 105 | */ |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 106 | public void unregisterListener(OnLoadCompleteListener<D> listener) { |
| 107 | if (mListener == null) { |
| 108 | throw new IllegalStateException("No listener register"); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 109 | } |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 110 | if (mListener != listener) { |
| 111 | throw new IllegalArgumentException("Attempting to unregister the wrong listener"); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 112 | } |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 113 | mListener = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 114 | } |
| 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 Plotnikov | 8605395 | 2010-04-29 10:22:01 -0700 | [diff] [blame] | 121 | * {@link #stopLoading} will stop the delivery of callbacks. |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 122 | * |
| 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 Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 134 | * Stops delivery of updates until the next time {@link #startLoading()} is called |
| 135 | * |
| 136 | * Must be called from the UI thread |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 137 | */ |
| 138 | public abstract void stopLoading(); |
| 139 | |
| 140 | /** |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 141 | * Destroys the loader and frees it's resources, making it unusable. |
| 142 | * |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 143 | * Must be called from the UI thread |
| 144 | */ |
| 145 | public abstract void destroy(); |
| 146 | } |