From 3899cd6715d6153a3b2c5cb514b8e9b46767e906 Mon Sep 17 00:00:00 2001 From: codestation Date: Sun, 22 Mar 2015 22:27:44 -0430 Subject: [PATCH] Removed context from logging. Fixed homedir detection. Fixed config file creation. Send smartphone device model on broadcast. --- android/android.pro | 2 +- android/main_android.cpp | 51 +++++++++++++++++++++++++++++++++++++-- android/sevicemanager.cpp | 34 +++++++++++++++++++------- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/android/android.pro b/android/android.pro index fb9dd70..4512e9b 100644 --- a/android/android.pro +++ b/android/android.pro @@ -3,7 +3,7 @@ include(../common/defines.pri) TARGET = qcma_android TEMPLATE=app -QT += network sql +QT += network sql androidextras LIBS += -L../common -lqcma_common # this library needs to link statically their deps but Qt doesn't pass --static to PKGCONFIG diff --git a/android/main_android.cpp b/android/main_android.cpp index b8c1792..2e721e1 100644 --- a/android/main_android.cpp +++ b/android/main_android.cpp @@ -21,20 +21,57 @@ #include #include #include +#include #include #include #include #include +#include #include +#include + #include +#include + #include "servicemanager.h" +static QString getAppName() +{ + pid_t pid = getpid(); + QString cmdline = QString("/proc/%1/cmdline").arg(pid); + QFile file(cmdline); + return file.open(QIODevice::ReadOnly) ? file.readLine() : ""; +} + + +static void cleanOutput(QtMsgType type, const QMessageLogContext &, const QString & msg) +{ + QByteArray localMsg = msg.toLocal8Bit(); + const char *message = localMsg.constData(); + + switch (type) { + case QtDebugMsg: + __android_log_print(ANDROID_LOG_INFO, "qcma", "%s", message); + break; + case QtWarningMsg: + __android_log_print(ANDROID_LOG_WARN, "qcma", "%s", message); + break; + case QtCriticalMsg: + __android_log_print(ANDROID_LOG_ERROR, "qcma", "%s", message); + break; + case QtFatalMsg: + __android_log_print(ANDROID_LOG_FATAL, "qcma", "%s", message); + abort(); + } +} + + int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); - app.setApplicationName("Qcma"); + app.setApplicationName("qcma"); // FIXME: libmtp sends SIGPIPE if a socket write fails crashing the whole app // the proper fix is to libmtp to handle the cancel properly or ignoring @@ -44,8 +81,18 @@ int main(int argc, char *argv[]) // stdout goes to /dev/null on android VitaMTP_Set_Logging(VitaMTP_NONE); + qInstallMessageHandler(cleanOutput); + qDebug("Starting Qcma %s", QCMA_VER); + QString appname = getAppName(); + qDebug("Class name: %s", qPrintable(appname)); + + // set $HOME, Qt/Android doesn't currently do this + qputenv("HOME", QString("/data/data/%1").arg(appname).toLocal8Bit()); + + app.addLibraryPath(appname + "/lib"); + QTranslator translator; QString locale = "en"; //QLocale().system().name(); qDebug() << "Current locale:" << locale; @@ -71,7 +118,7 @@ int main(int argc, char *argv[]) qDebug("Starting main thread: 0x%016" PRIxPTR, (uintptr_t)QThread::currentThreadId()); // set the organization/application for QSettings to work properly - app.setOrganizationName("codestation"); + app.setOrganizationName("qcma"); app.setApplicationName("qcma"); ServiceManager manager; diff --git a/android/sevicemanager.cpp b/android/sevicemanager.cpp index 0306466..1bd821e 100644 --- a/android/sevicemanager.cpp +++ b/android/sevicemanager.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include ServiceManager::ServiceManager(QObject *obj_parent) : @@ -56,10 +57,7 @@ void ServiceManager::refreshDatabase() void ServiceManager::start() { - if(VitaMTP_Init() < 0) { - qCritical("Cannot initialize VitaMTP library"); - return; - } + VitaMTP_Init(); loadDefaultSettings(); @@ -144,9 +142,19 @@ void ServiceManager::threadStopped() void ServiceManager::loadDefaultSettings() { - QString defaultdir; + QString defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); + + // save config to /sdcard, remove when the app works properly + qputenv("XDG_CONFIG_HOME", defaultdir.toLocal8Bit()); + QSettings settings; + // skip initialization if some config is already present + if(!settings.value("appsPath").isNull()) + return; + + qDebug("saving to: %s", qPrintable(settings.fileName())); + defaultdir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); qDebug("photoPath: %s", qPrintable(defaultdir)); settings.setValue("photoPath", defaultdir); @@ -159,23 +167,23 @@ void ServiceManager::loadDefaultSettings() qDebug("photoPath: %s", qPrintable(defaultdir)); settings.setValue("videoPath", defaultdir); - defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); + defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); defaultdir.append(QDir::separator()).append("PS Vita"); qDebug("appsPath: %s", qPrintable(defaultdir)); settings.setValue("appsPath", defaultdir); - defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); + defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); defaultdir.append(QDir::separator()).append("PSV Updates"); qDebug("urlPath: %s", qPrintable(defaultdir)); settings.setValue("urlPath", defaultdir); - defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); + defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); defaultdir.append(QDir::separator()).append("PSV Packages"); qDebug("pkgPath: %s", qPrintable(defaultdir)); settings.setValue("pkgPath", defaultdir); settings.setValue("offlineMode", true); - settings.setValue("skipMetadata", false); + settings.setValue("skipMetadata", true); // disable USB for now settings.setValue("disableUSB", true); @@ -192,4 +200,12 @@ void ServiceManager::loadDefaultSettings() settings.setValue("protocolIndex", 0); settings.setValue("protocolVersion", VITAMTP_PROTOCOL_MAX_VERSION); + + QString deviceName = QAndroidJniObject::getStaticObjectField( + "android/os/Build", "MODEL", "Ljava/lang/String;").toString(); + + qDebug("Detected device model: %s", qPrintable(deviceName)); + settings.setValue("hostName", deviceName); + + settings.sync(); }