Skip to content

Commit

Permalink
dashboard: sync ability coded into avatars surround color (fixes #3505)…
Browse files Browse the repository at this point in the history
… (#3507)

Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored May 28, 2024
1 parent 1403b51 commit db52d4f
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1543
versionName "0.15.43"
versionCode 1544
versionName "0.15.44"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/org/ole/planet/myplanet/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import org.ole.planet.myplanet.callback.TeamPageListener
import org.ole.planet.myplanet.datamanager.DatabaseService
import org.ole.planet.myplanet.model.RealmApkLog
Expand All @@ -21,10 +25,13 @@ import org.ole.planet.myplanet.service.TaskNotificationWorker
import org.ole.planet.myplanet.service.UserProfileDbHandler
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import org.ole.planet.myplanet.utilities.LocaleHelper
import org.ole.planet.myplanet.utilities.NetworkUtils.initialize
import org.ole.planet.myplanet.utilities.NetworkUtils.startListenNetworkState
import org.ole.planet.myplanet.utilities.NotificationUtil.cancellAll
import org.ole.planet.myplanet.utilities.Utilities
import org.ole.planet.myplanet.utilities.VersionUtils.getVersionName
import java.util.*
import java.util.Date
import java.util.UUID
import java.util.concurrent.TimeUnit

class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
Expand Down Expand Up @@ -55,8 +62,12 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
return "0"
}
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onCreate() {
super.onCreate()
initialize(CoroutineScope(Dispatchers.IO))


context = this
preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
val builder = VmPolicy.Builder()
Expand All @@ -76,6 +87,7 @@ class MainApplication : Application(), Application.ActivityLifecycleCallbacks {
handleUncaughtException(e)
}
registerActivityLifecycleCallbacks(this)
startListenNetworkState()
}

private fun scheduleAutoSyncWork(syncInterval: Int?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ole.planet.myplanet.ui.dashboard

import android.content.ContentValues.TAG
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -8,8 +9,11 @@ import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import io.realm.Case
import io.realm.Realm
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.*
import org.json.JSONException
import org.json.JSONObject
import org.ole.planet.myplanet.R
Expand All @@ -24,14 +28,17 @@ import org.ole.planet.myplanet.ui.courses.CoursesFragment
import org.ole.planet.myplanet.ui.courses.MyProgressFragment
import org.ole.planet.myplanet.ui.courses.TakeCourseFragment
import org.ole.planet.myplanet.ui.feedback.FeedbackListFragment
import org.ole.planet.myplanet.ui.mylife.LifeFragment
import org.ole.planet.myplanet.ui.resources.AddResourceFragment
import org.ole.planet.myplanet.ui.resources.ResourcesFragment
import org.ole.planet.myplanet.ui.mylife.LifeFragment
import org.ole.planet.myplanet.ui.submission.AdapterMySubmission
import org.ole.planet.myplanet.ui.submission.MySubmissionFragment
import org.ole.planet.myplanet.ui.survey.SurveyFragment
import org.ole.planet.myplanet.ui.team.TeamFragment
import org.ole.planet.myplanet.utilities.NetworkUtils.coroutineScope
import org.ole.planet.myplanet.utilities.NetworkUtils.isNetworkConnectedFlow
import org.ole.planet.myplanet.utilities.TimeUtils
import java.net.*
import java.util.Date

class BellDashboardFragment : BaseDashboardFragment() {
Expand All @@ -51,6 +58,25 @@ class BellDashboardFragment : BaseDashboardFragment() {
super.onViewCreated(view, savedInstanceState)
fragmentHomeBellBinding.cardProfileBell.txtDate.text = TimeUtils.formatDate(Date().time)
fragmentHomeBellBinding.cardProfileBell.txtCommunityName.text = model.planetCode
isNetworkConnectedFlow.onEach { isConnected ->
if (isConnected) {
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_yellow_600)
val serverUrl = settings?.getString("serverURL", "")
if (!serverUrl.isNullOrEmpty()) {
lifecycleScope.launch {
val canReachServer = withContext(Dispatchers.IO) {
isServerReachable(serverUrl)
}
if (canReachServer) {
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.green)
}
}
}
} else {
fragmentHomeBellBinding.cardProfileBell.imageView.borderColor = ContextCompat.getColor(requireActivity(), R.color.md_red_700)
}
}.launchIn(coroutineScope)

(activity as DashboardActivity?)?.supportActionBar?.hide()
fragmentHomeBellBinding.addResource.setOnClickListener {
AddResourceFragment().show(childFragmentManager, getString(R.string.add_res))
Expand Down Expand Up @@ -92,6 +118,27 @@ class BellDashboardFragment : BaseDashboardFragment() {
}
}

private suspend fun isServerReachable(urlString: String): Boolean {
return try {
val url = URL(urlString)
val connection = withContext(Dispatchers.IO) {
url.openConnection()
} as HttpURLConnection
connection.requestMethod = "GET"
connection.connectTimeout = 5000
connection.readTimeout = 5000
withContext(Dispatchers.IO) {
connection.connect()
}
val responseCode = connection.responseCode
connection.disconnect()
responseCode in 200..299

} catch (e: Exception) {
false
}
}

private fun showBadges() {
fragmentHomeBellBinding.cardProfileBell.llBadges.removeAllViews()
val courseCount = countCourseIds(mRealm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
inputName.setText(settings.getString(getString(R.string.login_user), ""))
inputPassword.setText(settings.getString(getString(R.string.login_password), ""))
}
if (isNetworkConnected()) {
if (isNetworkConnected) {
service.syncPlanetServers(mRealm) { success: String? ->
Utilities.toast(this, success)
}
Expand Down
110 changes: 100 additions & 10 deletions app/src/main/java/org/ole/planet/myplanet/utilities/NetworkUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,126 @@ package org.ole.planet.myplanet.utilities
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import androidx.annotation.RequiresApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.*
import org.ole.planet.myplanet.MainApplication.Companion.context
import org.ole.planet.myplanet.ui.dashboard.DashboardFragment
import org.ole.planet.myplanet.utilities.Constants.PREFS_NAME
import java.net.NetworkInterface
import java.util.Collections
import java.util.Locale

object NetworkUtils {
private val connectivityManager by lazy {
lateinit var coroutineScope: CoroutineScope

fun initialize(coroutineScope: CoroutineScope) {
this.coroutineScope = coroutineScope
}

private val connectivityManager: ConnectivityManager by lazy {
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
}

private val _currentNetwork = MutableStateFlow(provideDefaultCurrentNetwork())

val isNetworkConnectedFlow: StateFlow<Boolean> by lazy {
_currentNetwork
.map { it.isConnected() }
.stateIn(scope = coroutineScope, started = SharingStarted.WhileSubscribed(), initialValue = _currentNetwork.value.isConnected())
}

val isNetworkConnected: Boolean
get() = isNetworkConnectedFlow.value

private val networkCallback = NetworkCallback()

@RequiresApi(Build.VERSION_CODES.N)
fun startListenNetworkState() {
if (_currentNetwork.value.isListening) {
return
}

_currentNetwork.update {
provideDefaultCurrentNetwork().copy(isListening = true)
}

connectivityManager.registerDefaultNetworkCallback(networkCallback)
}

fun stopListenNetworkState() {
if (!_currentNetwork.value.isListening) {
return
}

_currentNetwork.update {
it.copy(isListening = false)
}

connectivityManager.unregisterNetworkCallback(networkCallback)
}

private class NetworkCallback : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
_currentNetwork.update {
it.copy(isAvailable = true)
}
}

override fun onLost(network: Network) {
_currentNetwork.update {
it.copy(isAvailable = false, networkCapabilities = null)
}
}

override fun onUnavailable() {
_currentNetwork.update {
it.copy(isAvailable = false, networkCapabilities = null)
}
}

override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
_currentNetwork.update {
it.copy(networkCapabilities = networkCapabilities)
}
}

override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
_currentNetwork.update {
it.copy(isBlocked = blocked)
}
}
}

private fun provideDefaultCurrentNetwork(): CurrentNetwork {
return CurrentNetwork(isListening = false, networkCapabilities = null, isAvailable = false, isBlocked = false)
}

private data class CurrentNetwork(val isListening: Boolean, val networkCapabilities: NetworkCapabilities?, val isAvailable: Boolean, val isBlocked: Boolean)

private fun CurrentNetwork.isConnected(): Boolean {
return isListening && isAvailable && !isBlocked && networkCapabilities.isNetworkCapabilitiesValid()
}

private fun NetworkCapabilities?.isNetworkCapabilitiesValid(): Boolean = when {
this == null -> false
hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) && hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) && (hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || hasTransport(NetworkCapabilities.TRANSPORT_VPN) || hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) -> true
else -> false
}

@JvmStatic
fun isWifiEnabled(): Boolean {
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
return wifiManager.isWifiEnabled
}

@RequiresApi(Build.VERSION_CODES.M)
@JvmStatic
fun isWifiConnected(): Boolean {
val network = connectivityManager.activeNetwork
Expand All @@ -44,6 +141,7 @@ object NetworkUtils {
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled
}

@RequiresApi(Build.VERSION_CODES.M)
@JvmStatic
fun getCurrentNetworkId(context: Context): Int {
var ssid = -1
Expand All @@ -60,14 +158,6 @@ object NetworkUtils {
return ssid
}

@JvmStatic
fun isNetworkConnected(): Boolean {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connManager.activeNetwork
return network != null
}


@JvmStatic
fun getUniqueIdentifier(): String {
val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
Expand Down

0 comments on commit db52d4f

Please sign in to comment.