Skip to content

Commit a4988a8

Browse files
committed
remove specific logic from assetExtractor for handling snapshots; make assetExtractor parameterized regarding input and output dirs
1 parent 9d0f2e9 commit a4988a8

6 files changed

Lines changed: 125 additions & 165 deletions

File tree

src/jni/AssetExtractor.cpp

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,66 +14,41 @@ using namespace tns;
1414
std::string jstringToString(JNIEnv *env, jstring value);
1515
void mkdir_rec(const char *dir);
1616

17-
void AssetExtractor::ExtractAssets(JNIEnv *env, jobject obj, jstring apk, jstring outputDir, jboolean _forceOverwrite, jstring architecture)
17+
void AssetExtractor::ExtractAssets(JNIEnv *env, jobject obj, jstring apk, jstring input, jstring outputDir, jboolean _forceOverwrite)
1818
{
19-
auto arch = jstringToString(env, architecture);
20-
2119
auto forceOverwrite = JNI_TRUE == _forceOverwrite;
2220
auto strApk = jstringToString(env, apk);
21+
2322
auto baseDir = jstringToString(env, outputDir);
23+
24+
std::string filePrefix("assets/");
25+
int prefixLen = filePrefix.length();
26+
filePrefix.append(jstringToString(env, input));
27+
auto prfx = filePrefix.c_str();
28+
2429
int err = 0;
2530
auto z = zip_open(strApk.c_str(), 0, &err);
31+
2632
assert(z != nullptr);
2733
zip_int64_t num = zip_get_num_entries(z, 0);
2834
struct zip_stat sb;
2935
struct zip_file *zf;
3036
char buf[65536];
3137
auto pathcopy = new char[1024];
32-
bool snapshotFound = 0;
3338

3439
for (zip_int64_t i = 0; i < num; i++)
3540
{
3641
zip_stat_index(z, i, ZIP_STAT_MTIME, &sb);
37-
if (strstr(sb.name, "assets/") == sb.name)
42+
if (strstr(sb.name, prfx) == sb.name)
3843
{
39-
auto name = sb.name + 7; // strlen("assets/") == 7
40-
41-
std::string assetFullname(baseDir);
42-
assetFullname.append(name);
43-
4444

45-
auto nameStr = std::string(name);
4645

47-
/*
48-
* Look for snapshots and extract only those suitable for the current CPU architecture
49-
*/
50-
auto sSpos = nameStr.find("snapshots/");
46+
DEBUG_WRITE("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %s", sb.name);
5147

52-
if(sSpos != std::string::npos) {
53-
const std::string ARMEABIV7 = "armeabi-v7a";
54-
const std::string X86 = "x86";
55-
const std::string ARM64V8 = "arm64_v8a";
48+
auto name = sb.name + prefixLen; // strlen("assets/") == 7
5649

57-
if(snapshotFound) {
58-
continue;
59-
}
60-
61-
if (arch == ARMEABIV7 && nameStr.find(ARMEABIV7) != std::string::npos) {
62-
DEBUG_WRITE("EXTRACTED THE armeabi-v7a architecture snapshot blob!");
63-
snapshotFound = 1;
64-
} else if (arch == X86 && nameStr.find(X86) != std::string::npos) {
65-
DEBUG_WRITE("EXTRACTED THE x86 architecture snapshot blob!");
66-
snapshotFound = 1;
67-
} else if (arch == ARM64V8 && nameStr.find(ARM64V8) != std::string::npos) {
68-
DEBUG_WRITE("EXTRACTED THE arm64-v8a architecture snapshot blob!");
69-
snapshotFound = 1;
70-
}
71-
72-
// the snapshot that was found does not satisfy the architecture
73-
if(!snapshotFound) {
74-
continue;
75-
}
76-
}
50+
std::string assetFullname(baseDir);
51+
assetFullname.append(name);
7752

7853
struct stat attrib;
7954
auto shouldOverwrite = true;

src/jni/AssetExtractor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace tns
88
class AssetExtractor
99
{
1010
public:
11-
static void ExtractAssets(JNIEnv *env, jobject obj, jstring apk, jstring outputDir, jboolean _forceOverwrite, jstring architecture);
11+
static void ExtractAssets(JNIEnv *env, jobject obj, jstring apk, jstring inputDir, jstring outputDir, jboolean _forceOverwrite);
1212

1313
private:
1414
static std::string jstringToString(JNIEnv *env, jstring value);

src/jni/com_tns_AssetExtractor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ using namespace std;
1010

1111
void mkdir_rec(const char *dir);
1212

13-
extern "C" void Java_com_tns_AssetExtractor_extractAssets(JNIEnv *env, jobject obj, jstring apk, jstring outputDir, jboolean _forceOverwrite, jstring architecture)
13+
extern "C" void Java_com_tns_AssetExtractor_extractAssets(JNIEnv *env, jobject obj, jstring apk, jstring inputDir, jstring outputDir, jboolean _forceOverwrite)
1414
{
1515
try
1616
{
17-
AssetExtractor::ExtractAssets(env, obj, apk, outputDir, _forceOverwrite, architecture);
17+
AssetExtractor::ExtractAssets(env, obj, apk, inputDir, outputDir, _forceOverwrite);
1818
}
1919
catch (NativeScriptException& e)
2020
{

src/src/com/tns/AssetExtractor.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@
44

55

66
import android.content.Context;
7-
import android.os.Build;
87

98
public class AssetExtractor
109
{
11-
private native void extractAssets(String apkPath, String outputDir, boolean checkForNewerFiles, String architecture);
12-
10+
private native void extractAssets(String apkPath, String input, String outputDir, boolean checkForNewerFiles);
1311
private final Logger logger;
1412

1513
public AssetExtractor(File libPath, Logger logger)
1614
{
1715
this.logger = logger;
1816
}
1917

20-
public void extractAssets(Context context, ExtractPolicy extractPolicy)
18+
public void extractAssets(Context context, String inputPath, String outputPath, ExtractPolicy extractPolicy)
2119
{
2220
FileExtractor extractor = extractPolicy.extractor();
2321
if (extractor != null)
@@ -30,15 +28,11 @@ public void extractAssets(Context context, ExtractPolicy extractPolicy)
3028
}
3129
else if (extractPolicy.shouldExtract(context))
3230
{
33-
String appRoot = context.getFilesDir().getPath() + File.separator;
3431
String apkPath = context.getPackageCodePath();
3532

36-
String arch = Build.CPU_ABI; //Build.SUPPORTED_ABIS;
37-
// String abi2 = Build.CPU_ABI2;
38-
3933
boolean forceOverwrite = extractPolicy.forceOverwrite();
40-
// String arch = System.getProperty("os.arch");
41-
extractAssets(apkPath, appRoot, forceOverwrite, arch);
34+
35+
extractAssets(apkPath, inputPath, outputPath, forceOverwrite);
4236
}
4337
}
4438
}

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

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,122 +13,111 @@
1313
import android.content.Context;
1414
import android.content.pm.PackageInfo;
1515
import android.content.pm.PackageManager;
16+
import android.content.pm.PackageManager.NameNotFoundException;
1617

1718
import com.tns.Logger;
1819
import com.tns.ExtractPolicy;
1920
import com.tns.FileExtractor;
2021

21-
public class DefaultExtractPolicy implements ExtractPolicy
22-
{
22+
public class DefaultExtractPolicy implements ExtractPolicy {
2323
private final Logger logger;
2424

2525
private final static String ASSETS_THUMB_FILENAME = "assetsThumb";
2626

27-
public DefaultExtractPolicy(Logger logger)
28-
{
27+
public DefaultExtractPolicy(Logger logger) {
2928
this.logger = logger;
3029
}
3130

32-
public boolean shouldExtract(android.content.Context context)
33-
{
34-
String assetsThumb = generateAssetsThumb(context);
35-
if (assetsThumb != null)
36-
{
37-
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
38-
String oldAssetsThumb = getCachedAssetsThumb(assetsThumbFilePath);
39-
if (oldAssetsThumb == null || !assetsThumb.equals(oldAssetsThumb))
40-
{
41-
saveNewAssetsThumb(assetsThumb, assetsThumbFilePath);
31+
public boolean shouldExtract(Context context)
32+
{
33+
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
34+
String oldAssetsThumb = getCachedAssetsThumb(assetsThumbFilePath);
35+
if (oldAssetsThumb == null) {
36+
return true;
37+
} else {
38+
String currentThumb = getAssetThumb(context);
39+
40+
if(currentThumb != null && !currentThumb.equals(oldAssetsThumb)) {
4241
return true;
4342
}
4443
}
45-
44+
4645
return false;
4746
}
4847

49-
public boolean forceOverwrite()
50-
{
48+
public void setAssetsThumb(Context context) {
49+
String assetsThumb = generateAssetsThumb(context);
50+
if(assetsThumb != null) {
51+
String assetsThumbFilePath = context.getFilesDir().getPath() + File.separatorChar + ASSETS_THUMB_FILENAME;
52+
saveNewAssetsThumb(assetsThumb, assetsThumbFilePath);
53+
}
54+
}
55+
56+
public boolean forceOverwrite() {
5157
return true;
5258
}
5359

54-
public FileExtractor extractor()
55-
{
60+
public FileExtractor extractor() {
5661
return null;
5762
}
5863

59-
private String generateAssetsThumb(Context context)
60-
{
61-
try
62-
{
64+
public String getAssetThumb(Context context) {
65+
return generateAssetsThumb(context);
66+
}
67+
68+
private String generateAssetsThumb(Context context) {
69+
try {
6370
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
6471
int code = packageInfo.versionCode;
6572
long updateTime = packageInfo.lastUpdateTime;
6673
return String.valueOf(updateTime) + "-" + String.valueOf(code);
67-
}
68-
catch (PackageManager.NameNotFoundException e)
69-
{
74+
} catch (PackageManager.NameNotFoundException e) {
7075
logger.write("Error while getting current assets thumb");
7176
e.printStackTrace();
7277
}
7378

7479
return null;
7580
}
7681

77-
private String getCachedAssetsThumb(String assetsThumbFilePath)
78-
{
79-
try
80-
{
82+
private String getCachedAssetsThumb(String assetsThumbFilePath) {
83+
try {
8184
File cachedThumbFile = new File(assetsThumbFilePath);
82-
if (cachedThumbFile.exists())
83-
{
85+
if (cachedThumbFile.exists()) {
8486
FileInputStream in = new FileInputStream(cachedThumbFile);
8587
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
8688
String cachedThumb = reader.readLine();
8789
reader.close();
8890
in.close();
8991
return cachedThumb;
9092
}
91-
}
92-
catch (FileNotFoundException e)
93-
{
93+
} catch (FileNotFoundException e) {
9494
logger.write("Error while getting current assets thumb");
9595
e.printStackTrace();
96-
}
97-
catch (IOException e)
98-
{
96+
} catch (IOException e) {
9997
logger.write("Error while getting current asstes thumb");
10098
e.printStackTrace();
10199
}
102100

103101
return null;
104102
}
105103

106-
private void saveNewAssetsThumb(String newThumb, String assetsThumbFile)
107-
{
104+
private void saveNewAssetsThumb(String newThumb, String assetsThumbFile) {
108105
File cachedThumbFile = new File(assetsThumbFile);
109-
try
110-
{
106+
try {
111107
FileOutputStream out = new FileOutputStream(cachedThumbFile, false);
112108
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
113-
try
114-
{
109+
try {
115110
writer.write(newThumb);
116111
writer.newLine();
117112
writer.flush();
118-
}
119-
finally
120-
{
113+
} finally {
121114
writer.close();
122115
out.close();
123116
}
124-
}
125-
catch (FileNotFoundException e)
126-
{
117+
} catch (FileNotFoundException e) {
127118
logger.write("Error while writting current assets thumb");
128119
e.printStackTrace();
129-
}
130-
catch (IOException e)
131-
{
120+
} catch (IOException e) {
132121
logger.write("Error while writting current assets thumb");
133122
e.printStackTrace();
134123
}

0 commit comments

Comments
 (0)