1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
package ...;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SyncStatusObserver;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.support.v4.content.AsyncTaskLoader;
import com.activeandroid.Model;
import com.activeandroid.content.ContentProvider;
import com.activeandroid.query.From;
import com.activeandroid.query.Select;
/**
* The Class ModelLoader.
*
* @param <T>
* the generic type
*/
public class ModelLoader<T extends Model> extends AsyncTaskLoader<List<T>>
{
private ContentResolver mContentResolver;
private ContentObserver mContentObserver;
private From mQuery;
private List<T> mResults;
private Class<T> mClass;
private Object mHandle;
private boolean mUpdateOnRelationshipChanges;
/**
* Instantiates a new model loader. Will retrieve all models of the specified subclass. Will not
* be reloaded on relationship changes.
*
* @param context
* the model subclass you wish to query
* @param clazz
* the clazz
*/
public ModelLoader(Context context, Class<T> clazz)
{
this(context, clazz, null, false);
}
/**
* Instantiates a new model loader. Will retrieve all models of the specified subclass.
*
* @param context
* the context
* @param clazz
* the model subclass you wish to query
* @param updateOnRelationshipChanges
* if true, loader will updated when tables related to the one detected are changed
*/
public ModelLoader(Context context, Class<T> clazz, boolean updateOnRelationshipChanges)
{
this(context, clazz, null, updateOnRelationshipChanges);
}
/**
* Instantiates a new model loader.
*
* @param context
* the context
* @param clazz
* the model subclass you wish to query
* @param from
* a select/from statement that will be executed to retrieve the objects
* @param updateOnRelationshipChanges
* if true, loader will updated when tables related to the one detected are changed
*/
public ModelLoader(Context context, Class<T> clazz, From from,
boolean updateOnRelationshipChanges)
{
super(context);
mQuery = from;
mClass = clazz;
mContentResolver = context.getContentResolver();
mUpdateOnRelationshipChanges = updateOnRelationshipChanges;
}
/**
* Called when there is new data to deliver to the client. The super class
* will take care of delivering it; the implementation here just adds a
* little more logic.
*
* @param models
* the new models to be delivered
*/
@Override
public void deliverResult(List<T> models)
{
if (isReset())
{
// An async query came in while the loader is stopped. We
// don't need the result.
return;
}
mResults = models;
if (isStarted())
{
// If the Loader is currently started, we can immediately
// deliver its results.
super.deliverResult(models);
}
}
/**
* This is where the bulk of our work is done. This function is called in a
* background thread and should generate a new set of data to be published
* by the loader.
*
* @return the list
*/
@Override
public List<T> loadInBackground()
{
List<T> results;
if (mQuery == null)
{
results = new Select().from(mClass)
.execute();
}
else
{
results = mQuery.execute();
}
return results;
}
/**
* Handles a request to completely reset the Loader.
*/
@Override
protected void onReset()
{
super.onReset();
// Ensure the loader is stopped
onStopLoading();
mResults = null;
// Stop monitoring for changes.
if (mContentObserver != null)
{
mContentResolver.unregisterContentObserver(mContentObserver);
ContentResolver.removeStatusChangeListener(mHandle);
mHandle = null;
mContentObserver = null;
}
}
/**
* Handles a request to start the Loader.
*/
@Override
protected void onStartLoading()
{
if (mResults != null)
{
// If we currently have a result available, deliver it
// immediately.
deliverResult(mResults);
}
// Start watching for changes in the job data.
if (mContentObserver == null)
{
mContentObserver = new ContentObserver(new Handler(getContext().getMainLooper()))
{
@Override
public void onChange(boolean selfChange)
{
onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri)
{
onContentChanged();
}
};
SyncStatusObserver statusObserver = new SyncStatusObserver()
{
@Override
public void onStatusChanged(int which)
{
onContentChanged();
}
};
mHandle = ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, statusObserver);
mContentResolver.registerContentObserver(ContentProvider.createUri(mClass, null), true,
mContentObserver);
// Register for updates from related tables
if (mUpdateOnRelationshipChanges)
{
Field[] fields = mClass.getDeclaredFields();
for (Field field : fields)
{
Class<?> clazz = field.getType();
if (Model.class.isAssignableFrom(clazz))
{
mContentResolver.registerContentObserver(
ContentProvider.createUri(clazz.asSubclass(Model.class), null),
true, mContentObserver);
}
}
Method[] methods = mClass.getDeclaredMethods();
for (Method method : methods)
{
Class<?> returnClass = method.getReturnType();
if (Model.class.isAssignableFrom(returnClass))
{
mContentResolver.registerContentObserver(
ContentProvider.createUri(returnClass.asSubclass(Model.class), null),
true, mContentObserver);
}
else
{
Type type = method.getGenericReturnType();
if (type instanceof ParameterizedType)
{
ParameterizedType parameterizedType = (ParameterizedType) type;
for (Type actualType : parameterizedType.getActualTypeArguments())
{
Class<T> genericClass = (Class<T>) actualType;
if (Model.class.isAssignableFrom(genericClass))
{
mContentResolver.registerContentObserver(
ContentProvider.createUri(
genericClass.asSubclass(Model.class), null),
true, mContentObserver);
}
}
}
}
}
}
}
if (takeContentChanged() || (mResults == null))
{
// If the data has changed since the last time it was loaded
// or is not currently available, start a load.
forceLoad();
}
}
/**
* Handles a request to stop the Loader.
*/
@Override
protected void onStopLoading()
{
// Attempt to cancel the current load task if possible.
cancelLoad();
}
} |
Partager