Index: trunk/android-client/src/org/mediawiki/android/AndCache.java |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | * set.
|
30 | 30 | *
|
31 | 31 | * @author Chad Horohoe
|
32 | | - * @version 0.1
|
| 32 | + * @version 0.2
|
33 | 33 | * @license GNU GPL v2 (or later)
|
34 | 34 | *
|
35 | 35 | * This program is free software; you can redistribute it and/or modify
|
— | — | @@ -54,6 +54,9 @@ |
55 | 55 | // For debugging
|
56 | 56 | private static final String LOG = "org.mediawiki.android.Cache";
|
57 | 57 |
|
| 58 | + // Call lockCache() to get a lock on the cache
|
| 59 | + private static boolean locked = false;
|
| 60 | +
|
58 | 61 | // Database info
|
59 | 62 | private static final String DB_NAME = "cache.db";
|
60 | 63 | private static final int DB_VERSION = 1;
|
— | — | @@ -165,15 +168,27 @@ |
166 | 169 | private static void delete( String key ) throws CacheException {
|
167 | 170 | SQLiteDatabase db = mWrapper.getWritableDatabase();
|
168 | 171 | try {
|
169 | | - db.execSQL( "DELETE FROM " + TBL_NAME + " WHERE " + TBL_SCHEMA.cols.KEY + " = '" + key + "'" );
|
| 172 | + db.delete( TBL_NAME, TBL_SCHEMA.cols.KEY + " = '" + key + "'", null );
|
170 | 173 | } catch ( SQLiteException e ) {
|
171 | | - throw new CacheException( "SQL Error in AndCache.delete() :" + e.getMessage() );
|
| 174 | + throw new CacheException( "SQL", "AndCache.delete", e.getMessage() );
|
172 | 175 | } finally {
|
173 | 176 | Log.i( "org.mediawiki.android.Cache", "Deleting key: " + key );
|
174 | 177 | }
|
175 | 178 | }
|
176 | 179 |
|
177 | 180 | /**
|
| 181 | + * Flush the entire cache. Helpful if you've got stuff caught in it
|
| 182 | + */
|
| 183 | + public static void flush() throws CacheException {
|
| 184 | + SQLiteDatabase db = mWrapper.getWritableDatabase();
|
| 185 | + try {
|
| 186 | + db.delete( TBL_NAME , "1", null );
|
| 187 | + } catch( SQLiteException e ) {
|
| 188 | + throw new CacheException( "Query", "flush()", e.getMessage() );
|
| 189 | + }
|
| 190 | + }
|
| 191 | +
|
| 192 | + /**
|
178 | 193 | * Is the entry expired?
|
179 | 194 | * @param
|
180 | 195 | * @return boolean
|
— | — | @@ -198,11 +213,11 @@ |
199 | 214 | oOut.writeObject(obj);
|
200 | 215 | bytes = bOut.toByteArray();
|
201 | 216 | } catch ( IOException e ) {
|
202 | | - throw new CacheException( "Failed to serialize, IOException: " + e.getMessage() );
|
| 217 | + throw new CacheException( "IOException", "AndCache.serialize", e.getMessage() );
|
203 | 218 | }
|
204 | 219 | return bytes;
|
205 | 220 | }
|
206 | | -
|
| 221 | +
|
207 | 222 | /**
|
208 | 223 | * Unserialize an object
|
209 | 224 | * @param byte[] bytes A byte array to be deserialized into an object
|
— | — | @@ -217,9 +232,9 @@ |
218 | 233 | bIn.close();
|
219 | 234 | return obj;
|
220 | 235 | } catch( IOException e ) {
|
221 | | - throw new CacheException( "Failed to unserialize, IOException: " + e.getMessage() );
|
| 236 | + throw new CacheException( "IOException", "AndCache.unserialize", e.getMessage() );
|
222 | 237 | } catch (ClassNotFoundException e) {
|
223 | | - throw new CacheException( "Failed to unserialize: " + e.getMessage() );
|
| 238 | + throw new CacheException( "ClassNotFoundException", "AndCache.unserialize" ,e.getMessage() );
|
224 | 239 | }
|
225 | 240 | }
|
226 | 241 |
|
— | — | @@ -266,18 +281,36 @@ |
267 | 282 | * Need to be able to set our context
|
268 | 283 | * @param Context c the new Context to use
|
269 | 284 | */
|
270 | | - public static void setContext( Context c ) throws CacheException {
|
271 | | - try {
|
272 | | - AndCache.ctx = c;
|
273 | | - mWrapper = new DbWrapper( AndCache.ctx );
|
| 285 | + public static void setContext( Context c, boolean lock ) throws CacheException {
|
| 286 | + String curPackage = AndCache.ctx.getPackageName();
|
| 287 | + if ( c.getPackageName() == curPackage ) {
|
| 288 | + return;
|
274 | 289 | }
|
275 | | - catch ( SQLiteException e ) {
|
276 | | - throw new CacheException( "Could not setup db for ctx " +
|
277 | | - c.getClass().getName() + ". SQL error was" + e.getMessage() );
|
| 290 | + else if ( AndCache.locked ) {
|
| 291 | + throw new CacheException( "Context locked", "AndCache.setContext",
|
| 292 | + "locked to " + curPackage );
|
278 | 293 | }
|
| 294 | + else {
|
| 295 | + try {
|
| 296 | + AndCache.ctx = c;
|
| 297 | + mWrapper = new DbWrapper( AndCache.ctx );
|
| 298 | + AndCache.locked = lock;
|
| 299 | + }
|
| 300 | + catch ( SQLiteException e ) {
|
| 301 | + throw new CacheException( "Create DB", "AndCache.setContext", e.getMessage() );
|
| 302 | + }
|
| 303 | + }
|
279 | 304 | }
|
280 | 305 |
|
281 | 306 | /**
|
| 307 | + * Unlock the cache. Call this if you've gotten a lock and you're
|
| 308 | + * done with the cache.
|
| 309 | + */
|
| 310 | + public static void unlockCache() {
|
| 311 | + AndCache.locked = false;
|
| 312 | + }
|
| 313 | +
|
| 314 | + /**
|
282 | 315 | * Get the current context and re-init the DbWrapper
|
283 | 316 | * @return Context
|
284 | 317 | */
|
— | — | @@ -291,8 +324,19 @@ |
292 | 325 | public static class CacheException extends Exception {
|
293 | 326 | // Auto-gen
|
294 | 327 | private static final long serialVersionUID = 675324595007281374L;
|
295 | | - public CacheException( String msg ) {
|
296 | | - super(msg);
|
| 328 | + /**
|
| 329 | + * Cache exceptions. We generally throw these when catching higher
|
| 330 | + * exceptions so we can bail out nicer.
|
| 331 | + * @param error The specific type of error or exception being caught
|
| 332 | + * @param method The method that is throwing the error (eg: AndCache.get)
|
| 333 | + * @param eMsg Any extra info to return, perhaps from a caught exception
|
| 334 | + */
|
| 335 | + public CacheException( String error, String caller, String eMsg ) {
|
| 336 | + // Java is stupid and wants the parent call first, so I can't
|
| 337 | + // just make a string and use it twice :\
|
| 338 | + super( error + " error in " + caller + "()" + ( eMsg != null ? ": " + eMsg : "" ) );
|
| 339 | + Log.e( AndCache.LOG, error + " error in " + caller + "()" +
|
| 340 | + ( eMsg != null ? ": " + eMsg : "" ) );
|
297 | 341 | }
|
298 | 342 | }
|
299 | 343 | }
|