Skip to content

Commit

Permalink
feature: graceful shizuku binder handling (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyujin-cho authored Jan 28, 2024
1 parent 8bc6123 commit 1b740ef
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ android {
}
buildFeatures {
compose true
aidl true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.0"
Expand Down
45 changes: 38 additions & 7 deletions app/src/main/java/dev/bluehouse/enablevolte/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand Down Expand Up @@ -93,6 +94,14 @@ fun PixelIMSApp() {
})
}

fun generateInitialNavBuilder(): (NavGraphBuilder.() -> Unit) {
return {
composable("home", "Home") {
Home(navController)
}
}
}

fun generateNavBuilder(): (NavGraphBuilder.() -> Unit) {
return {
composable("home", "Home") {
Expand All @@ -114,14 +123,16 @@ fun PixelIMSApp() {
}
}

OnLifecycleEvent { _, event ->
if (event == Lifecycle.Event.ON_CREATE) {
try {
if (checkShizukuPermission(0)) {
fun loadApplication() {
val shizukuStatus = checkShizukuPermission(0)
try {
when (shizukuStatus) {
ShizukuStatus.GRANTED -> {
Log.d(dev.bluehouse.enablevolte.pages.TAG, "Shizuku granted")
subscriptions = carrierModer.subscriptions
navBuilder = generateNavBuilder()
} else {
}
ShizukuStatus.NOT_GRANTED -> {
Shizuku.addRequestPermissionResultListener { _, grantResult ->
if (grantResult == PackageManager.PERMISSION_GRANTED) {
Log.d(dev.bluehouse.enablevolte.pages.TAG, "Shizuku granted")
Expand All @@ -130,8 +141,18 @@ fun PixelIMSApp() {
}
}
}
} catch (_: IllegalStateException) {
else -> {
subscriptions = listOf()
navBuilder = generateInitialNavBuilder()
}
}
} catch (_: IllegalStateException) {
}
}

OnLifecycleEvent { _, event ->
if (event == Lifecycle.Event.ON_CREATE) {
loadApplication()
}
}
Scaffold(
Expand All @@ -145,7 +166,17 @@ fun PixelIMSApp() {
IconButton(onClick = { navController.popBackStack() }, colors = IconButtonDefaults.filledIconButtonColors(contentColor = MaterialTheme.colorScheme.onPrimary)) {
Icon(
imageVector = Icons.Filled.ArrowBack,
contentDescription = "Localized description",
contentDescription = "Go back",
)
}
}
},
actions = {
if (currentBackStackEntry?.destination?.route == "home") {
IconButton(onClick = { loadApplication() }, colors = IconButtonDefaults.filledIconButtonColors(contentColor = MaterialTheme.colorScheme.onPrimary)) {
Icon(
imageVector = Icons.Filled.Refresh,
contentDescription = "Refresh contents",
)
}
}
Expand Down
21 changes: 14 additions & 7 deletions app/src/main/java/dev/bluehouse/enablevolte/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@ import com.github.kittinunf.fuel.json.responseJson
import com.github.kittinunf.result.Result
import rikka.shizuku.Shizuku

fun checkShizukuPermission(code: Int): Boolean {
return if (Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED) {
true
} else if (Shizuku.shouldShowRequestPermissionRationale()) {
false
enum class ShizukuStatus {
GRANTED, NOT_GRANTED, STOPPED
}
fun checkShizukuPermission(code: Int): ShizukuStatus {
return if (Shizuku.getBinder() != null) {
if (Shizuku.checkSelfPermission() == PackageManager.PERMISSION_GRANTED) {
ShizukuStatus.GRANTED
} else {
if (!Shizuku.shouldShowRequestPermissionRationale()) {
Shizuku.requestPermission(0)
}
ShizukuStatus.NOT_GRANTED
}
} else {
Shizuku.requestPermission(code)
false
ShizukuStatus.STOPPED
}
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/dev/bluehouse/enablevolte/pages/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.bluehouse.enablevolte.HeaderText
import dev.bluehouse.enablevolte.InfiniteLoadingDialog
import dev.bluehouse.enablevolte.KeyValueEditView
import dev.bluehouse.enablevolte.R
import dev.bluehouse.enablevolte.ShizukuStatus
import dev.bluehouse.enablevolte.SubscriptionModer
import dev.bluehouse.enablevolte.UserAgentPropertyView
import dev.bluehouse.enablevolte.ValueType
Expand Down Expand Up @@ -106,7 +107,7 @@ fun Config(navController: NavController, subId: Int) {
}

LaunchedEffect(true) {
if (checkShizukuPermission(0)) {
if (checkShizukuPermission(0) == ShizukuStatus.GRANTED) {
if (carrierModer.deviceSupportsIMS && subId >= 0) {
configurable = try {
withContext(Dispatchers.Default) {
Expand Down
26 changes: 17 additions & 9 deletions app/src/main/java/dev/bluehouse/enablevolte/pages/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.bluehouse.enablevolte.CarrierModer
import dev.bluehouse.enablevolte.ClickablePropertyView
import dev.bluehouse.enablevolte.HeaderText
import dev.bluehouse.enablevolte.R
import dev.bluehouse.enablevolte.ShizukuStatus
import dev.bluehouse.enablevolte.StringPropertyView
import dev.bluehouse.enablevolte.SubscriptionModer
import dev.bluehouse.enablevolte.checkShizukuPermission
Expand Down Expand Up @@ -62,19 +63,26 @@ fun Home(navController: NavController) {
}

LaunchedEffect(Unit) {
shizukuEnabled = try {
if (checkShizukuPermission(0)) {
loadFlags()
} else {
Shizuku.addRequestPermissionResultListener { _, grantResult ->
if (grantResult == PackageManager.PERMISSION_GRANTED) {
loadFlags()
try {
when (checkShizukuPermission(0)) {
ShizukuStatus.GRANTED -> {
shizukuEnabled = true
loadFlags()
}
ShizukuStatus.NOT_GRANTED -> {
shizukuEnabled = true
Shizuku.addRequestPermissionResultListener { _, grantResult ->
if (grantResult == PackageManager.PERMISSION_GRANTED) {
loadFlags()
}
}
}
else -> {
shizukuEnabled = false
}
}
true
} catch (e: IllegalStateException) {
false
shizukuEnabled = false
}
getLatestAppVersion {
Log.d(TAG, "Fetched version $it")
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
}
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Feb 06 01:14:51 KST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

0 comments on commit 1b740ef

Please sign in to comment.