Skip to content

Commit 19747db

Browse files
author
Nathanael Anderson
committed
Fix metadata crash on startup with Local Notification plugin.
1 parent 2ab8fd1 commit 19747db

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

test-app/runtime/src/main/cpp/MetadataNode.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@
1212
#include <sstream>
1313
#include <cctype>
1414
#include <dirent.h>
15+
#include <errno.h>
16+
#include <android/log.h>
17+
#include <unistd.h>
1518
#include "ManualInstrumentation.h"
1619
#include "JSONObjectHelper.h"
1720

21+
22+
1823
#include "v8.h"
1924

2025
using namespace v8;
@@ -1785,8 +1790,37 @@ void MetadataNode::BuildMetadata(const string& filesPath) {
17851790
baseDir.append("/metadata");
17861791

17871792
DIR* dir = opendir(baseDir.c_str());
1793+
17881794
if(dir == nullptr){
1789-
throw NativeScriptException(string("metadata folder couldn't be opened!"));
1795+
stringstream ss;
1796+
ss << "metadata folder couldn't be opened! (Error: ";
1797+
ss << errno;
1798+
ss << ") ";
1799+
1800+
// TODO: Is there a way to detect if the screen is locked as verification
1801+
// We assume based on the error that this is the only way to get this specific error here at this point
1802+
if (errno == 2) {
1803+
// Log the error with error code
1804+
__android_log_print(ANDROID_LOG_ERROR, "TNS.error", "%s", ss.str().c_str());
1805+
1806+
// While the screen is locked after boot; we cannot access our own apps directory on Android 9+
1807+
// So the only thing to do at this point is just exit normally w/o crashing!
1808+
1809+
// The only reason we should be in this specific path; is if:
1810+
// 1) android:directBootAware="true" flag is set on receiver
1811+
// 2) android.intent.action.LOCKED_BOOT_COMPLETED intent is set in manifest on above receiver
1812+
// See: https://developer.android.com/guide/topics/manifest/receiver-element
1813+
// and: https://developer.android.com/training/articles/direct-boot
1814+
// This specific path occurs if you using the NativeScript-Local-Notification plugin, the
1815+
// receiver code runs fine, but the app actually doesn't need to startup. The Native code tries to
1816+
// startup because the receiver is triggered. So even though we are exiting, the receiver will have
1817+
// done its job
1818+
1819+
exit(0);
1820+
}
1821+
else {
1822+
throw NativeScriptException(ss.str());
1823+
}
17901824
}
17911825

17921826
string nodesFile = baseDir + "/treeNodeStream.dat";

0 commit comments

Comments
 (0)