-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass at a Wi-Fi Spectrum chart. Only added 2.4 GHz, and Vico do…
…es not yet support custom data labels
- Loading branch information
1 parent
1a037ed
commit e14975a
Showing
18 changed files
with
814 additions
and
8 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
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
127 changes: 127 additions & 0 deletions
127
networksurvey/src/main/java/com/craxiom/networksurvey/fragments/WifiSpectrumFragment.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,127 @@ | ||
package com.craxiom.networksurvey.fragments | ||
|
||
import android.content.SharedPreferences | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.ViewCompositionStrategy | ||
import androidx.lifecycle.viewmodel.compose.viewModel | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.preference.PreferenceManager | ||
import com.craxiom.networksurvey.constants.NetworkSurveyConstants | ||
import com.craxiom.networksurvey.listeners.IWifiSurveyRecordListener | ||
import com.craxiom.networksurvey.model.WifiRecordWrapper | ||
import com.craxiom.networksurvey.services.NetworkSurveyService | ||
import com.craxiom.networksurvey.ui.wifi.WifiSpectrumChartViewModel | ||
import com.craxiom.networksurvey.ui.wifi.WifiSpectrumScreen | ||
import com.craxiom.networksurvey.util.NsTheme | ||
import com.craxiom.networksurvey.util.PreferenceUtils | ||
|
||
/** | ||
* The fragment that displays the details of a single Wifi network from the scan results. | ||
*/ | ||
class WifiSpectrumFragment : AServiceDataFragment(), IWifiSurveyRecordListener { | ||
private lateinit var viewModel: WifiSpectrumChartViewModel | ||
|
||
private lateinit var sharedPreferences: SharedPreferences | ||
private val preferenceChangeListener = | ||
SharedPreferences.OnSharedPreferenceChangeListener { _, key -> | ||
if (key == NetworkSurveyConstants.PROPERTY_WIFI_SCAN_INTERVAL_SECONDS) { | ||
val wifiScanRateMs = PreferenceUtils.getScanRatePreferenceMs( | ||
NetworkSurveyConstants.PROPERTY_WIFI_SCAN_INTERVAL_SECONDS, | ||
NetworkSurveyConstants.DEFAULT_WIFI_SCAN_INTERVAL_SECONDS, | ||
context | ||
) | ||
viewModel.setScanRateSeconds(wifiScanRateMs / 1_000) | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
val composeView = ComposeView(requireContext()) | ||
|
||
composeView.apply { | ||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
setContent { | ||
viewModel = viewModel() | ||
|
||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) | ||
sharedPreferences.registerOnSharedPreferenceChangeListener( | ||
preferenceChangeListener | ||
) | ||
val scanRateMs = PreferenceUtils.getScanRatePreferenceMs( | ||
NetworkSurveyConstants.PROPERTY_WIFI_SCAN_INTERVAL_SECONDS, | ||
NetworkSurveyConstants.DEFAULT_WIFI_SCAN_INTERVAL_SECONDS, | ||
context | ||
) | ||
viewModel.setScanRateSeconds(scanRateMs / 1_000) | ||
viewModel.initializeCharts() | ||
|
||
NsTheme { | ||
WifiSpectrumScreen( | ||
viewModel = viewModel, | ||
wifiSpectrumFragment = this@WifiSpectrumFragment | ||
) | ||
} | ||
} | ||
} | ||
|
||
return composeView | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
|
||
startAndBindToService() | ||
} | ||
|
||
override fun onPause() { | ||
sharedPreferences.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener) | ||
|
||
super.onPause() | ||
} | ||
|
||
override fun onSurveyServiceConnected(service: NetworkSurveyService?) { | ||
if (service == null) return | ||
service.registerWifiSurveyRecordListener(this) | ||
} | ||
|
||
override fun onSurveyServiceDisconnecting(service: NetworkSurveyService?) { | ||
if (service == null) return | ||
service.unregisterWifiSurveyRecordListener(this) | ||
} | ||
|
||
override fun onWifiBeaconSurveyRecords(wifiBeaconRecords: MutableList<WifiRecordWrapper>?) { | ||
val wifiNetworkInfoList: List<WifiNetworkInfo> = wifiBeaconRecords | ||
?.filter { it.wifiBeaconRecord.data.hasSignalStrength() && it.wifiBeaconRecord.data.ssid != null && it.wifiBeaconRecord.data.hasChannel() } | ||
?.map { | ||
WifiNetworkInfo( | ||
it.wifiBeaconRecord.data.ssid!!, | ||
it.wifiBeaconRecord.data.signalStrength.value.toInt(), | ||
it.wifiBeaconRecord.data.channel.value | ||
) | ||
} | ||
?: emptyList() | ||
|
||
viewModel.onWifiScanResults(wifiNetworkInfoList) | ||
} | ||
|
||
|
||
/** | ||
* Navigates to the Settings UI (primarily for the user to change the scan rate) | ||
*/ | ||
fun navigateToSettings() { | ||
findNavController().navigate(WifiDetailsFragmentDirections.actionWifiDetailsToSettings()) | ||
} | ||
} | ||
|
||
data class WifiNetworkInfo( | ||
val ssid: String, | ||
val signalStrength: Int, | ||
val channel: Int | ||
) |
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
64 changes: 64 additions & 0 deletions
64
networksurvey/src/main/java/com/craxiom/networksurvey/ui/wifi/SpectrumChartStyle.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,64 @@ | ||
package com.craxiom.networksurvey.ui.wifi | ||
|
||
import androidx.compose.foundation.isSystemInDarkTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.toArgb | ||
import com.patrykandpatrick.vico.compose.component.shape.shader.color | ||
import com.patrykandpatrick.vico.compose.style.ChartStyle | ||
import com.patrykandpatrick.vico.core.DefaultColors | ||
import com.patrykandpatrick.vico.core.DefaultDimens | ||
import com.patrykandpatrick.vico.core.chart.layer.LineCartesianLayer | ||
import com.patrykandpatrick.vico.core.component.shape.LineComponent | ||
import com.patrykandpatrick.vico.core.component.shape.Shapes | ||
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders | ||
|
||
@Composable | ||
internal fun rememberSpectrumChartStyle( | ||
columnLayerColors: List<Color>, | ||
lineLayerColors: List<Color>, | ||
): ChartStyle { | ||
val isSystemInDarkTheme = isSystemInDarkTheme() | ||
// TODO Use the textComponent for the SSID labels when support is added to the library | ||
/*val textComponent = rememberTextComponent( | ||
color = Color(if (isSystemInDarkTheme) DefaultColors.Dark.axisLabelColor else DefaultColors.Light.axisLabelColor), | ||
)*/ | ||
|
||
return remember(columnLayerColors, lineLayerColors, isSystemInDarkTheme) { | ||
val defaultColors = if (isSystemInDarkTheme) DefaultColors.Dark else DefaultColors.Light | ||
ChartStyle( | ||
ChartStyle.Axis( | ||
axisLabelColor = Color(defaultColors.axisLabelColor), | ||
axisGuidelineColor = Color(defaultColors.axisGuidelineColor), | ||
axisLineColor = Color(defaultColors.axisLineColor), | ||
), | ||
ChartStyle.ColumnLayer( | ||
columnLayerColors.map { columnChartColor -> | ||
LineComponent( | ||
columnChartColor.toArgb(), | ||
DefaultDimens.COLUMN_WIDTH, | ||
Shapes.roundedCornerShape(DefaultDimens.COLUMN_ROUNDNESS_PERCENT), | ||
) | ||
}, | ||
), | ||
ChartStyle.LineLayer( | ||
lineLayerColors.map { lineChartColor -> | ||
LineCartesianLayer.LineSpec( | ||
pointConnector = SpectrumPointConnector(), | ||
//dataLabel = textComponent, | ||
thicknessDp = 3f, | ||
shader = DynamicShaders.color(lineChartColor), | ||
//backgroundShader = null, | ||
) | ||
}, | ||
), | ||
ChartStyle.Marker(), | ||
Color(defaultColors.elevationOverlayColor), | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun rememberSpectrumChartStyle(chartColors: List<Color>) = | ||
rememberSpectrumChartStyle(columnLayerColors = chartColors, lineLayerColors = chartColors) |
Oops, something went wrong.