Ticket #13229: 20180304_1431_android_hostid.patch

File 20180304_1431_android_hostid.patch, 2.2 KB (added by Peter Bennett, 6 years ago)

Updated patch for hostid

  • mythtv/libs/libmyth/mythcontext.cpp

    diff --git a/mythtv/libs/libmyth/mythcontext.cpp b/mythtv/libs/libmyth/mythcontext.cpp
    index c70a54a..934f2a5 100644
    a b  
    77#include <QTcpSocket>
    88#include <QEventLoop>
    99
     10#ifdef Q_OS_ANDROID
     11#include <QtAndroidExtras>
     12#endif
     13
    1014#include <cmath>
    1115#include <iostream>
    1216
    bool MythContextPrivate::LoadDatabaseSettings(void) 
    597601                    "MCP: Error, could not determine host name." + ENO);
    598602            localhostname[0] = '\0';
    599603        }
     604#ifdef Q_OS_ANDROID
     605#define ANDROID_EXCEPTION_CHECK \
     606  if (env->ExceptionCheck()) { \
     607    env->ExceptionClear(); \
     608    exception=true; \
     609  }
     610        if (strcmp(localhostname, "localhost") == 0
     611            || localhostname[0] == '\0')
     612        {
     613            hostname = "android";
     614            bool exception=false;
     615            QAndroidJniEnvironment env;
     616            QAndroidJniObject myID = QAndroidJniObject::fromString("android_id");
     617            QAndroidJniObject activity = QtAndroid::androidActivity();
     618            ANDROID_EXCEPTION_CHECK;
     619            QAndroidJniObject appctx = activity.callObjectMethod
     620                ("getApplicationContext","()Landroid/content/Context;");
     621            ANDROID_EXCEPTION_CHECK;
     622            QAndroidJniObject contentR = appctx.callObjectMethod
     623                ("getContentResolver", "()Landroid/content/ContentResolver;");
     624            ANDROID_EXCEPTION_CHECK;
     625            QAndroidJniObject androidId = QAndroidJniObject::callStaticObjectMethod
     626                ("android/provider/Settings$Secure","getString",
     627                 "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;",
     628                 contentR.object<jobject>(),
     629                 myID.object<jstring>());
     630            ANDROID_EXCEPTION_CHECK;
     631            if (exception)
     632                LOG(VB_GENERAL, LOG_ALERT,
     633                    "Java exception looking for android id");
     634            else
     635                hostname = QString("android-%1").arg(androidId.toString());
     636        }
     637        else
     638            hostname = localhostname;
     639#else
    600640        hostname = localhostname;
     641#endif
    601642        LOG(VB_GENERAL, LOG_NOTICE, "Empty LocalHostName.");
    602643    }
    603644    else