Skip to content

Commit

Permalink
chore: enable app lifecycle and deep link events
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Feb 29, 2024
1 parent ceb8915 commit c00d1c6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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<String>("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..")
}

Expand Down Expand Up @@ -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<String>("apiKey")!!, context = ctxt)
call.argument<Int>("flushQueueSize")?.let { configuration.flushQueueSize = it }
Expand Down
12 changes: 12 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<!-- Deep link to open this activity -->
<data
android:scheme="app"
android:host="amplitude_flutter_example" />
</intent-filter>
</activity>
</application>
<meta-data
Expand Down

0 comments on commit c00d1c6

Please sign in to comment.