Skip to content

Commit f8ec348

Browse files
authored
refactor(in_app_messaging): migrate Android plugin to Kotlin (#18526)
* refactor(in_app_messaging): migrate Android plugin to Kotlin * style(in_app_messaging): format Kotlin plugin
1 parent 1522bd8 commit f8ec348

5 files changed

Lines changed: 130 additions & 126 deletions

File tree

packages/firebase_in_app_messaging/firebase_in_app_messaging/android/build.gradle

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ apply plugin: 'com.android.library'
55
apply from: file("local-config.gradle")
66

77
buildscript {
8+
ext.kotlin_version = "2.0.0"
89
repositories {
910
google()
1011
mavenCentral()
1112
}
13+
dependencies {
14+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15+
}
1216
}
1317

1418
rootProject.allprojects {
@@ -18,6 +22,16 @@ rootProject.allprojects {
1822
}
1923
}
2024

25+
// AGP 9+ has built-in Kotlin support unless Flutter opts out via android.builtInKotlin=false.
26+
def agpMajor = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')[0] as int
27+
def builtInKotlin = providers.gradleProperty("android.builtInKotlin")
28+
.map { it.toBoolean() }
29+
.orElse(agpMajor >= 9)
30+
.get()
31+
if (agpMajor < 9 || !builtInKotlin) {
32+
apply plugin: 'kotlin-android'
33+
}
34+
2135
def firebaseCoreProject = findProject(':firebase_core')
2236
if (firebaseCoreProject == null) {
2337
throw new GradleException('Could not find the firebase_core FlutterFire plugin, have you added it as a dependency in your pubspec?')
@@ -49,6 +63,11 @@ android {
4963
targetCompatibility project.ext.javaVersion
5064
}
5165

66+
sourceSets {
67+
main.java.srcDirs += "src/main/kotlin"
68+
test.java.srcDirs += "src/test/kotlin"
69+
}
70+
5271
buildFeatures {
5372
buildConfig = true
5473
}
@@ -65,4 +84,12 @@ android {
6584
}
6685
}
6786

87+
plugins.withId("org.jetbrains.kotlin.android") {
88+
kotlin {
89+
compilerOptions {
90+
jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.fromTarget(project.ext.javaVersion.toString())
91+
}
92+
}
93+
}
94+
6895
apply from: file("./user-agent.gradle")

packages/firebase_in_app_messaging/firebase_in_app_messaging/android/src/main/java/io/flutter/plugins/firebase/inappmessaging/FirebaseInAppMessagingPlugin.java

Lines changed: 0 additions & 105 deletions
This file was deleted.

packages/firebase_in_app_messaging/firebase_in_app_messaging/android/src/main/java/io/flutter/plugins/firebase/inappmessaging/FlutterFirebaseAppRegistrar.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
package io.flutter.plugins.firebase.inappmessaging
5+
6+
import com.google.android.gms.tasks.Task
7+
import com.google.android.gms.tasks.TaskCompletionSource
8+
import com.google.firebase.FirebaseApp
9+
import com.google.firebase.inappmessaging.FirebaseInAppMessaging
10+
import io.flutter.embedding.engine.plugins.FlutterPlugin
11+
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
12+
import io.flutter.plugin.common.MethodCall
13+
import io.flutter.plugin.common.MethodChannel
14+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
15+
import io.flutter.plugin.common.MethodChannel.Result
16+
import io.flutter.plugins.firebase.core.FlutterFirebasePlugin
17+
18+
/** FirebaseInAppMessagingPlugin */
19+
class FirebaseInAppMessagingPlugin : FlutterFirebasePlugin, FlutterPlugin, MethodCallHandler {
20+
private var channel: MethodChannel? = null
21+
22+
override fun onAttachedToEngine(binding: FlutterPluginBinding) {
23+
channel =
24+
MethodChannel(binding.binaryMessenger, METHOD_CHANNEL_NAME).also {
25+
it.setMethodCallHandler(this)
26+
}
27+
}
28+
29+
override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
30+
channel?.setMethodCallHandler(null)
31+
channel = null
32+
}
33+
34+
override fun onMethodCall(call: MethodCall, result: Result) {
35+
when (call.method) {
36+
"FirebaseInAppMessaging#triggerEvent" -> {
37+
val eventName = requireNotNull(call.argument<String>("eventName"))
38+
FirebaseInAppMessaging.getInstance().triggerEvent(eventName)
39+
result.success(null)
40+
}
41+
"FirebaseInAppMessaging#setMessagesSuppressed" -> {
42+
val suppress = requireNotNull(call.argument<Boolean>("suppress"))
43+
FirebaseInAppMessaging.getInstance().setMessagesSuppressed(suppress)
44+
result.success(null)
45+
}
46+
"FirebaseInAppMessaging#setAutomaticDataCollectionEnabled" -> {
47+
val enabled = call.argument<Boolean>("enabled")
48+
FirebaseInAppMessaging.getInstance().setAutomaticDataCollectionEnabled(enabled)
49+
result.success(null)
50+
}
51+
else -> result.notImplemented()
52+
}
53+
}
54+
55+
override fun getPluginConstantsForFirebaseApp(firebaseApp: FirebaseApp): Task<Map<String, Any>?> {
56+
val taskCompletionSource = TaskCompletionSource<Map<String, Any>?>()
57+
58+
FlutterFirebasePlugin.cachedThreadPool.execute {
59+
try {
60+
taskCompletionSource.setResult(null)
61+
} catch (exception: Exception) {
62+
taskCompletionSource.setException(exception)
63+
}
64+
}
65+
66+
return taskCompletionSource.task
67+
}
68+
69+
override fun didReinitializeFirebaseCore(): Task<Void> {
70+
val taskCompletionSource = TaskCompletionSource<Void>()
71+
72+
FlutterFirebasePlugin.cachedThreadPool.execute {
73+
try {
74+
taskCompletionSource.setResult(null)
75+
} catch (exception: Exception) {
76+
taskCompletionSource.setException(exception)
77+
}
78+
}
79+
80+
return taskCompletionSource.task
81+
}
82+
83+
companion object {
84+
private const val METHOD_CHANNEL_NAME = "plugins.flutter.io/firebase_in_app_messaging"
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2021 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
package io.flutter.plugins.firebase.inappmessaging
5+
6+
import androidx.annotation.Keep
7+
import com.google.firebase.components.Component
8+
import com.google.firebase.components.ComponentRegistrar
9+
import com.google.firebase.platforminfo.LibraryVersionComponent
10+
11+
@Keep
12+
class FlutterFirebaseAppRegistrar : ComponentRegistrar {
13+
override fun getComponents(): List<Component<*>> {
14+
return listOf(
15+
LibraryVersionComponent.create(BuildConfig.LIBRARY_NAME, BuildConfig.LIBRARY_VERSION))
16+
}
17+
}

0 commit comments

Comments
 (0)