Skip to content

Commit

Permalink
Fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi04151309 committed Jun 6, 2020
1 parent 8d90c75 commit 1cea892
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "io.github.domi04151309.powerapp"
minSdkVersion 23
targetSdkVersion 29
versionCode 185
versionName "1.8.5"
versionCode 186
versionName "1.8.6"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/io/github/domi04151309/powerapp/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.domi04151309.powerapp

import android.app.AlertDialog
import android.content.Intent
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
Expand Down Expand Up @@ -30,31 +31,31 @@ class MainActivity : AppCompatActivity() {
actionBar.elevation = 0f
}

val po = PowerOptions(this)
val po = PowerOptions(this, true)

findViewById<View>(R.id.shutdown).setOnClickListener {
po.shutdown()
askBefore { po.shutdown() }
}
findViewById<View>(R.id.reboot).setOnClickListener {
po.reboot()
askBefore { po.reboot() }
}
findViewById<View>(R.id.recovery).setOnClickListener {
po.rebootIntoRecovery()
askBefore { po.rebootIntoRecovery() }
}
findViewById<View>(R.id.bootloader).setOnClickListener {
po.rebootIntoBootloader()
askBefore { po.rebootIntoBootloader() }
}
findViewById<View>(R.id.edl).setOnClickListener {
po.rebootIntoEDL()
askBefore { po.rebootIntoEDL() }
}
findViewById<View>(R.id.soft_reboot).setOnClickListener {
po.softReboot()
askBefore { po.softReboot() }
}
findViewById<View>(R.id.system_ui).setOnClickListener {
po.restartSystemUI()
askBefore { po.restartSystemUI() }
}
findViewById<View>(R.id.screen_off).setOnClickListener {
po.turnOffScreen()
askBefore { po.turnOffScreen() }
}
findViewById<View>(R.id.root).setOnClickListener {
val p: Process
Expand Down Expand Up @@ -82,4 +83,15 @@ class MainActivity : AppCompatActivity() {
override fun onBackPressed() {
startActivity(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}

private fun askBefore(function : () -> Unit) {
AlertDialog.Builder(this)
.setTitle(R.string.confirm_dialog)
.setMessage(R.string.confirm_dialog_summary)
.setPositiveButton(android.R.string.ok) { _, _ ->
function()
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
}
}
19 changes: 4 additions & 15 deletions app/src/main/java/io/github/domi04151309/powerapp/PowerOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,19 @@ import android.content.Context
import android.util.Log
import android.widget.Toast

class PowerOptions(private val context: Context) {
class PowerOptions(private val context: Context, private val dialog: Boolean = false) {

private fun shell(command: String) {
try {
try {
AlertDialog.Builder(context)
.setTitle(R.string.confirm_dialog)
.setMessage(R.string.confirm_dialog_summary)
.setPositiveButton(android.R.string.ok) { _, _ ->
Runtime.getRuntime().exec(arrayOf("su", "-c", command)).waitFor()
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
} catch (e: Exception) {
Runtime.getRuntime().exec(arrayOf("su", "-c", command)).waitFor()
}
Runtime.getRuntime().exec(arrayOf("su", "-c", command)).waitFor()
} catch (e: Exception) {
try {
if (dialog) {
AlertDialog.Builder(context)
.setTitle(R.string.action_failed)
.setMessage(R.string.action_failed_summary)
.setPositiveButton(android.R.string.ok) { _, _ -> }
.show()
} catch (e: Exception) {
} else {
Toast.makeText(context,R.string.action_failed_summary,Toast.LENGTH_LONG).show()
}
Log.e("Superuser", e.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ShutdownActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PowerOptions(this).shutdown()
finish()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SoftRebootActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PowerOptions(this).softReboot()
finish()
}

}
5 changes: 5 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/186.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- Add confirmation dialogs
- Add shortcuts
- Fix about screen
- Add EDL shortcut
- Fix crash on not rooted devices

0 comments on commit 1cea892

Please sign in to comment.