Skip to content

Commit

Permalink
Update the Tower Map if the SIM count changes
Browse files Browse the repository at this point in the history
  • Loading branch information
christianrowlands committed Jul 25, 2024
1 parent 74b1cee commit 44ba1ca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.craxiom.networksurvey.fragments

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationListener
import android.location.LocationManager
import android.os.Bundle
Expand All @@ -15,9 +18,11 @@ import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.core.view.MenuProvider
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.craxiom.networksurvey.R
import com.craxiom.networksurvey.SimChangeReceiver
import com.craxiom.networksurvey.listeners.ICellularSurveyRecordListener
import com.craxiom.networksurvey.model.CellularProtocol
import com.craxiom.networksurvey.model.CellularRecordWrapper
Expand All @@ -27,6 +32,7 @@ import com.craxiom.networksurvey.ui.cellular.model.ServingCellInfo
import com.craxiom.networksurvey.ui.cellular.model.TowerMapViewModel
import com.craxiom.networksurvey.util.NsTheme
import com.craxiom.networksurvey.util.PreferenceUtils
import timber.log.Timber
import java.util.Collections

/**
Expand All @@ -37,6 +43,25 @@ class TowerMapFragment : AServiceDataFragment(), MenuProvider, ICellularSurveyRe
private lateinit var composeView: ComposeView
private var servingCell: ServingCellInfo? = null
private var locationListener: LocationListener? = null
private var simBroadcastReceiver = object : BroadcastReceiver(
) {
override fun onReceive(context: Context, intent: Intent) {
Timber.i("SIM State Change Detected. Updating the tower map view model")
viewModel?.resetSimCount()
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val context = context
if (context != null) {
LocalBroadcastManager.getInstance(context).registerReceiver(
simBroadcastReceiver,
IntentFilter(SimChangeReceiver.SIM_CHANGED_INTENT)
)
}
}

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -77,6 +102,15 @@ class TowerMapFragment : AServiceDataFragment(), MenuProvider, ICellularSurveyRe
super.onPause()
}

override fun onDestroy() {
val context = context
if (context != null) {
LocalBroadcastManager.getInstance(context).unregisterReceiver(simBroadcastReceiver)
}

super.onDestroy()
}

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.cellular_map_menu, menu)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ internal class TowerMapViewModel : ASignalChartViewModel() {
recreateOverlaysFromTowerData(mapView, false)
}

/**
* Triggers any necessary updates to SIM count aware variables.
*/
fun resetSimCount() {
_servingCells.update {
it.clear()
it
}
_servingSignals.value = ServingSignalInfo(CellularProtocol.NONE, 0, 0)
}

/**
* Recreates the overlays on the map based on the current tower data.
* @param mapView The map view to add the overlays to.
Expand Down

0 comments on commit 44ba1ca

Please sign in to comment.