Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion packages/google_mobile_ads/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ android {

defaultConfig {
minSdkVersion 19
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
Expand All @@ -36,7 +35,13 @@ android {
testImplementation 'junit:junit:4.12'
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'org.mockito:mockito-inline:3.9.0'
testImplementation 'org.robolectric:robolectric:4.4'
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package io.flutter.plugins.googlemobileads;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.ResponseInfo;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMethodCodec;
import io.flutter.plugins.googlemobileads.FlutterAd.FlutterAdError;
import io.flutter.plugins.googlemobileads.FlutterAd.FlutterResponseInfo;
import java.util.HashMap;
Expand All @@ -35,23 +35,29 @@
* provide access until the ad is disposed.
*/
class AdInstanceManager {
@NonNull Activity activity;
@Nullable private Activity activity;

@NonNull private final Map<Integer, FlutterAd> ads;
@NonNull private final MethodChannel channel;

AdInstanceManager(@NonNull Activity activity, @NonNull BinaryMessenger binaryMessenger) {
this.activity = activity;
/**
* Initializes the ad instance manager. We only need a method channel to start loading ads, but an
* activity must be present in order to attach any to the view hierarchy.
*/
AdInstanceManager(@NonNull MethodChannel channel) {
this.channel = channel;
this.ads = new HashMap<>();
final StandardMethodCodec methodCodec = new StandardMethodCodec(new AdMessageCodec(activity));
this.channel =
new MethodChannel(binaryMessenger, "plugins.flutter.io/google_mobile_ads", methodCodec);
}

void setActivity(@NonNull Activity activity) {
void setActivity(@Nullable Activity activity) {
this.activity = activity;
}

@Nullable
Activity getActivity() {
return activity;
}

@Nullable
FlutterAd adForId(int id) {
return ads.get(id);
Expand Down Expand Up @@ -210,12 +216,13 @@ boolean showAdWithId(int id) {

/** Invoke the method channel using the UI thread. Otherwise the message gets silently dropped. */
private void invokeOnAdEvent(final Map<Object, Object> arguments) {
activity.runOnUiThread(
new Runnable() {
@Override
public void run() {
channel.invokeMethod("onAdEvent", arguments);
}
});
new Handler(Looper.getMainLooper())
.post(
new Runnable() {
@Override
public void run() {
channel.invokeMethod("onAdEvent", arguments);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AdMessageCodec extends StandardMessageCodec {
static final byte VALUE_NATIVE_AD_OPTIONS = (byte) 144;
static final byte VALUE_VIDEO_OPTIONS = (byte) 145;

@NonNull final Context context;
@NonNull Context context;
@NonNull final FlutterAdSize.AdSizeFactory adSizeFactory;

AdMessageCodec(@NonNull Context context) {
Expand All @@ -64,6 +64,10 @@ class AdMessageCodec extends StandardMessageCodec {
this.adSizeFactory = adSizeFactory;
}

void setContext(@NonNull Context context) {
this.context = context;
}

@Override
protected void writeValue(ByteArrayOutputStream stream, Object value) {
if (value instanceof FlutterAdSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
*/
public class FlutterAdLoader {

public FlutterAdLoader() {}
@NonNull private final Context context;

public FlutterAdLoader(@NonNull Context context) {
this.context = context;
}

/** Load an app open ad. */
public void loadAppOpen(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdRequest adRequest,
int orientation,
Expand All @@ -50,7 +53,6 @@ public void loadAppOpen(

/** Load an ad manager app open ad. */
public void loadAdManagerAppOpen(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdManagerAdRequest adRequest,
int orientation,
Expand All @@ -60,7 +62,6 @@ public void loadAdManagerAppOpen(

/** Load an interstitial ad. */
public void loadInterstitial(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdRequest adRequest,
@NonNull InterstitialAdLoadCallback loadCallback) {
Expand All @@ -69,7 +70,6 @@ public void loadInterstitial(

/** Load an ad manager interstitial ad. */
public void loadAdManagerInterstitial(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdManagerAdRequest adRequest,
@NonNull AdManagerInterstitialAdLoadCallback loadCallback) {
Expand All @@ -78,7 +78,6 @@ public void loadAdManagerInterstitial(

/** Load a rewarded ad. */
public void loadRewarded(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdRequest adRequest,
@NonNull RewardedAdLoadCallback loadCallback) {
Expand All @@ -87,7 +86,6 @@ public void loadRewarded(

/** Load an ad manager rewarded ad. */
public void loadAdManagerRewarded(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull AdManagerAdRequest adRequest,
@NonNull RewardedAdLoadCallback loadCallback) {
Expand All @@ -96,7 +94,6 @@ public void loadAdManagerRewarded(

/** Load a native ad. */
public void loadNativeAd(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull OnNativeAdLoadedListener onNativeAdLoadedListener,
@NonNull NativeAdOptions nativeAdOptions,
Expand All @@ -112,7 +109,6 @@ public void loadNativeAd(

/** Load an ad manager native ad. */
public void loadAdManagerNativeAd(
@NonNull Context context,
@NonNull String adUnitId,
@NonNull OnNativeAdLoadedListener onNativeAdLoadedListener,
@NonNull NativeAdOptions nativeAdOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public FlutterAdManagerInterstitialAd(
@Override
void load() {
flutterAdLoader.loadAdManagerInterstitial(
manager.activity,
adUnitId,
request.asAdManagerAdRequest(),
new DelegatingAdManagerInterstitialAdCallbacks(this));
Expand All @@ -85,8 +84,12 @@ public void show() {
Log.e(TAG, "The interstitial wasn't loaded yet.");
return;
}
if (manager.getActivity() == null) {
Log.e(TAG, "Tried to show interstitial before activity was bound to the plugin.");
return;
}
ad.setFullScreenContentCallback(new FlutterFullScreenContentCallback(manager, adId));
ad.show(manager.activity);
ad.show(manager.getActivity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ class FlutterAppOpenAd extends FlutterAd.FlutterOverlayAd {
void load() {
if (request != null) {
flutterAdLoader.loadAppOpen(
manager.activity,
adUnitId,
request.asAdRequest(),
getOrientation(),
new DelegatingAppOpenAdLoadCallback(this));
} else if (adManagerAdRequest != null) {
flutterAdLoader.loadAdManagerAppOpen(
manager.activity,
adUnitId,
adManagerAdRequest.asAdManagerAdRequest(),
getOrientation(),
Expand Down Expand Up @@ -101,9 +99,12 @@ void show() {
Log.w(TAG, "Tried to show app open ad before it was loaded");
return;
}

if (manager.getActivity() == null) {
Log.e(TAG, "Tried to show app open ad before activity was bound to the plugin.");
return;
}
ad.setFullScreenContentCallback(new FlutterFullScreenContentCallback(manager, adId));
ad.show(manager.activity);
ad.show(manager.getActivity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public FlutterInterstitialAd(
void load() {
if (manager != null && adUnitId != null && request != null) {
flutterAdLoader.loadInterstitial(
manager.activity,
adUnitId,
request.asAdRequest(),
new DelegatingInterstitialAdLoadCallback(this));
adUnitId, request.asAdRequest(), new DelegatingInterstitialAdLoadCallback(this));
}
}

Expand All @@ -76,8 +73,12 @@ public void show() {
Log.e(TAG, "Error showing interstitial - the interstitial ad wasn't loaded yet.");
return;
}
if (manager.getActivity() == null) {
Log.e(TAG, "Tried to show interstitial before activity was bound to the plugin.");
return;
}
ad.setFullScreenContentCallback(new FlutterFullScreenContentCallback(manager, adId));
ad.show(manager.activity);
ad.show(manager.getActivity());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static class Builder {
@Nullable private Map<String, Object> customOptions;
@Nullable private Integer id;
@Nullable private FlutterNativeAdOptions nativeAdOptions;
@Nullable private FlutterAdLoader flutterAdLoader;

public Builder setAdFactory(@NonNull NativeAdFactory adFactory) {
this.adFactory = adFactory;
Expand Down Expand Up @@ -90,6 +91,11 @@ public Builder setNativeAdOptions(@Nullable FlutterNativeAdOptions nativeAdOptio
return this;
}

public Builder setFlutterAdLoader(@NonNull FlutterAdLoader flutterAdLoader) {
this.flutterAdLoader = flutterAdLoader;
return this;
}

FlutterNativeAd build() {
if (manager == null) {
throw new IllegalStateException("AdInstanceManager cannot not be null.");
Expand All @@ -110,7 +116,7 @@ FlutterNativeAd build() {
adUnitId,
adFactory,
adManagerRequest,
new FlutterAdLoader(),
flutterAdLoader,
customOptions,
nativeAdOptions);
} else {
Expand All @@ -121,7 +127,7 @@ FlutterNativeAd build() {
adUnitId,
adFactory,
request,
new FlutterAdLoader(),
flutterAdLoader,
customOptions,
nativeAdOptions);
}
Expand Down Expand Up @@ -179,15 +185,10 @@ void load() {
: nativeAdOptions.asNativeAdOptions();
if (request != null) {
flutterAdLoader.loadNativeAd(
manager.activity, adUnitId, loadedListener, options, adListener, request.asAdRequest());
adUnitId, loadedListener, options, adListener, request.asAdRequest());
} else if (adManagerRequest != null) {
flutterAdLoader.loadAdManagerNativeAd(
manager.activity,
adUnitId,
loadedListener,
options,
adListener,
adManagerRequest.asAdManagerAdRequest());
adUnitId, loadedListener, options, adListener, adManagerRequest.asAdManagerAdRequest());
} else {
Log.e(TAG, "A null or invalid ad request was provided.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,10 @@ public FlutterRewardedAd(
void load() {
final RewardedAdLoadCallback adLoadCallback = new DelegatingRewardedCallback(this);
if (request != null) {
flutterAdLoader.loadRewarded(
manager.activity, adUnitId, request.asAdRequest(), adLoadCallback);
flutterAdLoader.loadRewarded(adUnitId, request.asAdRequest(), adLoadCallback);
} else if (adManagerRequest != null) {
flutterAdLoader.loadAdManagerRewarded(
manager.activity, adUnitId, adManagerRequest.asAdManagerAdRequest(), adLoadCallback);
adUnitId, adManagerRequest.asAdManagerAdRequest(), adLoadCallback);
} else {
Log.e(TAG, "A null or invalid ad request was provided.");
}
Expand All @@ -138,10 +137,13 @@ public void show() {
Log.e(TAG, "Error showing rewarded - the rewarded ad wasn't loaded yet.");
return;
}

if (manager.getActivity() == null) {
Log.e(TAG, "Tried to show rewarded ad before activity was bound to the plugin.");
return;
}
rewardedAd.setFullScreenContentCallback(new FlutterFullScreenContentCallback(manager, adId));
rewardedAd.setOnAdMetadataChangedListener(new DelegatingRewardedCallback(this));
rewardedAd.show(manager.activity, new DelegatingRewardedCallback(this));
rewardedAd.show(manager.getActivity(), new DelegatingRewardedCallback(this));
}

@Override
Expand Down
Loading