Skip to content

Commit 18c896c

Browse files
author
Nathanael Anderson
authored
Merge pull request #1621 from NativeScript/fix_metadata_crash
(Fix) metadata crash on startup with Local Notification plugin.
2 parents 2ab8fd1 + b2423b1 commit 18c896c

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

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

Lines changed: 51 additions & 4 deletions
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 == ENOENT) {
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";
@@ -1795,7 +1829,12 @@ void MetadataNode::BuildMetadata(const string& filesPath) {
17951829

17961830
FILE* f = fopen(nodesFile.c_str(), "rb");
17971831
if (f == nullptr) {
1798-
throw NativeScriptException(string("metadata file (treeNodeStream.dat) couldn't be opened!"));
1832+
stringstream ss;
1833+
ss << "metadata file (treeNodeStream.dat) couldn't be opened! (Error: ";
1834+
ss << errno;
1835+
ss << ") ";
1836+
1837+
throw NativeScriptException(ss.str());
17991838
}
18001839
fseek(f, 0, SEEK_END);
18011840
int lenNodes = ftell(f);
@@ -1809,7 +1848,11 @@ void MetadataNode::BuildMetadata(const string& filesPath) {
18091848

18101849
f = fopen(namesFile.c_str(), "rb");
18111850
if (f == nullptr) {
1812-
throw NativeScriptException(string("metadata file (treeStringsStream.dat) couldn't be opened!"));
1851+
stringstream ss;
1852+
ss << "metadata file (treeStringsStream.dat) couldn't be opened! (Error: ";
1853+
ss << errno;
1854+
ss << ") ";
1855+
throw NativeScriptException(ss.str());
18131856
}
18141857
fseek(f, 0, SEEK_END);
18151858
int lenNames = ftell(f);
@@ -1820,7 +1863,11 @@ void MetadataNode::BuildMetadata(const string& filesPath) {
18201863

18211864
f = fopen(valuesFile.c_str(), "rb");
18221865
if (f == nullptr) {
1823-
throw NativeScriptException(string("metadata file (treeValueStream.dat) couldn't be opened!"));
1866+
stringstream ss;
1867+
ss << "metadata file (treeValueStream.dat) couldn't be opened! (Error: ";
1868+
ss << errno;
1869+
ss << ") ";
1870+
throw NativeScriptException(ss.str());
18241871
}
18251872
fseek(f, 0, SEEK_END);
18261873
int lenValues = ftell(f);

0 commit comments

Comments
 (0)