11package com .tns ;
22
33import java .io .File ;
4+ import java .io .IOException ;
45
56import android .content .Context ;
67import android .util .Log ;
@@ -13,14 +14,22 @@ public AssetExtractor(File libPath, Logger logger) {
1314 this .logger = logger ;
1415 }
1516
16- public void extractAssets (Context context , String inputPath , String outputPath , ExtractPolicy extractPolicy ) {
17+ public void extractAssets (Context context , String inputPath , String outputPath , ExtractPolicy extractPolicy , boolean shouldCleanUpPreviousAssets ) {
1718 FileExtractor extractor = extractPolicy .extractor ();
1819 if (extractor != null ) {
1920 boolean success = extractor .extract (context );
2021 if (logger .isEnabled ()) {
2122 logger .write ("extract returned " + success );
2223 }
2324 } else if (extractPolicy .shouldExtract (context )) {
25+ if (shouldCleanUpPreviousAssets ) {
26+ try {
27+ delete (new File (outputPath + inputPath ));
28+ } catch (IOException e ) {
29+ Log .d (LogTag , "Problem occurred while deleting assets from previous app version: " + outputPath + inputPath );
30+ }
31+ }
32+
2433 String apkPath = context .getPackageCodePath ();
2534
2635 boolean forceOverwrite = extractPolicy .forceOverwrite ();
@@ -32,4 +41,33 @@ public void extractAssets(Context context, String inputPath, String outputPath,
3241 }
3342 }
3443 }
44+
45+ /**
46+ * Delete a file or a directory and its children.
47+ * @param file The directory to delete.
48+ * @throws IOException Exception when problem occurs during deleting the directory.
49+ */
50+ private static void delete (File file ) throws IOException {
51+ File [] files = file .listFiles ();
52+ if (files == null ) {
53+ Log .d (LogTag , "Can't remove previously installed assets in " + file .getAbsolutePath ());
54+ return ;
55+ }
56+
57+ for (File childFile : files ) {
58+ if (childFile .isDirectory ()) {
59+ delete (childFile );
60+ } else {
61+ if (!childFile .delete ()) {
62+ throw new IOException ();
63+ }
64+ }
65+ }
66+
67+ if (!file .delete ()) {
68+ throw new IOException ();
69+ }
70+ }
71+
72+ private static String LogTag = "JS: AssetExtraction" ;
3573}
0 commit comments