Skip to content

Commit f7a8222

Browse files
authoredMay 31, 2017
Merge pull request #771 from NativeScript/pete/clear-assets-on-update
Fix asset extraction
2 parents 3d50b50 + f4efc8b commit f7a8222

File tree

4 files changed

+71
-15
lines changed

4 files changed

+71
-15
lines changed
 

‎build-artifacts/project-template-gradle/src/main/java/com/tns/RuntimeHelper.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ public static Runtime initRuntime(Application app) {
8585

8686
String outputDir = app.getFilesDir().getPath() + File.separator;
8787

88-
aE.extractAssets(app, "app", outputDir, extractPolicy);
89-
aE.extractAssets(app, "internal", outputDir, extractPolicy);
90-
aE.extractAssets(app, "metadata", outputDir, extractPolicy);
88+
// will force deletion of previously extracted files in app/files directories
89+
// see https://github.com/NativeScript/NativeScript/issues/4137 for reference
90+
boolean removePreviouslyInstalledAssets = true;
91+
aE.extractAssets(app, "app", outputDir, extractPolicy, removePreviouslyInstalledAssets);
92+
aE.extractAssets(app, "internal", outputDir, extractPolicy, removePreviouslyInstalledAssets);
93+
aE.extractAssets(app, "metadata", outputDir, extractPolicy, false);
9194

92-
// enable with flags?
9395
boolean shouldExtractSnapshots = true;
9496

9597
// will extract snapshot of the device appropriate architecture
@@ -98,7 +100,7 @@ public static Runtime initRuntime(Application app) {
98100
logger.write("Extracting snapshot blob");
99101
}
100102

101-
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy);
103+
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy, removePreviouslyInstalledAssets);
102104
}
103105

104106
extractPolicy.setAssetsThumb(app);

‎runtime/src/main/java/com/tns/AssetExtractor.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.tns;
22

33
import java.io.File;
4+
import java.io.IOException;
45

56
import android.content.Context;
67
import 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
}

‎test-app/app/src/main/java/com/tns/DefaultExtractPolicy.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public DefaultExtractPolicy(Logger logger) {
2929
}
3030

3131
public boolean shouldExtract(Context context) {
32-
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
32+
String assetsThumbFilePath = getFilesDir(context) + File.separatorChar + ASSETS_THUMB_FILENAME;
3333
String oldAssetsThumb = getCachedAssetsThumb(assetsThumbFilePath);
3434
if (oldAssetsThumb == null) {
3535
return true;
@@ -47,7 +47,7 @@ public boolean shouldExtract(Context context) {
4747
public void setAssetsThumb(Context context) {
4848
String assetsThumb = generateAssetsThumb(context);
4949
if (assetsThumb != null) {
50-
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
50+
String assetsThumbFilePath = getFilesDir(context) + File.separatorChar + ASSETS_THUMB_FILENAME;
5151
saveNewAssetsThumb(assetsThumb, assetsThumbFilePath);
5252
}
5353
}
@@ -114,12 +114,26 @@ private void saveNewAssetsThumb(String newThumb, String assetsThumbFile) {
114114
out.close();
115115
}
116116
} catch (FileNotFoundException e) {
117-
logger.write("Error while writting current assets thumb");
117+
logger.write("Error while writing current assets thumb");
118118
e.printStackTrace();
119119
} catch (IOException e) {
120-
logger.write("Error while writting current assets thumb");
120+
logger.write("Error while writing current assets thumb");
121121
e.printStackTrace();
122122
}
123123
}
124124

125+
/*
126+
Write assetsThumb file to a no-backup directory to prevent the thumb from being
127+
backed up on devices of API Level 23 and up.
128+
Devices Level 22 and lower don't support the Auto Backup feature,
129+
so it is safe to keep the thumb in the /files directory
130+
*/
131+
private static String getFilesDir(Context context) {
132+
if (android.os.Build.VERSION.SDK_INT >= /* 21 */ android.os.Build.VERSION_CODES.LOLLIPOP) {
133+
return context.getNoBackupFilesDir().getPath();
134+
} else {
135+
return context.getFilesDir().getPath();
136+
}
137+
}
138+
125139
}

‎test-app/app/src/main/java/com/tns/RuntimeHelper.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ public static Runtime initRuntime(Application app) {
8585

8686
String outputDir = app.getFilesDir().getPath() + File.separator;
8787

88-
aE.extractAssets(app, "app", outputDir, extractPolicy);
89-
aE.extractAssets(app, "internal", outputDir, extractPolicy);
90-
aE.extractAssets(app, "metadata", outputDir, extractPolicy);
88+
// will force deletion of previously extracted files in app/files directories
89+
// see https://github.com/NativeScript/NativeScript/issues/4137 for reference
90+
boolean removePreviouslyInstalledAssets = true;
91+
aE.extractAssets(app, "app", outputDir, extractPolicy, removePreviouslyInstalledAssets);
92+
aE.extractAssets(app, "internal", outputDir, extractPolicy, removePreviouslyInstalledAssets);
93+
aE.extractAssets(app, "metadata", outputDir, extractPolicy, false);
9194

92-
// enable with flags?
9395
boolean shouldExtractSnapshots = true;
9496

9597
// will extract snapshot of the device appropriate architecture
@@ -98,7 +100,7 @@ public static Runtime initRuntime(Application app) {
98100
logger.write("Extracting snapshot blob");
99101
}
100102

101-
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy);
103+
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy, removePreviouslyInstalledAssets);
102104
}
103105

104106
extractPolicy.setAssetsThumb(app);

0 commit comments

Comments
 (0)