1 //
2 // Copyright 2011 The Android Open Source Project
3 //
4 // Cache manager for pre-processed PNG files.
5 // Contains code for managing which PNG files get processed
6 // at build time.
7 //
8 
9 #ifndef CRUNCHCACHE_H
10 #define CRUNCHCACHE_H
11 
12 #include <utils/KeyedVector.h>
13 #include <utils/String8.h>
14 #include "FileFinder.h"
15 #include "CacheUpdater.h"
16 
17 using namespace android;
18 
19 /** CrunchCache
20  *  This class is a cache manager which can pre-process PNG files and store
21  *  them in a mirror-cache. It's capable of doing incremental updates to its
22  *  cache.
23  *
24  *  Usage:
25  *      Create an instance initialized with the root of the source tree, the
26  *      root location to store the cache files, and an instance of a file finder.
27  *      Then update the cache by calling crunch.
28  */
29 class CrunchCache {
30 public:
31     // Constructor
32     CrunchCache(String8 sourcePath, String8 destPath, FileFinder* ff);
33 
34     // Nobody should be calling the default constructor
35     // So this space is intentionally left blank
36 
37     // Default Copy Constructor and Destructor are fine
38 
39     /** crunch is the workhorse of this class.
40      * It goes through all the files found in the sourcePath and compares
41      * them to the cached versions in the destPath. If the optional
42      * argument forceOverwrite is set to true, then all source files are
43      * re-crunched even if they have not been modified recently. Otherwise,
44      * source files are only crunched when they needUpdating. Afterwards,
45      * we delete any leftover files in the cache that are no longer present
46      * in source.
47      *
48      * PRECONDITIONS:
49      *      No setup besides construction is needed
50      * POSTCONDITIONS:
51      *      The cache is updated to fully reflect all changes in source.
52      *      The function then returns the number of files changed in cache
53      *      (counting deletions).
54      */
55     size_t crunch(CacheUpdater* cu, bool forceOverwrite=false);
56 
57 private:
58     /** loadFiles is a wrapper to the FileFinder that places matching
59      * files into mSourceFiles and mDestFiles.
60      *
61      *  POSTCONDITIONS
62      *      mDestFiles and mSourceFiles are refreshed to reflect the current
63      *      state of the files in the source and dest directories.
64      *      Any previous contents of mSourceFiles and mDestFiles are cleared.
65      */
66     void loadFiles();
67 
68     /** needsUpdating takes a file path
69      * and returns true if the file represented by this path is newer in the
70      * sourceFiles than in the cache (mDestFiles).
71      *
72      * PRECONDITIONS:
73      *      mSourceFiles and mDestFiles must be initialized and filled.
74      * POSTCONDITIONS:
75      *      returns true if and only if source file's modification time
76      *      is greater than the cached file's mod-time. Otherwise returns false.
77      *
78      * USAGE:
79      *      Should be used something like the following:
80      *      if (needsUpdating(filePath))
81      *          // Recrunch sourceFile out to destFile.
82      *
83      */
84     bool needsUpdating(const String8& relativePath) const;
85 
86     // DATA MEMBERS ====================================================
87 
88     String8 mSourcePath;
89     String8 mDestPath;
90 
91     Vector<String8> mExtensions;
92 
93     // Each vector of paths contains one entry per PNG file encountered.
94     // Each entry consists of a path pointing to that PNG.
95     DefaultKeyedVector<String8,time_t> mSourceFiles;
96     DefaultKeyedVector<String8,time_t> mDestFiles;
97 
98     // Pointer to a FileFinder to use
99     FileFinder* mFileFinder;
100 };
101 
102 #endif // CRUNCHCACHE_H
103