File tree Expand file tree Collapse file tree 2 files changed +58
-2
lines changed
samples/kotlin-android-app/src/main/java/com/segment/analytics/next Expand file tree Collapse file tree 2 files changed +58
-2
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import com.segment.analytics.next.plugins.AndroidAdvertisingIdPlugin
10
10
import com.segment.analytics.next.plugins.AndroidRecordScreenPlugin
11
11
import com.segment.analytics.next.plugins.PushNotificationTracking
12
12
import com.segment.analytics.kotlin.core.platform.Plugin
13
+ import com.segment.analytics.kotlin.core.platform.policies.CountBasedFlushPolicy
14
+ import com.segment.analytics.kotlin.core.platform.policies.FrequencyFlushPolicy
13
15
import com.segment.analytics.kotlin.core.utilities.*
14
16
15
17
class MainApplication : Application () {
@@ -24,8 +26,12 @@ class MainApplication : Application() {
24
26
this .collectDeviceId = true
25
27
this .trackApplicationLifecycleEvents = true
26
28
this .trackDeepLinks = true
27
- this .flushAt = 1
28
- this .flushInterval = 0
29
+ this .flushPolicies = listOf (
30
+ CountBasedFlushPolicy (3 ), // Flush after 3 events
31
+ FrequencyFlushPolicy (5000 ), // Flush after 5 secs
32
+ UnmeteredFlushPolicy (applicationContext) // Flush if network is not metered
33
+ )
34
+ this .flushPolicies = listOf (UnmeteredFlushPolicy (applicationContext))
29
35
}
30
36
analytics.add(AndroidRecordScreenPlugin ())
31
37
analytics.add(object : Plugin {
Original file line number Diff line number Diff line change
1
+ package com.segment.analytics.next
2
+
3
+ import android.content.Context
4
+ import android.net.ConnectivityManager
5
+ import android.net.NetworkCapabilities
6
+ import android.os.Build
7
+ import androidx.annotation.RequiresApi
8
+ import androidx.core.content.ContextCompat.getSystemService
9
+ import com.segment.analytics.kotlin.core.platform.policies.FlushPolicy
10
+ import java.lang.ref.WeakReference
11
+
12
+ /* *
13
+ * Flush policy that checks where the Android device is connected to a metered (pay) network or an
14
+ * unmetered network.
15
+ *
16
+ * If the network is NOT metered this Flush policy returns true; false otherwise.
17
+ */
18
+ class UnmeteredFlushPolicy (context : Context ) : FlushPolicy {
19
+
20
+ lateinit var weakContext: WeakReference <Context >
21
+
22
+ init {
23
+ weakContext = WeakReference (context)
24
+ }
25
+
26
+ @RequiresApi(Build .VERSION_CODES .S )
27
+ override fun shouldFlush (): Boolean {
28
+ val context = weakContext.get()
29
+ var shouldFlush = false
30
+ context?.let {
31
+ val cm = getSystemService(context, ConnectivityManager ::class .java) as ConnectivityManager
32
+
33
+ val nc = cm.getNetworkCapabilities(cm.activeNetwork)
34
+ nc?.let {
35
+ nc.capabilities.forEach {
36
+ if (it == NetworkCapabilities .NET_CAPABILITY_NOT_METERED || it == NetworkCapabilities .NET_CAPABILITY_TEMPORARILY_NOT_METERED ) {
37
+ println (" UMFP: YAY network is NOT metered!!" )
38
+ shouldFlush = true
39
+ }
40
+ }
41
+
42
+ if (! shouldFlush) {
43
+ println (" UMFP: Sorry, this network was IS metered!" )
44
+ }
45
+ }
46
+ }
47
+
48
+ return shouldFlush
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments