From 37a29c1afd437e84646beb20640f1a810a6811cf Mon Sep 17 00:00:00 2001 From: sakebook Date: Tue, 4 Aug 2020 01:16:16 +0900 Subject: [PATCH 1/3] Migrate Android plugin to v2 --- .../github/com/native_ads/NativeAdsPlugin.kt | 53 ++++++++++++------- .../github/com/native_ads/UnifiedAdLayout.kt | 7 ++- .../android/app/src/main/AndroidManifest.xml | 21 +++++--- .../com/native_ads_example/MainActivity.kt | 13 ----- .../main/res/drawable/launch_background.xml | 12 ----- .../main/res/drawable/splash_background.xml | 4 ++ .../app/src/main/res/values/styles.xml | 8 +-- 7 files changed, 63 insertions(+), 55 deletions(-) delete mode 100644 example/android/app/src/main/kotlin/sakebook/github/com/native_ads_example/MainActivity.kt delete mode 100644 example/android/app/src/main/res/drawable/launch_background.xml create mode 100644 example/android/app/src/main/res/drawable/splash_background.xml 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..f6ebe6d 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,52 @@ 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) { + android.util.Log.d("NativeAdsPlugin", "onAttachedToEngine") + 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) { + android.util.Log.d("NativeAdsPlugin", "onAttachedToActivity") + 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 @@ + From 92da6d62e94c03187d2e65bb865f1a5a02f5b273 Mon Sep 17 00:00:00 2001 From: sakebook Date: Tue, 4 Aug 2020 01:19:34 +0900 Subject: [PATCH 2/3] Update README --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 From 075e929a6579446d328ae092200a19a6dddfd5f3 Mon Sep 17 00:00:00 2001 From: sakebook Date: Tue, 4 Aug 2020 01:29:20 +0900 Subject: [PATCH 3/3] Remove debug log --- .../kotlin/sakebook/github/com/native_ads/NativeAdsPlugin.kt | 2 -- 1 file changed, 2 deletions(-) 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 f6ebe6d..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 @@ -15,7 +15,6 @@ class NativeAdsPlugin : FlutterPlugin, ActivityAware, MethodCallHandler { private var channel: MethodChannel? = null override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { - android.util.Log.d("NativeAdsPlugin", "onAttachedToEngine") channel = MethodChannel(binding.binaryMessenger, "native_ads") channel?.setMethodCallHandler(this) binding.platformViewRegistry @@ -24,7 +23,6 @@ class NativeAdsPlugin : FlutterPlugin, ActivityAware, MethodCallHandler { } override fun onAttachedToActivity(binding: ActivityPluginBinding) { - android.util.Log.d("NativeAdsPlugin", "onAttachedToActivity") MobileAds.initialize(binding.activity) }