|
| 1 | +package org.osservatorionessuno.bugbane.screens |
| 2 | + |
| 3 | +import androidx.compose.foundation.border |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.layout.* |
| 6 | +import androidx.compose.foundation.lazy.LazyColumn |
| 7 | +import androidx.compose.foundation.lazy.items |
| 8 | +import androidx.compose.material3.* |
| 9 | +import androidx.compose.runtime.* |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.res.stringResource |
| 12 | +import androidx.compose.ui.text.font.FontWeight |
| 13 | +import androidx.compose.ui.text.style.TextOverflow |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | +import org.json.JSONObject |
| 16 | +import java.io.File |
| 17 | +import java.text.DateFormat |
| 18 | +import java.time.Instant |
| 19 | +import java.util.Date |
| 20 | +import org.osservatorionessuno.bugbane.R |
| 21 | + |
| 22 | +@Composable |
| 23 | +fun ScanDetailScreen(acquisitionDir: File, scanFile: File) { |
| 24 | + val dateFormat = remember { DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT) } |
| 25 | + var acquisitionMeta by remember { mutableStateOf<JSONObject?>(null) } |
| 26 | + var scanMeta by remember { mutableStateOf<JSONObject?>(null) } |
| 27 | + var results by remember { mutableStateOf(listOf<ScanResult>()) } |
| 28 | + var selected by remember { mutableStateOf<ScanResult?>(null) } |
| 29 | + |
| 30 | + LaunchedEffect(acquisitionDir, scanFile) { |
| 31 | + val metaFile = File(acquisitionDir, "acquisition.json") |
| 32 | + if (metaFile.exists()) { |
| 33 | + try { acquisitionMeta = JSONObject(metaFile.readText()) } catch (_: Throwable) {} |
| 34 | + } |
| 35 | + try { |
| 36 | + val obj = JSONObject(scanFile.readText()) |
| 37 | + scanMeta = obj |
| 38 | + val arr = obj.optJSONArray("results") |
| 39 | + val tmp = mutableListOf<ScanResult>() |
| 40 | + if (arr != null) { |
| 41 | + for (i in 0 until arr.length()) { |
| 42 | + val o = arr.getJSONObject(i) |
| 43 | + tmp += ScanResult( |
| 44 | + o.optString("file"), |
| 45 | + o.optString("type"), |
| 46 | + o.optString("ioc"), |
| 47 | + o.optString("context") |
| 48 | + ) |
| 49 | + } |
| 50 | + } |
| 51 | + results = tmp |
| 52 | + } catch (_: Exception) { |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + Column( |
| 57 | + modifier = Modifier |
| 58 | + .fillMaxSize() |
| 59 | + .padding(16.dp), |
| 60 | + verticalArrangement = Arrangement.spacedBy(12.dp) |
| 61 | + ) { |
| 62 | + Text( |
| 63 | + stringResource(R.string.analysis_details_acquisition_name, acquisitionDir.name), |
| 64 | + style = MaterialTheme.typography.bodyLarge |
| 65 | + ) |
| 66 | + acquisitionMeta?.let { |
| 67 | + val uuid = it.optString("uuid") |
| 68 | + val completed = it.optString("completed").let { s -> |
| 69 | + try { dateFormat.format(Date.from(Instant.parse(s))) } catch (_: Exception) { s } |
| 70 | + } |
| 71 | + Text( |
| 72 | + stringResource(R.string.analysis_details_acquisition_uuid, uuid), |
| 73 | + style = MaterialTheme.typography.bodyLarge |
| 74 | + ) |
| 75 | + Text( |
| 76 | + stringResource(R.string.analysis_details_acquisition_completed, completed), |
| 77 | + style = MaterialTheme.typography.bodyLarge |
| 78 | + ) |
| 79 | + } |
| 80 | + scanMeta?.let { |
| 81 | + val completed = it.optString("completed").let { s -> |
| 82 | + try { dateFormat.format(Date.from(Instant.parse(s))) } catch (_: Exception) { s } |
| 83 | + } |
| 84 | + Text( |
| 85 | + stringResource(R.string.analysis_details_analysis_completed, completed), |
| 86 | + style = MaterialTheme.typography.bodyLarge |
| 87 | + ) |
| 88 | + } |
| 89 | + ResultList(results = results, onSelect = { selected = it }, modifier = Modifier.weight(1f)) |
| 90 | + } |
| 91 | + |
| 92 | + selected?.let { res -> |
| 93 | + AlertDialog( |
| 94 | + onDismissRequest = { selected = null }, |
| 95 | + confirmButton = { |
| 96 | + TextButton(onClick = { selected = null }) { |
| 97 | + Text(stringResource(R.string.acquisition_passphrase_close)) |
| 98 | + } |
| 99 | + }, |
| 100 | + title = { Text(stringResource(R.string.analysis_details_context_dialog_title)) }, |
| 101 | + text = { Text(res.context) } |
| 102 | + ) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +data class ScanResult( |
| 107 | + val file: String, |
| 108 | + val type: String, |
| 109 | + val ioc: String, |
| 110 | + val context: String, |
| 111 | +) |
| 112 | + |
| 113 | +@Composable |
| 114 | +private fun ResultList( |
| 115 | + results: List<ScanResult>, |
| 116 | + onSelect: (ScanResult) -> Unit, |
| 117 | + modifier: Modifier = Modifier |
| 118 | +) { |
| 119 | + Box( |
| 120 | + modifier = modifier |
| 121 | + .border(1.dp, MaterialTheme.colorScheme.outline) |
| 122 | + .fillMaxSize() |
| 123 | + ) { |
| 124 | + LazyColumn(modifier = Modifier.fillMaxSize().padding(8.dp)) { |
| 125 | + item { |
| 126 | + Row(modifier = Modifier.fillMaxWidth()) { |
| 127 | + Text( |
| 128 | + stringResource(R.string.analysis_details_artifact), |
| 129 | + modifier = Modifier.weight(1f), |
| 130 | + fontWeight = FontWeight.SemiBold |
| 131 | + ) |
| 132 | + Text( |
| 133 | + stringResource(R.string.analysis_details_type), |
| 134 | + modifier = Modifier.weight(1f), |
| 135 | + fontWeight = FontWeight.SemiBold |
| 136 | + ) |
| 137 | + Text( |
| 138 | + stringResource(R.string.analysis_details_ioc), |
| 139 | + modifier = Modifier.weight(1f), |
| 140 | + fontWeight = FontWeight.SemiBold |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | + items(results) { r -> |
| 145 | + Row( |
| 146 | + modifier = Modifier |
| 147 | + .fillMaxWidth() |
| 148 | + .clickable { onSelect(r) } |
| 149 | + ) { |
| 150 | + Text(r.file, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) |
| 151 | + Text(r.type, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) |
| 152 | + Text(r.ioc, modifier = Modifier.weight(1f), maxLines = 1, overflow = TextOverflow.Ellipsis) |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments