Skip to content

Commit

Permalink
Do not raise exception when NativeScriptApplication.onCreate is calle…
Browse files Browse the repository at this point in the history
…d for a second time during initialization.
  • Loading branch information
atanasovg committed Feb 22, 2016
1 parent 8661343 commit a89aa46
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 62 deletions.
7 changes: 6 additions & 1 deletion src/jni/NativePlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,12 @@ void NativePlatform::AppInitCallback(const v8::FunctionCallbackInfo<v8::Value>&

// TODO: find another way to get "com/tns/NativeScriptApplication" metadata (move it to more appropriate place)
auto node = MetadataNode::GetOrCreate("com/tns/NativeScriptApplication");
auto appInstance = node->CreateJSWrapper(isolate);
auto appInstance = g_objectManager->GetJsObjectByJavaObject(AppJavaObjectID);
if(appInstance.IsEmpty())
{
appInstance = node->CreateJSWrapper(isolate);
}

DEBUG_WRITE("Application object created id: %d", appInstance->GetIdentityHash());

auto implementationObject = args[0]->ToObject();
Expand Down
135 changes: 74 additions & 61 deletions src/src/com/tns/NativeScriptApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -959,83 +959,96 @@ private boolean hasErrorIntent()

return hasErrorIntent;
}

private boolean initializing;

public void onCreate()
{
System.loadLibrary("NativeScript");

Logger logger = new LogcatLogger(BuildConfig.DEBUG, this);

if(this.initializing)
{
// This may happen if the app.init object is not populated while accessing the Application object
return;
}

boolean showErrorIntent = hasErrorIntent();
if (!showErrorIntent)
if(showErrorIntent)
{
appInstance = this;

Thread.UncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, this);
return;
}

this.initializing = true;

System.loadLibrary("NativeScript");
Logger logger = new LogcatLogger(BuildConfig.DEBUG, this);

appInstance = this;

prepareAppBuilderCallbackImpl(logger, exHandler);
Thread.UncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, this);

Thread.setDefaultUncaughtExceptionHandler(exHandler);
prepareAppBuilderCallbackImpl(logger, exHandler);

// TODO: refactor
ExtractPolicy extractPolicy = (appBuilderCallbackImpl != null)
? appBuilderCallbackImpl.getExtractPolicy()
: new DefaultExtractPolicy(logger);
boolean skipAssetExtraction = Util.runPlugin(logger, this);
if (!skipAssetExtraction)
{
new AssetExtractor(null, logger).extractAssets(this, extractPolicy);
}
Thread.setDefaultUncaughtExceptionHandler(exHandler);

if (appBuilderCallbackImpl != null)
{
appBuilderCallbackImpl.onCreate(this);
}
// TODO: refactor
ExtractPolicy extractPolicy = (appBuilderCallbackImpl != null)
? appBuilderCallbackImpl.getExtractPolicy()
: new DefaultExtractPolicy(logger);
boolean skipAssetExtraction = Util.runPlugin(logger, this);
if (!skipAssetExtraction)
{
new AssetExtractor(null, logger).extractAssets(this, extractPolicy);
}

if (NativeScriptSyncService.isSyncEnabled(this))
{
NativeScriptSyncService syncService = new NativeScriptSyncService(logger, this);
if (appBuilderCallbackImpl != null)
{
appBuilderCallbackImpl.onCreate(this);
}

syncService.sync();
syncService.startServer();
if (NativeScriptSyncService.isSyncEnabled(this))
{
NativeScriptSyncService syncService = new NativeScriptSyncService(logger, this);

// preserve this instance as strong reference
// do not preserve in NativeScriptApplication field inorder to make the code more portable
Platform.getOrCreateJavaObjectID(syncService);
}
else
{
if (logger.isEnabled())
{
logger.write("NativeScript LiveSync is not enabled.");
}
}
syncService.sync();
syncService.startServer();

String appName = this.getPackageName();
File rootDir = new File(this.getApplicationInfo().dataDir);
File appDir = this.getFilesDir();

ClassLoader classLoader = this.getClassLoader();
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
String dexThumb = null;
try
{
dexThumb = Util.getDexThumb(this);
}
catch (NameNotFoundException e)
// preserve this instance as strong reference
// do not preserve in NativeScriptApplication field inorder to make the code more portable
Platform.getOrCreateJavaObjectID(syncService);
}
else
{
if (logger.isEnabled())
{
if (logger.isEnabled())
logger.write("Error while getting current proxy thumb");
e.printStackTrace();
logger.write("NativeScript LiveSync is not enabled.");
}
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
// TODO: Refactor these 11 method parameters!!! E.g. create Settings abstract object and add default implementation object
Platform.init(this, workThreadScheduler, logger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb);
Platform.runScript(new File(appDir, "internal/prepareExtend.js"));
Platform.run();
}

onCreateInternal();
String appName = this.getPackageName();
File rootDir = new File(this.getApplicationInfo().dataDir);
File appDir = this.getFilesDir();

ClassLoader classLoader = this.getClassLoader();
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
String dexThumb = null;
try
{
dexThumb = Util.getDexThumb(this);
}
catch (NameNotFoundException e)
{
if (logger.isEnabled())
logger.write("Error while getting current proxy thumb");
e.printStackTrace();
}
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
// TODO: Refactor these 11 method parameters!!! E.g. create Settings abstract object and add default implementation object
Platform.init(this, workThreadScheduler, logger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb);
Platform.runScript(new File(appDir, "internal/prepareExtend.js"));
Platform.run();

onCreateInternal();

this.initializing = false;
}

private void prepareAppBuilderCallbackImpl(Logger logger, UncaughtExceptionHandler exHandler)
Expand Down Expand Up @@ -2166,4 +2179,4 @@ else if (name.equals("unregisterReceiver"))
private AppBuilderCallback appBuilderCallbackImpl;

private final String logTag = "NativeScriptApplication";
}
}

1 comment on commit a89aa46

@slavchev
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix for #362

Please sign in to comment.