Skip to content

Commit

Permalink
Merge pull request #155 from bmax121/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
bmax121 authored Jan 17, 2024
2 parents 0528021 + 5a98026 commit f019fb8
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ on:
pull_request:
branches: [ "main" ]
paths:
- '.github/workflows/build.yml'
- 'app/**'
- 'apd/**'
- 'build.gradle.kts'
workflow_call:
workflow_dispatch:

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<application
android:name=".APApplication"
Expand Down Expand Up @@ -43,6 +42,7 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>

<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
Expand All @@ -51,6 +51,7 @@
android:name="autoStoreLocales"
android:value="true" />
</service>

</application>

</manifest>
9 changes: 9 additions & 0 deletions app/src/main/aidl/me/bmax/apatch/IAPRootService.aidl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// IAPRootService.aidl
package me.bmax.apatch;

import android.content.pm.PackageInfo;
import rikka.parcelablelist.ParcelableListSlice;

interface IAPRootService {
ParcelableListSlice<PackageInfo> getPackages(int flags);
}
3 changes: 3 additions & 0 deletions app/src/main/java/me/bmax/apatch/APatchApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.bmax.apatch

import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.util.Log
import androidx.lifecycle.LiveData
Expand Down Expand Up @@ -211,6 +212,8 @@ class APApplication : Application() {
sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE)
superKey = sharedPreferences.getString(SUPER_KEY, "") ?: ""



val context = this
val iconSize = resources.getDimensionPixelSize(android.R.dimen.app_icon_size)
Coil.setImageLoader(
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/me/bmax/apatch/services/RootServices.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package me.bmax.apatch.services;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import androidx.annotation.NonNull;

import com.topjohnwu.superuser.ipc.RootService;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import me.bmax.apatch.IAPRootService;
import me.bmax.apatch.Natives;
import rikka.parcelablelist.ParcelableListSlice;

public class RootServices extends RootService {
private static final String TAG = "RootServices";

class Stub extends IAPRootService.Stub {
@Override
public ParcelableListSlice<PackageInfo> getPackages(int flags) {
List<PackageInfo> list = getInstalledPackagesAll(flags);
Log.i(TAG, "getPackages: " + list.size());
return new ParcelableListSlice<>(list);
}
}

@Override
public IBinder onBind(@NonNull Intent intent) {
return new Stub();
}

List<Integer> getUserIds() {
List<Integer> result = new ArrayList<>();
UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
List<UserHandle> userProfiles = um.getUserProfiles();
for (UserHandle userProfile : userProfiles) {
int userId = userProfile.hashCode();
result.add(userProfile.hashCode());
}
return result;
}

ArrayList<PackageInfo> getInstalledPackagesAll(int flags) {
ArrayList<PackageInfo> packages = new ArrayList<>();
for (Integer userId : getUserIds()) {
Log.i(TAG, "getInstalledPackagesAll: " + userId);
packages.addAll(getInstalledPackagesAsUser(flags, userId));
}
return packages;
}

List<PackageInfo> getInstalledPackagesAsUser(int flags, int userId) {
try {
PackageManager pm = getPackageManager();
Method getInstalledPackagesAsUser = pm.getClass().getDeclaredMethod("getInstalledPackagesAsUser", int.class, int.class);
return (List<PackageInfo>) getInstalledPackagesAsUser.invoke(pm, flags, userId);
} catch (Throwable e) {
Log.e(TAG, "err", e);
}

return new ArrayList<>();
}
}
75 changes: 61 additions & 14 deletions app/src/main/java/me/bmax/apatch/ui/viewmodel/SuperUserViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
package me.bmax.apatch.ui.viewmodel

import android.content.ComponentName
import android.content.Context.BIND_AUTO_CREATE
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.os.IBinder
import android.os.Parcelable
import android.os.SystemClock
import android.util.Log
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.parcelize.Parcelize
import me.bmax.apatch.IAPRootService
import me.bmax.apatch.Natives
import me.bmax.apatch.apApp
import me.bmax.apatch.services.RootServices
import me.bmax.apatch.util.APatchCli
import me.bmax.apatch.util.HanziToPinyin
import me.bmax.apatch.util.PkgConfig
import me.bmax.apatch.util.ServicesUtil
import java.text.Collator
import java.util.*
import kotlin.collections.HashMap
import kotlin.concurrent.thread
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine


class SuperUserViewModel : ViewModel() {
Expand Down Expand Up @@ -67,28 +78,64 @@ class SuperUserViewModel : ViewModel() {
}
}

private suspend inline fun connectRootService(
crossinline onDisconnect: () -> Unit = {}
): Pair<IBinder, ServiceConnection> = suspendCoroutine {
val connection = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
onDisconnect()
}
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
it.resume(binder as IBinder to this)
}
}
val intent = Intent(apApp, RootServices::class.java);
val task = RootServices.bindOrTask(
intent,
Shell.EXECUTOR,
connection,
)
val shell = APatchCli.SHELL
task?.let { it1 -> shell.execTask(it1) }
}

fun fetchAppList() {
private fun stopRootService() {
val intent = Intent(apApp, RootServices::class.java);
RootServices.stop(intent)
}

suspend fun fetchAppList() {
isRefreshing = true

thread {
Natives.su(0, "")
val result = connectRootService {
Log.w(TAG, "RootService disconnected")
}

withContext(Dispatchers.IO) {
val binder = result.first
val allPackages = IAPRootService.Stub.asInterface(binder).getPackages(0)

withContext(Dispatchers.Main) {
stopRootService()
}
val uids = Natives.suUids().toList()
Log.d(TAG, "all allows: ${uids}")

val configs: HashMap<String, PkgConfig.Config> = PkgConfig.configs
Log.d(TAG, "all configs: ${configs}")

val allPackages = ServicesUtil.getInstalledPackagesAll(apApp, 0)

apps = allPackages.map { it ->
apps = allPackages.list.map { it ->
val appInfo = it.applicationInfo
val uid = appInfo.uid
val actProfile = if(uids.contains(uid)) Natives.suProfile(uid) else null
val config = configs.getOrDefault(appInfo.packageName,
PkgConfig.Config(appInfo.packageName, 1, 0, Natives.Profile(uid)))
val actProfile = if (uids.contains(uid)) Natives.suProfile(uid) else null
val config = configs.getOrDefault(
appInfo.packageName,
PkgConfig.Config(appInfo.packageName, 1, 0, Natives.Profile(uid))
)
config.allow = 0
// profile from kernel
if(actProfile != null) {

// from kernel
if (actProfile != null) {
config.allow = 1
config.profile = actProfile
}
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/java/me/bmax/apatch/util/APatchCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ fun getRootShell(): Shell {

fun createRootShell(): Shell {
Shell.enableVerboseLogging = BuildConfig.DEBUG
val builder = Shell.Builder.create().apply {
setFlags(Shell.FLAG_MOUNT_MASTER)
}
val builder = Shell.Builder.create()
return try {
builder.build(
getKPatchPath(),
APApplication.superKey,
"su",
"-x",
"-Z",
APApplication.MAGISK_SCONTEXT
)
} catch (e: Throwable) {
Expand All @@ -54,7 +52,7 @@ fun createRootShellForLog(): Shell {
getKPatchPath(),
APApplication.superKey,
"su",
"-x",
"-Z",
APApplication.MAGISK_SCONTEXT
)
} catch (e: Throwable) {
Expand Down Expand Up @@ -173,14 +171,14 @@ fun hasMagisk(): Boolean {
fun isGlobalNamespaceEnabled(): Boolean {
val shell = getRootShell()
val result =
ShellUtils.fastCmd(shell, "nsenter --mount=/proc/1/ns/mnt cat ${APApplication.GLOBAL_NAMESPACE_FILE}")
ShellUtils.fastCmd(shell, "cat ${APApplication.GLOBAL_NAMESPACE_FILE}")
Log.i(TAG, "is global namespace enabled: $result")
return result == "1"
}

fun setGlobalNamespaceEnabled(value: String) {
getRootShell().newJob()
.add("nsenter --mount=/proc/1/ns/mnt echo $value > ${APApplication.GLOBAL_NAMESPACE_FILE}")
.add("echo $value > ${APApplication.GLOBAL_NAMESPACE_FILE}")
.submit { result ->
Log.i(TAG, "setGlobalNamespaceEnabled result: ${result.isSuccess} [${result.out}]")
}
Expand Down
47 changes: 0 additions & 47 deletions app/src/main/java/me/bmax/apatch/util/ServicesUtil.java

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cmaker {
}
}

project.ext.set("kernelPatchVersion", "0.8.4")
project.ext.set("kernelPatchVersion", "0.8.5")

val androidMinSdkVersion = 26
val androidTargetSdkVersion = 33
Expand Down

0 comments on commit f019fb8

Please sign in to comment.