org.hermit.android.net
Class CachedFile

java.lang.Object
  extended by java.util.Observable
      extended by org.hermit.android.net.CachedFile
All Implemented Interfaces:
WebFetcher.Listener

public class CachedFile
extends java.util.Observable
implements WebFetcher.Listener

This class manages a set of web-based files and caches them locally. Data about the files is held in a local database.


Nested Class Summary
static class CachedFile.Entry
          Class describing an entry in the cache; i.e.
 
Constructor Summary
CachedFile(java.lang.String name, java.net.URL[] urls)
          Create a cache of web-based files.
 
Method Summary
 void createTable(SQLiteDatabase db)
          Create our table in the database.
 CachedFile.Entry getFile(java.net.URL url)
          Query for a given file in the cache.
 void invalidate(java.net.URL url)
          Invalidate the given file in the cache (perhaps it was corrupted).
 void onWebData(java.net.URL url, java.lang.Object obj, long date)
          This method is invoked when a data item is retrieved from the URL we were invoked on.
 void onWebDone()
          This method is invoked when the given URL has been fully fetched.
 void onWebError(java.lang.String msg)
          Handle an error while fetching web data.
 void setDatabase(SQLiteDatabase db)
          Set the database we use for storing our data.
 void update(Context context, long now)
          Check to see whether we need to update our cached copies of the files.
 void upgradeTable(SQLiteDatabase db, int oldV, int newV)
          Upgrade or table in the database to a new version.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedFile

public CachedFile(java.lang.String name,
                  java.net.URL[] urls)
Create a cache of web-based files. This is private, as only one instance exists.

Parameters:
name - The name of this source, and its database table.
urls - The list of URLs to cache.
Method Detail

createTable

public void createTable(SQLiteDatabase db)
Create our table in the database.

Parameters:
db - The database.

upgradeTable

public void upgradeTable(SQLiteDatabase db,
                         int oldV,
                         int newV)
Upgrade or table in the database to a new version.

Parameters:
db - The database.
oldV - Version we're upgrading from.
newV - Version we're upgrading to.

setDatabase

public void setDatabase(SQLiteDatabase db)
Set the database we use for storing our data.

Parameters:
db - The database. Will be null if the database is being closed.

getFile

public CachedFile.Entry getFile(java.net.URL url)
Query for a given file in the cache.

Parameters:
url - The URL of the file we want.
Returns:
null if the URL is not being cached at all. Otherwise, an Entry containing info on the file; path will be non-null if the file has been loaded, and date will be its reported last mod time.

invalidate

public void invalidate(java.net.URL url)
Invalidate the given file in the cache (perhaps it was corrupted).

Parameters:
url - The URL of the file to invalidate.

update

public void update(Context context,
                   long now)
Check to see whether we need to update our cached copies of the files. If so, kick off a web fetch. The observers will be notified for each file that we load.

Parameters:
context - The application context. Used for determining where the local files are kept.
now - The current time in millis.

onWebData

public void onWebData(java.net.URL url,
                      java.lang.Object obj,
                      long date)
This method is invoked when a data item is retrieved from the URL we were invoked on.

Specified by:
onWebData in interface WebFetcher.Listener
Parameters:
url - The URL of the source being loaded.
obj - The object that was loaded; the type depends on the fetcher class used.
date - The last modified time of the source file, as reported by the server, in ms UTC.

onWebDone

public void onWebDone()
This method is invoked when the given URL has been fully fetched.

Specified by:
onWebDone in interface WebFetcher.Listener

onWebError

public void onWebError(java.lang.String msg)
Handle an error while fetching web data.

Specified by:
onWebError in interface WebFetcher.Listener
Parameters:
msg - The error message.