Skip to content

Commit e22b7ed

Browse files
authored
Merge pull request #1128 from NativeScript/trifonov/livesync-on-create
Add livesync in onCreate method
2 parents f289322 + 887f600 commit e22b7ed

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public static Runtime initRuntime(Application app) {
192192

193193
// if app is in debuggable mode run livesync service
194194
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
195-
initLiveSync(runtime, logger, app);
195+
// initLiveSync(runtime, logger, app);
196196
}
197197

198198
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
@@ -262,6 +262,15 @@ public void onReceive(Context context, Intent intent) {
262262
context.registerReceiver(timezoneReceiver, timezoneFilter);
263263
}
264264

265+
public static void initLiveSync(Application app) {
266+
Runtime currentRuntime = Runtime.getCurrentRuntime();
267+
if (!currentRuntime.getIsLiveSyncStarted()) {
268+
initLiveSync(currentRuntime, currentRuntime.getLogger(), app);
269+
currentRuntime.setIsLiveSyncStarted(true);
270+
}
271+
272+
}
273+
265274
public static void initLiveSync(Runtime runtime, Logger logger, Application app){
266275
boolean isDebuggable = Util.isDebuggableApp(app);
267276

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
342342
w.writeln();
343343

344344
boolean isApplicationClass = isApplicationClass(clazz, classes);
345+
boolean isActivityClass = isActivityClass(clazz, classes);
346+
345347
if (isApplicationClass && !packageName.equals("com.tns")) {
346348
w.writeln("import com.tns.RuntimeHelper;");
347349
w.writeln("import com.tns.Runtime;");
@@ -435,7 +437,7 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
435437
}
436438
for (Method m : ifaceMethods) {
437439
if (!notImplementedObjectMethods.contains(m)) {
438-
writeMethodBody(m, w, isApplicationClass, true);
440+
writeMethodBody(m, w, isApplicationClass, isActivityClass, true);
439441
}
440442
}
441443
} else {
@@ -449,7 +451,7 @@ private void writeBinding(Writer w, DataRow dataRow, JavaClass clazz, String pac
449451
if (interfaceMethods.contains(m)) {
450452
isInterfaceMethod = true;
451453
}
452-
writeMethodBody(m, w, isApplicationClass, isInterfaceMethod);
454+
writeMethodBody(m, w, isApplicationClass, isActivityClass, isInterfaceMethod);
453455
}
454456
}
455457
}
@@ -479,7 +481,7 @@ private boolean isClassApplication(JavaClass clazz) {
479481
className.equals("android.test.mock.MockApplication");
480482
}
481483

482-
private void writeMethodBody(Method m, Writer w, boolean isApplicationClass, boolean isInterfaceMethod) {
484+
private void writeMethodBody(Method m, Writer w, boolean isApplicationClass, boolean isActivityClass, boolean isInterfaceMethod) {
483485
String visibility = m.isPublic() ? "public" : "protected";
484486
w.write("\t");
485487
w.write(visibility);
@@ -491,7 +493,7 @@ private void writeMethodBody(Method m, Writer w, boolean isApplicationClass, boo
491493
w.write(" ");
492494
writeThrowsClause(m, w);
493495
w.writeln(" {");
494-
writeMethodBody(m, false, isApplicationClass, w, isInterfaceMethod);
496+
writeMethodBody(m, false, isApplicationClass, isActivityClass, w, isInterfaceMethod);
495497
w.writeln("\t}");
496498
w.writeln();
497499
}
@@ -570,7 +572,7 @@ private void writeConstructors(JavaClass clazz, String classname, boolean hasIni
570572
w.writeln("\t\tcom.tns.Runtime.initInstance(this);");
571573
}
572574
if (hasInitMethod) {
573-
writeMethodBody(c, true, false, w, false);
575+
writeMethodBody(c, true, false, false, w, false);
574576
}
575577
if (isClassApplication(clazz)) {
576578
//get instance method
@@ -584,7 +586,7 @@ private void writeConstructors(JavaClass clazz, String classname, boolean hasIni
584586
}
585587
}
586588

587-
private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicationClass, Writer w, boolean isInterfaceMethod) {
589+
private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicationClass, boolean isActivityClass, Writer w, boolean isInterfaceMethod) {
588590
if (m.getName().equals("onCreate") && isApplicationClass) {
589591
w.writeln("\t\tcom.tns.Runtime runtime = RuntimeHelper.initRuntime(this);");
590592
}
@@ -651,6 +653,11 @@ private void writeMethodBody(Method m, boolean isConstructor, boolean isApplicat
651653
w.writeln("\t\t\truntime.run();");
652654
w.writeln("\t\t}");
653655
}
656+
657+
// call liveSync initialization
658+
if (m.getName().equals("onCreate") && isActivityClass) {
659+
w.writeln("\t\tcom.tns.RuntimeHelper.initLiveSync(this.getApplication());");
660+
}
654661
}
655662

656663
private void writeType(Type t, Writer w) {
@@ -715,6 +722,31 @@ private boolean isApplicationClass(JavaClass clazz, Map<String, JavaClass> class
715722
return isApplicationClass;
716723
}
717724

725+
private boolean isActivityClass(JavaClass clazz, Map<String, JavaClass> classes) throws ClassNotFoundException {
726+
boolean isActivityClass = false;
727+
728+
String activityClassname = "android.app.Activity";
729+
730+
JavaClass currentClass = clazz;
731+
while (true) {
732+
String currentClassname = currentClass.getClassName();
733+
734+
isActivityClass = currentClassname.equals(activityClassname);
735+
if (isActivityClass) {
736+
break;
737+
}
738+
739+
if (currentClassname.endsWith("java.lang.Object")) {
740+
break;
741+
}
742+
743+
String superClassName = currentClass.getSuperclassName();
744+
currentClass = getClass(superClassName);
745+
}
746+
747+
return isActivityClass;
748+
}
749+
718750
private JavaClass getClass(String className) throws ClassNotFoundException {
719751
JavaClass clazz = classes.get(className.replace('$', '.'));
720752

test-app/runtime/src/main/java/com/tns/Runtime.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ void passUncaughtExceptionToJs(Throwable ex, String stackTrace) {
104104

105105
private Logger logger;
106106

107+
private boolean isLiveSyncStarted;
108+
public boolean getIsLiveSyncStarted() { return this.isLiveSyncStarted; }
109+
public void setIsLiveSyncStarted(boolean value) { this.isLiveSyncStarted = value; }
110+
111+
public Logger getLogger() {
112+
return this.logger;
113+
}
114+
107115
private ThreadScheduler threadScheduler;
108116

109117
private DexFactory dexFactory;

0 commit comments

Comments
 (0)