From 19747db26051883db213b777771cfe2b85e1abf5 Mon Sep 17 00:00:00 2001 From: Nathanael Anderson Date: Sat, 20 Jun 2020 23:54:52 -0500 Subject: [PATCH 1/2] Fix metadata crash on startup with Local Notification plugin. --- .../runtime/src/main/cpp/MetadataNode.cpp | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 1a69c29f0..42c98aaa5 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -12,9 +12,14 @@ #include #include #include +#include +#include +#include #include "ManualInstrumentation.h" #include "JSONObjectHelper.h" + + #include "v8.h" using namespace v8; @@ -1785,8 +1790,37 @@ void MetadataNode::BuildMetadata(const string& filesPath) { baseDir.append("/metadata"); DIR* dir = opendir(baseDir.c_str()); + if(dir == nullptr){ - throw NativeScriptException(string("metadata folder couldn't be opened!")); + stringstream ss; + ss << "metadata folder couldn't be opened! (Error: "; + ss << errno; + ss << ") "; + + // TODO: Is there a way to detect if the screen is locked as verification + // We assume based on the error that this is the only way to get this specific error here at this point + if (errno == 2) { + // Log the error with error code + __android_log_print(ANDROID_LOG_ERROR, "TNS.error", "%s", ss.str().c_str()); + + // While the screen is locked after boot; we cannot access our own apps directory on Android 9+ + // So the only thing to do at this point is just exit normally w/o crashing! + + // The only reason we should be in this specific path; is if: + // 1) android:directBootAware="true" flag is set on receiver + // 2) android.intent.action.LOCKED_BOOT_COMPLETED intent is set in manifest on above receiver + // See: https://developer.android.com/guide/topics/manifest/receiver-element + // and: https://developer.android.com/training/articles/direct-boot + // This specific path occurs if you using the NativeScript-Local-Notification plugin, the + // receiver code runs fine, but the app actually doesn't need to startup. The Native code tries to + // startup because the receiver is triggered. So even though we are exiting, the receiver will have + // done its job + + exit(0); + } + else { + throw NativeScriptException(ss.str()); + } } string nodesFile = baseDir + "/treeNodeStream.dat"; From b2423b14a9bbe1ddcd36d1325c4f2d630a974771 Mon Sep 17 00:00:00 2001 From: Nathanael Anderson Date: Mon, 22 Jun 2020 14:07:32 -0500 Subject: [PATCH 2/2] Cleanup error checks. --- .../runtime/src/main/cpp/MetadataNode.cpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test-app/runtime/src/main/cpp/MetadataNode.cpp b/test-app/runtime/src/main/cpp/MetadataNode.cpp index 42c98aaa5..6a45716a6 100644 --- a/test-app/runtime/src/main/cpp/MetadataNode.cpp +++ b/test-app/runtime/src/main/cpp/MetadataNode.cpp @@ -1799,7 +1799,7 @@ void MetadataNode::BuildMetadata(const string& filesPath) { // TODO: Is there a way to detect if the screen is locked as verification // We assume based on the error that this is the only way to get this specific error here at this point - if (errno == 2) { + if (errno == ENOENT) { // Log the error with error code __android_log_print(ANDROID_LOG_ERROR, "TNS.error", "%s", ss.str().c_str()); @@ -1829,7 +1829,12 @@ void MetadataNode::BuildMetadata(const string& filesPath) { FILE* f = fopen(nodesFile.c_str(), "rb"); if (f == nullptr) { - throw NativeScriptException(string("metadata file (treeNodeStream.dat) couldn't be opened!")); + stringstream ss; + ss << "metadata file (treeNodeStream.dat) couldn't be opened! (Error: "; + ss << errno; + ss << ") "; + + throw NativeScriptException(ss.str()); } fseek(f, 0, SEEK_END); int lenNodes = ftell(f); @@ -1843,7 +1848,11 @@ void MetadataNode::BuildMetadata(const string& filesPath) { f = fopen(namesFile.c_str(), "rb"); if (f == nullptr) { - throw NativeScriptException(string("metadata file (treeStringsStream.dat) couldn't be opened!")); + stringstream ss; + ss << "metadata file (treeStringsStream.dat) couldn't be opened! (Error: "; + ss << errno; + ss << ") "; + throw NativeScriptException(ss.str()); } fseek(f, 0, SEEK_END); int lenNames = ftell(f); @@ -1854,7 +1863,11 @@ void MetadataNode::BuildMetadata(const string& filesPath) { f = fopen(valuesFile.c_str(), "rb"); if (f == nullptr) { - throw NativeScriptException(string("metadata file (treeValueStream.dat) couldn't be opened!")); + stringstream ss; + ss << "metadata file (treeValueStream.dat) couldn't be opened! (Error: "; + ss << errno; + ss << ") "; + throw NativeScriptException(ss.str()); } fseek(f, 0, SEEK_END); int lenValues = ftell(f);