Skip to content

Commit 05920e2

Browse files
JillSongcopybara-github
authored andcommitted
Add Ad Inspector in the settings menu.
PiperOrigin-RevId: 668686900
1 parent 04defc4 commit 05920e2

49 files changed

Lines changed: 255 additions & 104 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

kotlin/admanager/AppOpenExample/app/src/main/java/com/google/android/gms/example/appopenexample/MainActivity.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.widget.PopupMenu
88
import android.widget.Toast
99
import androidx.activity.OnBackPressedCallback
1010
import androidx.appcompat.app.AppCompatActivity
11+
import com.google.android.gms.ads.MobileAds
1112

1213
/** The main activity in the app. */
1314
class MainActivity : AppCompatActivity() {
@@ -32,8 +33,6 @@ class MainActivity : AppCompatActivity() {
3233

3334
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
3435
menuInflater.inflate(R.menu.action_menu, menu)
35-
val moreMenu = menu?.findItem(R.id.action_more)
36-
moreMenu?.isVisible = googleMobileAdsConsentManager.isPrivacyOptionsRequired
3736
return super.onCreateOptionsMenu(menu)
3837
}
3938

@@ -42,6 +41,9 @@ class MainActivity : AppCompatActivity() {
4241
val activity = this
4342
PopupMenu(this, menuItemView).apply {
4443
menuInflater.inflate(R.menu.popup_menu, menu)
44+
menu
45+
.findItem(R.id.privacy_settings)
46+
.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired)
4547
show()
4648
setOnMenuItemClickListener { popupMenuItem ->
4749
when (popupMenuItem.itemId) {
@@ -54,6 +56,13 @@ class MainActivity : AppCompatActivity() {
5456
}
5557
true
5658
}
59+
R.id.ad_inspector -> {
60+
MobileAds.openAdInspector(activity) { error ->
61+
// Error will be non-null if ad inspector closed due to an error.
62+
error?.let { Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show() }
63+
}
64+
true
65+
}
5766
// Handle other branches here.
5867
else -> false
5968
}

kotlin/admanager/AppOpenExample/app/src/main/res/menu/action_menu.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
android:id="@+id/action_more"
66
android:icon="@android:drawable/ic_menu_preferences"
77
android:title="@string/more_menu"
8-
app:showAsAction="always"
9-
android:visible="false"/>
8+
app:showAsAction="always" />
109
</menu>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item
4+
android:id="@+id/ad_inspector"
5+
android:title="@string/ad_inspector" />
36
<item
47
android:id="@+id/privacy_settings"
5-
android:title="@string/privacy_settings" />
8+
android:title="@string/privacy_settings"
9+
android:visible="false" />
610
</menu>

kotlin/admanager/AppOpenExample/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
2+
<string name="ad_inspector" translatable="false">Ad Inspector</string>
23
<string name="app_name" translatable="false">Ad Manager App Open Example</string>
34
<string name="main_activity" translatable="false">Main Activity</string>
45
<string name="main_activity_text" translatable="false">

kotlin/admanager/BannerExample/app/src/main/kotlin/com/google/android/gms/example/bannerexample/MyActivity.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,27 +112,36 @@ class MyActivity : AppCompatActivity() {
112112

113113
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
114114
menuInflater.inflate(R.menu.action_menu, menu)
115-
val moreMenu = menu?.findItem(R.id.action_more)
116-
moreMenu?.isVisible = googleMobileAdsConsentManager.isPrivacyOptionsRequired
117115
return super.onCreateOptionsMenu(menu)
118116
}
119117

120118
override fun onOptionsItemSelected(item: MenuItem): Boolean {
121119
val menuItemView = findViewById<View>(item.itemId)
120+
val activity = this
122121
PopupMenu(this, menuItemView).apply {
123122
menuInflater.inflate(R.menu.popup_menu, menu)
123+
menu
124+
.findItem(R.id.privacy_settings)
125+
.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired)
124126
show()
125127
setOnMenuItemClickListener { popupMenuItem ->
126128
when (popupMenuItem.itemId) {
127129
R.id.privacy_settings -> {
128130
// Handle changes to user consent.
129-
googleMobileAdsConsentManager.showPrivacyOptionsForm(this@MyActivity) { formError ->
131+
googleMobileAdsConsentManager.showPrivacyOptionsForm(activity) { formError ->
130132
if (formError != null) {
131-
Toast.makeText(this@MyActivity, formError.message, Toast.LENGTH_SHORT).show()
133+
Toast.makeText(activity, formError.message, Toast.LENGTH_SHORT).show()
132134
}
133135
}
134136
true
135137
}
138+
R.id.ad_inspector -> {
139+
MobileAds.openAdInspector(activity) { error ->
140+
// Error will be non-null if ad inspector closed due to an error.
141+
error?.let { Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show() }
142+
}
143+
true
144+
}
136145
else -> false
137146
}
138147
}

kotlin/admanager/BannerExample/app/src/main/res/menu/action_menu.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
android:id="@+id/action_more"
66
android:icon="@android:drawable/ic_menu_preferences"
77
android:title="@string/more_menu"
8-
app:showAsAction="always"
9-
android:visible="false"/>
8+
app:showAsAction="always" />
109
</menu>
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<menu xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item
4+
android:id="@+id/ad_inspector"
5+
android:title="@string/ad_inspector" />
36
<item
47
android:id="@+id/privacy_settings"
5-
android:title="@string/privacy_settings" />
8+
android:title="@string/privacy_settings"
9+
android:visible="false" />
610
</menu>

kotlin/admanager/BannerExample/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="action_settings" translatable="false">Settings</string>
4+
<string name="ad_inspector" translatable="false">Ad Inspector</string>
45
<string name="app_name" translatable="false">Ad Manager Banner Example</string>
56
<string name="hello_world" translatable="false">Hello world!</string>
67
<string name="more_menu" translatable="false">More</string>

kotlin/admanager/InterstitialExample/app/src/main/kotlin/com/google/android/gms/example/interstitialexample/MyActivity.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ class MyActivity : AppCompatActivity() {
9090

9191
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
9292
menuInflater.inflate(R.menu.action_menu, menu)
93-
menu?.findItem(R.id.action_more)?.apply {
94-
isVisible = googleMobileAdsConsentManager.isPrivacyOptionsRequired
95-
}
9693
return super.onCreateOptionsMenu(menu)
9794
}
9895

@@ -101,6 +98,9 @@ class MyActivity : AppCompatActivity() {
10198
val activity = this
10299
PopupMenu(this, menuItemView).apply {
103100
menuInflater.inflate(R.menu.popup_menu, menu)
101+
menu
102+
.findItem(R.id.privacy_settings)
103+
.setVisible(googleMobileAdsConsentManager.isPrivacyOptionsRequired)
104104
show()
105105
setOnMenuItemClickListener { popupMenuItem ->
106106
when (popupMenuItem.itemId) {
@@ -115,6 +115,13 @@ class MyActivity : AppCompatActivity() {
115115
}
116116
true
117117
}
118+
R.id.ad_inspector -> {
119+
MobileAds.openAdInspector(activity) { error ->
120+
// Error will be non-null if ad inspector closed due to an error.
121+
error?.let { Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show() }
122+
}
123+
true
124+
}
118125
// Handle other branches here.
119126
else -> false
120127
}

kotlin/admanager/InterstitialExample/app/src/main/res/menu/action_menu.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
android:id="@+id/action_more"
66
android:icon="@android:drawable/ic_menu_preferences"
77
android:title="@string/more_menu"
8-
app:showAsAction="always"
9-
android:visible="false"/>
8+
app:showAsAction="always" />
109
</menu>

0 commit comments

Comments
 (0)