-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* android embed v2 * update changelog * Update build images * lint * more lint
- Loading branch information
Showing
21 changed files
with
171 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.shatsy.admobflutter"> | ||
package="com.shatsy.admobflutter"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
134 changes: 76 additions & 58 deletions
134
android/src/main/kotlin/com/shatsy/admobflutter/AdmobFlutterPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,99 @@ | ||
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 io.flutter.embedding.engine.plugins.FlutterPlugin | ||
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.Registrar | ||
import kotlin.collections.HashMap | ||
|
||
fun createAdListener(channel: MethodChannel) : AdListener { | ||
return object: AdListener() { | ||
override fun onAdLoaded() = channel.invokeMethod("loaded", null) | ||
override fun onAdFailedToLoad(errorCode: Int) = 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) | ||
} | ||
fun createAdListener(channel: MethodChannel): AdListener { | ||
return object : AdListener() { | ||
override fun onAdLoaded() = channel.invokeMethod("loaded", null) | ||
override fun onAdFailedToLoad(errorCode: Int) = 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) | ||
} | ||
} | ||
|
||
class AdmobFlutterPlugin(private val context: Context): MethodCallHandler { | ||
companion object { | ||
@JvmStatic | ||
fun registerWith(registrar: Registrar) { | ||
val defaultChannel = MethodChannel(registrar.messenger(), "admob_flutter") | ||
defaultChannel.setMethodCallHandler(AdmobFlutterPlugin(registrar.context())) | ||
class AdmobFlutterPlugin : MethodCallHandler, FlutterPlugin { | ||
/// 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 | ||
|
||
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { | ||
context = flutterPluginBinding.applicationContext | ||
|
||
val interstitialChannel = MethodChannel(registrar.messenger(), "admob_flutter/interstitial") | ||
interstitialChannel.setMethodCallHandler(AdmobInterstitial(registrar)) | ||
defaultChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter") | ||
defaultChannel.setMethodCallHandler(this) | ||
|
||
val rewardChannel = MethodChannel(registrar.messenger(), "admob_flutter/reward") | ||
rewardChannel.setMethodCallHandler(AdmobReward(registrar)) | ||
interstitialChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter/interstitial") | ||
interstitialChannel.setMethodCallHandler(AdmobInterstitial(flutterPluginBinding)) | ||
|
||
registrar | ||
.platformViewRegistry() | ||
.registerViewFactory("admob_flutter/banner", AdmobBannerFactory(registrar.messenger())) | ||
rewardChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "admob_flutter/reward") | ||
rewardChannel.setMethodCallHandler(AdmobReward(flutterPluginBinding)) | ||
|
||
flutterPluginBinding.platformViewRegistry.registerViewFactory("admob_flutter/banner", AdmobBannerFactory(flutterPluginBinding.binaryMessenger)) | ||
} | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: Result) { | ||
when(call.method) { | ||
"initialize" -> { | ||
MobileAds.initialize(context) | ||
@Suppress("UNCHECKED_CAST") | ||
(call.arguments as? ArrayList<String>)?.apply { | ||
val configuration = RequestConfiguration.Builder().setTestDeviceIds(this).build() | ||
MobileAds.setRequestConfiguration(configuration) | ||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { | ||
defaultChannel.setMethodCallHandler(null) | ||
interstitialChannel.setMethodCallHandler(null) | ||
rewardChannel.setMethodCallHandler(null) | ||
context = null | ||
} | ||
|
||
override fun onMethodCall(call: MethodCall, result: Result) { | ||
|
||
if (context == null) { | ||
return result.error("null_android_context", "Android context is null.", "Android context is null.") | ||
} | ||
} | ||
"banner_size" -> { | ||
val args = call.arguments as HashMap<*, *> | ||
val name = args["name"] as String | ||
val width = args["width"] as Int | ||
when(name) { | ||
"SMART_BANNER" -> { | ||
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 | ||
)) | ||
} | ||
"ADAPTIVE_BANNER" -> { | ||
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width) | ||
result.success(hashMapOf( | ||
"width" to adSize.width, | ||
"height" to adSize.height | ||
)) | ||
} | ||
else -> result.error("banner_size", "not implemented name", name) | ||
|
||
when (call.method) { | ||
"initialize" -> { | ||
MobileAds.initialize(context) | ||
@Suppress("UNCHECKED_CAST") | ||
(call.arguments as? ArrayList<String>)?.apply { | ||
val configuration = RequestConfiguration.Builder().setTestDeviceIds(this).build() | ||
MobileAds.setRequestConfiguration(configuration) | ||
} | ||
} | ||
"banner_size" -> { | ||
val args = call.arguments as HashMap<*, *> | ||
val name = args["name"] as String | ||
val width = args["width"] as Int | ||
when (name) { | ||
"SMART_BANNER" -> { | ||
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 | ||
)) | ||
} | ||
"ADAPTIVE_BANNER" -> { | ||
val adSize = AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width) | ||
result.success(hashMapOf( | ||
"width" to adSize.width, | ||
"height" to adSize.height | ||
)) | ||
} | ||
else -> result.error("banner_size", "not implemented name", name) | ||
} | ||
} | ||
else -> result.notImplemented() | ||
} | ||
} | ||
else -> result.notImplemented() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ workflows: | |
- [email protected]: {} | ||
- [email protected]: | ||
inputs: | ||
- version: 2.0.0 | ||
- version: 2.8.1 | ||
- [email protected]: | ||
inputs: | ||
- content: |- | ||
|
@@ -43,3 +43,6 @@ app: | |
- opts: | ||
is_expand: false | ||
BITRISE_EXPORT_METHOD: development | ||
meta: | ||
bitrise.io: | ||
stack: osx-xcode-13.2.x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 1 addition & 8 deletions
9
example/android/app/src/main/kotlin/com/shatsy/admobflutterexample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
package com.shatsy.admobflutterexample | ||
|
||
import android.os.Bundle | ||
|
||
import io.flutter.app.FlutterActivity | ||
import io.flutter.plugins.GeneratedPluginRegistrant | ||
import io.flutter.embedding.android.FlutterActivity | ||
|
||
class MainActivity: FlutterActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
GeneratedPluginRegistrant.registerWith(this) | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
example/android/app/src/main/kotlin/com/shatsy/example/MainActivity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.