Skip to content

Commit 7c3301c

Browse files
authored
Merge pull request #468 from NativeScript/pete/asset-extractor-snapshots
Add logic to extract snapshot blob for the suitable device architecture
2 parents 1235020 + 164b8ed commit 7c3301c

11 files changed

Lines changed: 260 additions & 256 deletions

File tree

build/project-template-gradle/src/main/java/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 = getAssetsThumb(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 getAssetsThumb(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
}

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

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,37 @@
44

55
import android.app.Application;
66
import android.content.pm.PackageManager.NameNotFoundException;
7+
import android.os.Build;
78
import android.os.Handler;
89
import android.os.Looper;
910
import android.util.Log;
1011
import java.io.IOException;
1112

12-
public class RuntimeHelper
13-
{
13+
public class RuntimeHelper {
1414
private final Application app;
15-
16-
public RuntimeHelper(Application app)
17-
{
15+
16+
public RuntimeHelper(Application app) {
1817
this.app = app;
1918
}
20-
21-
// hasErrorIntent tells you if there was an event (with an uncaught exception) raised from ErrorReport
22-
public boolean hasErrorIntent()
23-
{
19+
20+
// hasErrorIntent tells you if there was an event (with an uncaught
21+
// exception) raised from ErrorReport
22+
public boolean hasErrorIntent() {
2423
boolean hasErrorIntent = false;
25-
26-
try
27-
{
28-
//empty file just to check if there was a raised uncaught error by ErrorReport
24+
25+
try {
26+
// empty file just to check if there was a raised uncaught error by
27+
// ErrorReport
2928
File errFile = new File(app.getFilesDir(), ErrorReport.ERROR_FILE_NAME);
30-
31-
if (errFile.exists())
32-
{
29+
30+
if (errFile.exists()) {
3331
errFile.delete();
3432
hasErrorIntent = true;
3533
}
36-
}
37-
catch (Exception e)
38-
{
34+
} catch (Exception e) {
3935
Log.d(logTag, e.getMessage());
4036
}
41-
37+
4238
return hasErrorIntent;
4339
}
4440

@@ -49,76 +45,94 @@ public void initRuntime()
4945
}
5046

5147
System.loadLibrary("NativeScript");
52-
48+
5349
Logger logger = new LogcatLogger(app);
5450
Debugger debugger = new AndroidJsDebugger(app, logger);
55-
51+
5652
boolean showErrorIntent = hasErrorIntent();
57-
if (!showErrorIntent)
58-
{
53+
if (!showErrorIntent) {
5954
NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app);
6055

6156
Thread.setDefaultUncaughtExceptionHandler(exHandler);
62-
57+
6358
Async.Http.setApplicationContext(this.app);
64-
65-
ExtractPolicy extractPolicy = new DefaultExtractPolicy(logger);
59+
60+
DefaultExtractPolicy extractPolicy = new DefaultExtractPolicy(logger);
6661
boolean skipAssetExtraction = Util.runPlugin(logger, app);
67-
if (!skipAssetExtraction)
68-
{
69-
new AssetExtractor(null, logger).extractAssets(app, extractPolicy);
70-
}
71-
62+
7263
String appName = app.getPackageName();
7364
File rootDir = new File(app.getApplicationInfo().dataDir);
7465
File appDir = app.getFilesDir();
75-
76-
try
77-
{
66+
67+
try {
7868
appDir = appDir.getCanonicalFile();
69+
} catch (IOException e1) {
7970
}
80-
catch (IOException e1)
81-
{
71+
72+
if (!skipAssetExtraction) {
73+
if(logger.isEnabled()) {
74+
logger.write("Extracting assets...");
75+
}
76+
77+
AssetExtractor aE = new AssetExtractor(null, logger);
78+
79+
String outputDir = app.getFilesDir().getPath() + File.separator;
80+
81+
aE.extractAssets(app, "app", outputDir, extractPolicy);
82+
aE.extractAssets(app, "internal", outputDir, extractPolicy);
83+
aE.extractAssets(app, "metadata", outputDir, extractPolicy);
84+
85+
// enable with flags?
86+
boolean shouldExtractSnapshots = true;
87+
88+
// will extract snapshot of the device appropriate architecture
89+
if(shouldExtractSnapshots) {
90+
if(logger.isEnabled()) {
91+
logger.write("Extracting snapshot blob");
92+
}
93+
94+
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy);
95+
}
96+
97+
extractPolicy.setAssetsThumb(app);
8298
}
8399

100+
Object[] v8Config = V8Config.fromPackageJSON(appDir);
101+
84102
ClassLoader classLoader = app.getClassLoader();
85103
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
86104
String dexThumb = null;
87-
try
88-
{
105+
try {
89106
dexThumb = Util.getDexThumb(app);
90-
}
91-
catch (NameNotFoundException e)
92-
{
93-
if (logger.isEnabled()) logger.write("Error while getting current proxy thumb");
107+
} catch (NameNotFoundException e) {
108+
if (logger.isEnabled())
109+
logger.write("Error while getting current proxy thumb");
94110
e.printStackTrace();
95111
}
96112
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
97-
Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb);
113+
Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir,
114+
appDir, classLoader, dexDir, dexThumb, v8Config);
98115
Runtime runtime = new Runtime(config);
99-
116+
100117
exHandler.setRuntime(runtime);
101-
102-
if (NativeScriptSyncService.isSyncEnabled(this.app))
103-
{
118+
119+
if (NativeScriptSyncService.isSyncEnabled(this.app)) {
104120
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, this.app);
105121

106122
syncService.sync();
107123
syncService.startServer();
108124

109125
// preserve this instance as strong reference
110-
// do not preserve in NativeScriptApplication field inorder to make the code more portable
126+
// do not preserve in NativeScriptApplication field inorder to
127+
// make the code more portable
111128
// @@@
112-
//Runtime.getOrCreateJavaObjectID(syncService);
113-
}
114-
else
115-
{
116-
if (logger.isEnabled())
117-
{
129+
// Runtime.getOrCreateJavaObjectID(syncService);
130+
} else {
131+
if (logger.isEnabled()) {
118132
logger.write("NativeScript LiveSync is not enabled.");
119133
}
120134
}
121-
135+
122136
runtime.init();
123137
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
124138
try {
@@ -131,23 +145,6 @@ public void initRuntime()
131145
runtime.run();
132146
}
133147
}
134-
135-
/* public static boolean isDebuggableApp(Context context)
136-
{
137-
int flags;
138-
try
139-
{
140-
flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags;
141-
}
142-
catch (NameNotFoundException e)
143-
{
144-
flags = 0;
145-
e.printStackTrace();
146-
}
147148

148-
boolean isDebuggableApp = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
149-
return isDebuggableApp;
150-
}*/
151-
152149
private final String logTag = "MyApp";
153150
}

0 commit comments

Comments
 (0)