Skip to content
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

Upgrade Native Deps #290

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [3.0.0-beta.0] - 2021/12/29
- Increase Android min api level to 19.
- Increase iOS min SDK to 12.
- Removed deprecated AdmobBannerSize.SMART_BANNER
- Removed deprecated callback `leftApplication`
- Update native SDK's to `'Google-Mobile-Ads-SDK', '~> 8.13.0'` and `com.google.android.gms:play-services-ads:20.5.0`

## [2.0.0] - 2021/12/19
- Migrate Android to v2 embedding [287](https://github.com/kmcgill88/admob_flutter/issues/287)
- Update Bitrise build machine to Xcode 13.2.x, on macOS (Monterey); Flutter 2.8.1
Expand Down
10 changes: 5 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ group 'com.shatsy.admobflutter'
version '1.0.0-beta.3'

buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.android.tools.build:gradle:3.6.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -25,13 +25,13 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
minSdkVersion 16
minSdkVersion 19
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
lintOptions {
Expand All @@ -41,5 +41,5 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api 'com.google.firebase:firebase-ads:19.1.0'
implementation 'com.google.android.gms:play-services-ads:20.5.0'
}
86 changes: 43 additions & 43 deletions android/src/main/kotlin/com/shatsy/admobflutter/AdmobBanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,58 @@ import io.flutter.plugin.platform.PlatformView


class AdmobBanner(context: Context, messenger: BinaryMessenger, id: Int, args: HashMap<*, *>) : PlatformView, MethodCallHandler {
private val channel: MethodChannel = MethodChannel(messenger, "admob_flutter/banner_$id")
private val adView: AdView = AdView(context)
private val channel: MethodChannel = MethodChannel(messenger, "admob_flutter/banner_$id")
private val adView: AdView = AdView(context)

init {
channel.setMethodCallHandler(this)
init {
channel.setMethodCallHandler(this)

adView.adSize = getSize(context, args["adSize"] as HashMap<*, *>)
adView.adUnitId = args["adUnitId"] as String?
adView.adSize = getSize(context, args["adSize"] as HashMap<*, *>)
adView.adUnitId = args["adUnitId"] as String

val adRequestBuilder = AdRequest.Builder()
val npa: Boolean? = args["nonPersonalizedAds"] as Boolean?
if(npa == true) {
val extras = Bundle()
extras.putString("npa", "1")
adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
}
val adRequestBuilder = AdRequest.Builder()
val npa: Boolean? = args["nonPersonalizedAds"] as Boolean?
if (npa == true) {
val extras = Bundle()
extras.putString("npa", "1")
adRequestBuilder.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
}

adView.loadAd(adRequestBuilder.build())
}
adView.loadAd(adRequestBuilder.build())
}

private fun getSize(context: Context, size: HashMap<*, *>) : AdSize {
val width = size["width"] as Int
val height = size["height"] as Int
val name = size["name"] as String
private fun getSize(context: Context, size: HashMap<*, *>): AdSize {
val width = size["width"] as Int
val height = size["height"] as Int
val name = size["name"] as String

return when(name) {
"BANNER" -> AdSize.BANNER
"LARGE_BANNER" -> AdSize.LARGE_BANNER
"MEDIUM_RECTANGLE" -> AdSize.MEDIUM_RECTANGLE
"FULL_BANNER" -> AdSize.FULL_BANNER
"LEADERBOARD" -> AdSize.LEADERBOARD
"SMART_BANNER" -> AdSize.SMART_BANNER
"ADAPTIVE_BANNER" -> AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width)
else -> AdSize(width, height)
return when (name) {
"BANNER" -> AdSize.BANNER
"LARGE_BANNER" -> AdSize.LARGE_BANNER
"MEDIUM_RECTANGLE" -> AdSize.MEDIUM_RECTANGLE
"FULL_BANNER" -> AdSize.FULL_BANNER
"LEADERBOARD" -> AdSize.LEADERBOARD
"FLUID" -> AdSize.FLUID
"ADAPTIVE_BANNER" -> AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width)
else -> AdSize(width, height)
}
}
}

override fun getView(): View {
return adView
}
override fun getView(): View {
return adView
}

override fun onMethodCall(call: MethodCall, result: Result) {
when(call.method) {
"setListener" -> adView.adListener = createAdListener(channel)
"dispose" -> dispose()
else -> result.notImplemented()
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"setListener" -> adView.adListener = createAdListener(channel)
"dispose" -> dispose()
else -> result.notImplemented()
}
}
}

override fun dispose() {
adView.visibility = View.GONE
adView.destroy()
channel.setMethodCallHandler(null)
}
override fun dispose() {
adView.visibility = View.GONE
adView.destroy()
channel.setMethodCallHandler(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory

class AdmobBannerFactory(private val messenger: BinaryMessenger): PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
return AdmobBanner(context, messenger, viewId, args as HashMap<*, *>)
}
class AdmobBannerFactory(private val messenger: BinaryMessenger) : PlatformViewFactory(StandardMessageCodec.INSTANCE) {
override fun create(context: Context, viewId: Int, args: Any?): PlatformView {
return AdmobBanner(context, messenger, viewId, args as HashMap<*, *>)
}
}
Original file line number Diff line number Diff line change
@@ -1,67 +1,85 @@
package com.shatsy.admobflutter

import android.content.Context
import androidx.annotation.NonNull
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.MobileAds
import com.google.android.gms.ads.RequestConfiguration
import com.google.android.gms.ads.*
import com.google.android.gms.ads.rewarded.RewardItem
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
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

fun createAdListener(channel: MethodChannel): AdListener {
return object : AdListener() {
abstract class AdmobFlutterAdListener : AdListener() {
open fun onRewarded(reward: RewardItem) {}
}

fun createAdListener(channel: MethodChannel): AdmobFlutterAdListener {
return object : AdmobFlutterAdListener() {
override fun onAdLoaded() = channel.invokeMethod("loaded", null)
override fun onAdFailedToLoad(errorCode: Int) = channel.invokeMethod("failedToLoad", hashMapOf("errorCode" to errorCode))
override fun onAdFailedToLoad(errorCode: LoadAdError) = channel.invokeMethod("failedToLoad", hashMapOf("errorCode" to errorCode))
override fun onAdClicked() = channel.invokeMethod("clicked", null)
override fun onAdImpression() = channel.invokeMethod("impression", null)
override fun onAdOpened() = channel.invokeMethod("opened", null)
override fun onAdLeftApplication() = channel.invokeMethod("leftApplication", null)
override fun onAdClosed() = channel.invokeMethod("closed", null)
override fun onRewarded(reward: RewardItem) = channel.invokeMethod("rewarded", hashMapOf("type" to (reward.type), "amount" to (reward.amount)))
}
}

class AdmobFlutterPlugin : MethodCallHandler, FlutterPlugin {
class AdmobFlutterPlugin : MethodCallHandler, FlutterPlugin, ActivityAware {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var defaultChannel: MethodChannel
private lateinit var interstitialChannel: MethodChannel
private lateinit var rewardChannel: MethodChannel
private var context: Context? = null
private var flutterPluginBinding: FlutterPlugin.FlutterPluginBinding? = null

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
context = flutterPluginBinding.applicationContext
this.flutterPluginBinding = flutterPluginBinding

defaultChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter")
defaultChannel.setMethodCallHandler(this)

interstitialChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter/interstitial")
interstitialChannel.setMethodCallHandler(AdmobInterstitial(flutterPluginBinding))

rewardChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter/reward")
rewardChannel.setMethodCallHandler(AdmobReward(flutterPluginBinding))

flutterPluginBinding.platformViewRegistry.registerViewFactory("admob_flutter/banner", AdmobBannerFactory(flutterPluginBinding.binaryMessenger))
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
defaultChannel.setMethodCallHandler(null)
interstitialChannel.setMethodCallHandler(null)
rewardChannel.setMethodCallHandler(null)
context = null
flutterPluginBinding = null
}

override fun onMethodCall(call: MethodCall, result: Result) {
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
flutterPluginBinding?.let {
interstitialChannel.setMethodCallHandler(AdmobInterstitial(it, binding.activity))
rewardChannel.setMethodCallHandler(AdmobReward(it, binding.activity))
}
}

if (context == null) {
return result.error("null_android_context", "Android context is null.", "Android context is null.")
override fun onDetachedFromActivityForConfigChanges() {
// no-op
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
flutterPluginBinding?.let {
interstitialChannel.setMethodCallHandler(AdmobInterstitial(it, binding.activity))
rewardChannel.setMethodCallHandler(AdmobReward(it, binding.activity))
}
}

override fun onDetachedFromActivity() {
interstitialChannel.setMethodCallHandler(null)
}

override fun onMethodCall(call: MethodCall, result: Result) {
if (flutterPluginBinding == null) {
return result.error("null_android_flutterPluginBinding", "flutterPluginBinding is null.", "flutterPluginBinding is null.")
}
val context = flutterPluginBinding!!.applicationContext
when (call.method) {
"initialize" -> {
MobileAds.initialize(context)
Expand All @@ -76,11 +94,11 @@ class AdmobFlutterPlugin : MethodCallHandler, FlutterPlugin {
val name = args["name"] as String
val width = args["width"] as Int
when (name) {
"SMART_BANNER" -> {
val metrics = context!!.resources.displayMetrics
"FLUID" -> {
val metrics = context.resources.displayMetrics
result.success(hashMapOf(
"width" to AdSize.SMART_BANNER.getWidthInPixels(context) / metrics.density,
"height" to AdSize.SMART_BANNER.getHeightInPixels(context) / metrics.density
"width" to AdSize.FLUID.getWidthInPixels(context) / metrics.density,
"height" to AdSize.FLUID.getHeightInPixels(context) / metrics.density
))
}
"ADAPTIVE_BANNER" -> {
Expand Down
Loading