Skip to content

Commit

Permalink
Merge pull request #60 from sakebook/feature/android_v2
Browse files Browse the repository at this point in the history
Migrate android plugin v2
  • Loading branch information
sakebook authored Aug 3, 2020
2 parents ea9c3ac + 075e929 commit 73c06dd
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 58 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -68,9 +68,10 @@ Use [`com.google.android.gms.ads.formats.MediaView`](https://developers.google.c
...

<!-- ViewGroup -->
<RelativeLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent">

...

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>) : PlatformView {
class UnifiedAdLayout(
context: Context,
messenger: BinaryMessenger,
id: Int,
arguments: HashMap<String, String>
) : PlatformView {

private val hostPackageName = arguments["package_name"]
private val layoutRes = context.resources.getIdentifier(arguments["layout_name"], "layout", hostPackageName)
Expand Down
21 changes: 13 additions & 8 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="sakebook.github.com.native_ads_example">

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
Expand All @@ -7,28 +8,32 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="native_ads_example"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:name="io.flutter.embedding.android.FlutterActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in @style/LaunchTheme). -->

<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/splash_background" />

<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" />

<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
Expand Down

This file was deleted.

12 changes: 0 additions & 12 deletions example/android/app/src/main/res/drawable/launch_background.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
</layer-list>
8 changes: 5 additions & 3 deletions example/android/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowBackground">@android:color/white</item>
</style>
</resources>

0 comments on commit 73c06dd

Please sign in to comment.