From 5a002c9176acc153d161081d0bda197cae79b695 Mon Sep 17 00:00:00 2001 From: Jill Song Date: Wed, 8 Nov 2023 08:22:57 -0800 Subject: [PATCH] Internal cleanup PiperOrigin-RevId: 580540808 --- .../GoogleMobileAdsConsentManager.java | 97 -------- .../adaptivebannerexample/MyActivity.java | 219 ------------------ 2 files changed, 316 deletions(-) delete mode 100644 java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java delete mode 100644 java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java diff --git a/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java b/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java deleted file mode 100644 index 9bfbb9403..000000000 --- a/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/GoogleMobileAdsConsentManager.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.google.android.gms.example.adaptivebannerexample; - -import android.app.Activity; -import android.content.Context; -import com.google.android.ump.ConsentDebugSettings; -import com.google.android.ump.ConsentForm.OnConsentFormDismissedListener; -import com.google.android.ump.ConsentInformation; -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; - -/** - * The Google Mobile Ads SDK provides the User Messaging Platform (Google's - * IAB Certified consent management platform) as one solution to capture - * consent for users in GDPR impacted countries. This is an example and - * you can choose another consent management platform to capture consent. - */ -@SuppressWarnings("NonFinalStaticField") -public class GoogleMobileAdsConsentManager { - private static GoogleMobileAdsConsentManager instance; - private final ConsentInformation consentInformation; - - /** Private constructor. */ - private GoogleMobileAdsConsentManager(Context context) { - this.consentInformation = UserMessagingPlatform.getConsentInformation(context); - } - - /** Public constructor. */ - public static GoogleMobileAdsConsentManager getInstance(Context context) { - if (instance == null) { - instance = new GoogleMobileAdsConsentManager(context); - } - - return instance; - } - - /** Interface definition for a callback to be invoked when consent gathering is complete. */ - public interface OnConsentGatheringCompleteListener { - void consentGatheringComplete(FormError error); - } - - /** Helper variable to determine if the app can request ads. */ - public boolean canRequestAds() { - return consentInformation.canRequestAds(); - } - - /** Helper variable to determine if the privacy options form is required. */ - public boolean isPrivacyOptionsRequired() { - return consentInformation.getPrivacyOptionsRequirementStatus() - == PrivacyOptionsRequirementStatus.REQUIRED; - } - - /** - * Helper method to call the UMP SDK methods to request consent information and load/present a - * consent form if necessary. - */ - public void gatherConsent( - Activity activity, OnConsentGatheringCompleteListener onConsentGatheringCompleteListener) { - // For testing purposes, you can force a DebugGeography of EEA or NOT_EEA. - ConsentDebugSettings debugSettings = new ConsentDebugSettings.Builder(activity) - // Uncomment the line below to set DebugGeography to EEA. - // .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA) - - // Check your logcat output for the hashed device ID e.g. - // "Use new ConsentDebugSettings.Builder().addTestDeviceHashedId("ABCDEF012345")" to use - // the debug functionality. - .addTestDeviceHashedId("TEST-DEVICE-HASHED-ID") - .build(); - - ConsentRequestParameters params = new ConsentRequestParameters.Builder() - .setConsentDebugSettings(debugSettings) - .build(); - - // Requesting an update to consent information should be called on every app launch. - consentInformation.requestConsentInfoUpdate( - activity, - params, - () -> - UserMessagingPlatform.loadAndShowConsentFormIfRequired( - activity, - formError -> { - // Consent has been gathered. - onConsentGatheringCompleteListener.consentGatheringComplete(formError); - }), - requestConsentError -> - onConsentGatheringCompleteListener.consentGatheringComplete(requestConsentError) - ); - } - - /** Helper method to call the UMP SDK method to present the privacy options form. */ - public void showPrivacyOptionsForm( - Activity activity, - OnConsentFormDismissedListener onConsentFormDismissedListener) { - UserMessagingPlatform.showPrivacyOptionsForm(activity, onConsentFormDismissedListener); - } -} diff --git a/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java b/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java deleted file mode 100644 index 6a68855b7..000000000 --- a/java/admob/adaptivebannerexample/app/src/main/java/com/google/android/gms/example/adaptivebannerexample/MyActivity.java +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (C) 2019 Google LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.gms.example.adaptivebannerexample; - -import android.os.Bundle; -import androidx.appcompat.app.AppCompatActivity; -import android.util.DisplayMetrics; -import android.util.Log; -import android.view.Display; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.FrameLayout; -import android.widget.PopupMenu; -import android.widget.Toast; -import com.google.android.gms.ads.AdRequest; -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.gms.ads.initialization.InitializationStatus; -import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; -import java.util.Arrays; -import java.util.concurrent.atomic.AtomicBoolean; - -/** Main Activity. Inflates main activity xml and child fragments. */ -public class MyActivity extends AppCompatActivity { - - // This is an ad unit ID for a test ad. Replace with your own banner ad unit ID. - private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/9214589741"; - private static final String TAG = "MyActivity"; - private final AtomicBoolean isMobileAdsInitializeCalled = new AtomicBoolean(false); - private GoogleMobileAdsConsentManager googleMobileAdsConsentManager; - private FrameLayout adContainerView; - private AdView adView; - private AtomicBoolean initialLayoutComplete = new AtomicBoolean(false); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_my); - adContainerView = findViewById(R.id.ad_view_container); - - // Log the Mobile Ads SDK version. - Log.d(TAG, "Google Mobile Ads SDK Version: " + MobileAds.getVersion()); - - googleMobileAdsConsentManager = - GoogleMobileAdsConsentManager.getInstance(getApplicationContext()); - googleMobileAdsConsentManager.gatherConsent( - this, - consentError -> { - if (consentError != null) { - // Consent not obtained in current session. - Log.w( - TAG, - String.format("%s: %s", consentError.getErrorCode(), consentError.getMessage())); - } - - if (googleMobileAdsConsentManager.canRequestAds()) { - initializeMobileAdsSdk(); - } - - if (googleMobileAdsConsentManager.isPrivacyOptionsRequired()) { - // Regenerate the options menu to include a privacy setting. - invalidateOptionsMenu(); - } - }); - - // This sample attempts to load ads using consent obtained in the previous session. - if (googleMobileAdsConsentManager.canRequestAds()) { - initializeMobileAdsSdk(); - } - - // Since we're loading the banner based on the adContainerView size, we need to wait until this - // view is laid out before we can get the width. - adContainerView - .getViewTreeObserver() - .addOnGlobalLayoutListener( - () -> { - if (!initialLayoutComplete.getAndSet(true) - && googleMobileAdsConsentManager.canRequestAds()) { - loadBanner(); - } - }); - - // Set your test devices. Check your logcat output for the hashed device ID to - // get test ads on a physical device. e.g. - // "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345")) - // to get test ads on this device." - MobileAds.setRequestConfiguration( - new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345")).build()); - } - - /** Called when leaving the activity */ - @Override - public void onPause() { - if (adView != null) { - adView.pause(); - } - super.onPause(); - } - - /** Called when returning to the activity */ - @Override - public void onResume() { - super.onResume(); - if (adView != null) { - adView.resume(); - } - } - - /** Called before the activity is destroyed */ - @Override - public void onDestroy() { - if (adView != null) { - adView.destroy(); - } - super.onDestroy(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.action_menu, menu); - MenuItem moreMenu = menu.findItem(R.id.action_more); - moreMenu.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired()); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - View menuItemView = findViewById(item.getItemId()); - PopupMenu popup = new PopupMenu(this, menuItemView); - popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); - popup.show(); - popup.setOnMenuItemClickListener( - popupMenuItem -> { - if (popupMenuItem.getItemId() == R.id.privacy_settings) { - // Handle changes to user consent. - googleMobileAdsConsentManager.showPrivacyOptionsForm( - this, - formError -> { - if (formError != null) { - Toast.makeText(this, formError.getMessage(), Toast.LENGTH_SHORT).show(); - } - }); - return true; - } - return false; - }); - return super.onOptionsItemSelected(item); - } - - private void loadBanner() { - // Create an ad request. - adView = new AdView(this); - adView.setAdUnitId(AD_UNIT_ID); - adContainerView.removeAllViews(); - adContainerView.addView(adView); - - AdSize adSize = getAdSize(); - adView.setAdSize(adSize); - - AdRequest adRequest = new AdRequest.Builder().build(); - - // Start loading the ad in the background. - adView.loadAd(adRequest); - } - - private void initializeMobileAdsSdk() { - if (isMobileAdsInitializeCalled.getAndSet(true)) { - return; - } - - // Initialize the Mobile Ads SDK. - MobileAds.initialize( - this, - new OnInitializationCompleteListener() { - @Override - public void onInitializationComplete(InitializationStatus initializationStatus) {} - }); - - // Load an ad. - if (initialLayoutComplete.get()) { - loadBanner(); - } - } - - private AdSize getAdSize() { - // Determine the screen width (less decorations) to use for the ad width. - Display display = getWindowManager().getDefaultDisplay(); - DisplayMetrics outMetrics = new DisplayMetrics(); - display.getMetrics(outMetrics); - - float density = outMetrics.density; - - float adWidthPixels = adContainerView.getWidth(); - - // If the ad hasn't been laid out, default to the full screen width. - if (adWidthPixels == 0) { - adWidthPixels = outMetrics.widthPixels; - } - - int adWidth = (int) (adWidthPixels / density); - return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth); - } -}