Esempio n. 1
0
void ScreenValues::updateScreenValues()
{
#ifdef Q_OS_ANDROID
    if (m_system.sdkInt() >= 19) {
        QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
                                                                               "activity",
                                                                               "()Landroid/app/Activity;");

        QAndroidJniObject wm = activity.callObjectMethod("getWindowManager", "()Landroid/view/WindowManager;");
        QAndroidJniObject display = wm.callObjectMethod("getDefaultDisplay", "()Landroid/view/Display;");

        QAndroidJniObject realSize = QAndroidJniObject("android/graphics/Point");
        display.callMethod<void>("getRealSize", "(Landroid/graphics/Point;)V", realSize.object());

        QAndroidJniObject displayFrame = QAndroidJniObject("android/graphics/Rect");
        QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
        QAndroidJniObject view = window.callObjectMethod("getDecorView", "()Landroid/view/View;");
        QAndroidJniObject rootView = view.callObjectMethod("getRootView", "()Landroid/view/View;");
        rootView.callMethod<void>("getWindowVisibleDisplayFrame", "(Landroid/graphics/Rect;)V", displayFrame.object());

        if (((int) realSize.getField<jint>("y") - (int) displayFrame.callMethod<jint>("height")) - m_statusBarHeight == 0)
            setNavBarVisible(false);
        else
            setNavBarVisible(true);
    }
#endif
}
void tst_QAndroidJniObject::compareOperatorTests()
{
    QString str("hello!");
    QAndroidJniObject stringObject = QAndroidJniObject::fromString(str);

    jobject obj = stringObject.object();
    jobject jobj = stringObject.object<jobject>();
    jstring jsobj = stringObject.object<jstring>();

    QVERIFY(obj == stringObject);
    QVERIFY(jobj == stringObject);
    QVERIFY(stringObject == jobj);
    QVERIFY(jsobj == stringObject);
    QVERIFY(stringObject == jsobj);

    QAndroidJniObject stringObject3 = stringObject.object<jstring>();
    QVERIFY(stringObject3 == stringObject);

    QAndroidJniObject stringObject2 = QAndroidJniObject::fromString(str);
    QVERIFY(stringObject != stringObject2);

    jstring jstrobj = 0;
    QAndroidJniObject invalidStringObject;
    QVERIFY(invalidStringObject == jstrobj);

    QVERIFY(jstrobj != stringObject);
    QVERIFY(stringObject != jstrobj);
    QVERIFY(!invalidStringObject.isValid());
}
void QQmlAndroidScrollView::onInflate(QAndroidJniObject &instance)
{
    QQmlAndroidFrameLayout::onInflate(instance);

    static bool nativeMethodsRegistered = false;
    if (!nativeMethodsRegistered) {
        onRegisterNativeMethods(instance.object());
        nativeMethodsRegistered = true;
    }
}
void QQmlAndroidImageView::setImageURI(const QUrl &uri)
{
    if (m_uri != uri) {
        m_uri = uri;
        if (isValid()) {
            QAndroidJniObject v = instance();
            QAndroidJniObject u = getUri();
            QtQmlAndroid::callFunction([=]() {
                v.callMethod<void>("setImageURI", "(Landroid/net/Uri;)V", u.object());
            });
        }
        emit imageURIChanged();
    }
}
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;
}
QAndroidJniObject QQmlAndroidImageView::getUri() const
{
    QAndroidJniObject str = QAndroidJniObject::fromString(m_uri.toString());
    return QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", str.object());
}
void QQmlAndroidImageView::onInflate(QAndroidJniObject &instance)
{
    QQmlAndroidView::onInflate(instance);

    if (m_uri.isValid())
        instance.callMethod<void>("setImageURI", "(Landroid/net/Uri;)V", getUri().object());
    if (m_resource > 0)
        instance.callMethod<void>("setImageResource", "(I)V", m_resource);
    if (!m_tint.isNull()) {
        QAndroidJniObject tint = QAndroidJniObject::callStaticObjectMethod("android/content/res/ColorStateList",
                                                                           "valueOf",
                                                                           "(I)Landroid/content/res/ColorStateList;",
                                                                           m_tint);
        instance.callMethod<void>("setImageTintList", "(Landroid/content/res/ColorStateList;)v", tint.object());
    }
}
void QQmlAndroidImageView::setImageTintColor(int color)
{
    if (m_tint.isNull() || m_tint != color) {
        m_tint = color;
        if (isValid()) {
            QAndroidJniObject view = instance();
            QtQmlAndroid::callFunction([=]() {
                QAndroidJniObject tint = QAndroidJniObject::callStaticObjectMethod("android/content/res/ColorStateList",
                                                                                   "valueOf",
                                                                                   "(I)Landroid/content/res/ColorStateList;",
                                                                                   color);
                view.callMethod<void>("setImageTintList", "(Landroid/content/res/ColorStateList;)v", tint.object());
            });
        }
        emit imageTintColorChanged();
    }
}
Esempio n. 9
0
void AndroidScreenDevPrivate::handleActivityResult(int requestCode,
                                                   int resultCode,
                                                   const QAndroidJniObject &intent)
{
    if (requestCode != SCREEN_CAPTURE_REQUEST_CODE)
        return;

    QAndroidJniObject mediaProjectionCallback;
    QAndroidJniObject resources;
    QAndroidJniObject metrics;
    QAndroidJniObject surface;
    auto displayName = QAndroidJniObject::fromString("VirtualDisplay");
    jint width;
    jint height;
    jint density;

    if (resultCode != RESULT_OK)
        goto handleActivityResult_fail;

    this->m_mediaProjection =
            this->m_service.callObjectMethod("getMediaProjection",
                                             "(ILandroid/content/Intent;)Landroid/media/projection/MediaProjection;",
                                             resultCode,
                                             intent.object());

    if (!this->m_mediaProjection.isValid())
        goto handleActivityResult_fail;

    mediaProjectionCallback = this->m_callbacks.callObjectMethod("mediaProjectionCallback",
                                                                 "()"
                                                                 JLCLASS("AkAndroidScreenCallbacks$MediaProjectionCallback"));
    this->m_mediaProjection.callMethod<void>("registerCallback",
                                             "(Landroid/media/projection/MediaProjection$Callback;"
                                             "Landroid/os/Handler;)V",
                                             mediaProjectionCallback.object(),
                                             nullptr);
    resources =
            this->m_activity.callObjectMethod("getResources",
                                              "()Landroid/content/res/Resources;");
    metrics = resources.callObjectMethod("getDisplayMetrics",
                                         "()Landroid/util/DisplayMetrics;");
    width = metrics.getField<jint>("widthPixels");
    height = metrics.getField<jint>("heightPixels");
    density = metrics.getField<jint>("densityDpi");

    this->m_imageReader =
            QAndroidJniObject::callStaticObjectMethod("android/media/ImageReader",
                                                      "newInstance",
                                                      "(IIII)Landroid/media/ImageReader;",
                                                      width,
                                                      height,
                                                      ImageFormat::RGBA_8888,
                                                      BUFFER_SIZE);

    if (!this->m_imageReader.isValid())
        goto handleActivityResult_fail;

    surface =
            this->m_imageReader.callObjectMethod("getSurface",
                                                 "()Landroid/view/Surface;");

    if (!surface.isValid())
        goto handleActivityResult_fail;

    this->m_imageReader.callMethod<void>("setOnImageAvailableListener",
                                         "(Landroid/media/ImageReader$OnImageAvailableListener;"
                                         "Landroid/os/Handler;)V",
                                         this->m_callbacks.object(),
                                         nullptr);

    this->m_virtualDisplay =
            this->m_mediaProjection.callObjectMethod("createVirtualDisplay",
                                                     "(Ljava/lang/String;"
                                                     "IIII"
                                                     "Landroid/view/Surface;"
                                                     "Landroid/hardware/display/VirtualDisplay$Callback;"
                                                     "Landroid/os/Handler;)"
                                                     "Landroid/hardware/display/VirtualDisplay;",
                                                     displayName.object(),
                                                     width,
                                                     height,
                                                     density,
                                                     VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
                                                     surface.object(),
                                                     nullptr,
                                                     nullptr);

    if (!this->m_virtualDisplay.isValid())
        goto handleActivityResult_fail;

    this->m_canCapture = true;

handleActivityResult_fail:
    this->m_mutex.lock();
    this->m_captureSetupReady.wakeAll();
    this->m_mutex.unlock();
}
Esempio n. 10
0
	void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject & data) { callbackQAndroidActivityResultReceiver_HandleActivityResult(this, this->objectNameAbs().toUtf8().data(), receiverRequestCode, resultCode, new QAndroidJniObject(data.object())); };
Esempio n. 11
0
/**
 * Get the file descriptor of first available matching device attached to usb in android.
 *
 * returns a fd to the device, or -1 and errno is set.
 */
int get_usb_fd(uint16_t idVendor, uint16_t idProduct)
{
	int i;
	jint fd, vendorid, productid;
	QAndroidJniObject usbName, usbDevice;

	// Get the current main activity of the application.
	QAndroidJniObject activity = QtAndroid::androidActivity();

	QAndroidJniObject usb_service = QAndroidJniObject::fromString(USB_SERVICE);

	// Get UsbManager from activity
	QAndroidJniObject usbManager = activity.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", usb_service.object());

	// Get a HashMap<Name, UsbDevice> of all USB devices attached to Android
	QAndroidJniObject deviceMap = usbManager.callObjectMethod("getDeviceList", "()Ljava/util/HashMap;");
	jint num_devices = deviceMap.callMethod<jint>("size", "()I");
	if (num_devices == 0) {
		// No USB device is attached.
		return -1;
	}

	// Iterate over all the devices and find the first available FTDI device.
	QAndroidJniObject keySet = deviceMap.callObjectMethod("keySet", "()Ljava/util/Set;");
	QAndroidJniObject iterator = keySet.callObjectMethod("iterator", "()Ljava/util/Iterator;");

	for (i = 0; i < num_devices; i++) {
		usbName = iterator.callObjectMethod("next", "()Ljava/lang/Object;");
		usbDevice = deviceMap.callObjectMethod ("get", "(Ljava/lang/Object;)Ljava/lang/Object;", usbName.object());
		vendorid = usbDevice.callMethod<jint>("getVendorId", "()I");
		productid = usbDevice.callMethod<jint>("getProductId", "()I");
		if(vendorid == idVendor && productid == idProduct) // Found the requested device
			break;
	}
	if (i == num_devices) {
		// No device found.
		errno = ENOENT;
		return -1;
	}

	jboolean hasPermission = usbManager.callMethod<jboolean>("hasPermission", "(Landroid/hardware/usb/UsbDevice;)Z", usbDevice.object());
	if (!hasPermission) {
		// You do not have permission to use the usbDevice.
		// Please remove and reinsert the USB device.
		// Could also give an dialogbox asking for permission.
		errno = EPERM;
		return -1;
	}

	// An device is present and we also have permission to use the device.
	// Open the device and get its file descriptor.
	QAndroidJniObject usbDeviceConnection = usbManager.callObjectMethod("openDevice", "(Landroid/hardware/usb/UsbDevice;)Landroid/hardware/usb/UsbDeviceConnection;", usbDevice.object());
	if (usbDeviceConnection.object() == NULL) {
		// Some error occurred while opening the device. Exit.
		errno = EINVAL;
		return -1;
	}

	// Finally get the required file descriptor.
	fd = usbDeviceConnection.callMethod<jint>("getFileDescriptor", "()I");
	if (fd == -1) {
		// The device is not opened. Some error.
		errno = ENODEV;
		return -1;
	}
	return fd;
}