Add tool for manage consent form

This commit is contained in:
FalsinSoft 2020-10-20 23:10:41 +02:00
parent 10e553aca9
commit cb38e39286
10 changed files with 422 additions and 1 deletions

View File

@ -0,0 +1,86 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "QAndroidUserMessagingPlatform.h"
QAndroidUserMessagingPlatform *QAndroidUserMessagingPlatform::m_pInstance = nullptr;
QAndroidUserMessagingPlatform::QAndroidUserMessagingPlatform() : m_javaUserMessagingPlatform("com/falsinsoft/qtandroidtools/AndroidUserMessagingPlatform",
"(Landroid/app/Activity;)V",
QtAndroid::androidActivity().object<jobject>())
{
m_pInstance = this;
if(m_javaUserMessagingPlatform.isValid())
{
const JNINativeMethod jniMethod[] = {
{"consentFormShowResult", "(I)V", reinterpret_cast<void *>(&QAndroidUserMessagingPlatform::deviceConsentFormShowResult)},
};
QAndroidJniEnvironment jniEnv;
jclass objectClass;
objectClass = jniEnv->GetObjectClass(m_javaUserMessagingPlatform.object<jobject>());
jniEnv->RegisterNatives(objectClass, jniMethod, sizeof(jniMethod)/sizeof(JNINativeMethod));
jniEnv->DeleteLocalRef(objectClass);
}
}
QObject* QAndroidUserMessagingPlatform::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
return new QAndroidUserMessagingPlatform();
}
QAndroidUserMessagingPlatform* QAndroidUserMessagingPlatform::instance()
{
return m_pInstance;
}
void QAndroidUserMessagingPlatform::deviceConsentFormShowResult(JNIEnv *env, jobject thiz, int eventId)
{
Q_UNUSED(env)
Q_UNUSED(thiz)
if(m_pInstance != nullptr)
{
Q_EMIT m_pInstance->consentFormShowResult(eventId);
}
}
void QAndroidUserMessagingPlatform::showConsentFormIfRequired()
{
if(m_javaUserMessagingPlatform.isValid())
{
m_javaUserMessagingPlatform.callMethod<void>("showConsentFormIfRequired");
}
}
void QAndroidUserMessagingPlatform::resetConsentInformation()
{
if(m_javaUserMessagingPlatform.isValid())
{
m_javaUserMessagingPlatform.callMethod<void>("resetConsentInformation");
}
}

View File

@ -0,0 +1,64 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <QtAndroidExtras>
#include <QQmlEngine>
class QAndroidUserMessagingPlatform : public QObject
{
Q_DISABLE_COPY(QAndroidUserMessagingPlatform)
Q_ENUMS(CONSENT_STATUS)
Q_OBJECT
QAndroidUserMessagingPlatform();
public:
enum CONSENT_STATUS
{
CONSENT_FORM_INFO_UPDATE_FAILURE = 0,
CONSENT_FORM_NOT_AVAILABLE = 1,
CONSENT_FORM_STATUS_UNKNOWN = 2,
CONSENT_FORM_STATUS_REQUIRED = 3,
CONSENT_FORM_STATUS_NOT_REQUIRED = 4,
CONSENT_FORM_STATUS_OBTAINED = 5,
CONSENT_FORM_LOAD_FAILURE = 6,
CONSENT_FORM_DISMISSED = 7
};
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
static QAndroidUserMessagingPlatform* instance();
Q_INVOKABLE void showConsentFormIfRequired();
Q_INVOKABLE void resetConsentInformation();
Q_SIGNALS:
void consentFormShowResult(int eventId);
private:
const QAndroidJniObject m_javaUserMessagingPlatform;
static QAndroidUserMessagingPlatform *m_pInstance;
static void deviceConsentFormShowResult(JNIEnv *env, jobject thiz, int eventId);
};

View File

@ -70,6 +70,9 @@
#ifdef QTAT_SHARING
#include "QAndroidSharing.h"
#endif
#ifdef QTAT_USER_MESSAGING_PLATFORM
#include "QAndroidUserMessagingPlatform.h"
#endif
#include "QtAndroidTools.h"
QtAndroidTools *QtAndroidTools::m_pInstance = nullptr;
@ -221,4 +224,7 @@ void QtAndroidTools::initializeQmlTools()
#ifdef QTAT_SHARING
qmlRegisterSingletonType<QAndroidSharing>("QtAndroidTools", 1, 0, "QtAndroidSharing", &QAndroidSharing::qmlInstance);
#endif
#ifdef QTAT_USER_MESSAGING_PLATFORM
qmlRegisterSingletonType<QAndroidUserMessagingPlatform>("QtAndroidTools", 1, 0, "QtAndroidUserMessagingPlatform", &QAndroidUserMessagingPlatform::qmlInstance);
#endif
}

View File

@ -209,3 +209,13 @@ contains(DEFINES, QTAT_SHARING) {
QMAKE_EXTRA_TARGETS += copy_sharing
}
}
contains(DEFINES, QTAT_USER_MESSAGING_PLATFORM) {
HEADERS += $$PWD/QAndroidUserMessagingPlatform.h
SOURCES += $$PWD/QAndroidUserMessagingPlatform.cpp
OTHER_FILES += $$PWD/src/com/falsinsoft/qtandroidtools/AndroidUserMessagingPlatform.java
equals(COPY_JAVA_FILE, true) {
copy_user_messaging_platform.commands = $(COPY_FILE) $$shell_path($$PWD/src/com/falsinsoft/qtandroidtools/AndroidUserMessagingPlatform.java) $$shell_path($$ANDROID_PACKAGE_SOURCE_DIR/src/com/falsinsoft/qtandroidtools/)
PRE_TARGETDEPS += copy_user_messaging_platform
QMAKE_EXTRA_TARGETS += copy_user_messaging_platform
}
}

View File

@ -0,0 +1,182 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.falsinsoft.qtandroidtools;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;
public class AndroidUserMessagingPlatform
{
private final ConsentInformation mConsentInformation;
private final ConsentListener mConsentListener;
private final Activity mActivityInstance;
private ConsentForm mConsentForm = null;
public AndroidUserMessagingPlatform(Activity activityInstance)
{
mConsentInformation = UserMessagingPlatform.getConsentInformation(activityInstance);
mConsentListener = new ConsentListener();
mActivityInstance = activityInstance;
}
public void showConsentFormIfRequired()
{
requestConsentInfoUpdate();
}
public void resetConsentInformation()
{
mConsentInformation.reset();
}
private void requestConsentInfoUpdate()
{
final ConsentRequestParameters params = new ConsentRequestParameters.Builder().build();
mConsentInformation.requestConsentInfoUpdate(mActivityInstance, params, mConsentListener, mConsentListener);
}
private void loadConsentForm()
{
if(mConsentInformation.isConsentFormAvailable())
{
mActivityInstance.runOnUiThread(new Runnable()
{
@Override
public void run()
{
UserMessagingPlatform.loadConsentForm(mActivityInstance, mConsentListener, mConsentListener);
}
});
}
else
{
consentFormShowResult(CONSENT_FORM_NOT_AVAILABLE);
}
}
private void showConsentForm()
{
if(mConsentForm != null)
{
mActivityInstance.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mConsentForm.show(mActivityInstance, mConsentListener);
}
});
}
else
{
consentFormShowResult(CONSENT_FORM_NOT_AVAILABLE);
}
}
private class ConsentListener implements ConsentInformation.OnConsentInfoUpdateSuccessListener,
ConsentInformation.OnConsentInfoUpdateFailureListener,
UserMessagingPlatform.OnConsentFormLoadSuccessListener,
UserMessagingPlatform.OnConsentFormLoadFailureListener,
ConsentForm.OnConsentFormDismissedListener
{
@Override
public void onConsentInfoUpdateSuccess()
{
if(mConsentInformation.isConsentFormAvailable())
{
loadConsentForm();
}
else
{
consentFormShowResult(CONSENT_FORM_NOT_AVAILABLE);
}
}
@Override
public void onConsentInfoUpdateFailure(FormError formError)
{
consentFormShowResult(CONSENT_FORM_INFO_UPDATE_FAILURE);
}
@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm)
{
final int consentStatus = mConsentInformation.getConsentStatus();
mConsentForm = consentForm;
if(consentStatus == ConsentInformation.ConsentStatus.REQUIRED)
{
showConsentForm();
}
else
{
int eventId = CONSENT_FORM_STATUS_UNKNOWN;
switch(consentStatus)
{
case ConsentInformation.ConsentStatus.NOT_REQUIRED:
eventId = CONSENT_FORM_STATUS_NOT_REQUIRED;
break;
case ConsentInformation.ConsentStatus.OBTAINED:
eventId = CONSENT_FORM_STATUS_OBTAINED;
break;
}
consentFormShowResult(eventId);
}
}
@Override
public void onConsentFormLoadFailure(FormError formError)
{
consentFormShowResult(CONSENT_FORM_LOAD_FAILURE);
}
@Override
public void onConsentFormDismissed(@Nullable FormError formError)
{
consentFormShowResult(CONSENT_FORM_DISMISSED);
}
}
private static final int CONSENT_FORM_INFO_UPDATE_FAILURE = 0;
private static final int CONSENT_FORM_NOT_AVAILABLE = 1;
private static final int CONSENT_FORM_STATUS_UNKNOWN = 2;
private static final int CONSENT_FORM_STATUS_REQUIRED = 3;
private static final int CONSENT_FORM_STATUS_NOT_REQUIRED = 4;
private static final int CONSENT_FORM_STATUS_OBTAINED = 5;
private static final int CONSENT_FORM_LOAD_FAILURE = 6;
private static final int CONSENT_FORM_DISMISSED = 7;
private static native void consentFormShowResult(int eventId);
}

View File

@ -106,6 +106,7 @@ ApplicationWindow {
ListElement { title: "GoogleAccount"; source: "qrc:/tools/AndroidGoogleAccount.qml" }
ListElement { title: "GoogleDrive"; source: "qrc:/tools/AndroidGoogleDrive.qml" }
ListElement { title: "Sharing"; source: "qrc:/tools/AndroidSharing.qml" }
ListElement { title: "UserMessagingPlatform"; source: "qrc:/tools/AndroidUserMessagingPlatform.qml" }
ListElement { title: "System"; source: "qrc:/tools/AndroidSystem.qml" }
}

View File

@ -38,7 +38,8 @@ DEFINES += \
QTAT_PLAY_STORE \
QTAT_GOOGLE_ACCOUNT \
QTAT_GOOGLE_DRIVE \
QTAT_SHARING
QTAT_SHARING \
QTAT_USER_MESSAGING_PLATFORM
include(../QtAndroidTools/QtAndroidTools.pri)
}

View File

@ -18,5 +18,6 @@
<file>tools/AndroidGoogleDrive.qml</file>
<file>tools/AndroidSystem.qml</file>
<file>tools/AndroidSharing.qml</file>
<file>tools/AndroidUserMessagingPlatform.qml</file>
</qresource>
</RCC>

View File

@ -27,6 +27,9 @@ dependencies {
implementation ('com.google.apis:google-api-services-drive:v3-rev173-1.25.0') {
exclude group: 'org.apache.httpcomponents'
}
implementation 'com.google.android.ump:user-messaging-platform:1.0.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.google.guava:guava:28.2-android'
}
android {

View File

@ -0,0 +1,67 @@
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Dialogs 1.3
import QtAndroidTools 1.0
Page {
id: page
padding: 20
Connections {
target: QtAndroidUserMessagingPlatform
function onConsentFormShowResult(eventId)
{
switch(eventId)
{
case QtAndroidUserMessagingPlatform.CONSENT_FORM_INFO_UPDATE_FAILURE:
consentFormShowResult.text = "CONSENT_FORM_INFO_UPDATE_FAILURE";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_NOT_AVAILABLE:
consentFormShowResult.text = "CONSENT_FORM_NOT_AVAILABLE";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_UNKNOWN:
consentFormShowResult.text = "CONSENT_FORM_STATUS_UNKNOWN";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_REQUIRED:
consentFormShowResult.text = "CONSENT_FORM_STATUS_REQUIRED";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_NOT_REQUIRED:
consentFormShowResult.text = "CONSENT_FORM_STATUS_NOT_REQUIRED";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_OBTAINED:
consentFormShowResult.text = "CONSENT_FORM_STATUS_OBTAINED";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_LOAD_FAILURE:
consentFormShowResult.text = "CONSENT_FORM_LOAD_FAILURE";
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_DISMISSED:
consentFormShowResult.text = "CONSENT_FORM_DISMISSED";
break;
}
}
}
Column {
anchors.fill: parent
spacing: 5
Label {
id: consentFormShowResult
width: parent.width
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 15
text: "-----"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "showConsentFormIfRequired"
onClicked: QtAndroidUserMessagingPlatform.showConsentFormIfRequired()
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "resetConsentInformation"
onClicked: QtAndroidUserMessagingPlatform.resetConsentInformation()
}
}
}