qint64 QSerialPortPrivate::writeToPort(const char *data, qint64 maxSize)
{
    if (deviceId == BAD_PORT)
    {
        q_ptr->setError(QSerialPort::NotOpenError);
        return 0;
    }

    QAndroidJniEnvironment jniEnv;
    jbyteArray jarrayL = jniEnv->NewByteArray(maxSize);
    jniEnv->SetByteArrayRegion(jarrayL, 0, maxSize, (jbyte *)data);
    if (jniEnv->ExceptionCheck())
        jniEnv->ExceptionClear();
    int resultL = QAndroidJniObject::callStaticMethod<jint>(
        kJniClassName,
        "write",
        "(I[BI)I",
        deviceId,
        jarrayL,
        internalWriteTimeoutMsec);

    if (jniEnv->ExceptionCheck())
    {
        jniEnv->ExceptionClear();
        q_ptr->setErrorString(QStringLiteral("Writing to the device threw an exception"));
        jniEnv->DeleteLocalRef(jarrayL);
        return 0;
    }
    jniEnv->DeleteLocalRef(jarrayL);
    return resultL;
}
LocalDeviceBroadcastReceiver::LocalDeviceBroadcastReceiver(QObject *parent) :
    AndroidBroadcastReceiver(parent), previousScanMode(0)
{
    addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionBondStateChanged));
    addAction(valueForStaticField(JavaNames::BluetoothAdapter, JavaNames::ActionScanModeChanged));
    addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionAclConnected));
    addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionAclDisconnected));
    if (QtAndroidPrivate::androidSdkVersion() >= 15)
        addAction(valueForStaticField(JavaNames::BluetoothDevice, JavaNames::ActionPairingRequest)); //API 15

    //cache integer values for host & bonding mode
    //don't use the java fields directly but refer to them by name
    QAndroidJniEnvironment env;
    for (uint i = 0; i < (sizeof(hostModePreset)/sizeof(hostModePreset[0])); i++) {
        hostModePreset[i] = QAndroidJniObject::getStaticField<jint>(
                                                "android/bluetooth/BluetoothAdapter",
                                                scanModes[i]);
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
            hostModePreset[i] = 0;
        }
    }

    for (uint i = 0; i < (sizeof(bondingModePreset)/sizeof(bondingModePreset[0])); i++) {
        bondingModePreset[i] = QAndroidJniObject::getStaticField<jint>(
                                                "android/bluetooth/BluetoothDevice",
                                                bondModes[i]);
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
            bondingModePreset[i] = 0;
        }
    }
}
bool QBluetoothSocketPrivate::setSocketDescriptor(const QAndroidJniObject &socket, QBluetoothServiceInfo::Protocol socketType_,
                         QBluetoothSocket::SocketState socketState, QBluetoothSocket::OpenMode openMode)
{
    Q_Q(QBluetoothSocket);

    if (q->state() != QBluetoothSocket::UnconnectedState || !socket.isValid())
        return false;

    if (!ensureNativeSocket(socketType_))
        return false;

    socketObject = socket;

    QAndroidJniEnvironment env;
    inputStream = socketObject.callObjectMethod("getInputStream", "()Ljava/io/InputStream;");
    outputStream = socketObject.callObjectMethod("getOutputStream", "()Ljava/io/OutputStream;");

    if (env->ExceptionCheck() || !inputStream.isValid() || !outputStream.isValid()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        //close socket again
        socketObject.callMethod<void>("close");
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        socketObject = inputStream = outputStream = remoteDevice = QAndroidJniObject();


        errorString = QBluetoothSocket::tr("Obtaining streams for service failed");
        q->setSocketError(QBluetoothSocket::NetworkError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return false;
    }

    remoteDevice = socketObject.callObjectMethod("getRemoteDevice", "()Landroid/bluetooth/BluetoothDevice;");

    if (inputThread) {
        inputThread->deleteLater();
        inputThread = 0;
    }
    inputThread = new InputStreamThread(this);
    QObject::connect(inputThread, SIGNAL(dataAvailable()),
                     q, SIGNAL(readyRead()), Qt::QueuedConnection);
    QObject::connect(inputThread, SIGNAL(error(int)),
                     this, SLOT(inputThreadError(int)), Qt::QueuedConnection);
    inputThread->run();


    q->setSocketState(socketState);
    q->setOpenMode(openMode | QIODevice::Unbuffered);

    if (openMode == QBluetoothSocket::ConnectedState)
        emit q->connected();

    return true;
}
Example #4
0
static bool registerNativeMethods()
{
    JNINativeMethod methods[] {
        {"OnImageCaptured", "(ILjava/lang/String;)V", (void*)onImageCaptured}
    };

    const char *classname = "an/qt/imageProcessor/ImageCaptureNative";
    jclass clazz;
    QAndroidJniEnvironment env;

    QAndroidJniObject javaClass(classname);
    clazz = env->GetObjectClass(javaClass.object<jobject>());
    //clazz = env->FindClass(classname);
    qDebug() << "find ImageCaptureNative - " << clazz;
    bool result = false;
    if(clazz)
    {
        jint ret = env->RegisterNatives(clazz,
                                        methods,
                                        sizeof(methods) / sizeof(methods[0]));
        env->DeleteLocalRef(clazz);
        qDebug() << "RegisterNatives return - " << ret;
        result = ret >= 0;
    }
    if(env->ExceptionCheck()) env->ExceptionClear();
    return result;
}
Example #5
0
bool JniNative::registerNativeMethods()
{
    JNINativeMethod methods[] {
        {"notifyMsg", "(III)V", (void*)notifyMsg},
        {"setDirectBuffer","(Ljava/lang/Object;I)V",(void*)setDirectBuffer}
    };

    const char *classname = "an/qt/useJar/ExtendsQtNative";
    jclass clazz;
    QAndroidJniEnvironment env;

    QAndroidJniObject javaClass(classname);
    clazz = env->GetObjectClass(javaClass.object<jobject>());
//    QDBG << "find ExtendsQtNative - " << clazz;
    bool result = false;
    if (clazz) {
        jint ret = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
        env->DeleteLocalRef(clazz);
//        QDBG << "RegisterNatives return - " << ret;
        result = ret >= 0;
    }
    if (env->ExceptionCheck())
        env->ExceptionClear();
    return result;
}
void AndroidMainWindow::fireSupportForumIntent()
{
#ifdef Q_OS_ANDROID
//    qDebug() << "fireSupportForumIntent()";
    QAndroidJniObject s1 = QAndroidJniObject::fromString("http://mdforum.designer2k2.at/viewtopic.php?f=15&t=334");
    QSettings settings;
    if ( settings.value("md/md", QVariant ( MDMODE ).toBool() )  == false ) {
        s1 = QAndroidJniObject::fromString("http://digifant-einzelabstimmung.de/bofh-ng/de/digifant-1/live-daten-auslesen");
    }


    QAndroidJniObject::callStaticMethod<void>( "de/gummelinformatics/mui/MuiIntentHelper",
                                           "openUrl",
                                           "(Ljava/lang/String;)V",
                                            s1.object<jstring>() );

    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        // Handle exception here.
        qDebug() << "*** JNI exception ***";
        env->ExceptionDescribe();
        env->ExceptionClear();
        env->ExceptionClear();
    } else {
        qDebug() << "NO JNI exception";
    }

//    QDesktopServices::openUrl( QUrl("http://mdforum.designer2k2.at/") );

#endif
}
Example #7
0
QBluetoothDeviceDiscoveryAgentPrivate::QBluetoothDeviceDiscoveryAgentPrivate(
    const QBluetoothAddress &deviceAdapter, QBluetoothDeviceDiscoveryAgent *parent) :
    inquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry),
    lastError(QBluetoothDeviceDiscoveryAgent::NoError),
    receiver(0),
    m_adapterAddress(deviceAdapter),
    m_active(NoScanActive),
    leScanTimeout(0),
    pendingCancel(false),
    pendingStart(false),
    lowEnergySearchTimeout(25000),
    q_ptr(parent)
{
    QAndroidJniEnvironment env;
    adapter = QAndroidJniObject::callStaticObjectMethod("android/bluetooth/BluetoothAdapter",
                                                        "getDefaultAdapter",
                                                        "()Landroid/bluetooth/BluetoothAdapter;");
    if (!adapter.isValid()) {
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }
        qCWarning(QT_BT_ANDROID) << "Device does not support Bluetooth";
    }
}
void QBluetoothSocketPrivate::inputThreadError(int errorCode)
{
    Q_Q(QBluetoothSocket);

    if (errorCode != -1) { //magic error which is expected and can be ignored
        errorString = QBluetoothSocket::tr("Network error during read");
        q->setSocketError(QBluetoothSocket::NetworkError);
    }

    //finally we can delete the InputStreamThread
    InputStreamThread *client = qobject_cast<InputStreamThread *>(sender());
    if (client)
        client->deleteLater();

    if (socketObject.isValid()) {
        //triggered when remote side closed the socket
        //cleanup internal objects
        //if it was call to local close()/abort() the objects are cleaned up already

        QAndroidJniEnvironment env;
        socketObject.callMethod<void>("close");
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        inputStream = outputStream = remoteDevice = socketObject = QAndroidJniObject();
    }

    q->setSocketState(QBluetoothSocket::UnconnectedState);
    q->setOpenMode(QIODevice::NotOpen);
    emit q->disconnected();
}
qint64 QBluetoothSocketPrivate::writeData(const char *data, qint64 maxSize)
{
    //TODO implement buffered behavior (so far only unbuffered)
    Q_Q(QBluetoothSocket);
    if (state != QBluetoothSocket::ConnectedState || !outputStream.isValid()) {
        qCWarning(QT_BT_ANDROID) << "Socket::writeData: " << state << outputStream.isValid();
        errorString = QBluetoothSocket::tr("Cannot write while not connected");
        q->setSocketError(QBluetoothSocket::OperationError);
        return -1;
    }

    QAndroidJniEnvironment env;
    jbyteArray nativeData = env->NewByteArray((qint32)maxSize);
    env->SetByteArrayRegion(nativeData, 0, (qint32)maxSize, reinterpret_cast<const jbyte*>(data));
    outputStream.callMethod<void>("write", "([BII)V", nativeData, 0, (qint32)maxSize);
    env->DeleteLocalRef(nativeData);
    emit q->bytesWritten(maxSize);

    if (env->ExceptionCheck()) {
        qCWarning(QT_BT_ANDROID) << "Error while writing";
        env->ExceptionDescribe();
        env->ExceptionClear();
        errorString = QBluetoothSocket::tr("Error during write on socket.");
        q->setSocketError(QBluetoothSocket::NetworkError);
        return -1;
    }

    return maxSize;
}
Example #10
0
const QString Wpp::getDownloadPath() const
{
	QString downloadPath;
#if defined(Q_OS_ANDROID)
	//https://qt-project.org/forums/viewthread/35519
	QAndroidJniObject mediaDir = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
	QAndroidJniObject mediaPath = mediaDir.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" );
	downloadPath = mediaPath.toString()+"/Download/";
	QAndroidJniEnvironment env;
	if (env->ExceptionCheck()) {
		// Handle exception here.
		qDebug() << "[ERR]:Android(Extras):getting SD card download path";
		env->ExceptionClear();
	}
#elif defined(Q_OS_IOS)
	QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
	qDebug() << "[IOS]download paths count:" << paths.size();
	downloadPath = paths.first();
#else
	QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
	qDebug() << "[Other OS]download paths count:" << paths.size();
	downloadPath = paths.first();

#endif
	qDebug() << "downloadPath:" << downloadPath;
	return downloadPath;
}
Example #11
0
QString Wpp::createAlbumPath(const QString& albumName)
{
	QString parentPath;

#if defined(Q_OS_ANDROID)
	QAndroidJniObject Environment__MEDIA_MOUNTED = QAndroidJniObject::getStaticObjectField(
				"android/os/Environment", "MEDIA_MOUNTED", "Ljava/lang/String;");
	qDebug() << "Environment__MEDIA_MOUNTED:isValid:" << Environment__MEDIA_MOUNTED.isValid() << ":toString:" << Environment__MEDIA_MOUNTED.toString();
	QString  Environment__MEDIA_MOUNTED_STRING =	Environment__MEDIA_MOUNTED.toString();
	QAndroidJniObject mediaState = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageState", "()Ljava/lang/String;");
	QString mediaStateString = mediaState.toString();

	qDebug() <<"mediaStateString:"<< mediaStateString;

	if(mediaStateString != Environment__MEDIA_MOUNTED_STRING )
	{
		qDebug() << "No SD Card...";
		return QString();
	}

		//https://qt-project.org/forums/viewthread/35519
		QAndroidJniObject mediaDir = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
		QAndroidJniObject mediaPath = mediaDir.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" );
		parentPath = mediaPath.toString();
		QAndroidJniEnvironment env;
		if (env->ExceptionCheck()) {
			// Handle exception here.
			qDebug() << "[ERR]:Android(Extras):getting SD card path";
			env->ExceptionClear();
		}
#elif defined(Q_OS_IOS)
		QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
		qDebug() << "[IOS]album parent paths count:" << paths.size();
		parentPath = paths.first();
#else
		QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
		qDebug() << "[Other OS]album parent paths count:" << paths.size();
		parentPath = paths.first();

#endif

		QDir dir(parentPath);

		qDebug() << __FUNCTION__ << ":dir=" << dir.dirName();

		QString userAlbumPath = parentPath.append("/").append(albumName);;
		QDir userAlbumDir = userAlbumPath;
		if( !userAlbumDir.exists() )
		{
			qDebug() << albumName << ":not exists, create it";
			dir.mkpath( albumName );
		}
		else
		{
			qDebug() << albumName << ":exists";
		}

		return userAlbumPath;
}
void cleanJavaException()
{
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
}
bool exceptionCheck()
{
    /*
      The QAndroidJniEnvironment attaches the current thread to the JavaVM on
      creation and detach when it goes out of scope.
     */
    QAndroidJniEnvironment qjniEnv;
    return qjniEnv->ExceptionCheck();
}
Example #14
0
void UnitsAttached::updateDPI()
{
    if (m_screen == nullptr) {
        m_dpi = DEFAULT_DPI;
        return;
    }

#if defined(Q_OS_IOS)
    // iOS integration of scaling (retina, non-retina, 4K) does itself.
    m_dpi = m_screen->physicalDotsPerInch();
#elif defined(Q_OS_ANDROID)
    // https://bugreports.qt-project.org/browse/QTBUG-35701
    // recalculate dpi for Android

    QAndroidJniEnvironment env;
    QAndroidJniObject activity = QtAndroid::androidActivity();
    QAndroidJniObject resources =
            activity.callObjectMethod("getResources", "()Landroid/content/res/Resources;");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        m_dpi = DEFAULT_DPI;
        return;
    }

    QAndroidJniObject displayMetrics =
            resources.callObjectMethod("getDisplayMetrics", "()Landroid/util/DisplayMetrics;");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        m_dpi = DEFAULT_DPI;
        return;
    }
    m_dpi = displayMetrics.getField<int>("densityDpi");
    m_multiplier = displayMetrics.getField<float>("density");
#else
    // standard dpi
    m_dpi = m_screen->logicalDotsPerInch() * m_screen->devicePixelRatio();
#endif
}
Example #15
0
CBSettings::CBSettings(QObject *parent) : QObject(parent),
    settings("settings.ini",QSettings::IniFormat)
{
    settings.setIniCodec("windows-1251");

    if (settings.value(kIsFirstRan,true).toBool()){
        // we have never use settings before,let's init them
        settings.setValue(kIsFirstRan,false);
        settings.setValue(kNeedCollect,true);
#if defined(Q_OS_WIN32) || defined(Q_OS_MAC) || defined(Q_OS_IOS)
        QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
        if (!paths.empty()){
            settings.setValue(kDefaultPath,"file:///"+paths.at(0)+"/Bases/");
        }
        paths = QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
        if (!paths.empty()){
            settings.setValue(kAttachSearchPath,"file:///"+paths.at(0)+"/");
        }
#endif

#ifdef  Q_OS_ANDROID

        QAndroidJniObject mediaDir = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
        QAndroidJniObject mediaPath = mediaDir.callObjectMethod( "getAbsolutePath", "()Ljava/lang/String;" );
        QString dataAbsPath = "file://" + mediaPath.toString();
        settings.setValue(kDefaultPath,dataAbsPath +"/Bases/");
        settings.setValue(kAttachSearchPath,dataAbsPath+"/Downloads/");
        QAndroidJniEnvironment env;
        if (env->ExceptionCheck()) {
                // Handle exception here.
                env->ExceptionClear();
        }
#endif
    }

    lastBasePath = settings.value(kLastBasePath,"").toString();
    needCollect = settings.value(kNeedCollect,false).toBool();
    m_defaultPath = settings.value(kDefaultPath,"").toString();
    m_attachSearchPath = settings.value(kAttachSearchPath,"").toString();

    int size = settings.beginReadArray(kRecent);
    for (int i = 0; i < size; ++i) {
        settings.setArrayIndex(i);
        QString name = settings.value(kBaseName).toString();
        QString path = settings.value(kBasePath).toString();
        recentBasesMap[name] = path;
    }
    settings.endArray();
    emit recentBasesChanged();

}
void QSerialPortPrivate::setNativeMethods(void)
{
    qCDebug(AndroidSerialPortLog) << "Registering Native Functions";

    //  REGISTER THE C++ FUNCTION WITH JNI
    JNINativeMethod javaMethods[] {
        {"nativeDeviceHasDisconnected", "(I)V",                     reinterpret_cast<void *>(jniDeviceHasDisconnected)},
        {"nativeDeviceNewData",         "(I[B)V",                   reinterpret_cast<void *>(jniDeviceNewData)},
        {"nativeDeviceException",       "(ILjava/lang/String;)V",   reinterpret_cast<void *>(jniDeviceException)},
        {"qgcLogDebug",                 "(Ljava/lang/String;)V",    reinterpret_cast<void *>(jniLogDebug)},
        {"qgcLogWarning",               "(Ljava/lang/String;)V",    reinterpret_cast<void *>(jniLogWarning)}
    };

    QAndroidJniEnvironment jniEnv;
    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }

    jclass objectClass = jniEnv->FindClass(kJniClassName);
    if(!objectClass) {
        qWarning() << "Couldn't find class:" << kJniClassName;
        return;
    }

    jint val = jniEnv->RegisterNatives(objectClass, javaMethods, sizeof(javaMethods) / sizeof(javaMethods[0]));

    if (val < 0) {
        qWarning() << "Error registering methods: " << val;
    } else {
        qCDebug(AndroidSerialPortLog) << "Native Functions Registered";
    }

    if (jniEnv->ExceptionCheck()) {
        jniEnv->ExceptionDescribe();
        jniEnv->ExceptionClear();
    }
}
Example #17
0
MyDevice::MyDevice(QObject *parent) : QObject(parent)
{
    m_screen = qApp->primaryScreen();
    m_dpi = m_screen->physicalDotsPerInch() * m_screen->devicePixelRatio();
    m_isMobile = false;
#if defined(Q_OS_IOS)
    m_dpi = m_screen->physicalDotsPerInch();
    m_isMobile = true;
#elif defined(Q_OS_ANDROID)
    QAndroidJniEnvironment env;
    QAndroidJniObject activity = QtAndroid::androidActivity();
    QAndroidJniObject resources = activity.callObjectMethod("getResources", "()Landroid/content/res/Resources;");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        //return EXIT_FAILURE;
    }

    QAndroidJniObject displayMetrics = resources.callObjectMethod("getDisplayMetrics", "()Landroid/util/DisplayMetrics;");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        //return EXIT_FAILURE;
    }
    m_dpi = displayMetrics.getField<int>("densityDpi");
    m_isMobile = true;

#else
    // standard dpi
    m_dpi = m_screen->logicalDotsPerInch() * m_screen->devicePixelRatio();
#endif
    m_dp = m_dpi / 160.f;
    emit dpChanged(m_dp);
    emit isMobileChanged(m_isMobile);
    //qDebug() << "*** dp:"<<m_dp<<" dpi:"<<m_dpi;
}
void Widget::onCaptureImage()
{
#ifdef ANDROID
    QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava",
                                       "captureImage");
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        // Handle exception here.
        qDebug() << "Widget,onCaptureImage, got exception";
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
#endif
}
Example #19
0
const char *system_default_filename(void)
{
	/* Replace this when QtCore/QStandardPaths getExternalStorageDirectory landed */
	QAndroidJniObject externalStorage = QAndroidJniObject::callStaticObjectMethod("android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
	QAndroidJniObject externalStorageAbsolute = externalStorage.callObjectMethod("getAbsolutePath", "()Ljava/lang/String;");
	QString system_default_filename = externalStorageAbsolute.toString() + "/subsurface.xml";
	QAndroidJniEnvironment env;
	if (env->ExceptionCheck()) {
		// FIXME: Handle exception here.
		env->ExceptionClear();
		return strdup("/sdcard/subsurface.xml");
	}
	return strdup(system_default_filename.toUtf8().data());
}
int HardwareUtilsAndroid::getNetworkStatus()
{
    jint status = QAndroidJniObject::callStaticMethod<jint>("fr/calaos/calaoshome/HardwareUtils",
                                                            "getNetworkStatus");

    qDebug() << "Android: HardwareUtilsAndroid::getNetworkStatus(): " << status;
    //Clear exception if any
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck())
    {
        qDebug() << "JNI call failed";
        env->ExceptionDescribe();
        env->ExceptionClear();
    }

    return status;
}
void HardwareUtilsAndroid::showAlertMessage(QString title, QString message, QString buttontext)
{
    QAndroidJniObject jTitle = QAndroidJniObject::fromString(title);
    QAndroidJniObject jMessage = QAndroidJniObject::fromString(message);
    QAndroidJniObject jButtontext = QAndroidJniObject::fromString(buttontext);
    QAndroidJniObject::callStaticMethod<void>("fr/calaos/calaoshome/HardwareUtils",
                                       "showAlertMessage",
                                       "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
                                       jTitle.object<jstring>(),
                                       jMessage.object<jstring>(),
                                       jButtontext.object<jstring>());

    //Clear exception if any
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck())
    {
        qDebug() << "JNI call failed";
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
}
void QBluetoothSocketPrivate::abort()
{
    if (state == QBluetoothSocket::UnconnectedState)
        return;

    if (socketObject.isValid()) {
        QAndroidJniEnvironment env;

        /*
         * BluetoothSocket.close() triggers an abort of the input stream
         * thread because inputStream.read() throws IOException
         * In turn the thread stops and throws an error which sets
         * new state, error and emits relevant signals.
         * See QBluetoothSocketPrivate::inputThreadError() for details
         */

        if (inputThread)
            inputThread->prepareForClosure();

        //triggers abort of input thread as well
        socketObject.callMethod<void>("close");
        if (env->ExceptionCheck()) {

            qCWarning(QT_BT_ANDROID) << "Error during closure of socket";
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        if (inputThread) {
            //don't delete here as signals caused by Java Thread are still
            //going to be emitted
            //delete occurs in inputThreadError()
            inputThread = 0;
        }

        inputStream = outputStream = socketObject = remoteDevice = QAndroidJniObject();
    }
}
void QBluetoothServiceDiscoveryAgentPrivate::start(const QBluetoothAddress &address)
{
    Q_Q(QBluetoothServiceDiscoveryAgent);

    if (!btAdapter.isValid()) {
        error = QBluetoothServiceDiscoveryAgent::UnknownError;
        errorString = QBluetoothServiceDiscoveryAgent::tr("Platform does not support Bluetooth");

        //abort any outstanding discoveries
        discoveredDevices.clear();
        emit q->error(error);
        _q_serviceDiscoveryFinished();

        return;
    }

    /* SDP discovery was officially added by Android API v15
     * BluetoothDevice.getUuids() existed in earlier APIs already and in the future we may use
     * reflection to support earlier Android versions than 15. Unfortunately
     * BluetoothDevice.fetchUuidsWithSdp() and related APIs had some structure changes
     * over time. Therefore we won't attempt this with reflection.
     *
     * TODO: Use reflection to support getUuuids() where possible.
     * */
    if (QtAndroidPrivate::androidSdkVersion() < 15) {
        qCWarning(QT_BT_ANDROID) << "Aborting SDP enquiry due to too low Android API version (requires v15+)";

        error = QBluetoothServiceDiscoveryAgent::UnknownError;
        errorString = QBluetoothServiceDiscoveryAgent::tr("Android API below v15 does not support SDP discovery");

        //abort any outstanding discoveries
        sdpCache.clear();
        discoveredDevices.clear();
        emit q->error(error);
        _q_serviceDiscoveryFinished();

        return;
    }

    QAndroidJniObject inputString = QAndroidJniObject::fromString(address.toString());
    QAndroidJniObject remoteDevice =
            btAdapter.callObjectMethod("getRemoteDevice",
                                               "(Ljava/lang/String;)Landroid/bluetooth/BluetoothDevice;",
                                               inputString.object<jstring>());
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
        env->ExceptionClear();
        env->ExceptionDescribe();

        //if it was only device then its error -> otherwise go to next device
        if (singleDevice) {
            error = QBluetoothServiceDiscoveryAgent::InputOutputError;
            errorString = QBluetoothServiceDiscoveryAgent::tr("Cannot create Android BluetoothDevice");

            qCWarning(QT_BT_ANDROID) << "Cannot start SDP for" << discoveredDevices.at(0).name()
                                     << "(" << address.toString() << ")";
            emit q->error(error);
        }
        _q_serviceDiscoveryFinished();
        return;
    }


    if (mode == QBluetoothServiceDiscoveryAgent::MinimalDiscovery) {
        qCDebug(QT_BT_ANDROID) << "Minimal discovery on (" << discoveredDevices.at(0).name()
                               << ")" << address.toString() ;

        //Minimal discovery uses BluetoothDevice.getUuids()
        QAndroidJniObject parcelUuidArray = remoteDevice.callObjectMethod(
                    "getUuids", "()[Landroid/os/ParcelUuid;");

        if (!parcelUuidArray.isValid()) {
            if (singleDevice) {
                error = QBluetoothServiceDiscoveryAgent::InputOutputError;
                errorString = QBluetoothServiceDiscoveryAgent::tr("Cannot obtain service uuids");
                emit q->error(error);
            }
            qCWarning(QT_BT_ANDROID) << "Cannot retrieve SDP UUIDs for" << discoveredDevices.at(0).name()
                                     << "(" << address.toString() << ")";
            _q_serviceDiscoveryFinished();
            return;
        }

        const QList<QBluetoothUuid> results = ServiceDiscoveryBroadcastReceiver::convertParcelableArray(parcelUuidArray);
        populateDiscoveredServices(discoveredDevices.at(0), results);

        _q_serviceDiscoveryFinished();
    } else {
        qCDebug(QT_BT_ANDROID) << "Full discovery on (" << discoveredDevices.at(0).name()
                               << ")" << address.toString();

        //Full discovery uses BluetoothDevice.fetchUuidsWithSdp()
        if (!receiver) {
            receiver = new ServiceDiscoveryBroadcastReceiver();
            QObject::connect(receiver, SIGNAL(uuidFetchFinished(QBluetoothAddress,QList<QBluetoothUuid>)),
                    q, SLOT(_q_processFetchedUuids(QBluetoothAddress,QList<QBluetoothUuid>)));
        }

        if (!localDeviceReceiver) {
            localDeviceReceiver = new LocalDeviceBroadcastReceiver();
            QObject::connect(localDeviceReceiver, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)),
                             q, SLOT(_q_hostModeStateChanged(QBluetoothLocalDevice::HostMode)));
        }

        jboolean result = remoteDevice.callMethod<jboolean>("fetchUuidsWithSdp");
        if (!result) {
            //kill receiver to limit load of signals
            receiver->unregisterReceiver();
            receiver->deleteLater();
            receiver = 0;
            qCWarning(QT_BT_ANDROID) << "Cannot start dynamic fetch.";
            _q_serviceDiscoveryFinished();
        }
    }
}
void NativeDateTimePicker::open()
{
#ifdef Q_OS_ANDROID
	QAndroidJniObject activity = QtAndroid::androidActivity();
	qDebug() << __FUNCTION__ << "activity.isValid()=" << activity.isValid();

	//QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod(
	//			"wpp/android/DatePickerDialog", "show", "(Landroid/content/Context;)Ljava/lang/String;", activity.object<jobject>());
	//qDebug() << "str.isValid()=" << str.isValid();

	QDateTime dateTime;
	dateTime.setTimeZone(QTimeZone("UTC"));
	dateTime.setTimeSpec(Qt::UTC);
	dateTime.setTime_t( this->dateTime().toTime_t() );
	QAndroidJniObject initDateISO8601 = QAndroidJniObject::fromString( dateTime.toString(Qt::ISODate) );
	qDebug() << "QT--initDateISO8601=" << initDateISO8601.toString();
	qDebug() << "QT--ts=" << dateTime.toTime_t();

	QAndroidJniObject timezoneId = QAndroidJniObject::fromString( QString( System::getSystemTimezoneId() ) );
	qDebug() << "QT--timezoneId=" << timezoneId.toString();

	jlong qmlDateTimePickerPtr = (jlong)this;

	QAndroidJniObject jsonStr =
			QAndroidJniObject::callStaticObjectMethod(
				"wpp/android/DatePickerDialog", "show",
				"(Landroid/app/Activity;Ljava/lang/String;Ljava/lang/String;J)Ljava/lang/String;",
				activity.object<jobject>(),
				initDateISO8601.object<jstring>(),
				timezoneId.object<jstring>(),
				qmlDateTimePickerPtr
				);
	/*QAndroidJniObject jsonStr = activity.callObjectMethod(
		"showDatePickerDialog", "()Ljava/lang/String;"
	);*/
			/*QAndroidJniObject::callStaticObjectMethod(
				"com/kuulabu/android/app/MainActivity", "showDatePickerDialog",
				"()Ljava/lang/String;",
				activity.object<jobject>());*/
	qDebug() << "jsonStr.isValid()=" << jsonStr.isValid() << ",jsonStr=" << jsonStr.toString();

	//QAndroidJniObject dialog( "wpp/android/DatePickerDialog", "(Landroid/content/Context;)V", activity.object<jobject>() );
	//qDebug() << "dialog.isValid()=" << dialog.isValid();

	//dialog.callMethod<void>("show","()V");
	//qDebug() << "dialog show ends...";

	QAndroidJniEnvironment env;
	if (env->ExceptionCheck())
	{
		// Handle exception here.
		qDebug() << "Exception 1....";
		env->ExceptionDescribe();
		env->ExceptionClear();
	}

	/*QAndroidJniObject::callStaticObjectMethod(
				"wpp/android/DatePickerDialog", "show",
				"(Landroid/content/Context;)V",
				activity.object<jobject>()
	);*/


	/*

	//android.provider.MediaStore.EXTRA_OUTPUT
	QAndroidJniObject MediaStore__EXTRA_OUTPUT
			= QAndroidJniObject::getStaticObjectField(
				"android/provider/MediaStore", "EXTRA_OUTPUT", "Ljava/lang/String;");
	qDebug() << "MediaStore__EXTRA_OUTPUT.isValid()=" << MediaStore__EXTRA_OUTPUT.isValid();


	QAndroidJniObject action = QAndroidJniObject::fromString("android.media.action.IMAGE_CAPTURE");
	QAndroidJniObject intent=QAndroidJniObject("android/content/Intent","(Ljava/lang/String;)V",
											   action.object<jstring>());
	qDebug() << __FUNCTION__ << "intent.isValid()=" << intent.isValid();

	QAndroidJniObject extDir = QAndroidJniObject::callStaticObjectMethod(
				"android/os/Environment", "getExternalStorageDirectory", "()Ljava/io/File;");
	qDebug() << "extDir.isValid()=" << extDir.isValid();

	QAndroidJniObject filename = QAndroidJniObject::fromString("camera.jpg");

	QAndroidJniObject photo=QAndroidJniObject("java/io/File","(Ljava/io/File;Ljava/lang/String;)V",
											   extDir.object<jobject>(), filename.object<jstring>());
	qDebug() << __FUNCTION__ << "photo.isValid()=" << photo.isValid();

	takePhotoSavedUri = QAndroidJniObject::callStaticObjectMethod(
				"android/net/Uri", "fromFile", "(Ljava/io/File;)Landroid/net/Uri;", photo.object<jobject>());
	qDebug() << "takePhotoSavedUri.isValid()=" << takePhotoSavedUri.isValid();

	intent.callObjectMethod(
				"putExtra","(Ljava/lang/String;Landroid/os/Parcelable;)Landroid/content/Intent;",
				MediaStore__EXTRA_OUTPUT.object<jstring>(), takePhotoSavedUri.object<jobject>());
	qDebug() << __FUNCTION__ << "intent.isValid()=" << intent.isValid();

	int SHOOT_PHOTO = 1;
	QtAndroid::startActivity(intent, SHOOT_PHOTO, this);*/
#endif

}
bool QBluetoothSocketPrivate::fallBackConnect(QAndroidJniObject uuid, int channel)
{
    qCWarning(QT_BT_ANDROID) << "Falling back to workaround.";

    QAndroidJniEnvironment env;
    jclass remoteDeviceClazz = env->GetObjectClass(remoteDevice.object());
    jmethodID getClassMethod = env->GetMethodID(remoteDeviceClazz, "getClass", "()Ljava/lang/Class;");
    if (!getClassMethod) {
        qCWarning(QT_BT_ANDROID) << "BluetoothDevice.getClass method could not be found.";
        return false;
    }


    QAndroidJniObject remoteDeviceClass = QAndroidJniObject(env->CallObjectMethod(remoteDevice.object(), getClassMethod));
    if (!remoteDeviceClass.isValid()) {
        qCWarning(QT_BT_ANDROID) << "Could not invoke BluetoothDevice.getClass.";
        return false;
    }

    jclass classClass = env->FindClass("java/lang/Class");
    jclass integerClass = env->FindClass("java/lang/Integer");
    jfieldID integerType = env->GetStaticFieldID(integerClass, "TYPE", "Ljava/lang/Class;");
    jobject integerObject = env->GetStaticObjectField(integerClass, integerType);
    if (!integerObject) {
        qCWarning(QT_BT_ANDROID) << "Could not get Integer.TYPE";
        return false;
    }

    jobjectArray paramTypes = env->NewObjectArray(1, classClass, integerObject);
    if (!paramTypes) {
        qCWarning(QT_BT_ANDROID) << "Could not create new Class[]{Integer.TYPE}";
        return false;
    }

    QAndroidJniObject parcelUuid("android/os/ParcelUuid", "(Ljava/util/UUID;)V",
                                 uuid.object());
    if (parcelUuid.isValid()) {
        jint socketChannel = remoteDevice.callMethod<jint>("getServiceChannel",
                                                           "(Landroid/os/ParcelUuid;)I",
                                                           parcelUuid.object());
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        if (socketChannel
                == remoteDevice.getStaticField<jint>("android/bluetooth/BluetoothDevice", "ERROR")) {
            qCWarning(QT_BT_ANDROID) << "Cannot determine RFCOMM service channel.";
        } else {
            qCWarning(QT_BT_ANDROID) << "Using found rfcomm channel" << socketChannel;
            channel = socketChannel;
        }
    }

    QAndroidJniObject method = remoteDeviceClass.callObjectMethod(
                "getMethod",
                "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;",
                QAndroidJniObject::fromString(QLatin1String("createRfcommSocket")).object<jstring>(),
                paramTypes);
    if (!method.isValid() || env->ExceptionCheck()) {
        qCWarning(QT_BT_ANDROID) << "Could not invoke getMethod";
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }
        return false;
    }

    jclass methodClass = env->GetObjectClass(method.object());
    jmethodID invokeMethodId = env->GetMethodID(
                methodClass, "invoke",
                "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;");
    if (!invokeMethodId) {
        qCWarning(QT_BT_ANDROID) << "Could not invoke method.";
        return false;
    }

    jmethodID valueOfMethodId = env->GetStaticMethodID(integerClass, "valueOf", "(I)Ljava/lang/Integer;");
    jclass objectClass = env->FindClass("java/lang/Object");
    jobjectArray invokeParams = env->NewObjectArray(1, objectClass, env->CallStaticObjectMethod(integerClass, valueOfMethodId, channel));


    jobject invokeResult = env->CallObjectMethod(method.object(), invokeMethodId,
                                                 remoteDevice.object(), invokeParams);
    if (!invokeResult)
    {
        qCWarning(QT_BT_ANDROID) << "Invoke Resulted with error.";
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }
        return false;
    }

    socketObject = QAndroidJniObject(invokeResult);
    socketObject.callMethod<void>("connect");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        qCWarning(QT_BT_ANDROID) << "Socket connect via workaround failed.";

        return false;
    }

    qCWarning(QT_BT_ANDROID) << "Workaround invoked.";
    return true;
}
Example #26
0
QList<QObject *> Gallery::fetchAll()
{

#ifdef Q_OS_ANDROID
	//ref(android): http://stackoverflow.com/questions/4195660/get-list-of-photo-galleries-on-android
	//ref(thumbnail): http://stackoverflow.com/questions/5013176/displaying-photo-thumbnails-on-map
	/*
		// which image properties are we querying
			String[] projection = new String[]{
					MediaStore.Images.Media._ID,
					MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
					MediaStore.Images.Media.DATE_TAKEN
			};
	 */
	QAndroidJniObject MediaStore_Images_Media__ID
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/BaseColumns", "_ID", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_Media__ID.isValid()=" << MediaStore_Images_Media__ID.isValid();

	QAndroidJniObject MediaStore_Images_Media_BUCKET_ID
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$ImageColumns", "BUCKET_ID", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_Media_BUCKET_ID.isValid()=" << MediaStore_Images_Media_BUCKET_ID.isValid();

	QAndroidJniObject MediaStore_Images_Media_BUCKET_DISPLAY_NAME
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$ImageColumns", "BUCKET_DISPLAY_NAME", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_Media_BUCKET_DISPLAY_NAME.isValid()=" << MediaStore_Images_Media_BUCKET_DISPLAY_NAME.isValid();

	QAndroidJniObject MediaStore_Images_Media_DATE_TAKEN
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$ImageColumns", "DATE_TAKEN", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_Media_DATE_TAKEN.isValid()=" << MediaStore_Images_Media_DATE_TAKEN.isValid();

	QAndroidJniObject MediaStore_MediaColumns_DATA
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
	qDebug() << "MediaStore_MediaColumns_DATA.isValid()=" << MediaStore_MediaColumns_DATA.isValid();

	QAndroidJniObject MediaStore_MediaColumns_WIDTH
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$MediaColumns", "WIDTH", "Ljava/lang/String;");
	qDebug() << "MediaStore_MediaColumns_WIDTH.isValid()=" << MediaStore_MediaColumns_WIDTH.isValid();

	QAndroidJniObject MediaStore_MediaColumns_HEIGHT
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$MediaColumns", "HEIGHT", "Ljava/lang/String;");
	qDebug() << "MediaStore_MediaColumns_HEIGHT.isValid()=" << MediaStore_MediaColumns_HEIGHT.isValid();

	QAndroidJniObject MediaStore_Images_ImageColumns_MINI_THUMB_MAGIC
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$ImageColumns", "MINI_THUMB_MAGIC", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_ImageColumns_MINI_THUMB_MAGIC.isValid()=" << MediaStore_Images_ImageColumns_MINI_THUMB_MAGIC.isValid();

	jint MediaStore_Images_Thumbnails_MICRO_KIND = QAndroidJniObject::getStaticField<jint>(
				"android/provider/MediaStore$Images$Thumbnails", "MICRO_KIND");
	qDebug() << "MediaStore_Images_Thumbnails_MICRO_KIND=" << MediaStore_Images_Thumbnails_MICRO_KIND;

	QAndroidJniObject MediaStore_Images_Thumbnails_IMAGE_ID
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$Thumbnails", "IMAGE_ID", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_Thumbnails_IMAGE_ID.isValid()=" << MediaStore_Images_Thumbnails_IMAGE_ID.isValid();

	QAndroidJniObject MediaStore_Images_ImageColumns_ORIENTATION
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$ImageColumns", "ORIENTATION", "Ljava/lang/String;");
	qDebug() << "MediaStore_Images_ImageColumns_ORIENTATION.isValid()=" << MediaStore_Images_ImageColumns_ORIENTATION.isValid();


	QAndroidJniEnvironment env;
	jstring emptyJString = env->NewStringUTF("");

	jobjectArray projection = (jobjectArray)env->NewObjectArray(
		9,
		env->FindClass("java/lang/String"),
		emptyJString
	);
	jobject projection0 = env->NewStringUTF( MediaStore_Images_Media__ID.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 0, projection0 );
	jobject projection1 = env->NewStringUTF( MediaStore_Images_Media_BUCKET_ID.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 1, projection1 );
	jobject projection2 = env->NewStringUTF( MediaStore_Images_Media_BUCKET_DISPLAY_NAME.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 2, projection2 );
	jobject projection3 = env->NewStringUTF( MediaStore_Images_Media_DATE_TAKEN.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 3, projection3 );
	jobject projection4 = env->NewStringUTF( MediaStore_MediaColumns_DATA.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 4, projection4 );
	jobject projection5 = env->NewStringUTF( MediaStore_MediaColumns_WIDTH.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 5, projection5 );
	jobject projection6 = env->NewStringUTF( MediaStore_MediaColumns_HEIGHT.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 6, projection6 );
	jobject projection7 = env->NewStringUTF( MediaStore_Images_ImageColumns_MINI_THUMB_MAGIC.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 7, projection7 );
	jobject projection8 = env->NewStringUTF( MediaStore_Images_ImageColumns_ORIENTATION.toString().toStdString().c_str() );
	env->SetObjectArrayElement(
		projection, 8, projection8 );


	/*
	// Get the base URI for the People table in the Contacts content provider.
	Uri Images_EXTERNAL_CONTENT_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
	 */
	QAndroidJniObject Images_EXTERNAL_CONTENT_URI
			= QAndroidJniObject::getStaticObjectField(
			"android/provider/MediaStore$Images$Media", "EXTERNAL_CONTENT_URI", "Landroid/net/Uri;");
	qDebug() << "Images_EXTERNAL_CONTENT_URI.isValid()=" << Images_EXTERNAL_CONTENT_URI.isValid();

	/*
	// Make the query.
		Cursor cur = managedQuery(Images_EXTERNAL_CONTENT_URI,
				projection, // Which columns to return
				"",         // Which rows to return (all rows)
				null,       // Selection arguments (none)
				""          // Ordering
				);
	*/
	QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
	qDebug() << "activity.isValid()=" << activity.isValid();

	QAndroidJniObject contentResolver = activity.callObjectMethod("getContentResolver","()Landroid/content/ContentResolver;");

	QAndroidJniObject emptyString=QAndroidJniObject::fromString(QString("")); //path is valid
	qDebug() << "emptyString.isValid()=" << emptyString.isValid();
	QAndroidJniObject nullObj;

	QAndroidJniObject cur = activity.callObjectMethod("managedQuery",
							"(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;",
							Images_EXTERNAL_CONTENT_URI.object<jobject>(), projection,
							emptyString.object<jstring>(), nullObj.object<jobject>(), emptyString.object<jstring>()
							);
	qDebug() << "cur.isValid()=" << cur.isValid();
	if (env->ExceptionCheck())
	{
		// Handle exception here.
		qDebug() << "Exception when getting \"cur\"....";
		env->ExceptionDescribe();
		env->ExceptionClear();
	}

	QMap<QString, wpp::qt::GalleryFolder> folderList;

	//if (cur.moveToFirst()) {
	if ( cur.callMethod<jboolean>("moveToFirst") )
	{
		/*
		int bucketColumn = cur.getColumnIndex(
			MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
		*/
		jint bucketColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_Media_BUCKET_DISPLAY_NAME.object<jstring>());
		qDebug() << "bucketColumn = " << bucketColumn;
		/*
		 int dateColumn = cur.getColumnIndex(
			MediaStore.Images.Media.DATE_TAKEN);
		*/
		jint dateColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_Media_DATE_TAKEN.object<jstring>());
		qDebug() << "dateColumn = " << dateColumn;


		jint idColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_Media__ID.object<jstring>());
		qDebug() << "idColumn = " << idColumn;

		jint bucketIdColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_Media_BUCKET_ID.object<jstring>());
		qDebug() << "bucketIdColumn = " << bucketIdColumn;

		jint mediaColumnsDataColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_MediaColumns_DATA.object<jstring>());
		qDebug() << "mediaColumnsDataColumn = " << mediaColumnsDataColumn;

		jint widthColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_MediaColumns_WIDTH.object<jstring>());
		qDebug() << "widthColumn = " << widthColumn;

		jint heightColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_MediaColumns_HEIGHT.object<jstring>());
		qDebug() << "heightColumn = " << heightColumn;

		jint thumbIdColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_ImageColumns_MINI_THUMB_MAGIC.object<jstring>());
		qDebug() << "thumbIdColumn = " << thumbIdColumn;

		//jint imageIdColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_Media__ID.object<jstring>());
		//qDebug() << "imageIdColumn = " << imageIdColumn;

		jint orientationColumn = cur.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", MediaStore_Images_ImageColumns_ORIENTATION.object<jstring>());
		qDebug() << "orientationColumn = " << orientationColumn;

		do
		{
			//QAndroidJniObject _id = cur.callObjectMethod("getString","(I)Ljava/lang/String;", idColumn );
			//qDebug() << "_id.isValid()=" << _id.isValid();
			jlong _id = cur.callMethod<jlong>("getLong","(I)J", idColumn );
			qDebug() << "_id=" << _id;

			QAndroidJniObject bucketId = cur.callObjectMethod("getString","(I)Ljava/lang/String;", bucketIdColumn );
			qDebug() << "bucketId.isValid()=" << bucketId.isValid();

			QAndroidJniObject bucket = cur.callObjectMethod("getString","(I)Ljava/lang/String;", bucketColumn );
			qDebug() << "bucket.isValid()=" << bucket.isValid();

			QAndroidJniObject date = cur.callObjectMethod("getString","(I)Ljava/lang/String;", dateColumn );
			qDebug() << "date.isValid()=" << date.isValid();

			QAndroidJniObject mediaColumnsData = cur.callObjectMethod("getString","(I)Ljava/lang/String;", mediaColumnsDataColumn );
			qDebug() << "mediaColumnsData.isValid()=" << mediaColumnsData.isValid();

			QAndroidJniObject width = cur.callObjectMethod("getString","(I)Ljava/lang/String;", widthColumn );
			qDebug() << "width.isValid()=" << width.isValid();

			QAndroidJniObject height = cur.callObjectMethod("getString","(I)Ljava/lang/String;", heightColumn );
			qDebug() << "height.isValid()=" << height.isValid();

			QAndroidJniObject thumbId = cur.callObjectMethod("getString","(I)Ljava/lang/String;", thumbIdColumn );
			qDebug() << "thumbId.isValid()=" << thumbId.isValid();

			//jlong thumbId = cur.callMethod<jlong>("getLong", "(I)J", thumbIdColumn);
			if (env->ExceptionCheck())
			{
				// Handle exception here.
				qDebug() << "Exception when getting \"thumbId\"....";
				env->ExceptionDescribe();
				env->ExceptionClear();
			}
			qDebug() << "thumbId=" << thumbId.toString();

			//QAndroidJniObject imageId = cur.callObjectMethod("getString","(I)Ljava/lang/String;", imageIdColumn );
			//qDebug() << "imageId.isValid()=" << imageId.isValid();



			// MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), imageID, MediaStore.Images.Thumbnails.MINI_KIND, null);
			/*QAndroidJniObject thumbnailBitmap = QAndroidJniObject::callStaticObjectMethod(
						"android/provider/MediaStore$Images$Thumbnails", "getThumbnail",
						"(Landroid/content/ContentResolver;JILandroid/graphics/BitmapFactory$Options;)Landroid/graphics/Bitmap;",
						contentResolver.object<jobject>(), _id,
						MediaStore_Images_Thumbnails_MICRO_KIND,
						nullObj.object<jobject>()
						);
			qDebug() << "thumbnailBitmap.isValid()=" << thumbnailBitmap.isValid();
			*/

			//QAndroidJniObject orientation = cur.callObjectMethod("getString","(I)Ljava/lang/String;", orientationColumn );
			//qDebug() << "orientation.isValid()=" << orientation.isValid();
			jint orientation = cur.callMethod<jint>("getInt","(I)I", orientationColumn );
			qDebug() << "orientation=" << orientation;



			QAndroidJniObject thumbData = QAndroidJniObject::fromString( QString("") );
			if ( false ) //if ( thumbId > 0 )
			{
				//        String[] args =  new String[]{String.valueOf(thumbId)};
				jobjectArray args = (jobjectArray)env->NewObjectArray(
					1,
					env->FindClass("java/lang/String"),
					emptyJString
				);
				jstring thumbIdJString = env->NewStringUTF( thumbId.toString().toStdString().c_str() );
				env->SetObjectArrayElement(
					//args, 0, env->NewStringUTF( QString().sprintf("%ld",thumbId).toStdString().c_str() ) );
					args, 0, thumbIdJString );

				//        Cursor curThumb = managedQuery(Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails._ID + "= ?", args, null);
				QAndroidJniObject Images_Thumbnails__ID
						= QAndroidJniObject::getStaticObjectField(
						"android/provider/BaseColumns", "_ID", "Ljava/lang/String;");
				qDebug() << "Images_Thumbnails__ID.isValid()=" << Images_Thumbnails__ID.isValid();
				QAndroidJniObject selection=QAndroidJniObject::fromString( Images_Thumbnails__ID.toString() + "= ?" ); //path is valid

				QAndroidJniObject curThumb = activity.callObjectMethod("managedQuery",
										"(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;",
										Images_EXTERNAL_CONTENT_URI.object<jobject>(), nullObj.object<jobject>(),
										selection.object<jstring>(), args, nullObj.object<jobject>()
										);
				qDebug() << "curThumb.isValid()=" << curThumb.isValid();

				//if( curThumb.moveToFirst() ){
				if ( curThumb.callMethod<jboolean>("moveToFirst") )
				{
					//            String path = curThumb.getString(curThumb.getColumnIndex(Thumbnails.DATA));
					QAndroidJniObject Images_Thumbnails_DATA
							= QAndroidJniObject::getStaticObjectField(
							"android/provider/MediaStore$Images$Thumbnails", "DATA", "Ljava/lang/String;");
					qDebug() << "Images_Thumbnails_DATA.isValid()=" << Images_Thumbnails_DATA.isValid();

					jint thumbDataColumn = curThumb.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", Images_Thumbnails_DATA.object<jstring>());
					qDebug() << "thumbDataColumn = " << thumbDataColumn;

					thumbData = cur.callObjectMethod("getString","(I)Ljava/lang/String;", thumbDataColumn );
					qDebug() << "thumbData.isValid()=" << thumbData.isValid();

				}
				env->DeleteLocalRef(args);
				env->DeleteLocalRef(thumbIdJString);
			}

			qDebug() << "_id=" << _id
					 << ", bucketId=" << bucketId.toString()
					 << ", bucket=" << bucket.toString()
					 << ", date_taken=" << date.toString()
					 << ", WxH=" << width.toString() << "x" << height.toString()
					 << ", orientation=" << orientation
					 << ", thumbId=" << thumbId.toString()
					 << ", thumbData=" << thumbData.toString()
					 << ", abs_path=" << mediaColumnsData.toString();

			if ( !folderList.contains( bucketId.toString() ) )
			{
				qDebug() << "create folder:" << bucketId.toString();
				GalleryFolder galleryFolder;
				galleryFolder.setId( bucketId.toString() );
				galleryFolder.setName( bucket.toString() );
				folderList.insert(bucketId.toString(), galleryFolder);
			}

			wpp::qt::GalleryPhoto photo;
			photo.setAbsolutePath( mediaColumnsData.toString() );
			photo.setWidth( width.toString().toInt() );
			photo.setHeight( height.toString().toInt() );
			photo.setOrientation( orientation );
			qDebug()<<"orientation===" << photo.getOrientation();
			folderList[ bucketId.toString() ].addPhoto( photo );
		}
		while ( cur.callMethod<jboolean>("moveToNext") );
		//while (cur.moveToNext());
	}

	env->DeleteLocalRef(emptyJString);
	env->DeleteLocalRef(projection);
	env->DeleteLocalRef(projection0);
	env->DeleteLocalRef(projection1);
	env->DeleteLocalRef(projection2);
	env->DeleteLocalRef(projection3);
	env->DeleteLocalRef(projection4);
	env->DeleteLocalRef(projection5);
	env->DeleteLocalRef(projection6);
	env->DeleteLocalRef(projection7);
	env->DeleteLocalRef(projection8);

	//change QMap to QList
	QList<QObject *> finalFolderList;
	for ( wpp::qt::GalleryFolder folder : folderList )
	{
		finalFolderList.push_back( new wpp::qt::GalleryFolder( folder ) );
	}
	this->setFolders( QVariant::fromValue( finalFolderList ) );

	for ( const QObject *obj : finalFolderList )
	{
		const wpp::qt::GalleryFolder *f = dynamic_cast< const wpp::qt::GalleryFolder * >( obj );
		qDebug() << "FOLDER" << f->getPhotos().value< QList<QObject*> >().size() << ":" << f->getName();
	}

	return finalFolderList;

#else
	return QList<QObject *>();
#endif
}
Example #27
0
void Wpp::setSoftInputMode(SoftInputMode softInputMode)
{
#ifdef Q_OS_ANDROID
	jint SOFT_INPUT_ADJUST_NOTHING = QAndroidJniObject::getStaticField<jint>(
				"android/view/WindowManager$LayoutParams", "SOFT_INPUT_ADJUST_NOTHING");
	qDebug() << __FUNCTION__ << "SOFT_INPUT_ADJUST_NOTHING=" << SOFT_INPUT_ADJUST_NOTHING;

	jint SOFT_INPUT_ADJUST_UNSPECIFIED = QAndroidJniObject::getStaticField<jint>(
				"android/view/WindowManager$LayoutParams", "SOFT_INPUT_ADJUST_UNSPECIFIED");
	qDebug() << __FUNCTION__ << "SOFT_INPUT_ADJUST_UNSPECIFIED=" << SOFT_INPUT_ADJUST_UNSPECIFIED;

	jint SOFT_INPUT_ADJUST_RESIZE = QAndroidJniObject::getStaticField<jint>(
				"android/view/WindowManager$LayoutParams", "SOFT_INPUT_ADJUST_RESIZE");
	qDebug() << __FUNCTION__ << "SOFT_INPUT_ADJUST_RESIZE=" << SOFT_INPUT_ADJUST_RESIZE;

	jint SOFT_INPUT_ADJUST_PAN = QAndroidJniObject::getStaticField<jint>(
				"android/view/WindowManager$LayoutParams", "SOFT_INPUT_ADJUST_PAN");
	qDebug() << __FUNCTION__ << "SOFT_INPUT_ADJUST_PAN=" << SOFT_INPUT_ADJUST_PAN;

	jint param = -1;
	switch ( softInputMode )
	{
	case ADJUST_NOTHING:
		param = SOFT_INPUT_ADJUST_NOTHING;
	break;
	case ADJUST_UNSPECIFIED:
		param = SOFT_INPUT_ADJUST_UNSPECIFIED;
	break;
	case ADJUST_RESIZE:
		param = SOFT_INPUT_ADJUST_RESIZE;
	break;
	case ADJUST_PAN:
		param = SOFT_INPUT_ADJUST_PAN;
	break;
	default:
		param = -1;
	}
	if ( param != -1 )
	{
		QAndroidJniObject activity = QtAndroid::androidActivity();
		qDebug() << __FUNCTION__ << "activity.isValid()=" << activity.isValid();

		QAndroidJniObject::callStaticMethod<void>(
					"wpp.android.Wpp", "setSoftInputMode",
					"(Landroid/app/Activity;I)V",
					activity.object<jobject>(), param
		);

		QAndroidJniEnvironment env;
		if (env->ExceptionCheck())
		{
			// Handle exception here.
			qDebug() << "Exception 1....";
			env->ExceptionDescribe();
			env->ExceptionClear();
		}

		/*QAndroidJniObject window = activity.callObjectMethod(
					"getWindow","()Landroid/view/Window;");
		qDebug() << __FUNCTION__ << "window.isValid()=" << window.isValid();

		QAndroidJniEnvironment env;
		if (env->ExceptionCheck())
		{
			// Handle exception here.
			qDebug() << "Exception 1....";
			env->ExceptionDescribe();
			env->ExceptionClear();
		}


		if ( window.isValid() )
		{
			qDebug() << __FUNCTION__ << ":param=" << param;
			window.callMethod<void>("setSoftInputMode","(I)V",param);
			if (env->ExceptionCheck())
			{
				// Handle exception here.
				qDebug() << "Exception 2....";
				env->ExceptionDescribe();
				env->ExceptionClear();
			}
		}*/
	}

#endif
	m_softInputMode = softInputMode;
}
void QBluetoothSocketPrivate::connectToServiceConc(const QBluetoothAddress &address,
                                                   const QBluetoothUuid &uuid, QIODevice::OpenMode openMode, int fallbackServiceChannel)
{
    Q_Q(QBluetoothSocket);
    Q_UNUSED(openMode);

    qCDebug(QT_BT_ANDROID) << "connectToServiceConc()" << address.toString() << uuid.toString();

    if (!adapter.isValid()) {
        qCWarning(QT_BT_ANDROID) << "Device does not support Bluetooth";
        errorString = QBluetoothSocket::tr("Device does not support Bluetooth");
        q->setSocketError(QBluetoothSocket::NetworkError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    const int state = adapter.callMethod<jint>("getState");
    if (state != 12 ) { //BluetoothAdapter.STATE_ON
        qCWarning(QT_BT_ANDROID) << "Bt device offline";
        errorString = QBluetoothSocket::tr("Device is powered off");
        q->setSocketError(QBluetoothSocket::NetworkError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    QAndroidJniEnvironment env;
    QAndroidJniObject inputString = QAndroidJniObject::fromString(address.toString());
    remoteDevice = adapter.callObjectMethod("getRemoteDevice",
                                            "(Ljava/lang/String;)Landroid/bluetooth/BluetoothDevice;",
                                            inputString.object<jstring>());
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        errorString = QBluetoothSocket::tr("Cannot access address %1", "%1 = Bt address e.g. 11:22:33:44:55:66").arg(address.toString());
        q->setSocketError(QBluetoothSocket::HostNotFoundError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    //cut leading { and trailing } {xxx-xxx}
    QString tempUuid = uuid.toString();
    tempUuid.chop(1); //remove trailing '}'
    tempUuid.remove(0, 1); //remove first '{'

    inputString = QAndroidJniObject::fromString(tempUuid);
    QAndroidJniObject uuidObject = QAndroidJniObject::callStaticObjectMethod("java/util/UUID", "fromString",
                                                                       "(Ljava/lang/String;)Ljava/util/UUID;",
                                                                       inputString.object<jstring>());

    socketObject = remoteDevice.callObjectMethod("createRfcommSocketToServiceRecord",
                                                 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothSocket;",
                                                 uuidObject.object<jobject>());

    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        socketObject = remoteDevice = QAndroidJniObject();
        errorString = QBluetoothSocket::tr("Cannot connect to %1 on %2",
                                           "%1 = uuid, %2 = Bt address").arg(uuid.toString()).arg(address.toString());
        q->setSocketError(QBluetoothSocket::ServiceNotFoundError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    socketObject.callMethod<void>("connect");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        bool success = fallBackConnect(uuidObject, fallbackServiceChannel);
        if (!success) {
            errorString = QBluetoothSocket::tr("Connection to service failed");
            socketObject = remoteDevice = QAndroidJniObject();
            q->setSocketError(QBluetoothSocket::ServiceNotFoundError);
            q->setSocketState(QBluetoothSocket::UnconnectedState);

            env->ExceptionClear(); //just in case
            return;
        }
    }

    if (inputThread) {
        inputThread->deleteLater();
        inputThread = 0;
    }

    inputStream = socketObject.callObjectMethod("getInputStream", "()Ljava/io/InputStream;");
    outputStream = socketObject.callObjectMethod("getOutputStream", "()Ljava/io/OutputStream;");

    if (env->ExceptionCheck() || !inputStream.isValid() || !outputStream.isValid()) {
        env->ExceptionDescribe();
        env->ExceptionClear();

        //close socket again
        socketObject.callMethod<void>("close");
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        socketObject = inputStream = outputStream = remoteDevice = QAndroidJniObject();


        errorString = QBluetoothSocket::tr("Obtaining streams for service failed");
        q->setSocketError(QBluetoothSocket::NetworkError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    inputThread = new InputStreamThread(this);
    QObject::connect(inputThread, SIGNAL(dataAvailable()),
                     q, SIGNAL(readyRead()), Qt::QueuedConnection);
    QObject::connect(inputThread, SIGNAL(error(int)),
                     this, SLOT(inputThreadError(int)), Qt::QueuedConnection);

    if (!inputThread->run()) {
        //close socket again
        socketObject.callMethod<void>("close");
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
        }

        socketObject = inputStream = outputStream = remoteDevice = QAndroidJniObject();

        delete inputThread;
        inputThread = 0;

        errorString = QBluetoothSocket::tr("Input stream thread cannot be started");
        q->setSocketError(QBluetoothSocket::NetworkError);
        q->setSocketState(QBluetoothSocket::UnconnectedState);
        return;
    }

    // only unbuffered behavior supported at this stage
    q->setOpenMode(QIODevice::ReadWrite|QIODevice::Unbuffered);

    q->setSocketState(QBluetoothSocket::ConnectedState);
    emit q->connected();
}
Example #29
0
/*
 * This function operates on the assumption that each
 * field is of type java/lang/String.
 */
QAndroidJniObject valueForStaticField(JavaNames javaName, JavaNames javaFieldName)
{
    //construct key
    //the switch statements are used to reduce the number of duplicated strings
    //in the library

    const char* className;
    switch (javaName) {
    case JavaNames::BluetoothAdapter:
        className = javaBluetoothAdapterClassName; break;
    case JavaNames::BluetoothDevice:
        className = javaBluetoothDeviceClassName; break;
    default:
        qCWarning(QT_BT_ANDROID) << "Unknown java class name passed to valueForStaticField():" << javaName;
        return QAndroidJniObject();
    }

    const char *fieldName;
    switch (javaFieldName) {
    case JavaNames::ActionAclConnected:
        fieldName = javaActionAclConnected; break;
    case JavaNames::ActionAclDisconnected:
        fieldName = javaActionAclDisconnected; break;
    case JavaNames::ActionBondStateChanged:
        fieldName = javaActionBondStateChanged; break;
    case JavaNames::ActionDiscoveryStarted:
        fieldName = javaActionDiscoveryStarted; break;
    case JavaNames::ActionDiscoveryFinished:
        fieldName = javaActionDiscoveryFinished; break;
    case JavaNames::ActionFound:
        fieldName = javaActionFound; break;
    case JavaNames::ActionPairingRequest:
        fieldName = javaActionPairingRequest; break;
    case JavaNames::ActionScanModeChanged:
        fieldName = javaActionScanModeChanged; break;
    case JavaNames::ActionUuid:
        fieldName = javaActionUuid; break;
    case JavaNames::ExtraBondState:
        fieldName = javaExtraBondState; break;
    case JavaNames::ExtraDevice:
        fieldName = javaExtraDevice; break;
    case JavaNames::ExtraPairingKey:
        fieldName = javaExtraPairingKey; break;
    case JavaNames::ExtraPairingVariant:
        fieldName = javaExtraPairingVariant; break;
    case JavaNames::ExtraRssi:
        fieldName = javaExtraRssi; break;
    case JavaNames::ExtraScanMode:
        fieldName = javaExtraScanMode; break;
    case JavaNames::ExtraUuid:
        fieldName = javaExtraUuid; break;
    default:
        qCWarning(QT_BT_ANDROID) << "Unknown java field name passed to valueForStaticField():" << javaFieldName;
        return QAndroidJniObject();
    }

    int offset_class = qstrlen(className);
    int offset_field = qstrlen(fieldName);
    QByteArray key(offset_class + offset_field, Qt::Uninitialized);
    memcpy(key.data(), className, offset_class);
    memcpy(key.data()+offset_class, fieldName, offset_field);

    JCachedStringFields::iterator it = cachedStringFields()->find(key);
    if (it == cachedStringFields()->end()) {
        QAndroidJniEnvironment env;
        QAndroidJniObject fieldValue = QAndroidJniObject::getStaticObjectField(
                                            className, fieldName, "Ljava/lang/String;");
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            env->ExceptionClear();
            cachedStringFields()->insert(key, QAndroidJniObject());
            return QAndroidJniObject();
        }

        cachedStringFields()->insert(key, fieldValue);
        return fieldValue;
    } else {
        return it.value();
    }
}