Skip to content

Commit

Permalink
Added UMP 2.2.0 Android implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LTPhantom committed Apr 18, 2024
1 parent 180f65e commit 2931e01
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.android.ump.ConsentInformation;
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateFailureListener;
import com.google.android.ump.ConsentInformation.OnConsentInfoUpdateSuccessListener;
import com.google.android.ump.ConsentInformation.PrivacyOptionsRequirementStatus;
import com.google.android.ump.ConsentRequestParameters;
import com.google.android.ump.FormError;
import com.google.android.ump.UserMessagingPlatform;
Expand Down Expand Up @@ -137,6 +138,18 @@ public void onConsentInfoUpdateFailure(FormError error) {
case "ConsentInformation#canRequestAds":
result.success(getConsentInformation().canRequestAds());
break;
case "ConsentInformation#getPrivacyOptionsRequirementStatus":
switch (getConsentInformation().getPrivacyOptionsRequirementStatus()) {
case NOT_REQUIRED:
result.success(0);
break;
case REQUIRED:
result.success(1);
break;
default:
result.success(2);
}
break;
case "UserMessagingPlatform#loadAndShowConsentFormIfRequired":
if (activity == null) {
result.error(
Expand All @@ -148,10 +161,8 @@ public void onConsentInfoUpdateFailure(FormError error) {
UserMessagingPlatform.loadAndShowConsentFormIfRequired(
activity,
loadAndShowError -> {
Log.d("UMP", "loadAndShow callback");
if (loadAndShowError != null) {
// Consent gathering failed.
Log.d("UMP", "Consent gathering failed");
result.error(Integer.toString(loadAndShowError.getErrorCode()),
loadAndShowError.getMessage(), null);
return;
Expand All @@ -178,6 +189,27 @@ public void onConsentFormLoadFailure(FormError formError) {
}
});
break;
case "UserMessagingPlatform#showPrivacyOptionsForm":
if (activity == null) {
result.error(
INTERNAL_ERROR_CODE,
"ConsentInformation#requestConsentInfoUpdate called before plugin has been registered to an activity.",
null);
break;
}
UserMessagingPlatform.showPrivacyOptionsForm(
activity,
loadAndShowError -> {
if (loadAndShowError != null) {
// Consent gathering failed.
result.error(Integer.toString(loadAndShowError.getErrorCode()),
loadAndShowError.getMessage(), null);
return;
}
result.success(null);
}
);
break;
case "ConsentInformation#isConsentFormAvailable": {
result.success(getConsentInformation().isConsentFormAvailable());
break;
Expand Down
12 changes: 10 additions & 2 deletions packages/google_mobile_ads/lib/src/ump/consent_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ abstract class ConsentForm {
/// Shows the consent form.
void show(OnConsentFormDismissedListener onConsentFormDismissedListener);

/// Presents a privacy options form.
static Future<void> showPrivacyOptionsForm(OnConsentFormDismissedListener onConsentFormDismissedListener) async {
onConsentFormDismissedListener(await UserMessagingChannel.instance.showPrivacyOptionsForm());
}

/// Free platform resources associated with this object.
///
/// Returns a future that completes when the platform resources are freed.
Expand All @@ -47,7 +52,10 @@ abstract class ConsentForm {
.loadConsentForm(successListener, failureListener);
}

static Future<void> loadAndShowConsentFormIfRequired(OnConsentFormDismissedListener onConsentFormDismissedListener) async {
onConsentFormDismissedListener(await UserMessagingChannel.instance.loadAndShowConsentFormIfRequired());
/// Loads a consent form and immediately shows it.
static Future<void> loadAndShowConsentFormIfRequired(
OnConsentFormDismissedListener onConsentFormDismissedListener) async {
onConsentFormDismissedListener(
await UserMessagingChannel.instance.loadAndShowConsentFormIfRequired());
}
}
13 changes: 13 additions & 0 deletions packages/google_mobile_ads/lib/src/ump/consent_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ abstract class ConsentInformation {
/// Indicates whether the app has completed the necessary steps for gathering updated user consent.
Future<bool> canRequestAds();

/// Indicates the privacy options requirement status as a [PrivacyOptionsRequirementStatus].
Future<PrivacyOptionsRequirementStatus> getPrivacyOptionsRequirementStatus();

/// The static [ConsentInformation] instance.
static ConsentInformation instance = ConsentInformationImpl();
}

/// Values indicate whether a privacy options button is required.
enum PrivacyOptionsRequirementStatus {
/// Privacy options entry point is not required.
notRequired,
/// Privacy options entry point is required.
required,
/// Privacy options requirement status is unknown.
unknown;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ class ConsentInformationImpl extends ConsentInformation {
Future<bool> canRequestAds() {
return UserMessagingChannel.instance.canRequestAds();
}

@override
Future<PrivacyOptionsRequirementStatus> getPrivacyOptionsRequirementStatus() {
return UserMessagingChannel.instance.getPrivacyOptionsRequirementStatus();
}
}
33 changes: 29 additions & 4 deletions packages/google_mobile_ads/lib/src/ump/user_messaging_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,23 @@ class UserMessagingChannel {

/// Returns indicating whether it is ok to request ads.
Future<bool> canRequestAds() async {
return (await _methodChannel.invokeMethod<bool>('ConsentInformation#canRequestAds'))!;
return (await _methodChannel
.invokeMethod<bool>('ConsentInformation#canRequestAds'))!;
}

/// Indicates the privacy options requirement status as a [PrivacyOptionsRequirementStatus].
Future<PrivacyOptionsRequirementStatus>
getPrivacyOptionsRequirementStatus() async {
int? privacyOptionsStatusInt = (await _methodChannel.invokeMethod<int>(
'ConsentInformation#getPrivacyOptionsRequirementStatus'));
switch (privacyOptionsStatusInt) {
case 0:
return PrivacyOptionsRequirementStatus.notRequired;
case 1:
return PrivacyOptionsRequirementStatus.required;
default:
return PrivacyOptionsRequirementStatus.unknown;
}
}

/// Loads a consent form and calls the corresponding listener.
Expand All @@ -122,9 +138,8 @@ class UserMessagingChannel {
/// Loads a consent form and calls the listener afterwards.
Future<FormError?> loadAndShowConsentFormIfRequired() async {
try {
return (await _methodChannel.invokeListMethod<FormError?>(
'UserMessagingPlatform#loadAndShowConsentFormIfRequired'
)) as FormError?;
return await _methodChannel.invokeMethod<FormError?>(
'UserMessagingPlatform#loadAndShowConsentFormIfRequired');
} on PlatformException catch (e) {
return _formErrorFromPlatformException(e);
}
Expand All @@ -146,6 +161,16 @@ class UserMessagingChannel {
}
}

/// Presents a privacy options form.
Future<FormError?> showPrivacyOptionsForm() async {
try {
return await _methodChannel.invokeMethod<FormError?>(
'UserMessagingPlatform#showPrivacyOptionsForm');
} on PlatformException catch (e) {
return _formErrorFromPlatformException(e);
}
}

FormError _formErrorFromPlatformException(PlatformException e) {
return FormError(
errorCode: int.tryParse(e.code) ?? -1, message: e.message ?? '');
Expand Down
2 changes: 1 addition & 1 deletion packages/google_mobile_ads/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ dev_dependencies:


environment:
sdk: ">=2.15.0 <4.0.0"
sdk: ">=2.17.0 <4.0.0"
flutter: ">=3.7.0"

0 comments on commit 2931e01

Please sign in to comment.