|
4 | 4 |
|
5 | 5 | import android.app.Application; |
6 | 6 | import android.content.pm.PackageManager.NameNotFoundException; |
| 7 | +import android.os.Build; |
7 | 8 | import android.os.Handler; |
8 | 9 | import android.os.Looper; |
9 | 10 | import android.util.Log; |
10 | 11 | import java.io.IOException; |
11 | 12 |
|
12 | | -public class RuntimeHelper |
13 | | -{ |
| 13 | +public class RuntimeHelper { |
14 | 14 | private final Application app; |
15 | | - |
16 | | - public RuntimeHelper(Application app) |
17 | | - { |
| 15 | + |
| 16 | + public RuntimeHelper(Application app) { |
18 | 17 | this.app = app; |
19 | 18 | } |
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() { |
24 | 23 | 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 |
29 | 28 | File errFile = new File(app.getFilesDir(), ErrorReport.ERROR_FILE_NAME); |
30 | | - |
31 | | - if (errFile.exists()) |
32 | | - { |
| 29 | + |
| 30 | + if (errFile.exists()) { |
33 | 31 | errFile.delete(); |
34 | 32 | hasErrorIntent = true; |
35 | 33 | } |
36 | | - } |
37 | | - catch (Exception e) |
38 | | - { |
| 34 | + } catch (Exception e) { |
39 | 35 | Log.d(logTag, e.getMessage()); |
40 | 36 | } |
41 | | - |
| 37 | + |
42 | 38 | return hasErrorIntent; |
43 | 39 | } |
44 | | - |
45 | | - public void initRuntime() |
46 | | - { |
| 40 | + |
| 41 | + public void initRuntime() { |
47 | 42 | System.loadLibrary("NativeScript"); |
48 | | - |
| 43 | + |
49 | 44 | Logger logger = new LogcatLogger(app); |
50 | 45 | Debugger debugger = new AndroidJsDebugger(app, logger); |
51 | | - |
| 46 | + |
52 | 47 | boolean showErrorIntent = hasErrorIntent(); |
53 | | - if (!showErrorIntent) |
54 | | - { |
| 48 | + if (!showErrorIntent) { |
55 | 49 | NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app); |
56 | 50 |
|
57 | 51 | Thread.setDefaultUncaughtExceptionHandler(exHandler); |
58 | | - |
| 52 | + |
59 | 53 | Async.Http.setApplicationContext(this.app); |
60 | | - |
61 | | - ExtractPolicy extractPolicy = new DefaultExtractPolicy(logger); |
| 54 | + |
| 55 | + DefaultExtractPolicy extractPolicy = new DefaultExtractPolicy(logger); |
62 | 56 | boolean skipAssetExtraction = Util.runPlugin(logger, app); |
63 | | - if (!skipAssetExtraction) |
64 | | - { |
65 | | - new AssetExtractor(null, logger).extractAssets(app, extractPolicy); |
66 | | - } |
67 | | - |
| 57 | + |
68 | 58 | String appName = app.getPackageName(); |
69 | 59 | File rootDir = new File(app.getApplicationInfo().dataDir); |
70 | 60 | File appDir = app.getFilesDir(); |
71 | | - |
72 | | - try |
73 | | - { |
| 61 | + |
| 62 | + try { |
74 | 63 | appDir = appDir.getCanonicalFile(); |
| 64 | + } catch (IOException e1) { |
75 | 65 | } |
76 | | - catch (IOException e1) |
77 | | - { |
| 66 | + |
| 67 | + if (!skipAssetExtraction) { |
| 68 | + if(logger.isEnabled()) { |
| 69 | + logger.write("Extracting assets..."); |
| 70 | + } |
| 71 | + |
| 72 | + AssetExtractor aE = new AssetExtractor(null, logger); |
| 73 | + |
| 74 | + String outputDir = app.getFilesDir().getPath() + File.separator; |
| 75 | + |
| 76 | + aE.extractAssets(app, "app", outputDir, extractPolicy); |
| 77 | + aE.extractAssets(app, "internal", outputDir, extractPolicy); |
| 78 | + aE.extractAssets(app, "metadata", outputDir, extractPolicy); |
| 79 | + |
| 80 | + // enable with flags? |
| 81 | + boolean shouldExtractSnapshots = true; |
| 82 | + |
| 83 | + // will extract snapshot of the device appropriate architecture |
| 84 | + if(shouldExtractSnapshots) { |
| 85 | + if(logger.isEnabled()) { |
| 86 | + logger.write("Extracting snapshot blob"); |
| 87 | + } |
| 88 | + |
| 89 | + aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy); |
| 90 | + } |
| 91 | + |
| 92 | + extractPolicy.setAssetsThumb(app); |
78 | 93 | } |
79 | 94 |
|
| 95 | + Object[] v8Config = V8Config.fromPackageJSON(appDir); |
| 96 | + |
80 | 97 | ClassLoader classLoader = app.getClassLoader(); |
81 | 98 | File dexDir = new File(rootDir, "code_cache/secondary-dexes"); |
82 | 99 | String dexThumb = null; |
83 | | - try |
84 | | - { |
| 100 | + try { |
85 | 101 | dexThumb = Util.getDexThumb(app); |
86 | | - } |
87 | | - catch (NameNotFoundException e) |
88 | | - { |
89 | | - if (logger.isEnabled()) logger.write("Error while getting current proxy thumb"); |
| 102 | + } catch (NameNotFoundException e) { |
| 103 | + if (logger.isEnabled()) |
| 104 | + logger.write("Error while getting current proxy thumb"); |
90 | 105 | e.printStackTrace(); |
91 | 106 | } |
92 | 107 | ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper())); |
93 | | - Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb); |
| 108 | + Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir, |
| 109 | + appDir, classLoader, dexDir, dexThumb, v8Config); |
94 | 110 | Runtime runtime = new Runtime(config); |
95 | | - |
| 111 | + |
96 | 112 | exHandler.setRuntime(runtime); |
97 | | - |
98 | | - if (NativeScriptSyncService.isSyncEnabled(this.app)) |
99 | | - { |
| 113 | + |
| 114 | + if (NativeScriptSyncService.isSyncEnabled(this.app)) { |
100 | 115 | NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, this.app); |
101 | 116 |
|
102 | 117 | syncService.sync(); |
103 | 118 | syncService.startServer(); |
104 | 119 |
|
105 | 120 | // preserve this instance as strong reference |
106 | | - // do not preserve in NativeScriptApplication field inorder to make the code more portable |
| 121 | + // do not preserve in NativeScriptApplication field inorder to |
| 122 | + // make the code more portable |
107 | 123 | // @@@ |
108 | | - //Runtime.getOrCreateJavaObjectID(syncService); |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - if (logger.isEnabled()) |
113 | | - { |
| 124 | + // Runtime.getOrCreateJavaObjectID(syncService); |
| 125 | + } else { |
| 126 | + if (logger.isEnabled()) { |
114 | 127 | logger.write("NativeScript LiveSync is not enabled."); |
115 | 128 | } |
116 | 129 | } |
117 | | - |
| 130 | + |
118 | 131 | runtime.init(); |
119 | 132 | runtime.runScript(new File(appDir, "internal/ts_helpers.js")); |
120 | 133 | Runtime.initInstance(this.app); |
121 | 134 | runtime.run(); |
122 | 135 | } |
123 | 136 | } |
124 | | - |
125 | | -/* public static boolean isDebuggableApp(Context context) |
126 | | - { |
127 | | - int flags; |
128 | | - try |
129 | | - { |
130 | | - flags = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.flags; |
131 | | - } |
132 | | - catch (NameNotFoundException e) |
133 | | - { |
134 | | - flags = 0; |
135 | | - e.printStackTrace(); |
136 | | - } |
137 | 137 |
|
138 | | - boolean isDebuggableApp = ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0); |
139 | | - return isDebuggableApp; |
140 | | - }*/ |
141 | | - |
142 | 138 | private final String logTag = "MyApp"; |
143 | 139 | } |
0 commit comments