Skip to content

Commit

Permalink
Fixing inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
LTPhantom committed Dec 7, 2023
1 parent 59f9014 commit 81623cf
Show file tree
Hide file tree
Showing 9 changed files with 5 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,6 @@ void onFluidAdHeightChanged(int adId, int height) {
invokeOnAdEvent(arguments);
}

void onPlatformViewSizeChanged(int adId, int width, int height) {
final Map<Object, Object> arguments = new HashMap<>();
arguments.put("adId", adId);
arguments.put("eventName", "onPlatformViewSizeChanged");
arguments.put("width", width);
arguments.put("height", height);
invokeOnAdEvent(arguments);
}

boolean showAdWithId(int id) {
final FlutterAd.FlutterOverlayAd ad = (FlutterAd.FlutterOverlayAd) adForId(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package io.flutter.plugins.googlemobileads;

import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.ads.AdError;
Expand Down Expand Up @@ -387,13 +386,3 @@ PlatformView getPlatformView() {
*/
abstract void dispose();
}

abstract class FlutterAdWithPlatformView extends FlutterAd {

FlutterAdWithPlatformView(int adId) {
super(adId);
}

@Nullable
abstract View getView();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Wrapper around {@link com.google.android.gms.ads.admanager.AdManagerAdView} for the Google Mobile
* Ads Plugin.
*/
class FlutterAdManagerBannerAd extends FlutterAdWithPlatformView implements FlutterAdLoadedListener {
class FlutterAdManagerBannerAd extends FlutterAd implements FlutterAdLoadedListener {

@NonNull protected final AdInstanceManager manager;
@NonNull private final String adUnitId;
Expand Down Expand Up @@ -121,10 +121,4 @@ FlutterAdSize getAdSize() {
}
return new FlutterAdSize(adView.getAdSize());
}

@Nullable
@Override
View getView() {
return adView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.flutter.util.Preconditions;

/** A wrapper for {@link AdView}. */
class FlutterBannerAd extends FlutterAdWithPlatformView implements FlutterAdLoadedListener {
class FlutterBannerAd extends FlutterAd implements FlutterAdLoadedListener {

@NonNull private final AdInstanceManager manager;
@NonNull private final String adUnitId;
Expand Down Expand Up @@ -63,9 +63,6 @@ public void onAdLoaded() {
@Override
void load() {
adView = bannerAdCreator.createAdView();
RelativeLayout.LayoutParams params = new RelativeLayout
.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adView.setLayoutParams(params);
adView.setAdUnitId(adUnitId);
adView.setAdSize(size.getAdSize());
adView.setOnPaidEventListener(new FlutterPaidEventListener(manager, this));
Expand Down Expand Up @@ -97,10 +94,4 @@ FlutterAdSize getAdSize() {
}
return new FlutterAdSize(adView.getAdSize());
}

@Nullable
@Override
View getView() {
return adView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@

package io.flutter.plugins.googlemobileads;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.platform.PlatformView;
Expand All @@ -43,52 +38,3 @@ public void dispose() {
this.view = null;
}
}

interface AdViewSizeChangeListener {
void onSizeChanged(int width, int height);
}

class AutoSizingPlatformViewContainer extends HorizontalScrollView {

private final AdViewSizeChangeListener listener;
private Integer width;
private Integer height;

public AutoSizingPlatformViewContainer(
Context context,
View childView,
final AdViewSizeChangeListener listener) {
super(context);
this.listener = listener;
ScrollView scrollView = new ScrollView(context);
scrollView.setClipChildren(false);
scrollView.setVerticalScrollBarEnabled(false);
scrollView.setHorizontalScrollBarEnabled(false);
scrollView.addView(childView);
addView(scrollView);
childView.addOnLayoutChangeListener(
new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
// Forward the new height to its container.
int newHeight = v.getMeasuredHeight();
int newWidth = v.getMeasuredWidth();
if (width == null || height == null || newHeight != height || newWidth != width) {
Log.d("FLUTTER", "newWidth = " + newWidth + " newHeight = " + newHeight);
listener.onSizeChanged(newWidth, newHeight);
}
width = newWidth;
height = newHeight;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public PlatformView create(Context context, int viewId, Object args) {
if (ad == null || ad.getPlatformView() == null) {
return getErrorView(context, adId);
}
return getErrorView(context, viewId);
return ad.getPlatformView();
}

/**
Expand Down
7 changes: 0 additions & 7 deletions packages/google_mobile_ads/lib/src/ad_containers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -883,13 +883,6 @@ class _AutoSizingAdWidgetState extends State<AutoSizingAdWidget> {
'Parameter ad is not loaded. Call Ad.load before AdWidget is inserted into the tree.'),
]);
}

instanceManager.registerPlatformViewSizeChangeListener(widget.ad, (width, height) {
setState(() {
_width = width;
_height = height;
});
});

Widget platformView;
double height;
Expand Down
27 changes: 1 addition & 26 deletions packages/google_mobile_ads/lib/src/ad_instance_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ class AdInstanceManager {

int _nextAdId = 0;
final _BiMap<int, Ad> _loadedAds = _BiMap<int, Ad>();
final Map<int, OnPlatformViewSizeChangeListener> _onPlatformViewSizeChangeListenerMap
= <int, OnPlatformViewSizeChangeListener>{};

/// Invokes load and dispose calls.
final MethodChannel channel;
Expand Down Expand Up @@ -158,9 +156,6 @@ class AdInstanceManager {
case 'onFluidAdHeightChanged':
_invokeFluidAdHeightChanged(ad, arguments);
break;
case 'onPlatformViewSizeChanged':
_invokePlatformViewSizeChangeListener(ad, arguments);
break;
default:
debugPrint('invalid ad event name: $eventName');
}
Expand Down Expand Up @@ -209,9 +204,6 @@ class AdInstanceManager {
case 'onAdClicked':
_invokeOnAdClicked(ad, eventName);
break;
case 'onPlatformViewSizeChanged':
_invokePlatformViewSizeChangeListener(ad, arguments);
break;
default:
debugPrint('invalid ad event name: $eventName');
}
Expand Down Expand Up @@ -792,22 +784,6 @@ class AdInstanceManager {
);
}

void registerPlatformViewSizeChangeListener(Ad ad, OnPlatformViewSizeChangeListener listener) {
int? adId = adIdFor(ad);
if (adId != null) {
_onPlatformViewSizeChangeListenerMap[adId] = listener;
} else {
debugPrint('You must load $ad before trying to render it');
}
}

void _invokePlatformViewSizeChangeListener(Ad ad, Map<dynamic, dynamic> arguments) {
int? adId = adIdFor(ad);
if (adId != null) {
_onPlatformViewSizeChangeListenerMap[adId]?.call(arguments['width'].toDouble(), arguments['height'].toDouble());
}
}

/// Opens the debug menu.
///
/// Returns a Future that completes when the platform side api has been
Expand Down Expand Up @@ -1410,5 +1386,4 @@ class _BiMap<K extends Object, V extends Object> extends MapBase<K, V> {
inverse._map.remove(value);
return _map.remove(key);
}
}

}
7 changes: 1 addition & 6 deletions packages/google_mobile_ads/lib/src/ad_listeners.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ typedef OnPaidEventCallback = void Function(
typedef OnFluidAdHeightChangedListener = void Function(
FluidAdManagerBannerAd ad, double height);

/// The callback type for when autosizing ad's dimensions change.
typedef OnPlatformViewSizeChangeListener = void Function(
double width, double height);

/// Allowed constants for precision type in [OnPaidEventCallback].
enum PrecisionType {
/// An ad value with unknown precision.
Expand Down Expand Up @@ -320,5 +316,4 @@ class RewardedInterstitialAdLoadCallback
required GenericAdEventCallback<RewardedInterstitialAd> onAdLoaded,
required FullScreenAdLoadErrorCallback onAdFailedToLoad,
}) : super(onAdLoaded: onAdLoaded, onAdFailedToLoad: onAdFailedToLoad);
}

}

0 comments on commit 81623cf

Please sign in to comment.