Skip to content

Commit

Permalink
8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscofranco committed Oct 2, 2020
1 parent 034344d commit 157cdaa
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 191 deletions.
34 changes: 26 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
viewBinding.enabled = true
compileSdkVersion 30
buildToolsVersion "30.0.2"

buildFeatures {
viewBinding true
}

defaultConfig {
applicationId 'simple.reboot.com'
minSdkVersion 21
targetSdkVersion 29
versionCode 1803261730
versionName "8.2"
targetSdkVersion 30
versionCode 1803261731
versionName "8.3"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

lintOptions {
abortOnError false
checkAllWarnings false
Expand Down Expand Up @@ -49,6 +57,16 @@ android {
}

dependencies {
implementation 'com.google.android.material:material:1.2.0-alpha06'
implementation 'com.github.topjohnwu.libsu:core:2.5.1'
implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation 'com.github.topjohnwu.libsu:core:3.0.2'
implementation "androidx.core:core-ktx:1.3.1"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
implementation "androidx.activity:activity-ktx:1.1.0"
}
repositories {
mavenCentral()
}
169 changes: 0 additions & 169 deletions app/src/main/java/simple/reboot/com/MainActivity.java

This file was deleted.

144 changes: 144 additions & 0 deletions app/src/main/java/simple/reboot/com/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package simple.reboot.com

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import com.google.android.material.snackbar.Snackbar
import com.topjohnwu.superuser.Shell
import simple.reboot.com.databinding.ActivityMainBinding
import java.util.*

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

private val viewModel by viewModels<MainActivityViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

viewModel.areWeRooted().observe(this, ::renderRootAccess)

binding.container.post {
binding.container.translationY = (binding.coordinator.height shr 2.toFloat().toInt()).toFloat()
binding.container.translationX = binding.container.width.toFloat()
binding.container.animate().apply {
cancel()
translationX(0f).startDelay = 100
setListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
binding.container.isVisible = true
}
})
start()
}
}

binding.root.setOnClickListener { onBackPressed() }
binding.shutdown.setOnClickListener { runCmd(SHUTDOWN) }
binding.reboot.setOnClickListener { runCmd(REBOOT_CMD) }
binding.softReboot.setOnClickListener { onSoftRebootClick() }
binding.rebootRecovery.setOnClickListener { onRebootRecoveryClick() }
binding.rebootBootloader.setOnClickListener { onRebootBootloaderClick() }
binding.rebootSafeMode.setOnClickListener { onRebootSafeModeClick() }
binding.restartSystemui.setOnClickListener { onRebootSystemUi() }
binding.softReboot.setOnClickListener { onSoftRebootClick() }
}

private fun renderRootAccess(areWeRooted: Boolean) {
if (!areWeRooted) {
Snackbar.make(binding.coordinator, R.string.root_status_no, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.close) { finishAnimation() }
.show()
}
}

private fun finishAnimation() {
binding.container.animate().apply {
cancel()
translationX(binding.container.width.toFloat())
setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
finish()
}
})
start()
}
}

private fun runCmd(vararg cmd: String) {
finishAnimation()
Shell.su(*cmd).submit()
}

private fun runCmdWithCallback(callback: Shell.ResultCallback, vararg cmd: String) {
finishAnimation()
Shell.su(*cmd).submit(callback)
}


private fun onSoftRebootClick() {
runCmdWithCallback({ out: Shell.Result ->
if (out.isSuccess) runCmd(REBOOT_SOFT_REBOOT_CMD)
}, *SHUTDOWN_BROADCAST)
}

private fun onRebootRecoveryClick() {
runCmdWithCallback({ out: Shell.Result ->
if (out.isSuccess) runCmd(REBOOT_RECOVERY_CMD)
}, *SHUTDOWN_BROADCAST)
}

private fun onRebootBootloaderClick() {
runCmdWithCallback({ out: Shell.Result ->
if (out.isSuccess) runCmd(REBOOT_BOOTLOADER_CMD)
}, *SHUTDOWN_BROADCAST)
}

private fun onRebootSafeModeClick() {
runCmdWithCallback({ out: Shell.Result ->
if (out.isSuccess) runCmd(*REBOOT_SAFE_MODE)
}, *SHUTDOWN_BROADCAST)
}

private fun onRebootSystemUi() {
runCmdWithCallback({ cmd: Shell.Result ->
cmd.apply {
if (isSuccess && out.size == 1) {
// pidof returns only one line anyway, guard just for safe measures
val stdout = cmd.out[0].trim { it <= ' ' }
val pid = stdout.toInt()
runCmd(String.format(Locale.getDefault(), REBOOT_SYSTEMUI_CMD, pid))
}
}
}, PIDOF_SYSTEMUI)
}

override fun onBackPressed() {
finishAnimation()
}

companion object {
// just for safe measure, we don't want any data corruption, right?
private val SHUTDOWN_BROADCAST = arrayOf( // we announce the device is going down so apps that listen for
// this broadcast can do whatever
"am broadcast android.intent.action.ACTION_SHUTDOWN", // we tell the file system to write any data buffered in memory out to disk
"sync", // we also instruct the kernel to drop clean caches, as well as
// reclaimable slab objects like dentries and inodes
"echo 3 > /proc/sys/vm/drop_caches", // and sync buffered data as before
"sync")
private const val SHUTDOWN = "svc power shutdown"
private const val REBOOT_CMD = "svc power reboot"
private const val REBOOT_SOFT_REBOOT_CMD = "setprop ctl.restart zygote"
private const val REBOOT_RECOVERY_CMD = "reboot recovery"
private const val REBOOT_BOOTLOADER_CMD = "reboot bootloader"
private const val REBOOT_SYSTEMUI_CMD = "kill %d"
private const val PKG_SYSTEMUI = "com.android.systemui"
private const val PIDOF_SYSTEMUI = "pidof $PKG_SYSTEMUI"
private val REBOOT_SAFE_MODE = arrayOf("setprop persist.sys.safemode 1", REBOOT_SOFT_REBOOT_CMD)
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/simple/reboot/com/MainActivityViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package simple.reboot.com

import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers

class MainActivityViewModel : ViewModel() {
fun areWeRooted() = liveData(Dispatchers.IO) {
emit(Shell.rootAccess())
}
}
Loading

0 comments on commit 157cdaa

Please sign in to comment.