Skip to content

Commit

Permalink
Merge pull request #128 from anil-shrestha/master
Browse files Browse the repository at this point in the history
migration to support Android V2 embedding
  • Loading branch information
SachinGanesh authored Dec 30, 2021
2 parents c2464d3 + 070edc7 commit 22bf678
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,68 +1,60 @@
package com.dsi.facebook_audience_network;

import android.app.Activity;

import android.content.Context;
import com.facebook.ads.*;

import androidx.annotation.NonNull;
import java.util.HashMap;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;

/**
* FacebookAudienceNetworkPlugin
*/
public class FacebookAudienceNetworkPlugin implements MethodCallHandler {

private final Activity mActivity;
public class FacebookAudienceNetworkPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

private FacebookAudienceNetworkPlugin(Activity activity) {
this.mActivity = activity;
}

/**
* Plugin registration.
*/
public static void registerWith(PluginRegistry.Registrar registrar) {
private MethodChannel channel, interstitialAdChannel, rewardedAdChannel;
private Activity _activity;
private Context _context;

// Main channel for initialization
final MethodChannel channel = new MethodChannel(registrar.messenger(),
FacebookConstants.MAIN_CHANNEL);
channel.setMethodCallHandler(new FacebookAudienceNetworkPlugin(registrar.activity()));
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), FacebookConstants.MAIN_CHANNEL);
channel.setMethodCallHandler(this);
_context = flutterPluginBinding.getApplicationContext();

// Interstitial Ad channel
final MethodChannel interstitialAdChannel = new MethodChannel(registrar.messenger(),
interstitialAdChannel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(),
FacebookConstants.INTERSTITIAL_AD_CHANNEL);
interstitialAdChannel
.setMethodCallHandler(new FacebookInterstitialAdPlugin(registrar.context(),
.setMethodCallHandler(new FacebookInterstitialAdPlugin(_context,
interstitialAdChannel));

// Rewarded video Ad channel
final MethodChannel rewardedAdChannel = new MethodChannel(registrar.messenger(),
rewardedAdChannel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(),
FacebookConstants.REWARDED_VIDEO_CHANNEL);
rewardedAdChannel
.setMethodCallHandler(new FacebookRewardedVideoAdPlugin(registrar.context(),
.setMethodCallHandler(new FacebookRewardedVideoAdPlugin(_context,
rewardedAdChannel));

// Banner Ad PlatformView channel
registrar.
platformViewRegistry().
flutterPluginBinding.
getPlatformViewRegistry().
registerViewFactory(FacebookConstants.BANNER_AD_CHANNEL,
new FacebookBannerAdPlugin(registrar.messenger()));

// Native Ad PlatformView channel
registrar.
platformViewRegistry().
new FacebookBannerAdPlugin(flutterPluginBinding.getBinaryMessenger()));
flutterPluginBinding.
getPlatformViewRegistry().
registerViewFactory(FacebookConstants.NATIVE_AD_CHANNEL,
new FacebookNativeAdPlugin(registrar.messenger()));

new FacebookNativeAdPlugin(flutterPluginBinding.getBinaryMessenger()));
}

@Override
public void onMethodCall(MethodCall call, Result result) {
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

if (call.method.equals(FacebookConstants.INIT_METHOD))
result.success(init((HashMap) call.arguments));
Expand All @@ -73,13 +65,36 @@ public void onMethodCall(MethodCall call, Result result) {
private boolean init(HashMap initValues) {
final String testingId = (String) initValues.get("testingId");

AudienceNetworkAds.initialize(mActivity.getApplicationContext());
AudienceNetworkAds.initialize(_activity.getApplicationContext());

if (testingId != null) {
AdSettings.addTestDevice(testingId);
}
return true;
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
interstitialAdChannel.setMethodCallHandler(null);
rewardedAdChannel.setMethodCallHandler(null);
}

@Override
public void onDetachedFromActivityForConfigChanges() {
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
_activity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
}
@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
onAttachedToActivity(binding);
}

}

18 changes: 9 additions & 9 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -56,7 +56,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0-nullsafety.0"
version: "1.0.1"
fake_async:
dependency: transitive
description:
Expand All @@ -80,14 +80,14 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -141,7 +141,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.3"
typed_data:
dependency: transitive
description:
Expand All @@ -155,7 +155,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=1.10.0"
16 changes: 8 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,14 +21,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand Down Expand Up @@ -66,14 +66,14 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
meta:
dependency: "direct main"
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -127,7 +127,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.3"
typed_data:
dependency: transitive
description:
Expand All @@ -141,7 +141,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=1.10.0"
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description:
Facebook Audience Network plugin for Flutter apps. You can show Facebook Banner
ads, Interstitial ads, Rewarded video ads and Native ads though this plugin.

version: 1.0.0-nullsafety.0
version: 1.0.1

homepage: https://github.com/dreamsoftin/facebook_audience_network

Expand All @@ -14,8 +14,8 @@ environment:
dependencies:
flutter:
sdk: flutter
meta: ^1.1.8
characters: ^1.0.0
meta: ^1.7.0
characters: ^1.2.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 22bf678

Please sign in to comment.