Skip to content

Commit

Permalink
Merge pull request #1621 from NativeScript/fix_metadata_crash
Browse files Browse the repository at this point in the history
(Fix) metadata crash on startup with Local Notification plugin.
  • Loading branch information
Nathanael Anderson authored Jun 27, 2020
2 parents 2ab8fd1 + b2423b1 commit 18c896c
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions test-app/runtime/src/main/cpp/MetadataNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
#include <sstream>
#include <cctype>
#include <dirent.h>
#include <errno.h>
#include <android/log.h>
#include <unistd.h>
#include "ManualInstrumentation.h"
#include "JSONObjectHelper.h"



#include "v8.h"

using namespace v8;
Expand Down Expand Up @@ -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 == ENOENT) {
// 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";
Expand All @@ -1795,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);
Expand All @@ -1809,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);
Expand All @@ -1820,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);
Expand Down

0 comments on commit 18c896c

Please sign in to comment.