Skip to content

refactor(admob): use Jetpack's Navigation Component #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions admob/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
}

buildFeatures {
viewBinding = true
}
viewBinding = true
}
}

dependencies {
Expand All @@ -38,10 +38,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.2'

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

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

debugImplementation "androidx.fragment:fragment-testing:1.2.5"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package com.google.samples.quickstart.admobexample;


import androidx.test.espresso.IdlingRegistry;
import androidx.fragment.app.testing.FragmentScenario;
import androidx.test.espresso.ViewInteraction;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;
import androidx.test.filters.LargeTest;

import com.google.samples.quickstart.admobexample.java.MainActivity;
import com.google.samples.quickstart.admobexample.kotlin.FirstFragment;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -28,26 +24,9 @@
@RunWith(AndroidJUnit4.class)
public class InterstitialAdTest {

private AdViewIdlingResource mAdResource;

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Before
public void setUp() {
mAdResource = new AdViewIdlingResource(mActivityTestRule.getActivity().getAdView());
IdlingRegistry.getInstance().register(mAdResource);
}

@After
public void tearDown() {
IdlingRegistry.getInstance().unregister(mAdResource);
}

@Test
public void interstitialAdTest() {
// Wait for ad to load
mAdResource.setIsLoadingAd(true);
FragmentScenario<FirstFragment> fragment = FragmentScenario.launchInContainer(FirstFragment.class);

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

// Confirm that we're on the second activity
onView(withText(R.string.second_activity_content))
onView(withText(R.string.second_fragment_content))
.check(matches(isDisplayed()));
}
}
18 changes: 1 addition & 17 deletions admob/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
</activity>

<activity android:name=".java.MainActivity" />
<activity android:name=".kotlin.MainActivity" />
<!-- [SNIPPET add_activity_config_changes]
Include the AdActivity configChanges and theme.
[START add_activity_config_changes] -->
Expand All @@ -43,23 +44,6 @@
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<!-- [END add_activity_config_changes] -->
<activity
android:name=".java.SecondActivity"
android:label="@string/second_activity_title"
android:parentActivityName=".java.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".java.MainActivity" />
</activity>
<activity android:name=".kotlin.MainActivity" />
<activity
android:name=".kotlin.SecondActivity"
android:label="@string/second_activity_title"
android:parentActivityName=".java.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".java.MainActivity" />
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.google.samples.quickstart.admobexample.java;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.samples.quickstart.admobexample.R;
import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding;

class FirstFragment extends Fragment {

private static final String TAG = "MainActivity";
private static final String TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713";

private AdView mAdView;
private InterstitialAd mInterstitialAd;
private Button mLoadInterstitialButton;
private FragmentFirstBinding binding;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FragmentFirstBinding.inflate(inflater, container, false);
return binding.getRoot();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
checkIds();

// Initialize the Google Mobile Ads SDK
MobileAds.initialize(getContext());

mAdView = binding.adView;
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// AdMob ad unit IDs are not currently stored inside the google-services.json file.
// Developers using AdMob can store them as custom values in a string resource file or
// simply use constants. Note that the ad units used here are configured to return only test
// ads, and should not be used outside this sample.

// Create an InterstitialAd object. This same object can be re-used whenever you want to
// show an interstitial.
mInterstitialAd = new InterstitialAd(getContext());
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
beginSecondActivity();
}

@Override
public void onAdLoaded() {
// Ad received, ready to display
if (mLoadInterstitialButton != null) {
mLoadInterstitialButton.setEnabled(true);
}
}

@Override
public void onAdFailedToLoad(LoadAdError error) {
Log.w(TAG, "onAdFailedToLoad:" + error.getMessage());
}
});

mLoadInterstitialButton = binding.loadInterstitialButton;
mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginSecondActivity();
}
}
});

// Disable button if an interstitial ad is not loaded yet.
mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded());
}

/**
* Load a new interstitial ad asynchronously.
*/
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();

mInterstitialAd.loadAd(adRequest);
}

private void beginSecondActivity() {
NavHostFragment.findNavController(this).navigate(R.id.action_FirstFragment_to_SecondFragment);
}

/** Called when leaving the activity */
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}

/** Called when returning to the activity */
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
if (!mInterstitialAd.isLoaded()) {
requestNewInterstitial();
}
}

/** Called before the activity is destroyed */
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}

@VisibleForTesting
public AdView getAdView() {
return mAdView;
}

private void checkIds() {
if (TEST_APP_ID.equals(getString(R.string.admob_app_id))) {
Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README");
}
}
}
Loading