Skip to content

Commit ebb97e9

Browse files
authored
Ump sdk (#571)
* Ump (#554) * Ump support - dart unit tests (#560) * Ump tests android (#561) * Add iOS unit tests. (#564) * Api updates (#565) * Update changelog and internal error code for Android. (#566)
1 parent ae97945 commit ebb97e9

34 files changed

+2976
-5
lines changed

packages/google_mobile_ads/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
## 1.3.0
22
* Adds support for programmatically opening the debug options menu using`MobileAds.openDebugMenu(String adUnitId)`
3-
* Adds support for Ad inspector APIs
3+
* Adds support for Ad inspector APIs. See the [AdMob](https://developers.google.com/admob/flutter/ad-inspector)
4+
and [Ad Manager](https://developers.google.com/ad-manager/mobile-ads-sdk/flutter/ad-inspector)
5+
sites for integration guides.
6+
* Adds support for User Messaging Platform. See the [AdMob](https://developers.google.com/admob/flutter/eu-consent)
7+
and [Ad Manager](https://developers.google.com/ad-manager/mobile-ads-sdk/flutter/eu-consent)
8+
sites for integration guides.
49

510
## 1.2.0
611
* Set new minimum height for `FluidAdWidget`.

packages/google_mobile_ads/android/src/main/java/io/flutter/plugins/googlemobileads/GoogleMobileAdsPlugin.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import io.flutter.plugin.common.MethodChannel.Result;
3939
import io.flutter.plugin.common.StandardMethodCodec;
4040
import io.flutter.plugins.googlemobileads.FlutterAd.FlutterOverlayAd;
41+
import io.flutter.plugins.googlemobileads.usermessagingplatform.UserMessagingPlatformManager;
4142
import java.util.HashMap;
4243
import java.util.List;
4344
import java.util.Map;
@@ -63,6 +64,7 @@ private static <T> T requireNonNull(T obj) {
6364
@Nullable private AdInstanceManager instanceManager;
6465
@Nullable private AdMessageCodec adMessageCodec;
6566
@Nullable private AppStateNotifier appStateNotifier;
67+
@Nullable private UserMessagingPlatformManager userMessagingPlatformManager;
6668
private final Map<String, NativeAdFactory> nativeAdFactories = new HashMap<>();
6769
@Nullable private MediationNetworkExtrasProvider mediationNetworkExtrasProvider;
6870
private final FlutterMobileAdsWrapper flutterMobileAds;
@@ -245,6 +247,9 @@ public void onAttachedToEngine(FlutterPluginBinding binding) {
245247
"plugins.flutter.io/google_mobile_ads/ad_widget",
246248
new GoogleMobileAdsViewFactory(instanceManager));
247249
appStateNotifier = new AppStateNotifier(binding.getBinaryMessenger());
250+
userMessagingPlatformManager =
251+
new UserMessagingPlatformManager(
252+
binding.getBinaryMessenger(), binding.getApplicationContext());
248253
}
249254

250255
@Override
@@ -263,6 +268,9 @@ public void onAttachedToActivity(ActivityPluginBinding binding) {
263268
if (adMessageCodec != null) {
264269
adMessageCodec.setContext(binding.getActivity());
265270
}
271+
if (userMessagingPlatformManager != null) {
272+
userMessagingPlatformManager.setActivity(binding.getActivity());
273+
}
266274
}
267275

268276
@Override
@@ -274,6 +282,9 @@ public void onDetachedFromActivityForConfigChanges() {
274282
if (instanceManager != null) {
275283
instanceManager.setActivity(null);
276284
}
285+
if (userMessagingPlatformManager != null) {
286+
userMessagingPlatformManager.setActivity(null);
287+
}
277288
}
278289

279290
@Override
@@ -284,6 +295,9 @@ public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBindin
284295
if (adMessageCodec != null) {
285296
adMessageCodec.setContext(binding.getActivity());
286297
}
298+
if (userMessagingPlatformManager != null) {
299+
userMessagingPlatformManager.setActivity(binding.getActivity());
300+
}
287301
}
288302

289303
@Override
@@ -294,6 +308,9 @@ public void onDetachedFromActivity() {
294308
if (instanceManager != null) {
295309
instanceManager.setActivity(null);
296310
}
311+
if (userMessagingPlatformManager != null) {
312+
userMessagingPlatformManager.setActivity(null);
313+
}
297314
}
298315

299316
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package io.flutter.plugins.googlemobileads.usermessagingplatform;
16+
17+
import android.content.Context;
18+
import androidx.annotation.Nullable;
19+
import com.google.android.ump.ConsentDebugSettings;
20+
import java.util.List;
21+
import java.util.Objects;
22+
23+
/** Wrapper for {@link ConsentDebugSettings}. */
24+
class ConsentDebugSettingsWrapper {
25+
26+
@Nullable private final Integer debugGeography;
27+
28+
@Nullable private final List<String> testIdentifiers;
29+
30+
ConsentDebugSettingsWrapper(
31+
@Nullable Integer debugGeography, @Nullable List<String> testIdentifiers) {
32+
this.debugGeography = debugGeography;
33+
this.testIdentifiers = testIdentifiers;
34+
}
35+
36+
@Nullable
37+
Integer getDebugGeography() {
38+
return debugGeography;
39+
}
40+
41+
@Nullable
42+
List<String> getTestIdentifiers() {
43+
return testIdentifiers;
44+
}
45+
46+
/** Get the corresponding {@link ConsentDebugSettings}. */
47+
ConsentDebugSettings getAsConsentDebugSettings(Context context) {
48+
ConsentDebugSettings.Builder builder = new ConsentDebugSettings.Builder(context);
49+
if (debugGeography != null) {
50+
builder.setDebugGeography(debugGeography);
51+
}
52+
if (testIdentifiers != null) {
53+
for (String testIdentifier : testIdentifiers) {
54+
builder.addTestDeviceHashedId(testIdentifier);
55+
}
56+
}
57+
return builder.build();
58+
}
59+
60+
@Override
61+
public boolean equals(@Nullable Object obj) {
62+
if (obj == this) {
63+
return true;
64+
} else if (!(obj instanceof ConsentDebugSettingsWrapper)) {
65+
return false;
66+
}
67+
68+
ConsentDebugSettingsWrapper other = (ConsentDebugSettingsWrapper) obj;
69+
return Objects.equals(debugGeography, other.getDebugGeography())
70+
&& Objects.equals(testIdentifiers, other.getTestIdentifiers());
71+
}
72+
73+
@Override
74+
public int hashCode() {
75+
return Objects.hash(debugGeography, testIdentifiers);
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package io.flutter.plugins.googlemobileads.usermessagingplatform;
16+
17+
import android.content.Context;
18+
import androidx.annotation.Nullable;
19+
import com.google.android.ump.ConsentRequestParameters;
20+
import java.util.Objects;
21+
22+
/** Wrapper for {@link ConsentRequestParameters}. */
23+
class ConsentRequestParametersWrapper {
24+
25+
@Nullable private final Boolean tfuac;
26+
27+
@Nullable private final ConsentDebugSettingsWrapper debugSettings;
28+
29+
ConsentRequestParametersWrapper(
30+
@Nullable Boolean tfuac, @Nullable ConsentDebugSettingsWrapper debugSettings) {
31+
this.tfuac = tfuac;
32+
this.debugSettings = debugSettings;
33+
}
34+
35+
@Nullable
36+
Boolean getTfuac() {
37+
return tfuac;
38+
}
39+
40+
@Nullable
41+
ConsentDebugSettingsWrapper getDebugSettings() {
42+
return debugSettings;
43+
}
44+
45+
/** Get as {@link ConsentRequestParameters}. */
46+
ConsentRequestParameters getAsConsentRequestParameters(Context context) {
47+
ConsentRequestParameters.Builder builder = new ConsentRequestParameters.Builder();
48+
if (tfuac != null) {
49+
builder.setTagForUnderAgeOfConsent(tfuac);
50+
}
51+
if (debugSettings != null) {
52+
builder.setConsentDebugSettings(debugSettings.getAsConsentDebugSettings(context));
53+
}
54+
return builder.build();
55+
}
56+
57+
@Override
58+
public boolean equals(@Nullable Object obj) {
59+
if (obj == this) {
60+
return true;
61+
} else if (!(obj instanceof ConsentRequestParametersWrapper)) {
62+
return false;
63+
}
64+
65+
ConsentRequestParametersWrapper other = (ConsentRequestParametersWrapper) obj;
66+
return Objects.equals(tfuac, other.getTfuac())
67+
&& Objects.equals(debugSettings, other.getDebugSettings());
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return Objects.hash(tfuac, debugSettings);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package io.flutter.plugins.googlemobileads.usermessagingplatform;
16+
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
import com.google.android.ump.ConsentForm;
20+
import io.flutter.plugin.common.StandardMessageCodec;
21+
import java.io.ByteArrayOutputStream;
22+
import java.nio.ByteBuffer;
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
/** Codec for UMP SDK. */
29+
public class UserMessagingCodec extends StandardMessageCodec {
30+
31+
private static final byte VALUE_CONSENT_REQUEST_PARAMETERS = (byte) 129;
32+
private static final byte VALUE_CONSENT_DEBUG_SETTINGS = (byte) 130;
33+
private static final byte VALUE_CONSENT_FORM = (byte) 131;
34+
35+
private final Map<Integer, ConsentForm> consentFormMap;
36+
37+
UserMessagingCodec() {
38+
consentFormMap = new HashMap<>();
39+
}
40+
41+
@Override
42+
protected void writeValue(@NonNull ByteArrayOutputStream stream, @NonNull Object value) {
43+
if (value instanceof ConsentRequestParametersWrapper) {
44+
stream.write(VALUE_CONSENT_REQUEST_PARAMETERS);
45+
ConsentRequestParametersWrapper params = (ConsentRequestParametersWrapper) value;
46+
writeValue(stream, params.getTfuac());
47+
writeValue(stream, params.getDebugSettings());
48+
} else if (value instanceof ConsentDebugSettingsWrapper) {
49+
stream.write(VALUE_CONSENT_DEBUG_SETTINGS);
50+
ConsentDebugSettingsWrapper debugSettings = (ConsentDebugSettingsWrapper) value;
51+
writeValue(stream, debugSettings.getDebugGeography());
52+
writeValue(stream, debugSettings.getTestIdentifiers());
53+
} else if (value instanceof ConsentForm) {
54+
stream.write(VALUE_CONSENT_FORM);
55+
writeValue(stream, value.hashCode());
56+
} else {
57+
super.writeValue(stream, value);
58+
}
59+
}
60+
61+
@Nullable
62+
private List<String> asList(@Nullable Object maybeList) {
63+
if (maybeList == null) {
64+
return null;
65+
}
66+
List<String> stringList = new ArrayList<>();
67+
if (maybeList instanceof List) {
68+
List<?> list = (List<?>) maybeList;
69+
for (Object obj : list) {
70+
if (obj instanceof String) {
71+
stringList.add((String) obj);
72+
}
73+
}
74+
}
75+
return stringList;
76+
}
77+
78+
@Override
79+
protected Object readValueOfType(byte type, @NonNull ByteBuffer buffer) {
80+
switch (type) {
81+
case VALUE_CONSENT_REQUEST_PARAMETERS:
82+
{
83+
Boolean tfuac = (Boolean) readValueOfType(buffer.get(), buffer);
84+
ConsentDebugSettingsWrapper debugSettings =
85+
(ConsentDebugSettingsWrapper) readValueOfType(buffer.get(), buffer);
86+
return new ConsentRequestParametersWrapper(tfuac, debugSettings);
87+
}
88+
case VALUE_CONSENT_DEBUG_SETTINGS:
89+
{
90+
Integer debugGeoInt = (Integer) readValueOfType(buffer.get(), buffer);
91+
List<String> testIdentifiers = asList(readValueOfType(buffer.get(), buffer));
92+
return new ConsentDebugSettingsWrapper(debugGeoInt, testIdentifiers);
93+
}
94+
case VALUE_CONSENT_FORM:
95+
{
96+
Integer hash = (Integer) readValueOfType(buffer.get(), buffer);
97+
return consentFormMap.get(hash);
98+
}
99+
default:
100+
return super.readValueOfType(type, buffer);
101+
}
102+
}
103+
104+
void trackConsentForm(ConsentForm form) {
105+
consentFormMap.put(form.hashCode(), form);
106+
}
107+
108+
void disposeConsentForm(ConsentForm form) {
109+
consentFormMap.remove(form.hashCode());
110+
}
111+
}

0 commit comments

Comments
 (0)