Skip to content

Commit

Permalink
Reworks the wifi details layout and makes some improvements to the Wi…
Browse files Browse the repository at this point in the history
…-Fi Chart
  • Loading branch information
christianrowlands committed Jan 8, 2024
1 parent d149ab7 commit 573c9d4
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 99 deletions.
1 change: 1 addition & 0 deletions networksurvey/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ dependencies {
debugImplementation "androidx.compose.ui:ui-tooling"
implementation "androidx.compose.ui:ui-tooling-preview"
implementation "androidx.lifecycle:lifecycle-viewmodel-compose"
implementation "androidx.lifecycle:lifecycle-runtime-compose"
implementation "com.google.accompanist:accompanist-themeadapter-material:0.28.0"

// Only include firebase in the google play build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.craxiom.networksurvey.constants.WifiBeaconMessageConstants;
import com.craxiom.networksurvey.model.WifiNetwork;
import com.craxiom.networksurvey.model.WifiRecordWrapper;
import com.craxiom.networksurvey.util.ColorUtils;

import timber.log.Timber;

Expand Down Expand Up @@ -69,9 +70,9 @@ public void onBindViewHolder(final ViewHolder holder, int position)

if (data.hasSignalStrength())
{
final float signalStrength = data.getSignalStrength().getValue();
final int signalStrength = (int) data.getSignalStrength().getValue();
holder.signalStrength.setText(context.getString(R.string.dbm_value, String.valueOf(signalStrength)));
holder.signalStrength.setTextColor(context.getResources().getColor(getColorForSignalStrength(signalStrength), null));
holder.signalStrength.setTextColor(context.getResources().getColor(ColorUtils.getColorForWifiSignalStrength(signalStrength), null));
} else
{
holder.signalStrength.setText("");
Expand All @@ -92,33 +93,6 @@ public int getItemCount()
return wifiRecords.size();
}

/**
* @param signalStrength The signal strength value in dBm.
* @return The resource ID for the color that should be used for the signal strength text.
*/
private int getColorForSignalStrength(float signalStrength)
{
final int colorResourceId;
if (signalStrength > -60)
{
colorResourceId = R.color.rssi_green;
} else if (signalStrength > -70)
{
colorResourceId = R.color.rssi_yellow;
} else if (signalStrength > -80)
{
colorResourceId = R.color.rssi_orange;
} else if (signalStrength > -90)
{
colorResourceId = R.color.rssi_red;
} else
{
colorResourceId = R.color.rssi_deep_red;
}

return colorResourceId;
}

/**
* Navigates to the Wi-Fi details screen for the selected Wi-Fi network.
*/
Expand Down Expand Up @@ -158,6 +132,7 @@ class ViewHolder extends RecyclerView.ViewHolder
passpoint = view.findViewById(R.id.wifi_passpoint);
capabilities = view.findViewById(R.id.wifi_capabilities);

// FIXME Make a click anywhere on the row navigate to the details screen, even when clicking on text
mView.setOnClickListener(v -> {
Float signalStrength = null;
WifiBeaconRecordData data = wifiRecord.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.craxiom.networksurvey.listeners.IWifiSurveyRecordListener
import com.craxiom.networksurvey.model.WifiNetwork
import com.craxiom.networksurvey.model.WifiRecordWrapper
import com.craxiom.networksurvey.services.NetworkSurveyService
import com.craxiom.networksurvey.ui.wifi.UNKNOWN_RSSI
import com.craxiom.networksurvey.ui.wifi.WifiDetailsScreen
import com.craxiom.networksurvey.ui.wifi.WifiDetailsViewModel
import com.craxiom.networksurvey.util.NsTheme
Expand All @@ -36,7 +37,9 @@ class WifiDetailsFragment : AServiceDataFragment(), IWifiSurveyRecordListener {
setContent {
viewModel = viewModel()
viewModel.wifiNetwork = wifiNetwork
if (wifiNetwork.signalStrength != null) {
if (wifiNetwork.signalStrength == null) {
viewModel.addInitialRssi(UNKNOWN_RSSI)
} else {
viewModel.addInitialRssi(wifiNetwork.signalStrength!!)
}
NsTheme {
Expand Down Expand Up @@ -70,13 +73,16 @@ class WifiDetailsFragment : AServiceDataFragment(), IWifiSurveyRecordListener {

if (targetWifiRecordWrapper == null) {
Timber.i("No wifi record found for ${wifiNetwork.bssid} in the wifi scan results")
viewModel.addNewRssi(UNKNOWN_RSSI)
return
}

if (targetWifiRecordWrapper.wifiBeaconRecord.data.hasSignalStrength()) {
viewModel.addNewRssi(targetWifiRecordWrapper.wifiBeaconRecord.data.signalStrength.value)
} else {
Timber.i("No signal strength present for ${wifiNetwork.bssid} in the wifi beacon record")
viewModel.addNewRssi(UNKNOWN_RSSI)

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.rememberLazyListState
Expand All @@ -13,9 +17,16 @@ import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.craxiom.networksurvey.R
import com.craxiom.networksurvey.constants.WifiBeaconMessageConstants.HIDDEN_SSID_PLACEHOLDER
import com.craxiom.networksurvey.util.ColorUtils

/**
* A Compose screen that shows the details of a single WiFi network. The main purpose for this
Expand All @@ -26,58 +37,163 @@ import androidx.compose.ui.unit.dp
internal fun WifiDetailsScreen(
viewModel: WifiDetailsViewModel
) {
val context = LocalContext.current
val rssi by viewModel.rssiFlow.collectAsStateWithLifecycle()
val colorId = ColorUtils.getColorForWifiSignalStrength(rssi)
val colorResource = Color(context.getColor(colorId))

LazyColumn(
state = rememberLazyListState(),
contentPadding = PaddingValues(padding),
verticalArrangement = Arrangement.spacedBy(padding),
) {
chartItems(viewModel)
chartItems(viewModel, colorResource, rssi)
}
}

private fun LazyListScope.chartItems(
viewModel: WifiDetailsViewModel,
signalStrengthColor: Color,
rssi: Float
) {
val hiddenSsid = viewModel.wifiNetwork.ssid.isEmpty()
// TODO Make text selectable
item {
Card(shape = MaterialTheme.shapes.large, colors = CardDefaults.elevatedCardColors()) {
Column(modifier = Modifier.padding(padding)) {
Text(
text = "BSSID: ${viewModel.wifiNetwork.bssid}",
style = MaterialTheme.typography.titleMedium
)
Text(
text = "SSID: ${if (viewModel.wifiNetwork.ssid.isEmpty()) "Hidden Network" else viewModel.wifiNetwork.ssid}",
style = MaterialTheme.typography.titleMedium
)
// TODO Update the signal strength with every scan
//Text(text = "Signal Strength: ${viewModel.wifiNetwork.signalStrength?.toString() ?: "Unknown"} dBm", style = MaterialTheme.typography.titleMedium)
Text(
text = "Frequency: ${viewModel.wifiNetwork.frequency?.toString() ?: "Unknown"} MHz",
style = MaterialTheme.typography.titleMedium
)
Text(
text = "Channel: ${viewModel.wifiNetwork.channel?.toString() ?: "Unknown"}",
style = MaterialTheme.typography.titleMedium
)
Text(
text = "Encryption: ${viewModel.wifiNetwork.encryptionType}",
style = MaterialTheme.typography.titleMedium
)
Text(
text = "Passpoint: ${if (viewModel.wifiNetwork.passpoint == true) "Yes" else "No"}",
style = MaterialTheme.typography.titleMedium
)
Text(
text = "Capabilities: ${viewModel.wifiNetwork.capabilities}",
style = MaterialTheme.typography.titleMedium
)
Row(
modifier = Modifier
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.SpaceAround
) {
Card(
modifier = Modifier
.fillMaxWidth(),
shape = MaterialTheme.shapes.large,
colors = CardDefaults.elevatedCardColors()
) {
Row(
modifier = Modifier
.padding(vertical = padding / 2)
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.SpaceEvenly
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = if (hiddenSsid) HIDDEN_SSID_PLACEHOLDER else viewModel.wifiNetwork.ssid,
style = MaterialTheme.typography.titleMedium.copy(
color = Color(
LocalContext.current.getColor(
if (hiddenSsid) R.color.red else R.color.colorAccent
)
)
)
)
Text(
text = "SSID",
style = MaterialTheme.typography.labelMedium
)
}

Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = if (rssi == UNKNOWN_RSSI) "Unknown" else "${rssi.toInt()} dBm",
style = MaterialTheme.typography.titleMedium.copy(color = signalStrengthColor)
)
Text(
text = "Signal Strength",
style = MaterialTheme.typography.labelMedium
)
}
}

Row(
modifier = Modifier
.padding(horizontal = padding, vertical = padding / 2)
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.SpaceBetween
) {
Column {
Text(
text = "BSSID: ${viewModel.wifiNetwork.bssid}",
style = MaterialTheme.typography.titleMedium
)
}
}

Row(
modifier = Modifier
.padding(start = padding, end = padding, bottom = padding / 2)
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = viewModel.wifiNetwork.encryptionType,
style = MaterialTheme.typography.titleMedium
)
}

Row(
modifier = Modifier
.padding(start = padding, end = padding, bottom = padding / 2)
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.Start
) {
Text(
text = "Channel: ${viewModel.wifiNetwork.channel?.toString() ?: "Unknown"}",
style = MaterialTheme.typography.titleMedium
)
Spacer(modifier = Modifier.width(padding * 2))
Text(
text = "${viewModel.wifiNetwork.frequency?.toString() ?: "Unknown"} MHz",
style = MaterialTheme.typography.titleMedium
)
}

if (viewModel.wifiNetwork.passpoint != null && viewModel.wifiNetwork.passpoint == true) {

Row(
modifier = Modifier
.padding(start = padding, end = padding, bottom = padding / 2)
.fillMaxWidth(),
verticalAlignment = Alignment.Top,
horizontalArrangement = Arrangement.Start
) {
Text(
text = "Passpoint",
style = MaterialTheme.typography.titleMedium.copy(
color = Color(
LocalContext.current.getColor(R.color.colorAccent)
)
)
)
}
}
}
}
}

cardItem { WifiRssiChart(viewModel.modelProducer) }
cardItem {
Column(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Signal Strength (Last 2 Minutes)",
style = MaterialTheme.typography.titleMedium
)
WifiRssiChart(viewModel.modelProducer)
}
}
}


private fun LazyListScope.cardItem(content: @Composable () -> Unit) {
item {
Card(shape = MaterialTheme.shapes.large, colors = CardDefaults.elevatedCardColors()) {
Expand All @@ -89,6 +205,3 @@ private fun LazyListScope.cardItem(content: @Composable () -> Unit) {
}

private val padding = 16.dp

private const val COLOR_1_CODE = 0xffa485e0
private val color1 = Color(COLOR_1_CODE)
Loading

0 comments on commit 573c9d4

Please sign in to comment.