Skip to content

Commit

Permalink
Refactor SAR logic
Browse files Browse the repository at this point in the history
  • Loading branch information
imknown committed Apr 29, 2020
1 parent f26b764 commit 95d7649
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ class HomeViewModel : BaseListViewModel() {
private const val PROP_VNDK_VERSION = "ro.vndk.version"

// https://source.android.com/devices/bootloader/system-as-root?hl=en
//
// https://twitter.com/topjohnwu/status/1174392824625676288
// https://github.com/topjohnwu/Magisk/blob/master/scripts/util_functions.sh#L193
// https://github.com/opengapps/opengapps/blob/master/scripts/inc.installer.sh#L710
// https://github.com/topjohnwu/Magisk/blob/master/docs/boot.md
// https://github.com/topjohnwu/Magisk/blob/master/scripts/util_functions.sh#L239
// https://github.com/opengapps/opengapps/blob/master/scripts/inc.installer.sh#L1120
//
// https://github.com/penn5/TrebleCheck/blob/master/app/src/main/java/tk/hack5/treblecheck/MountDetector.kt
// https://github.com/kevintresuelo/treble/blob/master/app/src/main/java/com/kevintresuelo/treble/checker/SystemAsRoot.kt
private const val PROP_SYSTEM_ROOT_IMAGE = "ro.build.system_root_image"
private const val CMD_MOUNT_DEV_ROOT = "grep '/dev/root / ' /proc/mounts"
private const val CMD_MOUNT_SYSTEM =
"grep ' /system ' /proc/mounts | grep -v 'tmpfs' | grep -v 'none'"

private const val CMD_MOUNT = "cat /proc/mounts"

Expand Down Expand Up @@ -284,7 +285,7 @@ class HomeViewModel : BaseListViewModel() {

val mounts = getMounts()

detectSar(tempModels)
detectSar(tempModels, mounts)

detectApex(tempModels, mounts)

Expand Down Expand Up @@ -709,24 +710,49 @@ class HomeViewModel : BaseListViewModel() {
)
}

private fun detectSar(tempModels: ArrayList<MyModel>) {
val hasSystemRootImage =
private fun detectSar(tempModels: ArrayList<MyModel>, mounts: List<Mount>) {
val isLAndroid9TheLegacySar =
getStringProperty(PROP_SYSTEM_ROOT_IMAGE, isAtLeastStableAndroid9()).toBoolean()

val mountDevRootResult = sh(CMD_MOUNT_DEV_ROOT, isAtLeastStableAndroid9())
val hasMountDevRoot = isShellResultSuccessful(mountDevRootResult)
val isTheLegacySarMount = isAtLeastStableAndroid9()
&& mounts.any { it.blockDevice == "/dev/root" && it.mountPoint == "/" }

val mountSystemResult =
sh(CMD_MOUNT_SYSTEM, isAtLeastStableAndroid9() && !hasSystemRootImage)
val hasMountSystem = isShellResultSuccessful(mountSystemResult)
val isTheLegacySar = isLAndroid9TheLegacySar && isTheLegacySarMount

val isThe2siSar = isAtLeastStableAndroid10()
&& mounts.none { it.blockDevice != "none" && it.mountPoint == "/system" && it.type != "tmpfs" }

val isSar = isTheLegacySar || isThe2siSar || isAtLeastStableAndroid10()
var result = translate(isSar)

@ColorRes var color = R.color.colorCritical
@StringRes val sarTypeRes: Int

if (isSar) {
when {
isTheLegacySar -> {
color = R.color.colorWaring
sarTypeRes = R.string.sar_type_legacy
}
isThe2siSar -> {
color = R.color.colorNoProblem
sarTypeRes = R.string.sar_type_2si
}
else -> {
color = R.color.colorCritical
sarTypeRes = android.R.string.unknownName
}
}

val sarType = MyApplication.getMyString(sarTypeRes)
result += " ($sarType)"
}

val isSar =
isAtLeastStableAndroid9() && (hasSystemRootImage || hasMountDevRoot || !hasMountSystem)
add(
tempModels,
MyApplication.getMyString(R.string.sar_status_title),
translate(isSar),
isSar
result,
color
)
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr-rFR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<string name="built_in_vndk_version_result">\u0020(Niveau: %s)</string>

<string name="sar_status_title">Status System-as-root</string>
<string name="sar_type_legacy">Héritage</string>

<string name="apex_status_title">Status APEX</string>
<string name="apex_legacy_flattened">\u0020(Héritage aplati)</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@
<string name="built_in_vndk_version_result">\u0020(等级: %s)</string>

<string name="sar_status_title">System-as-root 状态</string>
<string name="sar_type_legacy">旧版</string>

<string name="apex_status_title">APEX 状态</string>
<string name="apex_legacy_flattened">\u0020(旧式扁平的)</string>
<string name="apex_legacy_flattened">\u0020(旧版扁平的)</string>

<string name="developer_options_status_title">开发者选项 状态</string>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<string name="built_in_vndk_version_result">\u0020(Level: %s)</string>

<string name="sar_status_title">System-as-root status</string>
<string name="sar_type_legacy">Legacy</string>
<string name="sar_type_2si" translatable="false">2-Stage-Init</string>

<string name="apex_status_title">APEX status</string>
<string name="apex_legacy_flattened">\u0020(Legacy flattened)</string>
Expand Down

0 comments on commit 95d7649

Please sign in to comment.