-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save events on offline and send out when back to online automatically
- Loading branch information
Showing
11 changed files
with
1,219 additions
and
879 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
android/src/main/java/com/amplitude/android/utilities/AndroidNetworkConnectivityChecker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.amplitude.android.utilities | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.os.Build | ||
import com.amplitude.common.Logger | ||
import com.amplitude.core.platform.NetworkConnectivityChecker | ||
|
||
class AndroidNetworkConnectivityChecker(private val context: Context, private val logger: Logger) : NetworkConnectivityChecker { | ||
companion object { | ||
private const val ACCESS_NETWORK_STATE = "android.permission.ACCESS_NETWORK_STATE" | ||
} | ||
|
||
override suspend fun isConnected(): Boolean { | ||
// Assume connection and proceed. | ||
// Events will be treated like online | ||
// regardless network connectivity | ||
if (!hasPermission(context, ACCESS_NETWORK_STATE)) { | ||
logger.warn("No ACCESS_NETWORK_STATE permission, offline mode is not supported.") | ||
return true | ||
} | ||
|
||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
val network = cm.activeNetwork ?: return false | ||
val capabilities = cm.getNetworkCapabilities(network) ?: return false | ||
|
||
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || | ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) | ||
} else { | ||
@SuppressLint("MissingPermission") | ||
val networkInfo = cm.activeNetworkInfo | ||
return networkInfo != null && networkInfo.isConnectedOrConnecting | ||
} | ||
} | ||
|
||
private fun hasPermission( | ||
context: Context, | ||
permission: String, | ||
): Boolean { | ||
return context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
android/src/main/java/com/amplitude/android/utilities/AndroidNetworkListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import android.annotation.SuppressLint | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import android.os.Build | ||
import java.lang.IllegalArgumentException | ||
|
||
class AndroidNetworkListener(private val context: Context) { | ||
private var networkCallback: NetworkChangeCallback? = null | ||
private var networkCallbackForLowerApiLevels: BroadcastReceiver? = null | ||
private var networkCallbackForHigherApiLevels: ConnectivityManager.NetworkCallback? = null | ||
|
||
interface NetworkChangeCallback { | ||
fun onNetworkAvailable() | ||
|
||
fun onNetworkUnavailable() | ||
} | ||
|
||
fun setNetworkChangeCallback(callback: NetworkChangeCallback) { | ||
this.networkCallback = callback | ||
} | ||
|
||
fun startListening() { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
setupNetworkCallback() | ||
} else { | ||
setupBroadcastReceiver() | ||
} | ||
} | ||
|
||
@SuppressLint("NewApi") | ||
private fun setupNetworkCallback() { | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val networkRequest = | ||
NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.build() | ||
|
||
networkCallbackForHigherApiLevels = | ||
object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
networkCallback?.onNetworkAvailable() | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
networkCallback?.onNetworkUnavailable() | ||
} | ||
} | ||
|
||
connectivityManager.registerNetworkCallback(networkRequest, networkCallbackForHigherApiLevels!!) | ||
} | ||
|
||
private fun setupBroadcastReceiver() { | ||
networkCallbackForLowerApiLevels = | ||
object : BroadcastReceiver() { | ||
override fun onReceive( | ||
context: Context, | ||
intent: Intent, | ||
) { | ||
if (ConnectivityManager.CONNECTIVITY_ACTION == intent.action) { | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val activeNetwork = connectivityManager.activeNetworkInfo | ||
val isConnected = activeNetwork?.isConnectedOrConnecting == true | ||
|
||
if (isConnected) { | ||
networkCallback?.onNetworkAvailable() | ||
} else { | ||
networkCallback?.onNetworkUnavailable() | ||
} | ||
} | ||
} | ||
} | ||
|
||
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) | ||
context.registerReceiver(networkCallbackForLowerApiLevels, filter) | ||
} | ||
|
||
fun stopListening() { | ||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
networkCallbackForHigherApiLevels?.let { connectivityManager.unregisterNetworkCallback(it) } | ||
} else { | ||
networkCallbackForLowerApiLevels?.let { context.unregisterReceiver(it) } | ||
} | ||
} catch (e: IllegalArgumentException) { | ||
// callback was already unregistered. | ||
} catch (e: IllegalStateException) { | ||
// shutdown process is in progress and certain operations are not allowed. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.