private void cutCurrentCache ( ) { final File lock = Configuration . getInstance ( ) . getOsmdroidTileCache ( ) ; synchronized ( lock ) { if ( mUsedCacheSpace > Configuration . getInstance ( ) . getTileFileSystemCacheTrimBytes ( ) ) { Log . d ( IMapView . LOGTAG , "Trimming tile cache from " + mUsedCacheSpace + " to " + Configuration . getInstance ( ) . getTileFileSystemCacheTrimBytes ( ) ) ; final List < File > z = getDirectoryFileList ( Configuration . getInstance ( ) . getOsmdroidTileCache ( ) ) ; // order list by files day created from old to new final File [ ] files = z . toArray ( new File [ 0 ] ) ; Arrays . sort ( files , new Comparator < File > ( ) { @ Override public int compare ( final File f1 , final File f2 ) { return Long . valueOf ( f1 . lastModified ( ) ) . compareTo ( f2 . lastModified ( ) ) ; } } ) ; for ( final File file : files ) { if ( mUsedCacheSpace <= Configuration . getInstance ( ) . getTileFileSystemCacheTrimBytes ( ) ) { break ; } final long length = file . length ( ) ; if ( file . delete ( ) ) { if ( Configuration . getInstance ( ) . isDebugTileProviders ( ) ) { Log . d ( IMapView . LOGTAG , "Cache trim deleting " + file . getAbsolutePath ( ) ) ; } mUsedCacheSpace -= length ; } } Log . d ( IMapView . LOGTAG , "Finished trimming tile cache" ) ; } } }
If the cache size is greater than the max then trim it down to the trim level . This method is synchronized so that only one thread can run it at a time .