Skip to content

Commit 8024e44

Browse files
author
Mihail Slavchev
authored
Merge pull request #500 from NativeScript/slavchev/update-project-template
Update project template in order to execute Runtime.run() method at t…
2 parents 144ca97 + ffc5a2e commit 8024e44

File tree

6 files changed

+37
-36
lines changed

6 files changed

+37
-36
lines changed
90 Bytes
Binary file not shown.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public NativeScriptApplication() {
1212

1313
public void onCreate() {
1414
super.onCreate();
15-
new RuntimeHelper(this).initRuntime();
15+
com.tns.Runtime runtime = RuntimeHelper.initRuntime(this);
16+
if (runtime !=null) {
17+
runtime.run();
18+
}
1619
}
1720

1821
public static Application getInstance() {

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
import android.util.Log;
1111
import java.io.IOException;
1212

13-
public class RuntimeHelper {
14-
private final Application app;
15-
16-
public RuntimeHelper(Application app) {
17-
this.app = app;
18-
}
13+
public final class RuntimeHelper {
14+
private RuntimeHelper() {}
1915

2016
// hasErrorIntent tells you if there was an event (with an uncaught
2117
// exception) raised from ErrorReport
22-
public boolean hasErrorIntent() {
18+
public static boolean hasErrorIntent(Application app) {
2319
boolean hasErrorIntent = false;
2420

2521
try {
@@ -38,18 +34,19 @@ public boolean hasErrorIntent() {
3834
return hasErrorIntent;
3935
}
4036

41-
public void initRuntime()
37+
public static Runtime initRuntime(Application app)
4238
{
4339
if (Runtime.isInitialized()) {
44-
return;
40+
return Runtime.getCurrentRuntime();
4541
}
4642

4743
System.loadLibrary("NativeScript");
4844

45+
Runtime runtime = null;
4946
Logger logger = new LogcatLogger(app);
50-
Debugger debugger = AndroidJsDebugger.isDebuggableApp(this.app) ? new AndroidJsDebugger(app, logger) : null;
47+
Debugger debugger = AndroidJsDebugger.isDebuggableApp(app) ? new AndroidJsDebugger(app, logger) : null;
5148

52-
boolean showErrorIntent = hasErrorIntent();
49+
boolean showErrorIntent = hasErrorIntent(app);
5350
if (!showErrorIntent) {
5451
NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app);
5552

@@ -110,12 +107,12 @@ public void initRuntime()
110107
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
111108
Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir,
112109
appDir, classLoader, dexDir, dexThumb, v8Config);
113-
Runtime runtime = new Runtime(config);
110+
runtime = new Runtime(config);
114111

115112
exHandler.setRuntime(runtime);
116113

117-
if (NativeScriptSyncService.isSyncEnabled(this.app)) {
118-
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, this.app);
114+
if (NativeScriptSyncService.isSyncEnabled(app)) {
115+
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, app);
119116

120117
syncService.sync();
121118
syncService.startServer();
@@ -141,14 +138,14 @@ public void initRuntime()
141138

142139
try {
143140
// put this call in a try/catch block because with the latest changes in the modules it is not granted that NativeScriptApplication is extended through JavaScript.
144-
Runtime.initInstance(this.app);
141+
Runtime.initInstance(app);
145142
}
146143
catch (Exception e) {
147144

148145
}
149-
runtime.run();
150146
}
147+
return runtime;
151148
}
152149

153-
private final String logTag = "MyApp";
150+
private static final String logTag = "MyApp";
154151
}

runtime/src/main/java/com/tns/Runtime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public int getRuntimeId()
122122
return this.runtimeId;
123123
}
124124

125-
private static Runtime getCurrentRuntime()
125+
public static Runtime getCurrentRuntime()
126126
{
127127
Runtime runtime = currentRuntime.get();
128128

test-app/app/src/main/java/com/tns/NativeScriptApplication.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public NativeScriptApplication() {
1212

1313
public void onCreate() {
1414
super.onCreate();
15-
new RuntimeHelper(this).initRuntime();
15+
com.tns.Runtime runtime = RuntimeHelper.initRuntime(this);
16+
if (runtime !=null) {
17+
runtime.run();
18+
}
1619
}
1720

1821
public static Application getInstance() {

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
import android.util.Log;
1111
import java.io.IOException;
1212

13-
public class RuntimeHelper {
14-
private final Application app;
15-
16-
public RuntimeHelper(Application app) {
17-
this.app = app;
13+
public final class RuntimeHelper {
14+
private RuntimeHelper() {
1815
}
1916

2017
// hasErrorIntent tells you if there was an event (with an uncaught
2118
// exception) raised from ErrorReport
22-
public boolean hasErrorIntent() {
19+
private static boolean hasErrorIntent(Application app) {
2320
boolean hasErrorIntent = false;
2421

2522
try {
@@ -38,18 +35,19 @@ public boolean hasErrorIntent() {
3835
return hasErrorIntent;
3936
}
4037

41-
public void initRuntime()
38+
public static Runtime initRuntime(Application app)
4239
{
4340
if (Runtime.isInitialized()) {
44-
return;
41+
return Runtime.getCurrentRuntime();
4542
}
4643

4744
System.loadLibrary("NativeScript");
4845

4946
Logger logger = new LogcatLogger(app);
50-
Debugger debugger = AndroidJsDebugger.isDebuggableApp(this.app) ? new AndroidJsDebugger(app, logger) : null;
47+
Debugger debugger = AndroidJsDebugger.isDebuggableApp(app) ? new AndroidJsDebugger(app, logger) : null;
5148

52-
boolean showErrorIntent = hasErrorIntent();
49+
Runtime runtime = null;
50+
boolean showErrorIntent = hasErrorIntent(app);
5351
if (!showErrorIntent) {
5452
NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app);
5553

@@ -110,12 +108,12 @@ public void initRuntime()
110108
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
111109
Configuration config = new Configuration(workThreadScheduler, logger, debugger, appName, null, rootDir,
112110
appDir, classLoader, dexDir, dexThumb, v8Config);
113-
Runtime runtime = new Runtime(config);
111+
runtime = new Runtime(config);
114112

115113
exHandler.setRuntime(runtime);
116114

117-
if (NativeScriptSyncService.isSyncEnabled(this.app)) {
118-
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, this.app);
115+
if (NativeScriptSyncService.isSyncEnabled(app)) {
116+
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, app);
119117

120118
syncService.sync();
121119
syncService.startServer();
@@ -141,14 +139,14 @@ public void initRuntime()
141139

142140
try {
143141
// put this call in a try/catch block because with the latest changes in the modules it is not granted that NativeScriptApplication is extended through JavaScript.
144-
Runtime.initInstance(this.app);
142+
Runtime.initInstance(app);
145143
}
146144
catch (Exception e) {
147145

148146
}
149-
runtime.run();
150147
}
148+
return runtime;
151149
}
152150

153-
private final String logTag = "MyApp";
151+
private static final String logTag = "MyApp";
154152
}

0 commit comments

Comments
 (0)