Skip to content

Commit fc5d0a9

Browse files
authored
Flush sample (#132)
* Adding a sample flush policy for only flushing when on an unmetered connection. * Use flush policies instead of properties.
1 parent 3541e5e commit fc5d0a9

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

samples/kotlin-android-app/src/main/java/com/segment/analytics/next/MainApplication.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.segment.analytics.next.plugins.AndroidAdvertisingIdPlugin
1010
import com.segment.analytics.next.plugins.AndroidRecordScreenPlugin
1111
import com.segment.analytics.next.plugins.PushNotificationTracking
1212
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
1315
import com.segment.analytics.kotlin.core.utilities.*
1416

1517
class MainApplication : Application() {
@@ -24,8 +26,12 @@ class MainApplication : Application() {
2426
this.collectDeviceId = true
2527
this.trackApplicationLifecycleEvents = true
2628
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))
2935
}
3036
analytics.add(AndroidRecordScreenPlugin())
3137
analytics.add(object : Plugin {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)