Skip to content

Commit

Permalink
Updated UserMessagingPlatform tool
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsinSoft committed Dec 16, 2023
1 parent 5d88010 commit 8877149
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 204 deletions.
46 changes: 15 additions & 31 deletions Documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -620,40 +620,24 @@ <h3>Sharing</h3>
<h3>UserMessagingPlatform</h3>
<p>Under the Google <a href="https://www.google.com/about/company/user-consent-policy/" target="_blank">EU User Consent Policy</a>, you must make certain disclosures to your users in the European Economic Area (EEA) along with the UK and obtain their consent to use cookies or other local storage, where legally required, and to use personal data (such as AdID) to serve ads. This policy reflects the requirements of the EU ePrivacy Directive and the General Data Protection Regulation (GDPR).</p>
<p>This tool support the use of the UMP SDK but for a clear explanation about how to use this SDK and how to configure the consent form you have to read the official guide <a href="https://developers.google.com/admob/ump/android/quick-start" target="_blank">here</a>. At first you must to request the consent form that can be or not be available for the zone your current user is:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.loadAndShowConsentFormIfRequired(underAgeOfConsent)</pre>
<p>Param <i>underAgeOfConsent</i> is a boolean value to inform if the user is under age.</p>
<p>This function will generate two possibile events. One informing about failure with the error message string and the other informing about the form has been closed by the user. The two params inform, the first, if the form has been processed corrently or some error has occurred and, the second, if is required to show an option allowing the user to change the initial selection by showing again the form.</p>
<pre class="prettyprint">Connections {
target: QtAndroidUserMessagingPlatform
function onConsentFormRequestResult(eventId)
function onConsentFormRequestFailure(errorMessage)
{
switch(eventId)
{
case QtAndroidUserMessagingPlatform.CONSENT_FORM_INFO_UPDATE_FAILURE:
....
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_NOT_AVAILABLE:
....
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_LOAD_SUCCESS:
....
break;
case QtAndroidUserMessagingPlatform.CONSENT_FORM_LOAD_FAILURE:
....
break;
}
}

QtAndroidUserMessagingPlatform.requestConsentForm()</pre>

<p>Once request process finish the signal <i>consentFormRequestResult()</i> is emitted with the results. Any event different from CONSENT_FORM_LOAD_SUCCESS means the form is not available for various reasons. Once obtained the form is necessary to check if it must be showed to the user. You can get such information as follow:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.consentStatus()</pre>
<p>Possible returned values are:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_UNKNOWN
QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_REQUIRED
QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_NOT_REQUIRED
QtAndroidUserMessagingPlatform.CONSENT_FORM_STATUS_OBTAINED</pre>
<p>If the status is different from CONSENT_FORM_STATUS_REQUIRED you don't need to show the form. On the contrary, in case of form required or if the user want to change his preferences you can show the form using the call:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.showConsentForm()</pre>
<p>The event <i>consentFormClosed()</i> will inform you when the user closed the form and your app is ready to go. After the user accepted the form some data will be saved in the device and the form will not be required anymore. However if you want to reset these data for restart from scratch you can use the call:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.resetConsentInformation()</pre>
}
function onConsentFormDismissed(consentGathered, privacyOptionsRequired)
{
}
}</pre>
<p>To show the form again just call the function:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.showPrivacyOptionsForm()</pre>
<p>To know if it will be possible to show ads it should be used this function (but, at least right now, it seems always return true also if the user negate consent to show ads):</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.showPrivacyOptionsForm()</pre>
<p>If you want to reset these data for restart from scratch you can use the call:</p>
<pre class="prettyprint">QtAndroidUserMessagingPlatform.canRequestAds()</pre>
<p>This will clean all saved data.</p>
</div>
<div class="section-txt" id="Audio">
Expand Down
28 changes: 15 additions & 13 deletions QtAndroidTools/QAndroidUserMessagingPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ QAndroidUserMessagingPlatform::QAndroidUserMessagingPlatform(QObject *parent) :
if(m_javaUserMessagingPlatform.isValid())
{
const JNINativeMethod jniMethod[] = {
{"consentFormRequestResult", "(I)V", reinterpret_cast<void*>(&QAndroidUserMessagingPlatform::deviceConsentFormRequestResult)},
{"consentFormClosed", "()V", reinterpret_cast<void*>(&QAndroidUserMessagingPlatform::deviceConsentFormClosed)},
{"consentFormRequestFailure", "(Ljava/lang/String;)V", reinterpret_cast<void*>(&QAndroidUserMessagingPlatform::deviceConsentFormRequestFailure)},
{"consentFormDismissed", "(ZZ)V", reinterpret_cast<void*>(&QAndroidUserMessagingPlatform::deviceConsentFormDismissed)},
};
QJniEnvironment jniEnv;
jclass objectClass;
Expand All @@ -61,50 +61,52 @@ QAndroidUserMessagingPlatform* QAndroidUserMessagingPlatform::instance()
return m_pInstance;
}

void QAndroidUserMessagingPlatform::deviceConsentFormRequestResult(JNIEnv *env, jobject thiz, int eventId)
void QAndroidUserMessagingPlatform::deviceConsentFormRequestFailure(JNIEnv *env, jobject thiz, jstring errorMessage)
{
Q_UNUSED(env)
Q_UNUSED(thiz)

if(m_pInstance != nullptr)
{
Q_EMIT m_pInstance->consentFormRequestResult(eventId);
QJniEnvironment jniEnv;
Q_EMIT m_pInstance->consentFormRequestFailure(QString(jniEnv->GetStringUTFChars(errorMessage, NULL)));
}
}

void QAndroidUserMessagingPlatform::deviceConsentFormClosed(JNIEnv *env, jobject thiz)
void QAndroidUserMessagingPlatform::deviceConsentFormDismissed(JNIEnv *env, jobject thiz, jboolean consentGathered, jboolean privacyOptionsRequired)
{
Q_UNUSED(env)
Q_UNUSED(thiz)

if(m_pInstance != nullptr)
{
Q_EMIT m_pInstance->consentFormClosed();
Q_EMIT m_pInstance->consentFormDismissed(consentGathered, privacyOptionsRequired);
}
}

void QAndroidUserMessagingPlatform::requestConsentForm()
void QAndroidUserMessagingPlatform::loadAndShowConsentFormIfRequired(bool underAgeOfConsent)
{
if(m_javaUserMessagingPlatform.isValid())
{
m_javaUserMessagingPlatform.callMethod<void>("requestConsentForm");
m_javaUserMessagingPlatform.callMethod<void>("loadAndShowConsentFormIfRequired",
"(Z)V",
underAgeOfConsent);
}
}

int QAndroidUserMessagingPlatform::consentStatus()
void QAndroidUserMessagingPlatform::showPrivacyOptionsForm()
{
if(m_javaUserMessagingPlatform.isValid())
{
return m_javaUserMessagingPlatform.callMethod<jint>("consentStatus");
m_javaUserMessagingPlatform.callMethod<void>("showPrivacyOptionsForm");
}
return CONSENT_FORM_STATUS_UNKNOWN;
}

bool QAndroidUserMessagingPlatform::showConsentForm()
bool QAndroidUserMessagingPlatform::canRequestAds()
{
if(m_javaUserMessagingPlatform.isValid())
{
return m_javaUserMessagingPlatform.callMethod<jboolean>("showConsentForm");
return m_javaUserMessagingPlatform.callMethod<jboolean>("canRequestAds");
}
return false;
}
Expand Down
31 changes: 7 additions & 24 deletions QtAndroidTools/QAndroidUserMessagingPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,22 @@ class QAndroidUserMessagingPlatform : public QObject
public:
QAndroidUserMessagingPlatform(QObject *parent);

enum CONSENT_STATUS
{
CONSENT_FORM_STATUS_UNKNOWN = 0,
CONSENT_FORM_STATUS_REQUIRED = 1,
CONSENT_FORM_STATUS_NOT_REQUIRED = 2,
CONSENT_FORM_STATUS_OBTAINED = 3,
};
Q_ENUM(CONSENT_STATUS)
enum REQUEST_RESULT
{
CONSENT_FORM_INFO_UPDATE_FAILURE = 0,
CONSENT_FORM_NOT_AVAILABLE = 1,
CONSENT_FORM_LOAD_SUCCESS = 2,
CONSENT_FORM_LOAD_FAILURE = 3
};
Q_ENUM(REQUEST_RESULT)

static QAndroidUserMessagingPlatform* create(QQmlEngine *engine, QJSEngine *scriptEngine);
static QAndroidUserMessagingPlatform* instance();

Q_INVOKABLE void requestConsentForm();
Q_INVOKABLE int consentStatus();
Q_INVOKABLE bool showConsentForm();
Q_INVOKABLE void loadAndShowConsentFormIfRequired(bool underAgeOfConsent);
Q_INVOKABLE void showPrivacyOptionsForm();
Q_INVOKABLE bool canRequestAds();
Q_INVOKABLE void resetConsentInformation();

Q_SIGNALS:
void consentFormRequestResult(int eventId);
void consentFormClosed();
void consentFormRequestFailure(const QString &errorMessage);
void consentFormDismissed(bool consentGathered, bool privacyOptionsRequired);

private:
const QJniObject m_javaUserMessagingPlatform;
static QAndroidUserMessagingPlatform *m_pInstance;

static void deviceConsentFormRequestResult(JNIEnv *env, jobject thiz, int eventId);
static void deviceConsentFormClosed(JNIEnv *env, jobject thiz);
static void deviceConsentFormRequestFailure(JNIEnv *env, jobject thiz, jstring errorMessage);
static void deviceConsentFormDismissed(JNIEnv *env, jobject thiz, jboolean consentGathered, jboolean privacyOptionsRequired);
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import androidx.annotation.Nullable;
import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentInformation.PrivacyOptionsRequirementStatus;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;
Expand All @@ -40,8 +41,6 @@ public class AndroidUserMessagingPlatform
private final ConsentInformation mConsentInformation;
private final ConsentListener mConsentListener;
private final Activity mActivityInstance;
private int mConsentStatus = ConsentInformation.ConsentStatus.UNKNOWN;
private ConsentForm mConsentForm = null;

public AndroidUserMessagingPlatform(Activity activityInstance)
{
Expand All @@ -50,123 +49,70 @@ public AndroidUserMessagingPlatform(Activity activityInstance)
mActivityInstance = activityInstance;
}

public boolean showConsentForm()
public void resetConsentInformation()
{
if(mConsentForm != null)
{
mActivityInstance.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mConsentForm.show(mActivityInstance, mConsentListener);
}
});

return true;
}

return false;
mConsentInformation.reset();
}

public void resetConsentInformation()
public boolean canRequestAds()
{
mConsentInformation.reset();
return mConsentInformation.canRequestAds();
}

public void requestConsentForm()
public void loadAndShowConsentFormIfRequired(boolean underAgeOfConsent)
{
final ConsentRequestParameters params = new ConsentRequestParameters.Builder().build();
final ConsentRequestParameters params = new ConsentRequestParameters.Builder()
.setTagForUnderAgeOfConsent(underAgeOfConsent)
.build();

mConsentInformation.requestConsentInfoUpdate(mActivityInstance, params, mConsentListener, mConsentListener);
}

public int consentStatus()
public void showPrivacyOptionsForm()
{
int status = CONSENT_FORM_STATUS_UNKNOWN;

switch(mConsentStatus)
mActivityInstance.runOnUiThread(new Runnable()
{
case ConsentInformation.ConsentStatus.REQUIRED:
status = CONSENT_FORM_STATUS_REQUIRED;
break;
case ConsentInformation.ConsentStatus.NOT_REQUIRED:
status = CONSENT_FORM_STATUS_NOT_REQUIRED;
break;
case ConsentInformation.ConsentStatus.OBTAINED:
status = CONSENT_FORM_STATUS_OBTAINED;
break;
case ConsentInformation.ConsentStatus.UNKNOWN:
status = CONSENT_FORM_STATUS_UNKNOWN;
break;
}

return status;
@Override
public void run()
{
UserMessagingPlatform.showPrivacyOptionsForm(mActivityInstance, mConsentListener);
}
});
}

private class ConsentListener implements ConsentInformation.OnConsentInfoUpdateSuccessListener,
ConsentInformation.OnConsentInfoUpdateFailureListener,
UserMessagingPlatform.OnConsentFormLoadSuccessListener,
UserMessagingPlatform.OnConsentFormLoadFailureListener,
ConsentForm.OnConsentFormDismissedListener
{
@Override
public void onConsentInfoUpdateSuccess()
{
if(mConsentInformation.isConsentFormAvailable())
mActivityInstance.runOnUiThread(new Runnable()
{
mActivityInstance.runOnUiThread(new Runnable()
@Override
public void run()
{
@Override
public void run()
{
UserMessagingPlatform.loadConsentForm(mActivityInstance, mConsentListener, mConsentListener);
}
});
}
else
{
consentFormRequestResult(CONSENT_FORM_NOT_AVAILABLE);
}
UserMessagingPlatform.loadAndShowConsentFormIfRequired(mActivityInstance, mConsentListener);
}
});
}

@Override
public void onConsentInfoUpdateFailure(FormError formError)
{
consentFormRequestResult(CONSENT_FORM_INFO_UPDATE_FAILURE);
}

@Override
public void onConsentFormLoadSuccess(ConsentForm consentForm)
{
mConsentStatus = mConsentInformation.getConsentStatus();
mConsentForm = consentForm;

consentFormRequestResult(CONSENT_FORM_LOAD_SUCCESS);
}

@Override
public void onConsentFormLoadFailure(FormError formError)
{
consentFormRequestResult(CONSENT_FORM_LOAD_FAILURE);
consentFormRequestFailure(formError.getMessage());
}

@Override
public void onConsentFormDismissed(@Nullable FormError formError)
{
consentFormClosed();
final boolean privacyOptionsRequired = (mConsentInformation.getPrivacyOptionsRequirementStatus() == PrivacyOptionsRequirementStatus.REQUIRED) ? true : false;
final boolean consentGathered = (formError == null) ? true : false;

consentFormDismissed(consentGathered, privacyOptionsRequired);
}
}

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_LOAD_SUCCESS = 2;
private static final int CONSENT_FORM_LOAD_FAILURE = 3;

private static final int CONSENT_FORM_STATUS_UNKNOWN = 0;
private static final int CONSENT_FORM_STATUS_REQUIRED = 1;
private static final int CONSENT_FORM_STATUS_NOT_REQUIRED = 2;
private static final int CONSENT_FORM_STATUS_OBTAINED = 3;

private static native void consentFormRequestResult(int eventId);
private static native void consentFormClosed();
private static native void consentFormRequestFailure(String errorMessage);
private static native void consentFormDismissed(boolean consentGathered, boolean privacyOptionsRequired);
}
Loading

0 comments on commit 8877149

Please sign in to comment.