Skip to content

Commit

Permalink
Added unofficial code to check if user accapted to view ads on UMP form
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsinSoft committed Dec 19, 2023
1 parent 8877149 commit 0feff91
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,9 @@ <h3>UserMessagingPlatform</h3>
<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>
<p><b>PLEASE NOTE:</b> the problem is that if the user does not accept consent, the Ads is not displayed. Or if the user accepts only some parts the Ads must be set as non-personalised. Unfortunately there is no "official" way to understand what the user has selected. I found these two functions that allow you to understand whether the user has authorized complete Ads or only non-personalized Ads or nothing at all. However, as it is not an official code, it is not guaranteed that it will work correctly in the future so <b>keep this in mind when using them</b>. If the first funtion return false you should prompt the user or to accept all, or explain in details what to check to display at least Non-Personalized Ads, or ask the user to opt for a premium version of the app, otherwise you will earn absolutely nothing. If true you can check, using the second function, if user granted at least minimum requirements to show Personalized Ads.</p>
<pre class="prettyprint">QtAndroidSharing.consentCanShowAds()
QtAndroidSharing.consentCanShowPersonalizedAds()</pre>
</div>
<div class="section-txt" id="Audio">
<h3>Audio</h3>
Expand Down
19 changes: 19 additions & 0 deletions QtAndroidTools/QAndroidUserMessagingPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,22 @@ void QAndroidUserMessagingPlatform::resetConsentInformation()
m_javaUserMessagingPlatform.callMethod<void>("resetConsentInformation");
}
}


bool QAndroidUserMessagingPlatform::consentCanShowAds()
{
if(m_javaUserMessagingPlatform.isValid())
{
return m_javaUserMessagingPlatform.callMethod<jboolean>("consentCanShowAds");
}
return false;
}

bool QAndroidUserMessagingPlatform::consentCanShowPersonalizedAds()
{
if(m_javaUserMessagingPlatform.isValid())
{
return m_javaUserMessagingPlatform.callMethod<jboolean>("consentCanShowPersonalizedAds");
}
return false;
}
3 changes: 3 additions & 0 deletions QtAndroidTools/QAndroidUserMessagingPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class QAndroidUserMessagingPlatform : public QObject
Q_INVOKABLE bool canRequestAds();
Q_INVOKABLE void resetConsentInformation();

Q_INVOKABLE bool consentCanShowAds();
Q_INVOKABLE bool consentCanShowPersonalizedAds();

Q_SIGNALS:
void consentFormRequestFailure(const QString &errorMessage);
void consentFormDismissed(bool consentGathered, bool privacyOptionsRequired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import androidx.annotation.Nullable;
import com.google.android.ump.ConsentForm;
import com.google.android.ump.ConsentInformation;
Expand All @@ -36,6 +38,9 @@
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;

import java.util.ArrayList;
import java.util.List;

public class AndroidUserMessagingPlatform
{
private final ConsentInformation mConsentInformation;
Expand Down Expand Up @@ -115,4 +120,105 @@ public void onConsentFormDismissed(@Nullable FormError formError)

private static native void consentFormRequestFailure(String errorMessage);
private static native void consentFormDismissed(boolean consentGathered, boolean privacyOptionsRequired);

public boolean consentCanShowAds()
{
return new ConsentUMP().canShowAds(mActivityInstance);
}

public boolean consentCanShowPersonalizedAds()
{
return new ConsentUMP().canShowPersonalizedAds(mActivityInstance);
}

private class ConsentUMP
{
// Code from
// https://itnext.io/android-admob-consent-with-ump-personalized-or-non-personalized-ads-in-eea-3592e192ec90

public boolean canShowAds(Context context)
{
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String purposeConsent = prefs.getString("IABTCF_PurposeConsents", "");
String vendorConsent = prefs.getString("IABTCF_VendorConsents","");
String vendorLI = prefs.getString("IABTCF_VendorLegitimateInterests","");
String purposeLI = prefs.getString("IABTCF_PurposeLegitimateInterests","");
final int googleId = 755;
boolean hasGoogleVendorConsent = hasAttribute(vendorConsent, googleId);
boolean hasGoogleVendorLI = hasAttribute(vendorLI, googleId);

List<Integer> indexes = new ArrayList<>();
indexes.add(1);

List<Integer> indexesLI = new ArrayList<>();
indexesLI.add(2);
indexesLI.add(7);
indexesLI.add(9);
indexesLI.add(10);

return hasConsentFor(indexes, purposeConsent, hasGoogleVendorConsent)
&& hasConsentOrLegitimateInterestFor(indexesLI, purposeConsent, purposeLI, hasGoogleVendorConsent, hasGoogleVendorLI);

}

public boolean canShowPersonalizedAds(Context context)
{
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String purposeConsent = prefs.getString("IABTCF_PurposeConsents", "");
String vendorConsent = prefs.getString("IABTCF_VendorConsents","");
String vendorLI = prefs.getString("IABTCF_VendorLegitimateInterests","");
String purposeLI = prefs.getString("IABTCF_PurposeLegitimateInterests","");
final int googleId = 755;
boolean hasGoogleVendorConsent = hasAttribute(vendorConsent, googleId);
boolean hasGoogleVendorLI = hasAttribute(vendorLI, googleId);

List<Integer> indexes = new ArrayList<>();
indexes.add(1);
indexes.add(3);
indexes.add(4);

List<Integer> indexesLI = new ArrayList<>();
indexesLI.add(2);
indexesLI.add(7);
indexesLI.add(9);
indexesLI.add(10);

return hasConsentFor(indexes, purposeConsent, hasGoogleVendorConsent)
&& hasConsentOrLegitimateInterestFor(indexesLI, purposeConsent, purposeLI, hasGoogleVendorConsent, hasGoogleVendorLI);

}

private boolean hasAttribute(String input, int index)
{
if(input == null) return false;
return input.length() >= index && input.charAt(index-1) == '1';
}

private boolean hasConsentFor(List<Integer> indexes, String purposeConsent, boolean hasVendorConsent)
{
for(Integer p: indexes)
{
if(!hasAttribute(purposeConsent, p))
{
return false;
}
}
return hasVendorConsent;
}

private boolean hasConsentOrLegitimateInterestFor(List<Integer> indexes, String purposeConsent, String purposeLI, boolean hasVendorConsent, boolean hasVendorLI)
{
for(Integer p: indexes)
{
boolean purposeAndVendorLI = hasAttribute(purposeLI, p) && hasVendorLI;
boolean purposeConsentAndVendorConsent = hasAttribute(purposeConsent, p) && hasVendorConsent;
boolean isOk = purposeAndVendorLI || purposeConsentAndVendorConsent;
if(!isOk)
{
return false;
}
}
return true;
}
}
}
2 changes: 0 additions & 2 deletions QtAndroidToolsDemo/android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
<category android:name="android.intent.category.OPENABLE"/>
<data android:mimeType="image/*"/>
</intent-filter>
<meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/splashscreen"/>
<meta-data android:name="android.app.splash_screen_sticky" android:value="true"/>
</activity>

<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.qtprovider" android:exported="false" android:grantUriPermissions="true">
Expand Down
4 changes: 3 additions & 1 deletion QtAndroidToolsDemo/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ dependencies {
implementation 'com.google.android.gms:play-services-ads:21.+'
implementation 'com.google.android.gms:play-services-auth:20.+'
implementation 'com.google.android.ump:user-messaging-platform:2.1.0'
implementation(platform('org.jetbrains.kotlin:kotlin-bom:1.8.0'))
implementation 'androidx.preference:preference:1.2.1'
implementation 'androidx.annotation:annotation:1.7.0'
implementation 'androidx.core:core:1.10.1'
implementation 'androidx.core:core:1.10.1'
implementation 'com.google.guava:guava:24.1-jre'
implementation 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
Expand Down
18 changes: 15 additions & 3 deletions QtAndroidToolsDemo/tools/AndroidUserMessagingPlatform.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,28 @@ Page {
onClicked: QtAndroidUserMessagingPlatform.resetConsentInformation()
}
Label {
id: canRequestAds
id: consentCanShowAds
width: parent.width
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 15
text: "-----"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "canRequestAds"
onClicked: canRequestAds.text = QtAndroidUserMessagingPlatform.canRequestAds() ? "Yes" : "No"
text: "consentCanShowAds"
onClicked: consentCanShowAds.text = QtAndroidUserMessagingPlatform.consentCanShowAds() ? "Yes" : "No"
}
Label {
id: consentCanShowPersonalizedAds
width: parent.width
horizontalAlignment: Text.AlignHCenter
font.pixelSize: 15
text: "-----"
}
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "consentCanShowPersonalizedAds"
onClicked: consentCanShowPersonalizedAds.text = QtAndroidUserMessagingPlatform.consentCanShowPersonalizedAds() ? "Yes" : "No"
}
}
}

0 comments on commit 0feff91

Please sign in to comment.