diff --git a/android/src/main/kotlin/com/amplitude/amplitude_flutter/AmplitudeFlutterPlugin.kt b/android/src/main/kotlin/com/amplitude/amplitude_flutter/AmplitudeFlutterPlugin.kt index f41b4b0..62129e5 100644 --- a/android/src/main/kotlin/com/amplitude/amplitude_flutter/AmplitudeFlutterPlugin.kt +++ b/android/src/main/kotlin/com/amplitude/amplitude_flutter/AmplitudeFlutterPlugin.kt @@ -1,5 +1,6 @@ package com.amplitude.amplitude_flutter +import android.app.Activity import android.content.Context import com.amplitude.android.Amplitude import com.amplitude.android.Configuration @@ -14,10 +15,14 @@ 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 android.content.pm.PackageManager +import com.amplitude.android.utilities.DefaultEventUtils +import io.flutter.embedding.engine.plugins.activity.ActivityAware +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding -class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler { +class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { lateinit var amplitude: Amplitude - + private var activity: Activity? = null lateinit var ctxt: Context private lateinit var channel: MethodChannel @@ -27,6 +32,22 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler { private const val methodChannelName = "amplitude_flutter" } + override fun onAttachedToActivity(binding: ActivityPluginBinding) { + activity = binding.activity + } + + override fun onDetachedFromActivityForConfigChanges() { + activity = null + } + + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { + activity = binding.activity + } + + override fun onDetachedFromActivity() { + activity = null + } + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { ctxt = binding.applicationContext channel = MethodChannel(binding.binaryMessenger, methodChannelName) @@ -40,12 +61,18 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler { override fun onMethodCall(call: MethodCall, result: Result) { when (call.method) { "init" -> { - amplitude = Amplitude(getConfiguration(call)) + val configuration = getConfiguration(call) + amplitude = Amplitude(configuration) call.argument("logLevel")?.let { amplitude.logger.logMode = Logger.LogMode.valueOf(it.uppercase()) } amplitude.logger.debug("Amplitude has been successfully initialized.") + trackAppLifecycleAndDeepLinkEvents( + configuration.defaultTracking.appLifecycles, + configuration.defaultTracking.deepLinks + ) + result.success("init called..") } @@ -127,6 +154,31 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler { } } + private fun trackAppLifecycleAndDeepLinkEvents(appLifecycles: Boolean, deepLinks: Boolean) { + amplitude.isBuilt.invokeOnCompletion { exception -> + if (exception != null) { + println("isBuilt computation failed with exception: $exception") + } else { + val utils = DefaultEventUtils(amplitude) + + if (appLifecycles) { + val packageManager = ctxt.packageManager + var packageInfo = try { + packageManager.getPackageInfo(ctxt.packageName, 0) + } catch (ex: PackageManager.NameNotFoundException) { + println("Error occurred in getting package info. " + ex.message) + null + } + packageInfo?.let { utils.trackAppUpdatedInstalledEvent(it) } + } + + if (deepLinks) { + activity?.let { utils.trackDeepLinkOpenedEvent(it) } + } + } + } + } + private fun getConfiguration(call: MethodCall): Configuration { val configuration = Configuration(call.argument("apiKey")!!, context = ctxt) call.argument("flushQueueSize")?.let { configuration.flushQueueSize = it } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index f50ae53..57fd2f6 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -40,6 +40,18 @@ + + + + + + + + + +