Skip to content

Commit 187235e

Browse files
authored
Deprecate credentials and add adUnitMapping. (#541)
* Deprecate credentials and add adUnitMapping. * Remove credentials from toString and replace with adUnitMapping.
1 parent 96975d2 commit 187235e

File tree

14 files changed

+74
-4
lines changed

14 files changed

+74
-4
lines changed

packages/google_mobile_ads/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Update GMA Android dependency to 20.6.0 and iOS to 8.13.0.
66
* [Android release notes](https://developers.google.com/admob/android/rel-notes)
77
* [iOS release notes](https://developers.google.com/admob/ios/rel-notes)
8+
* Deprecate `AdapterResponseInfo.credentials` in favor of `adUnitMapping`
89

910
## 1.2.0
1011
* Deprecates `LocationParams` in `AdRequest` and `AdManagerAdRequest`.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ protected void writeValue(ByteArrayOutputStream stream, Object value) {
118118
writeValue(stream, responseInfo.getLatencyMillis());
119119
writeValue(stream, responseInfo.getDescription());
120120
writeValue(stream, responseInfo.getCredentials());
121+
writeValue(stream, responseInfo.getAdUnitMapping());
121122
writeValue(stream, responseInfo.getError());
122123
} else if (value instanceof FlutterResponseInfo) {
123124
stream.write(VALUE_RESPONSE_INFO);
@@ -240,6 +241,7 @@ protected Object readValueOfType(byte type, ByteBuffer buffer) {
240241
(long) readValueOfType(buffer.get(), buffer),
241242
(String) readValueOfType(buffer.get(), buffer),
242243
(String) readValueOfType(buffer.get(), buffer),
244+
(Map<String, String>) readValueOfType(buffer.get(), buffer),
243245
(FlutterAdError) readValueOfType(buffer.get(), buffer));
244246
case VALUE_RESPONSE_INFO:
245247
return new FlutterResponseInfo(

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import com.google.android.gms.ads.ResponseInfo;
2323
import io.flutter.plugin.platform.PlatformView;
2424
import java.util.ArrayList;
25+
import java.util.HashMap;
2526
import java.util.List;
27+
import java.util.Map;
2628
import java.util.Objects;
2729

2830
abstract class FlutterAd {
@@ -112,6 +114,7 @@ static class FlutterAdapterResponseInfo {
112114
private final long latencyMillis;
113115
@NonNull private final String description;
114116
@NonNull private final String credentials;
117+
@NonNull private final Map<String, String> adUnitMapping;
115118
@Nullable private FlutterAdError error;
116119

117120
FlutterAdapterResponseInfo(@NonNull AdapterResponseInfo responseInfo) {
@@ -120,8 +123,13 @@ static class FlutterAdapterResponseInfo {
120123
this.description = responseInfo.toString();
121124
if (responseInfo.getCredentials() != null) {
122125
this.credentials = responseInfo.getCredentials().toString();
126+
this.adUnitMapping = new HashMap<>();
127+
for (String key : responseInfo.getCredentials().keySet()) {
128+
adUnitMapping.put(key, responseInfo.getCredentials().get(key).toString());
129+
}
123130
} else {
124131
credentials = "unknown credentials";
132+
adUnitMapping = new HashMap<>();
125133
}
126134
if (responseInfo.getAdError() != null) {
127135
this.error = new FlutterAdError(responseInfo.getAdError());
@@ -132,12 +140,14 @@ static class FlutterAdapterResponseInfo {
132140
@NonNull String adapterClassName,
133141
long latencyMillis,
134142
@NonNull String description,
135-
@Nullable String credentials,
143+
@NonNull String credentials,
144+
@NonNull Map<String, String> adUnitMapping,
136145
@Nullable FlutterAdError error) {
137146
this.adapterClassName = adapterClassName;
138147
this.latencyMillis = latencyMillis;
139148
this.description = description;
140149
this.credentials = credentials;
150+
this.adUnitMapping = adUnitMapping;
141151
this.error = error;
142152
}
143153

@@ -160,6 +170,11 @@ public String getCredentials() {
160170
return credentials;
161171
}
162172

173+
@NonNull
174+
public Map<String, String> getAdUnitMapping() {
175+
return adUnitMapping;
176+
}
177+
163178
@Nullable
164179
public FlutterAdError getError() {
165180
return error;
@@ -178,7 +193,8 @@ public boolean equals(@Nullable Object obj) {
178193
&& latencyMillis == that.latencyMillis
179194
&& Objects.equals(description, that.description)
180195
&& Objects.equals(credentials, that.credentials)
181-
&& Objects.equals(error, that.error);
196+
&& Objects.equals(error, that.error)
197+
&& Objects.equals(adUnitMapping, that.adUnitMapping);
182198
}
183199

184200
@Override

packages/google_mobile_ads/android/src/test/java/io/flutter/plugins/googlemobileads/AdMessageCodecTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ public void encodeFlutterRewardItem() {
321321
@Test
322322
public void encodeFlutterLoadAdError() {
323323
List<FlutterAdapterResponseInfo> adapterResponseInfos = new ArrayList<>();
324+
Map<String, String> adUnitMapping = Collections.singletonMap("key", "value");
324325
adapterResponseInfos.add(
325-
new FlutterAdapterResponseInfo("adapter-class", 9999, "description", "credentials", null));
326+
new FlutterAdapterResponseInfo(
327+
"adapter-class", 9999, "description", "credentials", adUnitMapping, null));
326328
FlutterResponseInfo info =
327329
new FlutterResponseInfo("responseId", "className", adapterResponseInfos);
328330
final ByteBuffer message =

packages/google_mobile_ads/ios/Classes/FLTAd_Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
@property NSNumber *_Nullable latency;
101101
@property NSString *_Nullable dictionaryDescription;
102102
@property NSString *_Nullable credentialsDescription;
103+
@property NSDictionary<NSString *, NSString *> *_Nullable adUnitMapping;
103104
@property NSError *_Nullable error;
104105

105106
- (instancetype _Nonnull)initWithResponseInfo:

packages/google_mobile_ads/ios/Classes/FLTAd_Internal.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,19 @@ - (instancetype _Nonnull)initWithResponseInfo:
259259
[[NSNumber alloc] initWithDouble:responseInfo.latency * 1000];
260260
_latency = @(timeInMillis.longValue);
261261
_dictionaryDescription = responseInfo.dictionaryRepresentation.description;
262+
#pragma clang diagnostic push
263+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
262264
_credentialsDescription = responseInfo.credentials.description;
265+
#pragma clang diagnostic pop
266+
_adUnitMapping = [[NSMutableDictionary alloc] init];
267+
for (NSString *key in responseInfo.adUnitMapping) {
268+
if ([responseInfo.adUnitMapping[key] isKindOfClass:NSObject.class]) {
269+
NSObject *value = ((NSObject *)responseInfo.adUnitMapping[key]);
270+
[_adUnitMapping setValue:value.description forKey:key];
271+
} else {
272+
[_adUnitMapping setValue:@"Unrecognized credential" forKey:key];
273+
}
274+
}
263275
_error = responseInfo.error;
264276
}
265277
return self;

packages/google_mobile_ads/ios/Classes/FLTGoogleMobileAdsReaderWriter_Internal.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,16 @@ - (id _Nullable)readValueOfType:(UInt8)type {
124124
NSNumber *latency = [self readValueOfType:[self readByte]];
125125
NSString *dictionaryDescription = [self readValueOfType:[self readByte]];
126126
NSString *credentialsDescription = [self readValueOfType:[self readByte]];
127+
NSDictionary<NSString *, NSString *> *adUnitMapping =
128+
[self readValueOfType:[self readByte]];
127129
NSError *error = [self readValueOfType:[self readByte]];
128130
FLTGADAdNetworkResponseInfo *adNetworkResponseInfo =
129131
[[FLTGADAdNetworkResponseInfo alloc] init];
130132
adNetworkResponseInfo.adNetworkClassName = adNetworkClassName;
131133
adNetworkResponseInfo.latency = latency;
132134
adNetworkResponseInfo.dictionaryDescription = dictionaryDescription;
133135
adNetworkResponseInfo.credentialsDescription = credentialsDescription;
136+
adNetworkResponseInfo.adUnitMapping = adUnitMapping;
134137
adNetworkResponseInfo.error = error;
135138
return adNetworkResponseInfo;
136139
}
@@ -318,6 +321,7 @@ - (void)writeValue:(id _Nonnull)value {
318321
[self writeValue:networkResponseInfo.latency];
319322
[self writeValue:networkResponseInfo.dictionaryDescription];
320323
[self writeValue:networkResponseInfo.credentialsDescription];
324+
[self writeValue:networkResponseInfo.adUnitMapping];
321325
[self writeValue:networkResponseInfo.error];
322326
} else if ([value isKindOfClass:[FLTLoadAdError class]]) {
323327
[self writeByte:FLTAdMobFieldLoadError];

packages/google_mobile_ads/ios/Tests/FLTGoogleMobileAdsReaderWriterTest.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ - (void)testEncodeDecodeFLTGADResponseInfo {
445445
OCMStub([mockGADResponseInfo dictionaryRepresentation])
446446
.andReturn(descriptionsDict);
447447
OCMStub([mockGADResponseInfo credentials]).andReturn(credentialsDict);
448+
OCMStub([mockGADResponseInfo adUnitMapping]).andReturn(credentialsDict);
448449
OCMStub([mockGADResponseInfo error]).andReturn(error);
449450

450451
FLTGADAdNetworkResponseInfo *adNetworkResponseInfo =
@@ -473,6 +474,7 @@ - (void)testEncodeDecodeFLTGADResponseInfo {
473474
@"{\n descriptions = dict;\n}");
474475
XCTAssertEqualObjects(decodedInfo.credentialsDescription,
475476
@"{\n credentials = dict;\n}");
477+
XCTAssertEqualObjects(decodedInfo.adUnitMapping, credentialsDict);
476478
XCTAssertEqual(decodedInfo.error.code, 1);
477479
XCTAssertEqualObjects(decodedInfo.error.domain, @"domain");
478480
XCTAssertEqualObjects(decodedInfo.error.localizedDescription, @"error");

packages/google_mobile_ads/lib/src/ad_containers.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class AdapterResponseInfo {
9696
required this.latencyMillis,
9797
required this.description,
9898
required this.credentials,
99+
required this.adUnitMapping,
99100
this.adError,
100101
});
101102

@@ -111,8 +112,12 @@ class AdapterResponseInfo {
111112
final String description;
112113

113114
/// A string description of adapter credentials specified in the AdMob or Ad Manager UI.
115+
@Deprecated('Use [adUnitMapping] instead')
114116
final String credentials;
115117

118+
/// Network configuration set on the AdMob UI.
119+
final Map<String, String> adUnitMapping;
120+
116121
/// The error that occurred while rendering the ad.
117122
final AdError? adError;
118123

@@ -121,7 +126,7 @@ class AdapterResponseInfo {
121126
return '$runtimeType(adapterClassName: $adapterClassName, '
122127
'latencyMillis: $latencyMillis), '
123128
'description: $description, '
124-
'credentials: $credentials, '
129+
'adUnitMapping: $adUnitMapping, '
125130
'adError: $adError)';
126131
}
127132
}

packages/google_mobile_ads/lib/src/ad_instance_manager.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ class AdMessageCodec extends StandardMessageCodec {
829829
writeValue(buffer, value.latencyMillis);
830830
writeValue(buffer, value.description);
831831
writeValue(buffer, value.credentials);
832+
writeValue(buffer, value.adUnitMapping);
832833
writeValue(buffer, value.adError);
833834
} else if (value is LoadAdError) {
834835
buffer.putUint8(_valueLoadAdError);
@@ -965,6 +966,8 @@ class AdMessageCodec extends StandardMessageCodec {
965966
latencyMillis: readValueOfType(buffer.getUint8(), buffer),
966967
description: readValueOfType(buffer.getUint8(), buffer),
967968
credentials: readValueOfType(buffer.getUint8(), buffer),
969+
adUnitMapping:
970+
_deepCastStringMap(readValueOfType(buffer.getUint8(), buffer)),
968971
adError: readValueOfType(buffer.getUint8(), buffer));
969972
case _valueLoadAdError:
970973
return LoadAdError(
@@ -1071,6 +1074,16 @@ class AdMessageCodec extends StandardMessageCodec {
10711074
);
10721075
}
10731076

1077+
Map<String, String> _deepCastStringMap(Map<dynamic, dynamic>? map) {
1078+
if (map == null) return {};
1079+
return map.map<String, String>(
1080+
(dynamic key, dynamic value) => MapEntry<String, String>(
1081+
key,
1082+
value,
1083+
),
1084+
);
1085+
}
1086+
10741087
void writeAdSize(WriteBuffer buffer, AdSize value) {
10751088
if (value is InlineAdaptiveSize) {
10761089
buffer.putUint8(_valueInlineAdaptiveBannerAdSize);

0 commit comments

Comments
 (0)