Skip to content

Commit

Permalink
Added Jetpack Compose Banner Sample
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 623998885
  • Loading branch information
nventimigli authored and Copybara-Service committed Jun 11, 2024
1 parent 8c6be90 commit 958085f
Show file tree
Hide file tree
Showing 35 changed files with 1,421 additions and 0 deletions.
56 changes: 56 additions & 0 deletions kotlin/advanced/JetpackComposeDemo/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.example.jetpackcomposedemo"
compileSdk = 34

defaultConfig {
applicationId = "com.google.android.gms.example.jetpackcomposedemo"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { useSupportLibrary = true }
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions { jvmTarget = "1.8" }
buildFeatures { compose = true }
composeOptions { kotlinCompilerExtensionVersion = "1.5.1" }
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
}

dependencies {
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.0")
implementation("androidx.activity:activity-compose:1.9.0")
implementation(platform("androidx.compose:compose-bom:2024.05.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.android.gms:play-services-ads:23.1.0")
implementation("com.google.android.ump:user-messaging-platform:2.2.0")
implementation("com.google.android.gms:play-services-ads-lite:23.1.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.05.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
21 changes: 21 additions & 0 deletions kotlin/advanced/JetpackComposeDemo/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:taskAffinity=""
android:name="com.google.android.gms.example.jetpackcomposedemo.MobileAdsApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JetpackComposeDemo"
tools:targetApi="31">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
<activity
android:name="com.google.android.gms.example.jetpackcomposedemo.MainActivity"
android:exported="true"
android:theme="@style/Theme.JetpackComposeDemo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.example.jetpackcomposedemo.BannerActivity"
android:exported="true"
android:theme="@style/Theme.JetpackComposeDemo">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.google.android.gms.example.jetpackcomposedemo

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.example.jetpackcomposedemo.composables.BannerAd
import com.google.android.gms.example.jetpackcomposedemo.composables.BannerAdState
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.ColorStateError
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.ColorStateLoaded
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.ColorStateUnloaded
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.JetpackComposeDemoTheme

class BannerActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
JetpackComposeDemoTheme {
Surface(modifier = Modifier.fillMaxHeight(), color = MaterialTheme.colorScheme.background) {
BannerScreen()
}
}
}
}

@Preview
@Composable
fun BannerScreenPreview() {
JetpackComposeDemoTheme {
Surface(modifier = Modifier.fillMaxHeight(), color = MaterialTheme.colorScheme.background) {
BannerScreen()
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BannerScreen() {
// Cache the mutable state for our notification bar.
val context = LocalContext.current
var messageText by remember { mutableStateOf("Banner ad is not loaded.") }
var messageColor by remember { mutableStateOf(ColorStateUnloaded) }

// Construct a banner ad state which configures our BannerAd composable.
val bannerState =
BannerAdState(
adUnitId = ADUNIT_ID,
adSize = AdSize.BANNER,
adRequest = AdRequest.Builder().build(),
onAdLoaded = {
messageColor = ColorStateLoaded
messageText = "Banner ad is loaded."
Log.i(TAG, messageText)
},
onAdFailedToLoad = { error: LoadAdError ->
messageColor = ColorStateError
messageText = "Banner ad failed to load with error: ${error.message}"
Log.e(TAG, messageText)
},
onAdImpression = { Log.i(TAG, "Banner ad impression.") },
onAdClicked = { Log.i(TAG, "Banner ad clicked.") },
onAdOpened = { Log.i(TAG, "Banner ad opened.") },
onAdClosed = { Log.i(TAG, "Banner ad closed.") },
)

Column(
modifier = Modifier.verticalScroll(rememberScrollState()),
content = {
// Render title.
TopAppBar(
title = { Text(text = "Banner ad") },
navigationIcon = {
IconButton(
onClick = {
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
}
) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back")
}
},
)
// Render status.
Box(modifier = Modifier.fillMaxSize().background(messageColor)) {
Text(text = messageText, style = MaterialTheme.typography.bodyLarge)
}
// BannerAd composable.
BannerAd(bannerState, modifier = Modifier)
},
)
}

companion object {
const val TAG = "GoogleMobileAdsSample"
// Test AdUnitID for demonstrative purposes.
// https://developers.google.com/admob/android/test-ads
const val ADUNIT_ID = "ca-app-pub-3940256099942544/6300978111"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package com.google.android.gms.example.jetpackcomposedemo

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.annotation.IntDef
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.MobileAds
import com.google.android.ump.*
import java.util.concurrent.atomic.AtomicBoolean

/** This class manages the process of obtaining consent for and initializing Google Mobile Ads. */
class GoogleMobileAdsManager {

/** Represents initialization states for the Google Mobile Ads SDK. */
object MobileAdsState {
/** Initial start state. */
const val UNINITIALIZED = 0

/** User consent required but not yet obtained. */
const val CONSENT_REQUIRED = 2

/** User consent obtained. Personalized vs non-personalized undefined. */
const val CONSENT_OBTAINED = 3

/** Google Mobile Ads SDK initialized successfully. */
const val INITIALIZED = 100

/** An error occurred when requesting consent. */
const val CONSENT_REQUEST_ERROR = -1

/** An error occurred when showing the privacy options form. */
const val CONSENT_FORM_ERROR = -2

@Target(AnnotationTarget.TYPE)
@IntDef(
UNINITIALIZED,
INITIALIZED,
CONSENT_REQUIRED,
CONSENT_OBTAINED,
CONSENT_REQUEST_ERROR,
CONSENT_FORM_ERROR,
)
@Retention(AnnotationRetention.SOURCE)
annotation class State
}

/** Represents current initialization states for the Google Mobile Ads SDK. */
var mobileAdsState = mutableIntStateOf(MobileAdsState.UNINITIALIZED)

/** Indicates whether the app has completed the steps for gathering updated user consent. */
var canRequestAds = mutableStateOf(false)

/** Helper variable to determine if the privacy options form is required. */
var isPrivacyOptionsRequired = mutableStateOf(false)

private var isMobileAdsInitializeCalled = AtomicBoolean(false)

private lateinit var consentInformation: ConsentInformation

/**
* Initiates the consent process and initializes the Google Mobile Ads SDK if the SDK is
* UNINITIALIZED.
*
* @param activity Activity responsible for initializing the Google Mobile Ads SDK.
* @param consentRequestParameters Parameters for the consent request form.
*/
fun initializeWithConsent(
activity: Activity,
consentRequestParameters: ConsentRequestParameters,
) {

if (isMobileAdsInitializeCalled.getAndSet(true)) {
return
}

consentInformation = UserMessagingPlatform.getConsentInformation(activity)

consentInformation.requestConsentInfoUpdate(
activity,
consentRequestParameters,
{
// Success callback.
showConsentFormIfRequired(activity) { error ->
if (error != null) {
Log.w(TAG, "Consent form error: ${error.errorCode} - ${error.message}")
mobileAdsState.intValue = MobileAdsState.CONSENT_FORM_ERROR
} else {
mobileAdsState.intValue = MobileAdsState.CONSENT_OBTAINED
}
canRequestAds.value = consentInformation.canRequestAds()
isPrivacyOptionsRequired.value =
consentInformation.privacyOptionsRequirementStatus ==
ConsentInformation.PrivacyOptionsRequirementStatus.REQUIRED
if (consentInformation.canRequestAds()) {
initializeMobileAdsSdk(activity)
}
}
},
{ formError ->
// Failure callback.
Log.w(TAG, "Consent info update error: ${formError.errorCode} - ${formError.message}")
mobileAdsState.intValue = MobileAdsState.CONSENT_REQUEST_ERROR
},
)

// This sample attempts to load ads using consent obtained from the previous session.
if (consentInformation.canRequestAds()) {
initializeMobileAdsSdk(activity)
}
}

/** Shows the update consent form. */
fun showPrivacyOptionsForm(activity: Activity) {
UserMessagingPlatform.showPrivacyOptionsForm(activity) { error ->
if (error != null) {
mobileAdsState.intValue = MobileAdsState.CONSENT_FORM_ERROR
}
}
}

/**
* Initializes Mobile Ads SDK.
*
* @param activity Activity responsible for initializing the Google Mobile Ads SDK.
*/
fun initializeMobileAdsSdk(activity: Activity) {
MobileAds.initialize(activity) {
Log.d(TAG, "Mobile Ads SDK initialized")
mobileAdsState.intValue = MobileAdsState.INITIALIZED
}
}

/**
* Opens the Ad Inspector UI.
*
* @param context The application or activity context required for launching the inspector.
* @param onAdInspectorResult A callback to handle the result of opening the Ad Inspector.
*/
fun openAdInspector(context: Context, onAdInspectorResult: (AdError?) -> Unit) {
MobileAds.openAdInspector(context) { error -> onAdInspectorResult(error) }
}

private fun showConsentFormIfRequired(activity: Activity, onFormResult: (FormError?) -> Unit) {
UserMessagingPlatform.loadAndShowConsentFormIfRequired(activity, onFormResult)
}

companion object {
const val TAG = "GoogleMobileAdsSample"
}
}
Loading

0 comments on commit 958085f

Please sign in to comment.