Added privacy options
DreamingCodes committed
Aug 2, 2024 at 19:59 UTC
359e33162c536b46441ee20250c904edc71359e6
5 files changed
+68
-2
android/build.gradle.kts
+1
@@ -40,6 +40,7 @@ dependencies {
40
implementation("androidx.core:core-ktx:1.9.0")
41
implementation("androidx.appcompat:appcompat:1.6.0")
42
implementation("com.google.android.material:material:1.7.0")
43
+ implementation("com.google.android.ump:user-messaging-platform:2.2.0")
44
testImplementation("junit:junit:4.13.2")
45
androidTestImplementation("androidx.test.ext:junit:1.1.5")
46
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
android/src/main/java/AdmobPlugin.kt
+55
@@ -1,6 +1,8 @@
1
package com.plugin.admob
2
3
import android.app.Activity
4
+import android.content.ContentValues.TAG
5
+import android.util.Log
6
import android.webkit.WebView
7
import app.tauri.annotation.Command
8
import app.tauri.annotation.InvokeArg
@@ -10,6 +12,9 @@ import app.tauri.plugin.Plugin
12
import app.tauri.plugin.Invoke
13
import com.google.android.gms.ads.MobileAds
14
import com.google.android.gms.ads.initialization.InitializationStatus
15
+import com.google.android.ump.ConsentInformation
16
+import com.google.android.ump.ConsentRequestParameters
17
+import com.google.android.ump.UserMessagingPlatform
18
import com.plugin.admob.ads.Banner
19
import com.plugin.admob.ads.Interstitial
20
import com.plugin.admob.ads.Rewarded
@@ -40,6 +45,11 @@ class InvokeArgs {
45
46
@TauriPlugin
47
class AdmobPlugin(var activity: Activity) : Plugin(activity) {
48
+ private lateinit var consentInformation: ConsentInformation
49
+ val isPrivacyOptionsRequired: Boolean
50
+ get() =
51
+ consentInformation.privacyOptionsRequirementStatus ==
52
+ ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED
53
private var helper: Helper? = null
54
var webView: WebView? = null
55
@@ -52,6 +62,51 @@ class AdmobPlugin(var activity: Activity) : Plugin(activity) {
62
MobileAds.initialize(activity.baseContext) { status: InitializationStatus? ->
63
helper!!.configForTestLab()
64
}
65
+
66
+ val params = ConsentRequestParameters
67
+ .Builder()
68
+ .build()
69
+
70
+ consentInformation = UserMessagingPlatform.getConsentInformation(this.activity)
71
+ consentInformation.requestConsentInfoUpdate(
72
+ this.activity,
73
+ params,
74
+ {
75
+ UserMessagingPlatform.loadAndShowConsentFormIfRequired(
76
+ this.activity
77
+ ) { loadAndShowError ->
78
+ if (loadAndShowError != null) {
79
+ Log.w(TAG, "${loadAndShowError.errorCode}: ${loadAndShowError.message}")
80
+ return@loadAndShowConsentFormIfRequired
81
+ }
82
+ }
83
+ },
84
+ {
85
+ requestConsentError ->
86
+ Log.w(TAG, "${requestConsentError.errorCode}: ${requestConsentError.message}")
87
+ }
88
+ )
89
+ }
90
+
91
+ @Command
92
+ fun isPrivacyOptionsRequired(call: Invoke) {
93
+ try {
94
+ call.resolve(JSObject("{\"isPrivacyOptionsRequired\": ${isPrivacyOptionsRequired}}"))
95
+ } catch (ex: JSONException) {
96
+ call.reject(ex.toString())
97
+ }
98
+ }
99
+
100
+ @Command
101
+ fun showPrivacyOptionsForm(invoke: Invoke) {
102
+ UserMessagingPlatform.showPrivacyOptionsForm(this.activity) { formError ->
103
+ if (formError != null) {
104
+ Log.w(TAG, "${formError.errorCode}: ${formError.message}")
105
+ invoke.reject(formError.message)
106
+ return@showPrivacyOptionsForm
107
+ }
108
+ invoke.resolve()
109
+ }
110
}
111
112
@Command
build.rs
+1
-1
@@ -1,4 +1,4 @@
1
-const COMMANDS: &[&str] = &["trackingAuthorizationStatus", "requestTrackingAuthorization", "configure", "configRequest", "adCreate", "adIsLoaded", "adLoad", "adShow", "adHide"];
1
+const COMMANDS: &[&str] = &["trackingAuthorizationStatus", "requestTrackingAuthorization", "configure", "configRequest", "adCreate", "adIsLoaded", "adLoad", "adShow", "adHide", "isPrivacyOptionsRequired", "showPrivacyOptionsForm"];
2
3
fn main() {
4
tauri_plugin::Builder::new(COMMANDS)
examples/tauri-app/src/App.svelte
+3
-1
@@ -13,7 +13,9 @@
13
adUnitId: 'ca-app-pub-3940256099942544/9214589741',
14
position: 'top',
15
});
16
- await banner.load();
16
+ if (!(await banner.isLoaded())){
17
+ await banner.load();
18
+ }
19
await banner.show();
20
}
21
</script>
guest-js/ads/common.ts
+8
@@ -1,5 +1,13 @@
1
import {invoke} from '@tauri-apps/api/core'
2
3
+export async function isPrivacyOptionsRequired(): Promise<boolean> {
4
+ return (await invoke<{isPrivacyOptionsRequired: boolean}>('plugin:admob|isPrivacyOptionsRequired')).isPrivacyOptionsRequired
5
+}
6
+
7
+export async function showPrivacyOptionsForm(): Promise<void> {
8
+ await invoke('plugin:admob|showPrivacyOptionsForm')
9
+}
10
+
11
export class MobileAd<T extends MobileAdOptions = MobileAdOptions> {
12
private static allAdds: { [s: number]: MobileAd } = {};
13
private static idCounter = 0;