Skip to content

Commit c46cfca

Browse files
committed
bugfixing
1 parent 6926951 commit c46cfca

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,18 @@ private class CreatePartDataHolder(val vm: WizardActivityState, val desiredStart
139139
var code by mutableStateOf(code)
140140
var id by mutableStateOf(id)
141141
var sparse by mutableStateOf(sparse)
142+
fun resolveSectorSize(c: CreatePartDataHolder, remaining: Long): Long {
143+
return if (!isPercent /*bytes*/) {
144+
size / c.meta!!.logicalSectorSizeBytes
145+
} else /*percent*/ {
146+
(BigDecimal(remaining).multiply(BigDecimal(size).divide(BigDecimal(100)))).toLong()
147+
}
148+
}
142149
}
143150
val parts = mutableStateListOf<Part>()
144151
val inetAvailable = HashMap<String, Pair<String, String>>()
145152
val idNeeded = mutableStateListOf<String>()
153+
val extraIdNeeded = mutableListOf<String>()
146154
val chosen = mutableStateMapOf<String, DledFile>()
147155
val client by lazy { OkHttpClient().newBuilder().readTimeout(1L, TimeUnit.HOURS).addNetworkInterceptor {
148156
val originalResponse: Response = it.proceed(it.request())
@@ -263,7 +271,7 @@ private fun Start(c: CreatePartDataHolder) {
263271
}
264272
TextField(value = partitionName, onValueChange = {
265273
partitionName = it
266-
}, isError = partitionNameInvalid, label = {
274+
}, isError = partitionNameInvalid && partitionName.isNotEmpty(), label = {
267275
Text(stringResource(R.string.part_name))
268276
})
269277
Row(horizontalArrangement = Arrangement.End, modifier = Modifier
@@ -388,6 +396,7 @@ private fun Shop(c: CreatePartDataHolder) {
388396
if (idUnneeded.contains(id))
389397
throw IllegalStateException("id in both blockIdNeeded and extraIdNeeded")
390398
idNeeded.add(id)
399+
this.extraIdNeeded.add(id)
391400
i++
392401
}
393402
i = 0
@@ -550,11 +559,7 @@ private fun Os(c: CreatePartDataHolder) {
550559
var sizeInSectors: Long = -1
551560
var remaining = c.endSectorRelative - c.startSectorRelative
552561
for (iPart in c.parts.slice(0..i)) {
553-
sizeInSectors = if (!iPart.isPercent /*bytes*/) {
554-
iPart.size / c.meta!!.logicalSectorSizeBytes
555-
} else /*percent*/ {
556-
(BigDecimal(remaining).multiply(BigDecimal(iPart.size).divide(BigDecimal(100)))).toLong()
557-
}
562+
sizeInSectors = iPart.resolveSectorSize(c, remaining)
558563
remaining -= sizeInSectors
559564
}
560565
remaining += sizeInSectors
@@ -663,13 +668,7 @@ private fun Os(c: CreatePartDataHolder) {
663668
}
664669
var remaining = c.endSectorRelative - c.startSectorRelative
665670
for (part in c.parts) {
666-
val sizeInSectors = if (!part.isPercent /*bytes*/) {
667-
part.size / c.meta!!.logicalSectorSizeBytes
668-
} else /*percent*/ {
669-
// remaining * (part.int/100) -> part.int percent of remaining
670-
(BigDecimal(remaining).multiply(BigDecimal(part.size).divide(BigDecimal(100)))).toLong()
671-
}
672-
remaining -= sizeInSectors
671+
remaining -= part.resolveSectorSize(c, remaining)
673672
}
674673
Text(stringResource(R.string.remaining_sector, remaining, c.endSectorRelative - c.startSectorRelative))
675674
}
@@ -867,24 +866,31 @@ private fun Flash(c: CreatePartDataHolder) {
867866
terminal.add(vm.activity.getString(R.string.term_creating_pt))
868867

869868
vm.logic.unmountBootset()
869+
val startSectorAbsolute = c.p.startSector + c.startSectorRelative
870+
val endSectorAbsolute = c.p.startSector + c.endSectorRelative
871+
if (endSectorAbsolute > c.p.endSector)
872+
throw IllegalArgumentException("$endSectorAbsolute can't be bigger than ${c.p.endSector}")
870873
c.parts.forEachIndexed { index, part ->
871874
terminal.add(vm.activity.getString(R.string.term_create_part))
872-
val k = if (!part.isPercent /*bytes*/) {
873-
part.size / c.meta!!.logicalSectorSizeBytes
874-
} else /*percent*/ {
875-
(BigDecimal(c.p.size - (c.startSectorRelative + (c.p.size - c.endSectorRelative))).multiply(BigDecimal(part.size).divide(BigDecimal(100)))).toLong()
876-
}
877-
878-
val r = vm.logic.create(c.p, c.startSectorRelative, c.startSectorRelative + k, part.code, "").to(terminal).exec()
875+
val start = c.p.startSector.coerceAtLeast(startSectorAbsolute)
876+
val end = c.p.endSector.coerceAtMost(endSectorAbsolute)
877+
val k = part.resolveSectorSize(c, end - start)
878+
if (start + k > end)
879+
throw IllegalStateException("$start + $k = ${start + k} shouldn't be bigger than $end")
880+
if (k < 0)
881+
throw IllegalStateException("$k shouldn't be smaller than 0")
882+
// create(start, end) values are relative to the free space area
883+
val r = vm.logic.create(c.p, start - c.p.startSector,
884+
(start + k) - c.p.startSector, part.code, "").to(terminal).exec()
879885
if (r.out.joinToString("\n").contains("kpartx")) {
880886
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
881887
}
882888
createdParts[part] = c.meta!!.nid
883889
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
884890
// do not assert there is leftover space if we just created the last partition we want to create
885-
if (index != c.parts.size - 1) {
891+
if (index < c.parts.size - 1) {
886892
c.p =
887-
c.meta!!.s.find { it.type == SDUtils.PartitionType.FREE && (c.startSectorRelative + k) < it.startSector } as SDUtils.Partition.FreeSpace
893+
c.meta!!.s.find { it.type == SDUtils.PartitionType.FREE && start + k < it.startSector } as SDUtils.Partition.FreeSpace
888894
}
889895
if (r.isSuccess) {
890896
terminal.add(vm.activity.getString(R.string.term_created_part))
@@ -948,13 +954,11 @@ private fun Flash(c: CreatePartDataHolder) {
948954

949955
terminal.add(vm.activity.getString(R.string.term_patching_os))
950956
var cmd = "FORMATDATA=true " + tmpFile.absolutePath + " $fn"
951-
for (i in c.idNeeded) {
952-
val j = c.parts.find { it.id == i }
953-
if (j == null) {
954-
cmd += " " + c.chosen[i]!!.toFile(vm).absolutePath
955-
} else {
956-
cmd += createdParts[j]
957-
}
957+
for (i in c.extraIdNeeded) {
958+
cmd += " " + c.chosen[i]!!.toFile(vm).absolutePath
959+
}
960+
for (i in c.parts) {
961+
cmd += " " + createdParts[i]
958962
}
959963
val result = vm.logic.runShFileWithArgs(cmd).to(terminal).exec()
960964
if (!result.isSuccess) {

app/src/main/java/org/andbootmgr/app/MainActivity.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.andbootmgr.app
22

3+
import android.content.Intent
34
import android.net.Uri
45
import android.os.Build
56
import android.os.Bundle
@@ -147,15 +148,15 @@ class MainActivityState(val activity: MainActivity?) {
147148
class MainActivity : ComponentActivity() {
148149
private lateinit var newFile: ActivityResultLauncher<String>
149150
private var onFileCreated: ((Uri) -> Unit)? = null
150-
private lateinit var chooseFile: ActivityResultLauncher<String>
151+
private lateinit var chooseFile: ActivityResultLauncher<Array<String>>
151152
private var onFileChosen: ((Uri) -> Unit)? = null
152153

153154
override fun onCreate(savedInstanceState: Bundle?) {
154155
val ready = AtomicBoolean(false)
155156
installSplashScreen().setKeepOnScreenCondition { !ready.get() }
156157
super.onCreate(savedInstanceState)
157158
chooseFile =
158-
registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
159+
registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
159160
if (uri == null) {
160161
Toast.makeText(
161162
this,
@@ -165,6 +166,8 @@ class MainActivity : ComponentActivity() {
165166
onFileChosen = null
166167
return@registerForActivityResult
167168
}
169+
// need to persist, otherwise can't access from the background as StayAliveService
170+
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
168171
if (onFileChosen != null) {
169172
onFileChosen!!(uri)
170173
onFileChosen = null
@@ -183,6 +186,9 @@ class MainActivity : ComponentActivity() {
183186
onFileCreated = null
184187
return@registerForActivityResult
185188
}
189+
// need to persist, otherwise can't access from the background as StayAliveService
190+
contentResolver.takePersistableUriPermission(uri,
191+
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
186192
if (onFileCreated != null) {
187193
onFileCreated!!(uri)
188194
onFileCreated = null
@@ -278,7 +284,7 @@ class MainActivity : ComponentActivity() {
278284
throw IllegalStateException("expected onFileChosen to be null")
279285
}
280286
onFileChosen = callback
281-
chooseFile.launch(mime)
287+
chooseFile.launch(arrayOf(mime))
282288
}
283289

284290
fun createFile(name: String, callback: (Uri) -> Unit) {

0 commit comments

Comments
 (0)