Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions app/src/main/java/app/opendocument/droid/nonfree/AdManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.FormError
import com.google.android.ump.UserMessagingPlatform
Expand Down Expand Up @@ -95,35 +96,46 @@ class AdManager {
{
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity) { loadAndShowError
->
if (loadAndShowError != null || !consentInformation.canRequestAds()) {
// without this the banner just silently stays hidden,
// and the ump sdk only logs an unspecific "Error
// making request."
crashManager.log(
"consent form failed: " +
describe(loadAndShowError) +
", canRequestAds=" +
consentInformation.canRequestAds()
)

hideGoogleAds()

return@loadAndShowConsentFormIfRequired
if (loadAndShowError != null) {
// the ump sdk only logs an unspecific "Error making request."
crashManager.log("consent form failed: " + describe(loadAndShowError))
}

activity.runOnUiThread { showAdaptiveBanner() }
showAdsIfConsented(consentInformation)
}
},
{ requestConsentError ->
// fires for the mundane offline case too - a device that was asleep
// times out against fundingchoicesmessages.google.com
crashManager.log("consent info update failed: " + describe(requestConsentError))

hideGoogleAds()
showAdsIfConsented(consentInformation)
},
)
}

/**
* Whether we may load an ad is [ConsentInformation.canRequestAds] and nothing else - never
* whether the consent call itself came back clean.
*
* The sdk caches the user's decision, so a form that fails to show, or an update that times out
* because the device is offline, still leaves an earlier consent standing, and outside the
* regions where a form is required at all there is no decision to fail in the first place.
* Treating those errors as a refusal hid the banner for the rest of the session for users who
* had already said yes.
*/
private fun showAdsIfConsented(consentInformation: ConsentInformation) {
if (!consentInformation.canRequestAds()) {
crashManager.log("consent does not allow requesting ads")

hideGoogleAds()

return
}

activity.runOnUiThread { showAdaptiveBanner() }
}

// https://developers.google.com/admob/android/banner/adaptive
// the anchored adaptive size is deprecated in favour of the inline one, which sizes the
// banner differently - a change to make on its own rather than in passing
Expand Down
Loading