sk.baka.ambient.library
Class CoverCache

java.lang.Object
  extended by sk.baka.ambient.commons.AbstractFileStorage
      extended by sk.baka.ambient.library.CoverCache
All Implemented Interfaces:
ILibraryListener

public final class CoverCache
extends AbstractFileStorage
implements ILibraryListener

Provides covers and manages cover cache. Thread-safe. Essentially a singleton, there must be at most one instance.

Author:
Martin Vysny

Field Summary
 
Fields inherited from class sk.baka.ambient.commons.AbstractFileStorage
maxStorageSize, theCache
 
Constructor Summary
CoverCache()
          Creates new cache.
 
Method Summary
 void cleanup()
          Cleans up old files.
 void coverLoaded(TrackMetadataBean track)
          A cover for given track has been successfully downloaded.
protected  String[] getFilenameAndExt(URL url, Object fetchInfo)
          Retrieves target file name.
protected  boolean isProceedWithDownload(Object fetchInfo)
          Invoked before the download process starts.
 void libraryUpdate(boolean updateStarted, boolean interrupted, boolean userNotified)
          Informs that the file rescan is complete and the database is updated.
 void lyricsLoaded(TrackMetadataBean track, List<LyrdbTrack> lyrics)
          A lyrics event.
protected  void onFileDownloaded(URL url, Object fetchInfo, boolean success)
          The file was downloaded and is already registered in the storage.
 void setCacheOverflowBehavior(boolean dontDownload)
          Sets the cache behavior when the cache is full.
 Bitmap setCover(TrackMetadataBean track, ImageView view)
          Fetches a cover for a track and sets it into given view.
protected  URL toURL(Object fetchInfo)
          Converts given fetch info to an URL object.
 
Methods inherited from class sk.baka.ambient.commons.AbstractFileStorage
close, createFile, fetchFileAsync, getCacheFile, getCacheFileNull, getFile, getNames, isFull, purge, registerFile, removeFile, setMaxStorageSize, supportsExtension
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CoverCache

public CoverCache()
Creates new cache.

Method Detail

setCacheOverflowBehavior

public void setCacheOverflowBehavior(boolean dontDownload)
Sets the cache behavior when the cache is full.

Parameters:
dontDownload - if true then new covers will not be downloaded. If false then old covers will be purged.

isProceedWithDownload

protected boolean isProceedWithDownload(Object fetchInfo)
Description copied from class: AbstractFileStorage
Invoked before the download process starts. Must be thread-safe as it is not invoked from a Handler thread.

Overrides:
isProceedWithDownload in class AbstractFileStorage
Parameters:
fetchInfo - the fetch info object, never null.
Returns:
if false then this download is aborted.

getFilenameAndExt

protected String[] getFilenameAndExt(URL url,
                                     Object fetchInfo)
Description copied from class: AbstractFileStorage
Retrieves target file name. Default implementation throws UnsupportedOperationException. Must be thread-safe as it is not invoked from a Handler thread.

Overrides:
getFilenameAndExt in class AbstractFileStorage
Parameters:
url - the source URL. May be null - in this case the extension is not needed and returned extension may be null or the result may contain only the name.
fetchInfo - the fetch info object
Returns:
array containing two items - filename and extension. The extension must start with a dot. If null is returned then the object is not downloadable nor storable into the storage.

onFileDownloaded

protected void onFileDownloaded(URL url,
                                Object fetchInfo,
                                boolean success)
Description copied from class: AbstractFileStorage
The file was downloaded and is already registered in the storage. Default implementation does nothing. Must be thread-safe as it is not invoked from a Handler thread.

Overrides:
onFileDownloaded in class AbstractFileStorage
Parameters:
url - the source URL
fetchInfo - the fetch info object
success - if true then the file is available in the cache. If false then the download process was aborted ( AbstractFileStorage.toURL(Object) returned null).

toURL

protected URL toURL(Object fetchInfo)
             throws IOException
Description copied from class: AbstractFileStorage
Converts given fetch info to an URL object. Default implementation throws UnsupportedOperationException. Must be thread-safe as it is not invoked from a Handler thread.

Overrides:
toURL in class AbstractFileStorage
Parameters:
fetchInfo - the fetch info object, never null.
Returns:
URL which is used to download the file contents. If null then this download is aborted.
Throws:
IOException - if i/o error occurs

setCover

public Bitmap setCover(TrackMetadataBean track,
                       ImageView view)
Fetches a cover for a track and sets it into given view. If the cover is not present in the cache and we are not offline the cover is fetched in a worker thread and ILibraryListener.coverLoaded(TrackMetadataBean) is emitted.

Parameters:
track - the track.
view - set the image to this view.
Returns:
loaded bitmap instance. The bitmap should be recycled when not needed.

cleanup

public void cleanup()
Description copied from class: AbstractFileStorage
Cleans up old files. Superclass should implement a strategy to pick and delete obsolete files. Default implementation does nothing. Called in AbstractFileStorage.setMaxStorageSize(long) method.

Overrides:
cleanup in class AbstractFileStorage

coverLoaded

public void coverLoaded(TrackMetadataBean track)
Description copied from interface: ILibraryListener
A cover for given track has been successfully downloaded. Note that if the cache size is set to a very small value the image may already be purged from the cache. To avoid this either set a sensible cache size value, e.g. 128kb, or turn off the image downloading by setting offline mode to true.

Specified by:
coverLoaded in interface ILibraryListener
Parameters:
track - the track.

libraryUpdate

public void libraryUpdate(boolean updateStarted,
                          boolean interrupted,
                          boolean userNotified)
Description copied from interface: ILibraryListener
Informs that the file rescan is complete and the database is updated. Invoked in Scanner thread.

Specified by:
libraryUpdate in interface ILibraryListener
Parameters:
updateStarted - if true then the library update just started. If false then the update finished.
interrupted - if true then the scan was interrupted.
userNotified - if true then the user is already notified about the library being updated.

lyricsLoaded

public void lyricsLoaded(TrackMetadataBean track,
                         List<LyrdbTrack> lyrics)
Description copied from interface: ILibraryListener
A lyrics event. Either lyrics for given track has been successfully downloaded or user needs to select one from given lyrics list.

Specified by:
lyricsLoaded in interface ILibraryListener
Parameters:
track - the track.
lyrics - if non-null then user needs to select the correct karaoke from this list. The list may be empty - in this case no karaoke files are available. If null then the karaoke file is available locally. If non-null and is empty then no karaoke files are available.


Copyright © 2007-2008. All Rights Reserved.