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 6b41b6a
Show file tree
Hide file tree
Showing 35 changed files with 1,468 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,144 @@
/*
* Copyright 2024 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.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)
}
// Render the 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"
}
}
Loading

0 comments on commit 6b41b6a

Please sign in to comment.