Skip to content

Commit

Permalink
simulator io support
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 1, 2024
1 parent 7337642 commit c0d040a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/src/main/cpp/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <jni.h>

extern "C" void simulator_start(JNIEnv* env, jobject bitmap, jint w, jint h);
extern "C" void simulator_start(JNIEnv* env, jobject thiz, jobject bitmap, jint w, jint h);
extern "C" void simulator_stop(JNIEnv* env);
extern "C" void simulator_key(jint key);

Expand All @@ -17,5 +17,5 @@ extern "C" JNIEXPORT void JNICALL Java_org_andbootmgr_app_Simulator_stop(JNIEnv*
}

extern "C" JNIEXPORT void JNICALL Java_org_andbootmgr_app_Simulator_start(JNIEnv* env, jobject thiz, jobject bitmap, jint w, jint h) {
simulator_start(env, bitmap, w, h);
simulator_start(env, thiz, bitmap, w, h);
}
2 changes: 1 addition & 1 deletion app/src/main/cpp/droidboot_gui
113 changes: 113 additions & 0 deletions app/src/main/java/org/andbootmgr/app/Simulator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
import com.topjohnwu.superuser.io.SuFileInputStream
import com.topjohnwu.superuser.nio.ExtendedFile
import java.io.EOFException
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.Arrays
import kotlin.math.min


Expand All @@ -22,12 +31,14 @@ class Simulator : AppCompatActivity() {
private lateinit var bitmap: Bitmap
private var w: Int = 0
private var h: Int = 0
private lateinit var f: File

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
w = 1080
h = 1920
bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
f = File("/dev/block/mmcblk1")
val l = LinearLayout(this)
l.addView(object : View(this) {
override fun onDraw(canvas: Canvas) {
Expand Down Expand Up @@ -57,4 +68,106 @@ class Simulator : AppCompatActivity() {
start()
}
}

private fun blockCount(): Long {
return Shell.cmd("blockdev --getsz ${f.absolutePath}").exec().out.join("\n").toLong().also {
Log.i("Simulator", "block count: $it")
}
}

private fun readBlocks(offset: Long, count: Int): ByteArray {
val fo = SuFileInputStream.open(f)
fo.skipNBytesSupport(offset)
val b = fo.readNBytesSupport(count)!!
fo.close()
return b
}

@Throws(IOException::class)
private fun InputStream.skipNBytesSupport(nn: Long) {
var n = nn
while (n > 0) {
val ns: Long = skip(n)
if (ns in 1..n) {
// adjust number to skip
n -= ns
} else if (ns == 0L) { // no bytes skipped
// read one byte to check for EOS
if (read() == -1) {
throw EOFException()
}
// one byte read so decrement number to skip
n--
} else { // skipped negative or too many bytes
throw IOException("Unable to skip exactly")
}
}
}

@Throws(IOException::class)
fun InputStream.readNBytesSupport(len: Int): ByteArray? {
require(len >= 0) { "len < 0" }

var bufs: MutableList<ByteArray>? = null
var result: ByteArray? = null
var total = 0
var remaining = len
var n: Int
do {
var buf = ByteArray(
min(remaining, 8192)
)
var nread = 0

// read to EOF which may read more or less than buffer size
while ((read(
buf, nread,
min((buf.size - nread), remaining)
).also { n = it }) > 0
) {
nread += n
remaining -= n
}

if (nread > 0) {
if (8192 - total < nread) {
throw OutOfMemoryError("Required array size too large")
}
if (nread < buf.size) {
buf = Arrays.copyOfRange(buf, 0, nread)
}
total += nread
if (result == null) {
result = buf
} else {
if (bufs == null) {
bufs = ArrayList()
bufs.add(result)
}
bufs.add(buf)
}
}
// if the last call to read returned -1 or the number of bytes
// requested have been read then break
} while (n >= 0 && remaining > 0)

if (bufs == null) {
if (result == null) {
return ByteArray(0)
}
return if (result.size == total) result else result.copyOf(total)
}

result = ByteArray(total)
var offset = 0
remaining = total
for (b in bufs) {
val count = min(b.size.toDouble(), remaining.toDouble()).toInt()
System.arraycopy(b, 0, result, offset, count)
offset += count
remaining -= count
}

return result
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/org/andbootmgr/app/util/RootFsService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.andbootmgr.app.util

import android.content.Intent
import android.os.IBinder
import com.topjohnwu.superuser.ipc.RootService
import com.topjohnwu.superuser.nio.FileSystemManager

class RootFsService : RootService() {
override fun onBind(intent: Intent): IBinder {
return FileSystemManager.getService()
}
}

0 comments on commit c0d040a

Please sign in to comment.