QList<QSerialPortInfo> availablePortsByFiltersOfDevices(bool &ok)
{
    QList<QSerialPortInfo> serialPortInfoList;

    //__android_log_print(ANDROID_LOG_INFO, V_TAG, "Collecting device list");
    QAndroidJniObject resultL = QAndroidJniObject::callStaticObjectMethod(
        V_jniClassName,
        "availableDevicesInfo",
        "()[Ljava/lang/String;");
    
    if (!resultL.isValid()) {
        __android_log_print(ANDROID_LOG_ERROR, V_TAG, "Error from availableDevicesInfo");
        ok = false;
        return serialPortInfoList;
    }

    QAndroidJniEnvironment envL;
    jobjectArray objArrayL = resultL.object<jobjectArray>();
    int countL = envL->GetArrayLength(objArrayL);

    for (int iL = 0; iL < countL; iL++)
    {
        QSerialPortInfoPrivate priv;
        jstring stringL = (jstring)(envL->GetObjectArrayElement(objArrayL, iL));
        const char *rawStringL = envL->GetStringUTFChars(stringL, 0);
        //__android_log_print(ANDROID_LOG_INFO, V_TAG, "Adding device: %s", rawStringL);
        QStringList strListL = QString::fromUtf8(rawStringL).split(QStringLiteral(":"));
        envL->ReleaseStringUTFChars(stringL, rawStringL);

        priv.portName               = strListL[0];
        priv.device                 = strListL[0];
        priv.manufacturer           = strListL[1];
        priv.productIdentifier      = strListL[2].toInt();
        priv.hasProductIdentifier   = (priv.productIdentifier != 0) ? true: false;
        priv.vendorIdentifier       = strListL[3].toInt();
        priv.hasVendorIdentifier    = (priv.vendorIdentifier  != 0) ? true: false;

        serialPortInfoList.append(priv);
    }

    return serialPortInfoList;
}
Exemplo n.º 2
0
void ImagePicker::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject & data)
{
	qDebug() << __FUNCTION__;
	int PICK_FROM_FILE = 2;
	int PICK_FROM_FILE_KITKAT = 3;
	jint Activity__RESULT_OK = QAndroidJniObject::getStaticField<jint>(
				"android/app/Activity", "RESULT_OK");
	qDebug() << __FUNCTION__ <<":receiverRequestCode=" << receiverRequestCode
								<< ",resultCode=" << resultCode
								<< ",Activity__RESULT_OK=" << Activity__RESULT_OK;

	if ( ( receiverRequestCode == PICK_FROM_FILE || receiverRequestCode == PICK_FROM_FILE_KITKAT
		   || receiverRequestCode == 200
		   || receiverRequestCode == 9000 )
		 && resultCode == Activity__RESULT_OK )
	{
		if ( receiverRequestCode == 200 )
		{
			QAndroidJniEnvironment env;

			QAndroidJniObject pathsJavaObj = data.callObjectMethod("getStringArrayExtra","(Ljava/lang/String;)[Ljava/lang/String;");
			qDebug() << __FUNCTION__ << "pathsJavaObj.isValid()=" << pathsJavaObj.isValid();

			jobjectArray pathArray = pathsJavaObj.object<jobjectArray>();
			int stringCount = env->GetArrayLength(pathArray);

			QStringList paths;
			for (int i=0; i<stringCount; i++)
			{
				jstring string = (jstring) env->GetObjectArrayElement(pathArray, i);
				const char *rawString = env->GetStringUTFChars(string, 0);
				// Don't forget to call `ReleaseStringUTFChars` when you're done.

				QString path(rawString);
				qDebug() << "path[]=" << path;
				paths.append(path);

				env->ReleaseStringUTFChars(string, rawString);
			}
			emit this->accepted(paths);

		}
		else if ( receiverRequestCode == 9000 )//PhotoPicker
		{
			qDebug() << __FUNCTION__ << ":code=9000";
			QAndroidJniEnvironment env;

			QAndroidJniObject PhotoPickerActivity__KEY_SELECTED_PHOTOS
					= QAndroidJniObject::getStaticObjectField(
						"me/iwf/photopicker/PhotoPickerActivity", "KEY_SELECTED_PHOTOS", "Ljava/lang/String;");
			qDebug() << "PhotoPickerActivity__KEY_SELECTED_PHOTOS.isValid()=" << PhotoPickerActivity__KEY_SELECTED_PHOTOS.isValid();

			QAndroidJniObject photos = data.callObjectMethod("getStringArrayListExtra","(Ljava/lang/String;)Ljava/util/ArrayList;",
														  PhotoPickerActivity__KEY_SELECTED_PHOTOS.object<jstring>());
			qDebug() << "photos.isValid()=" << photos.isValid();

			QAndroidJniObject photosObjectArray = photos.callObjectMethod("toArray","()[Ljava/lang/Object;");
			qDebug() << "photosObjectArray.isValid()=" << photosObjectArray.isValid();

			jobjectArray pathArray = photosObjectArray.object<jobjectArray>();
			int stringCount = env->GetArrayLength(pathArray);
			qDebug() << "pathArray=" << (void*)pathArray;
			qDebug() << "stringCount=" << stringCount;

			QStringList paths;
			for (int i=0; i<stringCount; i++)
			{
				qDebug() << "for pathArray...i=" << i;
				jstring string = (jstring) env->GetObjectArrayElement(pathArray, i);
				const char *rawString = env->GetStringUTFChars(string, 0);
				// Don't forget to call `ReleaseStringUTFChars` when you're done.
				qDebug() << "for pathArray...i=" << i << ":rawString=" << rawString;

				QString path(rawString);
				qDebug() << "path[]=" << path;
				paths.append(path);

				env->ReleaseStringUTFChars(string, rawString);
			}
			emit this->accepted(paths);

		}
		else
		{
			QAndroidJniEnvironment env;
			QAndroidJniObject uri = data.callObjectMethod("getData","()Landroid/net/Uri;");
			qDebug() << __FUNCTION__ << "uri.isValid()=" << uri.isValid();
			qDebug() << __FUNCTION__ << "uri=" << uri.toString();
			/*
			  url is like: "content://media/external/images/media/87332"
			  for KitKat(android 4.4), uri is like: "content://com.android.providers.media.documents/document/image:3951"
			 */
			QAndroidJniObject activity = QtAndroid::androidActivity();
			qDebug() << __FUNCTION__ << "activity.isValid()=" << activity.isValid();

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

			/*if ( receiverRequestCode == PICK_FROM_FILE_KITKAT )
			{
				jint Intent__FLAG_GRANT_READ_URI_PERMISSION = QAndroidJniObject::getStaticField<jint>(
							"android.content.Intent", "FLAG_GRANT_READ_URI_PERMISSION");
				jint Intent__FLAG_GRANT_WRITE_URI_PERMISSION = QAndroidJniObject::getStaticField<jint>(
							"android.content.Intent", "FLAG_GRANT_WRITE_URI_PERMISSION");
				jint takeFlags = data.callMethod<jint>("getFlags","()I");
				takeFlags = takeFlags & ( Intent__FLAG_GRANT_READ_URI_PERMISSION | Intent__FLAG_GRANT_WRITE_URI_PERMISSION );

				contentResolver.callMethod<void>("takePersistableUriPermission","(Landroid/net/Uri;I)V",
																  uri.object<jobject>(), takeFlags);
			}*/
	/*
	String [] proj={MediaStore.Images.Media.DATA};
		Cursor cursor = getContentResolver().query(uri, proj,  null, null, null);
		int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		String path = cursor.getString(column_index);
		cursor.close();
	 */
			QAndroidJniObject MediaStore_Images_Media_DATA
					= QAndroidJniObject::getStaticObjectField(
						"android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
						//"android/provider/MediaStore$Images$Media", "DATA", "Ljava/lang/String;");
			qDebug() << "MediaStore_Images_Media_DATA.isValid()=" << MediaStore_Images_Media_DATA.isValid();

			QAndroidJniObject nullObj;
			jstring emptyJString = env->NewStringUTF("");
			jobjectArray projection = (jobjectArray)env->NewObjectArray(
				1,
				env->FindClass("java/lang/String"),
				emptyJString
			);
			jobject projection0 = env->NewStringUTF( MediaStore_Images_Media_DATA.toString().toStdString().c_str() );
			env->SetObjectArrayElement(
				projection, 0, projection0 );

			//	Cursor cursor = getContentResolver().query(uri, proj,  null, null, null);
			QAndroidJniObject cursor = contentResolver.callObjectMethod("query",
				"(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;",
				uri.object<jobject>(), projection, nullObj.object<jstring>(), nullObj.object<jobjectArray>(), nullObj.object<jstring>());
			qDebug() << __FUNCTION__ << "cursor.isValid()=" << cursor.isValid();

			//int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			jint column_index = cursor.callMethod<jint>(
						"getColumnIndexOrThrow","(Ljava/lang/String;)I", MediaStore_Images_Media_DATA.object<jstring>());
			qDebug() << "column_index=" << column_index;

			//cursor.moveToFirst();
			cursor.callMethod<jboolean>("moveToFirst");

			//	String path = cursor.getString(column_index);
			QAndroidJniObject path = cursor.callObjectMethod(
						"getString",
						"(I)Ljava/lang/String;", column_index );
			qDebug() << __FUNCTION__ << "path.isValid()=" << path.isValid();
			QString filePath = path.toString();
			qDebug() << "filePath=" << filePath;
			//cursor.close();
			cursor.callMethod<jboolean>("close");


			env->DeleteLocalRef(emptyJString);
			env->DeleteLocalRef(projection);
			env->DeleteLocalRef(projection0);

			QStringList paths;
			paths.append(filePath);
			emit this->accepted(paths);
			//m_imagePath = filePath;
			//emit this->imagePathChanged();
		}
	}
}