Skip to content

Commit

Permalink
Store list of synchronized assets on external storage.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://svn.code.sf.net/p/cmusphinx/code/trunk/pocketsphinx-android@12419 94700074-3cef-4d97-a70e-9c8c206c02f5
  • Loading branch information
mbait committed Mar 30, 2014
1 parent edc5622 commit 7ca7328
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/edu/cmu/pocketsphinx/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public class Assets {

private static final String TAG = Assets.class.getSimpleName();

private static final String ASSETS_LIST_PATH = ".assets";
private static final String ASSET_LIST_NAME = ".assets";

/**
* Synchronizes asset files with the content on external storage. There
* must be special file "assets.lst" among the application assets
* containing relative paths of assets to synchronize. If the corresponding
* path does not exist on the external storage it is copied. If the path
* exists checksums are compared and the asset is copied only if there is a
* mismatch. Checksum is stored in a separate asset with the name that
* consists of the original name and a suffix that depends on the checksum
* algorithm (e.g. MD5). Checksum files are copied along with the
* must be special file {@value #ASSET_LIST_NAME} among the application
* assets containing relative paths of assets to synchronize. If the
* corresponding path does not exist on the external storage it is copied.
* If the path exists checksums are compared and the asset is copied only
* if there is a mismatch. Checksum is stored in a separate asset with the
* name that consists of the original name and a suffix that depends on the
* checksum algorithm (e.g. MD5). Checksum files are copied along with the
* corresponding asset files.
*
* @param Context application context
Expand All @@ -41,16 +41,16 @@ public class Assets {
*/
public static File syncAssets(Context context) throws IOException {
AssetManager assets = context.getAssets();
Reader reader = new InputStreamReader(assets.open("assets.lst"));
Reader reader = new InputStreamReader(assets.open(ASSET_LIST_NAME));
BufferedReader br = new BufferedReader(reader);
File extDir = getApplicationDir(context);
File appDir = getApplicationDir(context);
Set<String> assetPaths = new HashSet<String>();
String path;

while (null != (path = br.readLine())) {
File extFile = new File(extDir, path);
File extFile = new File(appDir, path);
String md5Path = path + ".md5";
File extHash = new File(extDir, md5Path);
File extHash = new File(appDir, md5Path);
extFile.getParentFile().mkdirs();
assetPaths.add(extFile.getPath());

Expand All @@ -73,9 +73,9 @@ public static File syncAssets(Context context) throws IOException {
copyStream(hashStream, new FileOutputStream(extHash));
}

removeUnusedAssets(context, assetPaths);
removeUnusedAssets(new File(appDir, ASSET_LIST_NAME), assetPaths);

return extDir;
return appDir;
}

/**
Expand All @@ -98,7 +98,8 @@ public static File syncAssets(Context context) throws IOException {
public static File copyAssets(Context context, String path)
throws IOException
{
File externalFile = new File(getApplicationDir(context), path);
File appDir = getApplicationDir(context);
File externalFile = new File(appDir, path);
AssetManager assets = context.getAssets();
String[] content = assets.list(path);
Set<String> assetPaths = new HashSet<String>();
Expand All @@ -113,17 +114,17 @@ public static File copyAssets(Context context, String path)
assetPaths.add(externalFile.getPath());
}

removeUnusedAssets(context, assetPaths);
removeUnusedAssets(new File(appDir, ASSET_LIST_NAME), assetPaths);

return externalFile;
}

private static void removeUnusedAssets(Context context,
private static void removeUnusedAssets(File assetsListFile,
Set<String> usedAssets)
throws IOException
{
try {
InputStream istream = context.openFileInput(ASSETS_LIST_PATH);
InputStream istream = new FileInputStream(assetsListFile);
Reader reader = new InputStreamReader(istream);
BufferedReader br = new BufferedReader(reader);
Set<String> unusedAssets = new HashSet<String>();
Expand All @@ -134,20 +135,22 @@ private static void removeUnusedAssets(Context context,

unusedAssets.removeAll(usedAssets);
for (String path : unusedAssets) {
new File(path).delete();
if (new File(path).delete()) // Skip missing files.
Log.i(TAG, "delete unused asset " + path);

new File(path + ".md5").delete();
Log.i(TAG, "delete unused asset " + path);
}

istream.close();
} catch (FileNotFoundException e) {
Log.i(TAG, ASSETS_LIST_PATH + " does not exist");
Log.i(TAG, assetsListFile + " does not exist");
Log.i(TAG, "unused assets are not removed");
}

OutputStream ostream = context.openFileOutput(ASSETS_LIST_PATH, 0);
OutputStream ostream = new FileOutputStream(assetsListFile);
PrintStream ps = new PrintStream(ostream);
for (String path : usedAssets)
ps.println(path);

ps.close();
}

Expand Down

0 comments on commit 7ca7328

Please sign in to comment.