diff --git a/README.md b/README.md index 7c26f4e..5bd052e 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ This plugin supported custom layout. You need to create a layout file. ### Android You can use anything if the parent is a ViewGroup. -The example uses RelativeLayout. +The example uses ConstraintLayout. Use [`com.google.android.gms.ads.formats.UnifiedNativeAdView`](https://developers.google.com/android/reference/com/google/android/gms/ads/formats/UnifiedNativeAdView) for the parent. @@ -68,9 +68,10 @@ Use [`com.google.android.gms.ads.formats.MediaView`](https://developers.google.c ... - + android:layout_height="match_parent"> ... @@ -213,6 +214,11 @@ Receive callbacks for some events by passing to the NativeAdView constructor - onAdLeftApplication - onAdLoaded +## Reference +- Mobile Ads SDK Release Notes + - [Android](https://developers.google.com/admob/android/rel-notes) + - [iOS](https://developers.google.com/admob/ios/rel-notes) + ## Limitations This is just an initial version of the plugin. There are still some diff --git a/android/src/main/kotlin/sakebook/github/com/native_ads/NativeAdsPlugin.kt b/android/src/main/kotlin/sakebook/github/com/native_ads/NativeAdsPlugin.kt index fbd6884..f7669c8 100644 --- a/android/src/main/kotlin/sakebook/github/com/native_ads/NativeAdsPlugin.kt +++ b/android/src/main/kotlin/sakebook/github/com/native_ads/NativeAdsPlugin.kt @@ -1,35 +1,50 @@ package sakebook.github.com.native_ads import com.google.android.gms.ads.MobileAds +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 -import io.flutter.plugin.common.PluginRegistry.Registrar - -class NativeAdsPlugin : MethodCallHandler { - - companion object { - private lateinit var registrar: Registrar - @JvmStatic - fun registerWith(registrar: Registrar) { - val channel = MethodChannel(registrar.messenger(), "native_ads") - channel.setMethodCallHandler(NativeAdsPlugin()) - this.registrar = registrar - registrar - .platformViewRegistry() - .registerViewFactory( - "com.github.sakebook.android/unified_ad_layout", UnifiedAdLayoutFactory(registrar.messenger())) - } + +class NativeAdsPlugin : FlutterPlugin, ActivityAware, MethodCallHandler { + + // for future use + private var channel: MethodChannel? = null + + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel = MethodChannel(binding.binaryMessenger, "native_ads") + channel?.setMethodCallHandler(this) + binding.platformViewRegistry + .registerViewFactory("com.github.sakebook.android/unified_ad_layout", + UnifiedAdLayoutFactory(binding.binaryMessenger)) + } + + override fun onAttachedToActivity(binding: ActivityPluginBinding) { + MobileAds.initialize(binding.activity) } override fun onMethodCall(call: MethodCall, result: Result) { when (call.method) { "initialize" -> { - MobileAds.initialize(registrar.context()) result.success(true) } else -> result.notImplemented() } } -} + + override fun onDetachedFromActivity() { + } + + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { + channel?.setMethodCallHandler(null) + } + + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { + } + + override fun onDetachedFromActivityForConfigChanges() { + } +} \ No newline at end of file diff --git a/android/src/main/kotlin/sakebook/github/com/native_ads/UnifiedAdLayout.kt b/android/src/main/kotlin/sakebook/github/com/native_ads/UnifiedAdLayout.kt index da796e0..bffed2a 100644 --- a/android/src/main/kotlin/sakebook/github/com/native_ads/UnifiedAdLayout.kt +++ b/android/src/main/kotlin/sakebook/github/com/native_ads/UnifiedAdLayout.kt @@ -16,7 +16,12 @@ import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.platform.PlatformView -class UnifiedAdLayout(context: Context, messenger: BinaryMessenger, id: Int, arguments: HashMap) : PlatformView { +class UnifiedAdLayout( + context: Context, + messenger: BinaryMessenger, + id: Int, + arguments: HashMap +) : PlatformView { private val hostPackageName = arguments["package_name"] private val layoutRes = context.resources.getIdentifier(arguments["layout_name"], "layout", hostPackageName) diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index ff68206..118fadb 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,4 +1,5 @@ - + + + + android:name="io.flutter.embedding.android.NormalTheme" + android:resource="@style/NormalTheme" /> + + diff --git a/example/android/app/src/main/kotlin/sakebook/github/com/native_ads_example/MainActivity.kt b/example/android/app/src/main/kotlin/sakebook/github/com/native_ads_example/MainActivity.kt deleted file mode 100644 index f7e2ab2..0000000 --- a/example/android/app/src/main/kotlin/sakebook/github/com/native_ads_example/MainActivity.kt +++ /dev/null @@ -1,13 +0,0 @@ -package sakebook.github.com.native_ads_example - -import android.os.Bundle - -import io.flutter.app.FlutterActivity -import io.flutter.plugins.GeneratedPluginRegistrant - -class MainActivity: FlutterActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - GeneratedPluginRegistrant.registerWith(this) - } -} diff --git a/example/android/app/src/main/res/drawable/launch_background.xml b/example/android/app/src/main/res/drawable/launch_background.xml deleted file mode 100644 index 304732f..0000000 --- a/example/android/app/src/main/res/drawable/launch_background.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - diff --git a/example/android/app/src/main/res/drawable/splash_background.xml b/example/android/app/src/main/res/drawable/splash_background.xml new file mode 100644 index 0000000..5782842 --- /dev/null +++ b/example/android/app/src/main/res/drawable/splash_background.xml @@ -0,0 +1,4 @@ + + + + diff --git a/example/android/app/src/main/res/values/styles.xml b/example/android/app/src/main/res/values/styles.xml index 00fa441..253fb8c 100644 --- a/example/android/app/src/main/res/values/styles.xml +++ b/example/android/app/src/main/res/values/styles.xml @@ -1,8 +1,10 @@ +