Skip to content

Commit

Permalink
Cleaning some code
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanGIG committed Apr 15, 2023
1 parent bf0d2ac commit 094b7fe
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 56 deletions.
10 changes: 2 additions & 8 deletions app/src/main/java/com/dumper/android/core/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ import com.dumper.android.dumper.process.Process
import com.dumper.android.messager.MSGConnection
import com.dumper.android.messager.MSGReceiver
import com.dumper.android.ui.console.ConsoleViewModel
import com.dumper.android.ui.memory.MemoryFragment
import com.dumper.android.ui.memory.MemoryViewModel
import com.topjohnwu.superuser.ipc.RootService

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
private lateinit var binding: ActivityMainBinding

var remoteMessenger: Messenger? = null
private val receiver = Messenger(Handler(Looper.getMainLooper(), MSGReceiver(this)))
Expand Down Expand Up @@ -103,12 +102,7 @@ class MainActivity : AppCompatActivity() {
} else {
val processList = Process.getAllProcess(this, false)

val navController = binding.navHostFragmentActivityMain.getFragment<NavHostFragment>()
val fragments = navController.childFragmentManager.fragments

fragments.find { it is MemoryFragment }?.let { fragment ->
(fragment as MemoryFragment).showProcess(processList)
}
memory.showProcess(this, processList)
}
}

Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/dumper/android/core/RootServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ class RootServices : RootService(), Handler.Callback {

MSG_DUMP_PROCESS -> {
val outputHandler = OutputHandler(msg, reply, MSG_DUMP_PROCESS)
val requestData = msg.data
val process = requestData.getString(PROCESS_NAME)
val listFile = requestData.getStringArray(LIST_FILE)
val isFlagCheck = requestData.getBoolean(IS_FLAG_CHECK, false)
val fixerPath = requestData.getString(LIBRARY_DIR_NAME, "")
val isAutoFix = requestData.getBoolean(IS_FIX_NAME, false)
val process = msg.data.getString(PROCESS_NAME)
val listFile = msg.data.getStringArray(LIST_FILE)
val isFlagCheck = msg.data.getBoolean(IS_FLAG_CHECK, false)
val fixerPath = msg.data.getString(LIBRARY_DIR_NAME, "")
val isAutoFix = msg.data.getBoolean(IS_FIX_NAME, false)
if (process != null && listFile != null) {
val dumper = Dumper(process)
for (file in listFile) {
Expand Down
11 changes: 3 additions & 8 deletions app/src/main/java/com/dumper/android/dumper/Dumper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Dumper(private val pkg: String) {
outputDir.mkdirs()

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

Expand Down Expand Up @@ -166,19 +166,14 @@ class Dumper(private val pkg: String) {

val lineStart = lines.find {
val map = MapLinux(it)
if (file.contains(".so")) {
if (checkFlag)
map.getPerms().contains("r-xp") && map.getPath().contains(file)
else {
map.getPath().contains(file)
}
return@find if (file.contains(".so") && checkFlag) {
map.getPerms().contains("r-xp") && map.getPath().contains(file)
} else {
map.getPath().contains(file)
}
} ?: throw RuntimeException("Unable find baseAddress of $file")

val mapStart = MapLinux(lineStart)

val lineEnd = lines.findLast {
val map = MapLinux(it)
mapStart.getInode() == map.getInode()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/dumper/android/dumper/Fixer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum class Arch {
object Fixer {

private val ELFMAG =
byteArrayOf(0x7F.toByte(), 'E'.code.toByte(), 'L'.code.toByte(), 'F'.code.toByte())
byteArrayOf(0x7F, 'E'.code.toByte(), 'L'.code.toByte(), 'F'.code.toByte())

/**
* Extract SoFixer into filesDir and
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/com/dumper/android/messager/MSGReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package com.dumper.android.messager
import android.os.Handler
import android.os.Message
import android.widget.Toast
import androidx.navigation.fragment.NavHostFragment
import com.dumper.android.core.MainActivity
import com.dumper.android.core.RootServices
import com.dumper.android.dumper.process.ProcessData
import com.dumper.android.ui.memory.MemoryFragment
import com.dumper.android.utils.getParcelableArrayListCompact

class MSGReceiver(private val activity: MainActivity) : Handler.Callback {
Expand All @@ -19,17 +17,11 @@ class MSGReceiver(private val activity: MainActivity) : Handler.Callback {
RootServices.MSG_GET_PROCESS_LIST -> {
message.data.getParcelableArrayListCompact<ProcessData>(RootServices.LIST_ALL_PROCESS)
?.let {
val navHost = activity.binding.navHostFragmentActivityMain
val navController = navHost.getFragment<NavHostFragment>()
val fragments = navController.childFragmentManager.fragments

fragments.find { it is MemoryFragment }
?.let { fragment ->
(fragment as MemoryFragment).showProcess(it)
}
activity.memory.showProcess(activity, it)
}
}


RootServices.MSG_DUMP_PROCESS -> {
message.data.getString(RootServices.DUMP_LOG)?.let {
activity.console.append(it)
Expand Down
31 changes: 8 additions & 23 deletions app/src/main/java/com/dumper/android/ui/memory/MemoryFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.dumper.android.core.MainActivity
import com.dumper.android.databinding.FragmentMemoryBinding
import com.dumper.android.dumper.process.ProcessData
import com.dumper.android.ui.console.ConsoleViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class MemoryFragment : Fragment() {

Expand Down Expand Up @@ -43,7 +41,7 @@ class MemoryFragment : Fragment() {
}

binding.selectApps.setOnClickListener {
getMainActivity().sendRequestAllProcess()
getMainActivity()?.sendRequestAllProcess()
}

binding.dumpButton.setOnClickListener {
Expand All @@ -64,7 +62,7 @@ class MemoryFragment : Fragment() {
if (binding.metadata.isChecked)
listDump.add("global-metadata.dat")

getMainActivity().sendRequestDump(
getMainActivity()?.sendRequestDump(
process,
listDump.toTypedArray(),
binding.autoFix.isChecked,
Expand All @@ -82,25 +80,12 @@ class MemoryFragment : Fragment() {
_binding = null
}

fun showProcess(list: ArrayList<ProcessData>) {
list.sortBy { lists -> lists.appName }

val appNames = list.map { processData ->
val processName = processData.processName
if (processName.contains(":"))
"${processData.appName} (${processName.substringAfter(":")})"
else
processData.appName
private fun getMainActivity() : MainActivity? {
val activity = requireActivity()
return if (activity is MainActivity) {
activity
} else {
null
}

MaterialAlertDialogBuilder(requireContext())
.setTitle("Select process")
.setSingleChoiceItems(appNames.toTypedArray(), -1) { dialog, which ->
memViewModel.selectedApps.value = list[which].processName
dialog.dismiss()
}
.show()
}

private fun getMainActivity() = requireActivity() as MainActivity
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/dumper/android/ui/memory/MemoryViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package com.dumper.android.ui.memory

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.dumper.android.dumper.process.ProcessData
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class MemoryViewModel : ViewModel() {

val selectedApps = MutableLiveData<String>()
val packageName = MutableLiveData<String>()
val libName = MutableLiveData<String>()


fun showProcess(ctx: Context, list: ArrayList<ProcessData>) {
list.sortBy { lists -> lists.appName }

val appNames = list.map { processData ->
val processName = processData.processName
if (processName.contains(":"))
"${processData.appName} (${processName.substringAfter(":")})"
else
processData.appName
}

MaterialAlertDialogBuilder(ctx)
.setTitle("Select process")
.setSingleChoiceItems(appNames.toTypedArray(), -1) { dialog, which ->
selectedApps.value = list[which].processName
dialog.dismiss()
}
.show()
}

}

0 comments on commit 094b7fe

Please sign in to comment.