Skip to content

Commit b19f4c8

Browse files
authored
refactor(admob): use Jetpack's Navigation Component (#1247)
1 parent 256c7e1 commit b19f4c8

File tree

16 files changed

+424
-425
lines changed

16 files changed

+424
-425
lines changed

admob/app/build.gradle

+5-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ android {
2828
}
2929

3030
buildFeatures {
31-
viewBinding = true
32-
}
31+
viewBinding = true
32+
}
3333
}
3434

3535
dependencies {
@@ -38,10 +38,10 @@ dependencies {
3838
implementation 'androidx.appcompat:appcompat:1.2.0'
3939
implementation 'com.google.android.material:material:1.2.1'
4040
implementation 'androidx.browser:browser:1.0.0'
41+
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2'
42+
implementation 'androidx.navigation:navigation-ui-ktx:2.3.2'
4143

42-
// [START gradle_play_config]
4344
implementation 'com.google.android.gms:play-services-ads:19.6.0'
44-
// [END gradle_play_config]
4545

4646
// Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom)
4747
implementation platform('com.google.firebase:firebase-bom:26.1.1')
@@ -50,6 +50,7 @@ dependencies {
5050
// for Google Analytics. This is recommended, but not required.
5151
implementation 'com.google.firebase:firebase-analytics'
5252

53+
debugImplementation "androidx.fragment:fragment-testing:1.2.5"
5354
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
5455
androidTestImplementation 'androidx.test:rules:1.3.0'
5556
androidTestImplementation 'androidx.test:runner:1.3.0'
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package com.google.samples.quickstart.admobexample;
22

33

4-
import androidx.test.espresso.IdlingRegistry;
4+
import androidx.fragment.app.testing.FragmentScenario;
55
import androidx.test.espresso.ViewInteraction;
66
import androidx.test.ext.junit.runners.AndroidJUnit4;
7-
import androidx.test.rule.ActivityTestRule;
87
import androidx.test.filters.LargeTest;
98

10-
import com.google.samples.quickstart.admobexample.java.MainActivity;
9+
import com.google.samples.quickstart.admobexample.kotlin.FirstFragment;
1110

12-
import org.junit.After;
13-
import org.junit.Before;
14-
import org.junit.Rule;
1511
import org.junit.Test;
1612
import org.junit.runner.RunWith;
1713

@@ -28,26 +24,9 @@
2824
@RunWith(AndroidJUnit4.class)
2925
public class InterstitialAdTest {
3026

31-
private AdViewIdlingResource mAdResource;
32-
33-
@Rule
34-
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
35-
36-
@Before
37-
public void setUp() {
38-
mAdResource = new AdViewIdlingResource(mActivityTestRule.getActivity().getAdView());
39-
IdlingRegistry.getInstance().register(mAdResource);
40-
}
41-
42-
@After
43-
public void tearDown() {
44-
IdlingRegistry.getInstance().unregister(mAdResource);
45-
}
46-
4727
@Test
4828
public void interstitialAdTest() {
49-
// Wait for ad to load
50-
mAdResource.setIsLoadingAd(true);
29+
FragmentScenario<FirstFragment> fragment = FragmentScenario.launchInContainer(FirstFragment.class);
5130

5231
// Confirm that banner ad appears
5332
onView(withId(R.id.adView))
@@ -66,7 +45,7 @@ public void interstitialAdTest() {
6645
closeInterstitialButton.perform(click());
6746

6847
// Confirm that we're on the second activity
69-
onView(withText(R.string.second_activity_content))
48+
onView(withText(R.string.second_fragment_content))
7049
.check(matches(isDisplayed()));
7150
}
7251
}

admob/app/src/main/AndroidManifest.xml

+1-17
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
</activity>
3636

3737
<activity android:name=".java.MainActivity" />
38+
<activity android:name=".kotlin.MainActivity" />
3839
<!-- [SNIPPET add_activity_config_changes]
3940
Include the AdActivity configChanges and theme.
4041
[START add_activity_config_changes] -->
@@ -43,23 +44,6 @@
4344
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
4445
android:theme="@android:style/Theme.Translucent" />
4546
<!-- [END add_activity_config_changes] -->
46-
<activity
47-
android:name=".java.SecondActivity"
48-
android:label="@string/second_activity_title"
49-
android:parentActivityName=".java.MainActivity">
50-
<meta-data
51-
android:name="android.support.PARENT_ACTIVITY"
52-
android:value=".java.MainActivity" />
53-
</activity>
54-
<activity android:name=".kotlin.MainActivity" />
55-
<activity
56-
android:name=".kotlin.SecondActivity"
57-
android:label="@string/second_activity_title"
58-
android:parentActivityName=".java.MainActivity">
59-
<meta-data
60-
android:name="android.support.PARENT_ACTIVITY"
61-
android:value=".java.MainActivity" />
62-
</activity>
6347
</application>
6448

6549
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.google.samples.quickstart.admobexample.java;
2+
3+
import android.os.Bundle;
4+
import android.util.Log;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.Button;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import androidx.annotation.VisibleForTesting;
13+
import androidx.fragment.app.Fragment;
14+
import androidx.navigation.fragment.NavHostFragment;
15+
16+
import com.google.android.gms.ads.AdListener;
17+
import com.google.android.gms.ads.AdRequest;
18+
import com.google.android.gms.ads.AdView;
19+
import com.google.android.gms.ads.InterstitialAd;
20+
import com.google.android.gms.ads.LoadAdError;
21+
import com.google.android.gms.ads.MobileAds;
22+
import com.google.samples.quickstart.admobexample.R;
23+
import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding;
24+
25+
class FirstFragment extends Fragment {
26+
27+
private static final String TAG = "MainActivity";
28+
private static final String TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713";
29+
30+
private AdView mAdView;
31+
private InterstitialAd mInterstitialAd;
32+
private Button mLoadInterstitialButton;
33+
private FragmentFirstBinding binding;
34+
35+
@Nullable
36+
@Override
37+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
38+
binding = FragmentFirstBinding.inflate(inflater, container, false);
39+
return binding.getRoot();
40+
}
41+
42+
@Override
43+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
44+
super.onViewCreated(view, savedInstanceState);
45+
checkIds();
46+
47+
// Initialize the Google Mobile Ads SDK
48+
MobileAds.initialize(getContext());
49+
50+
mAdView = binding.adView;
51+
AdRequest adRequest = new AdRequest.Builder().build();
52+
mAdView.loadAd(adRequest);
53+
54+
// AdMob ad unit IDs are not currently stored inside the google-services.json file.
55+
// Developers using AdMob can store them as custom values in a string resource file or
56+
// simply use constants. Note that the ad units used here are configured to return only test
57+
// ads, and should not be used outside this sample.
58+
59+
// Create an InterstitialAd object. This same object can be re-used whenever you want to
60+
// show an interstitial.
61+
mInterstitialAd = new InterstitialAd(getContext());
62+
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
63+
64+
mInterstitialAd.setAdListener(new AdListener() {
65+
@Override
66+
public void onAdClosed() {
67+
requestNewInterstitial();
68+
beginSecondActivity();
69+
}
70+
71+
@Override
72+
public void onAdLoaded() {
73+
// Ad received, ready to display
74+
if (mLoadInterstitialButton != null) {
75+
mLoadInterstitialButton.setEnabled(true);
76+
}
77+
}
78+
79+
@Override
80+
public void onAdFailedToLoad(LoadAdError error) {
81+
Log.w(TAG, "onAdFailedToLoad:" + error.getMessage());
82+
}
83+
});
84+
85+
mLoadInterstitialButton = binding.loadInterstitialButton;
86+
mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
87+
@Override
88+
public void onClick(View v) {
89+
if (mInterstitialAd.isLoaded()) {
90+
mInterstitialAd.show();
91+
} else {
92+
beginSecondActivity();
93+
}
94+
}
95+
});
96+
97+
// Disable button if an interstitial ad is not loaded yet.
98+
mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded());
99+
}
100+
101+
/**
102+
* Load a new interstitial ad asynchronously.
103+
*/
104+
private void requestNewInterstitial() {
105+
AdRequest adRequest = new AdRequest.Builder().build();
106+
107+
mInterstitialAd.loadAd(adRequest);
108+
}
109+
110+
private void beginSecondActivity() {
111+
NavHostFragment.findNavController(this).navigate(R.id.action_FirstFragment_to_SecondFragment);
112+
}
113+
114+
/** Called when leaving the activity */
115+
@Override
116+
public void onPause() {
117+
if (mAdView != null) {
118+
mAdView.pause();
119+
}
120+
super.onPause();
121+
}
122+
123+
/** Called when returning to the activity */
124+
@Override
125+
public void onResume() {
126+
super.onResume();
127+
if (mAdView != null) {
128+
mAdView.resume();
129+
}
130+
if (!mInterstitialAd.isLoaded()) {
131+
requestNewInterstitial();
132+
}
133+
}
134+
135+
/** Called before the activity is destroyed */
136+
@Override
137+
public void onDestroy() {
138+
if (mAdView != null) {
139+
mAdView.destroy();
140+
}
141+
super.onDestroy();
142+
}
143+
144+
@VisibleForTesting
145+
public AdView getAdView() {
146+
return mAdView;
147+
}
148+
149+
private void checkIds() {
150+
if (TEST_APP_ID.equals(getString(R.string.admob_app_id))) {
151+
Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README");
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)