Skip to content

Commit

Permalink
Fix toast complete
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGIG committed Aug 14, 2023
1 parent c2bf621 commit 1ca3895
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 29 deletions.
17 changes: 14 additions & 3 deletions app/src/main/java/com/dumper/android/core/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.MenuItem
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
Expand Down Expand Up @@ -111,8 +112,6 @@ class MainActivity : AppCompatActivity() {
) {
val soFixerPath = filesDir.path

Toast.makeText(this, "Please wait...", Toast.LENGTH_SHORT).show()

if (intent.getBooleanExtra("IS_ROOT", false)) {
val message = Message.obtain(null, MSG_DUMP_PROCESS)

Expand All @@ -134,7 +133,7 @@ class MainActivity : AppCompatActivity() {

dumpFile.forEach {
dumper.file = it
dumper.dumpFile(this, autoFix, null, outHandler)
outHandler.finish(dumper.dumpFile(this, autoFix, null, outHandler))
}
}
}
Expand Down Expand Up @@ -164,9 +163,21 @@ class MainActivity : AppCompatActivity() {
AppBarConfiguration(setOf(R.id.nav_memory_fragment, R.id.nav_console_fragment))

setupActionBarWithNavController(navController, appBarConfiguration)

binding.navView.setupWithNavController(navController)
}

fun toConsoleFragment() {
val navController =
binding.navHostFragmentActivityMain.getFragment<NavHostFragment>().navController

val navOptions = NavOptions.Builder()
.setPopUpTo(R.id.nav_memory_fragment, true)
.build()

navController.navigate(R.id.nav_console_fragment, null, navOptions)
}

private fun setupRootService() {
if (remoteMessenger == null) {
dumperConnection = MSGConnection(this)
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/dumper/android/core/RootServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class RootServices : RootService(), Handler.Callback {
}

MSG_DUMP_PROCESS -> {
val outputHandler = OutputHandler(msg, reply, MSG_DUMP_PROCESS)
val outCode = OutputHandler(msg, reply, MSG_DUMP_FINISH)
val outLog = OutputHandler(msg, reply, MSG_DUMP_PROCESS)
val process = msg.data.getString(PROCESS_NAME)
val listFile = msg.data.getStringArray(LIST_FILE)
val fixerPath = msg.data.getString(LIBRARY_DIR_NAME, "")
Expand All @@ -52,10 +53,11 @@ class RootServices : RootService(), Handler.Callback {
val dumper = Dumper(process)
for (file in listFile) {
dumper.file = file
dumper.dumpFile(null, isAutoFix, fixerPath, outputHandler)
outCode.finish(dumper.dumpFile(null, isAutoFix, fixerPath, outLog))
}
} else {
outputHandler.appendError("Data Error!")
outLog.appendError("Data Error!")
outCode.finish(-1)
}
}
else -> {
Expand All @@ -79,6 +81,8 @@ class RootServices : RootService(), Handler.Callback {
companion object {
const val MSG_DUMP_PROCESS = 1
const val MSG_GET_PROCESS_LIST = 2
const val MSG_DUMP_FINISH = 3
const val DUMP_CODE = "DUMP_CODE"
const val DUMP_LOG = "DUMP_LOG"
const val LIBRARY_DIR_NAME = "NATIVE_DIR"
const val LIST_ALL_PROCESS = "LIST_ALL_PROCESS"
Expand Down
41 changes: 21 additions & 20 deletions app/src/main/java/com/dumper/android/dumper/Dumper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class Dumper(private val pkg: String) {
if (!outputDir.exists())
outputDir.mkdirs()

val outputFile =
File(outputDir,"${mem.sAddress.toHex()}-${mem.eAddress.toHex()}-$file")
val outputFile = File(outputDir, "${mem.sAddress.toHex()}-${mem.eAddress.toHex()}-$file")
if (!outputDir.exists())
outputFile.createNewFile()

Expand Down Expand Up @@ -58,14 +57,14 @@ class Dumper(private val pkg: String) {
fileOutputDir.mkdirs()

outputDir.copyRecursively(fileOutputDir, true, onError = { file, exc ->
outLog.appendLine("[ERROR] Failed to copy: ${file.name}\n${exc.stackTraceToString()}")
outLog.appendError("Failed to copy: ${file.name}\n${exc.stackTraceToString()}")
OnErrorAction.TERMINATE
})

outLog.appendLine("Output: $fileOutputDir")
}

private fun dump(autoFix: Boolean,fixerPath: String, outputFile: File, outLog: OutputHandler) {
private fun dump(autoFix: Boolean, fixerPath: String, outputFile: File, outLog: OutputHandler) {

val inputChannel = RandomAccessFile("/proc/${mem.pid}/mem", "r").channel

Expand All @@ -78,7 +77,12 @@ class Dumper(private val pkg: String) {
inputChannel.close()
}

private fun fixDumpFile(fixerPath: String, archELF: Arch, outputFile: File, outLog: OutputHandler) {
private fun fixDumpFile(
fixerPath: String,
archELF: Arch,
outputFile: File,
outLog: OutputHandler
) {
if (archELF == Arch.UNKNOWN)
return

Expand All @@ -100,9 +104,9 @@ class Dumper(private val pkg: String) {
* @param ctx pass null if using root, vice versa
* @param autoFix if `true` the dumped file will be fixed after dumping
* @param fixerPath ELFixer path
* @return log of the dump
* @return 0 if success, -1 if failed
*/
fun dumpFile(ctx: Context?, autoFix: Boolean, fixerPath: String?, outLog: OutputHandler) {
fun dumpFile(ctx: Context?, autoFix: Boolean, fixerPath: String?, outLog: OutputHandler): Int {
try {
mem.pid = Process.getProcessID(pkg) ?: throw Exception("Process not found!")

Expand All @@ -116,37 +120,34 @@ class Dumper(private val pkg: String) {

outLog.appendLine("Start Address : ${mem.sAddress.toHex()}")
if (mem.sAddress < 1L) {
outLog.appendError("[ERROR] invalid Start Address!")
return
throw Exception("Invalid Start Address!")
}

outLog.appendLine("End Address : ${mem.eAddress.toHex()}")
if (mem.eAddress < 1L) {
outLog.appendError("[ERROR] invalid End Address!")
return
throw Exception("Invalid End Address!")
}

outLog.appendLine("Size Memory : ${mem.size}")
if (mem.size < 1L) {
outLog.appendError("[ERROR] invalid memory size!")
return
throw Exception("Invalid memory size!")
}


if (ctx == null) {
if (fixerPath == null)
throw Exception("Fixer path is null!")
dumpFileRoot(autoFix, fixerPath, outLog)
}
else
} else
dumpFileNonRoot(ctx, autoFix, outLog)

outLog.appendSuccess("Dump Success")
outLog.appendLine("Dump Success")
outLog.appendLine("==========================")
return 0
} catch (e: Exception) {
outLog.appendLine("[ERROR] ${e.stackTraceToString()}")
e.printStackTrace()
outLog.appendError(e.message ?: "Unknown Error")
}
outLog.appendLine("==========================")
return -1
}


Expand Down Expand Up @@ -184,7 +185,7 @@ class Dumper(private val pkg: String) {
}

if (mapStart == null || mapEnd == null)
throw RuntimeException("Start or End Address not found!")
throw Exception("Start or End Address not found!")

return Pair(mapStart!!.getStartAddress(), mapEnd!!.getEndAddress())
}
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/com/dumper/android/dumper/OutputHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ class OutputHandler {

private constructor()

/*
* This method is used to send message to client
* Use this method if you're on root services
* @param from: Message from client
* @param reply: Message to client
* @param what: What to reply
*/
constructor(from: Message, reply: Message, what: Int) : this() {
isRoot = true
this.from = from
this.reply = reply
this.what = what
}

/*
* This method is used to append message to console
* Use this method if you're on non-root
* @param console: ConsoleViewModel to append
*/
constructor(console: ConsoleViewModel) : this() {
isRoot = false
this.console = console
Expand All @@ -45,6 +57,22 @@ class OutputHandler {
}
}

fun finish(code: Int) {
if (isRoot) {
val data = Bundle()
data.putInt(RootServices.DUMP_CODE, code)
reply.what = what
reply.data = data
try {
from.replyTo.send(reply)
} catch (e: RemoteException) {
Log.e(TAG, "Remote error", e)
}
} else {
console.finish(code)
}
}


fun appendLine(text: String) {
processInput(text + "\n")
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/dumper/android/messager/MSGReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.dumper.android.messager

import android.os.Handler
import android.os.Message
import android.widget.Toast
import com.dumper.android.core.MainActivity
import com.dumper.android.core.RootServices
import com.dumper.android.dumper.process.ProcessData
Expand All @@ -24,10 +23,16 @@ class MSGReceiver(private val activity: MainActivity) : Handler.Callback {

RootServices.MSG_DUMP_PROCESS -> {
message.data.getString(RootServices.DUMP_LOG)?.let {
activity.console.append(it)
Toast.makeText(activity, "Dump Complete!", Toast.LENGTH_SHORT).show()
activity.console.append(it)
}
}

RootServices.MSG_DUMP_FINISH -> {
message.data.getInt(RootServices.DUMP_CODE, -1).let {
activity.console.finish(it)
}
}

}
return false
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/dumper/android/ui/Event.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dumper.android.ui

// Source: https://stackoverflow.com/a/60750407
open class Event<out T>(private val content: T) {

private var hasBeenHandled = false

/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}

/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
10 changes: 10 additions & 0 deletions app/src/main/java/com/dumper/android/ui/console/ConsoleFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class ConsoleFragment : Fragment() {
}
}

vm.finishCode.observe(viewLifecycleOwner) { event ->
event.getContentIfNotHandled()?.let {
if (it == 0) {
Toast.makeText(requireContext(), "Dump success!", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(requireContext(), "Dump error!", Toast.LENGTH_SHORT).show()
}
}
}

consoleBind.copyConsole.setOnClickListener {
val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("PADumper-Log", consoleBind.console.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package com.dumper.android.ui.console

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.dumper.android.ui.Event

class ConsoleViewModel : ViewModel() {

val console = MutableLiveData("")
val finishCode = MutableLiveData<Event<Int>>()

fun append(text: String) {
console.value = console.value + text
Expand Down Expand Up @@ -39,4 +41,7 @@ class ConsoleViewModel : ViewModel() {
appendLine("[SUCCESS] $text")
}

fun finish(ret: Int) {
finishCode.value = Event(ret)
}
}

0 comments on commit 1ca3895

Please sign in to comment.