コード例 #1
0
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;
}
コード例 #2
0
ファイル: Wpp.cpp プロジェクト: AndrewDesign/wpp-qt
void Wpp::test()
{
#ifdef Q_OS_ANDROID
	/*myUsbManager = (UsbManager) getSystemService(USB_SERVICE);
			HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList();

			if (!deviceList.isEmpty())
			{
				for (UsbDevice device : deviceList.values())
				{
					// 枚举设备
					if (device.getVendorId() == VendorID && device.getProductId() == ProductID)
					{
						myUsbDevice = device;
					}
				}
			}
	*/
	int VendorID = 0;
	int ProductID = 0;
	QAndroidJniObject myUsbDevice;

	QAndroidJniObject activity = QtAndroid::androidActivity();
	qDebug() << __FUNCTION__ << "activity.isValid()=" << activity.isValid();

	QAndroidJniObject Context__USB_SERVICE = QAndroidJniObject::getStaticObjectField(
				"android/content/Context", "USB_SERVICE", "Ljava/lang/String;");
	qDebug() << __FUNCTION__ << "Context__USB_SERVICE.isValid()=" << Context__USB_SERVICE.isValid();

	//myUsbManager = (UsbManager) getSystemService(USB_SERVICE);
	QAndroidJniObject myUsbManager = activity.callObjectMethod(
				"getSystemService","(Ljava/lang/String;)Ljava/lang/Object;",
				Context__USB_SERVICE.object<jobject>() );
	qDebug() << __FUNCTION__ << "myUsbManager.isValid()=" << myUsbManager.isValid();

	//HashMap<String, UsbDevice> deviceList = myUsbManager.getDeviceList();
	QAndroidJniObject deviceList = myUsbManager.callObjectMethod(
				"getDeviceList","()Ljava/util/HashMap;");
	qDebug() << __FUNCTION__ << "deviceList.isValid()=" << deviceList.isValid();


	jboolean isEmpty = true;
	isEmpty = deviceList.callMethod<jboolean>("isEmpty");
	if ( !isEmpty )
	{
		//HashMap - Collection<V> 	values()
		QAndroidJniObject values = deviceList.callObjectMethod(
					"values","()Ljava/util/Collection;");
		qDebug() << __FUNCTION__ << "values.isValid()=" << values.isValid();

		//Collection - abstract Object[] 	toArray()
		QAndroidJniObject valuesArrayObj = values.callObjectMethod(
					"toArray","()[Ljava/lang/Object;");
		qDebug() << __FUNCTION__ << "valuesArrayObj.isValid()=" << valuesArrayObj.isValid();

		QAndroidJniEnvironment env;

		jobjectArray valuesArray = valuesArrayObj.object<jobjectArray>();
		const int valuesCount = env->GetArrayLength(valuesArray);
		qDebug() << __FUNCTION__ << ":valuesArray=" << (void*)valuesArray;
		qDebug() << __FUNCTION__ << ":valuesCount=" << valuesCount;

		for ( int i = 0 ; i < valuesCount ; i++ )
		{
			jobject value = env->GetObjectArrayElement(valuesArray, i);

			QAndroidJniObject device(value);

			//device.getVendorId()
			jint vendorId = deviceList.callMethod<jint>("getVendorId","()I");
			qDebug() << "i=" << i << ":vendorId=" << vendorId;
			//device.getProductId()
			jint productId = deviceList.callMethod<jint>("getProductId","()I");
			qDebug() << "i=" << i << ":productId=" << productId;
			if ( productId == ProductID && vendorId == VendorID )
			{
				myUsbDevice = device;
			}
		}

	}

#endif
}
コード例 #3
0
ファイル: ImagePicker.cpp プロジェクト: AndrewDesign/wpp-qt
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();
		}
	}
}