Skip to content

Commit

Permalink
manager: Optimize PackageConfigListener implement
Browse files Browse the repository at this point in the history
Signed-off-by: GarfieldHan <[email protected]>
  • Loading branch information
pomelohan committed Apr 7, 2024
1 parent 6c7bac4 commit aae1651
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
22 changes: 19 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
<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" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<permission
android:name="${applicationId}.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
android:protectionLevel="signature"
tools:node="remove" />

<uses-permission
android:name="${applicationId}.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
tools:node="remove" />

<application
android:name=".APApplication"
android:allowBackup="true"
Expand Down Expand Up @@ -42,11 +53,16 @@
android:theme="@style/Theme.APatch.WebUI" />

<receiver android:name=".util.PackageConfigListener"
android:exported="false">
android:exported="false"
android:enabled="true" android:priority="999">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
<action android:name="android.intent.action.UID_REMOVED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_FULLY_REMOVED" />
<data android:scheme="package" />
</intent-filter>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/me/bmax/apatch/ui/screen/Patches.kt
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private fun SetSuperKeyView(viewModel: PatchesViewModel) {
style = MaterialTheme.typography.bodyMedium)
}
Column {
Spacer(modifier = Modifier.height(8.dp))
//Spacer(modifier = Modifier.height(8.dp))
Box(
contentAlignment = Alignment.CenterEnd,
) {
Expand Down
42 changes: 25 additions & 17 deletions app/src/main/java/me/bmax/apatch/util/PackageConfigListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.annotation.CallSuper
import me.bmax.apatch.APApplication
import me.bmax.apatch.Natives
import kotlin.concurrent.thread

class PackageConfigListener : BroadcastReceiver() {
companion object {
private const val TAG = "APatchPkgListener"
private const val TAG = "APatchPkgListener"
open class PackageConfigListener : BroadcastReceiver() {
private fun getUid(intent: Intent): Int? {
val uid = intent.getIntExtra(Intent.EXTRA_UID, -1)
return if (uid == -1) null else uid
}

private fun getPkg(intent: Intent): String? {
val pkg = intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME)
return pkg ?: intent.data?.schemeSpecificPart
}

override fun onReceive(context: Context?, intent: Intent?) {

intent?.let {
override fun onReceive(context: Context?, intent: Intent?) {
Log.i(TAG, "Recieved: ${intent?.action}")
intent?.let { it ->
if (it.action == Intent.ACTION_PACKAGE_REMOVED || it.action == Intent.ACTION_PACKAGE_FULLY_REMOVED) {
val prefs =
context?.getSharedPreferences(APApplication.SP_NAME, Context.MODE_PRIVATE)
Expand All @@ -25,20 +34,19 @@ class PackageConfigListener : BroadcastReceiver() {
}
}
APatchKeyHelper.setSharedPreferences(prefs)
APApplication.superKey = APatchKeyHelper.readSPSuperKey()

it.data?.encodedSchemeSpecificPart?.let { packageName ->
val packageInfo = context?.packageManager?.getPackageInfo(packageName, 0)
val uid = packageInfo?.applicationInfo?.uid
val superKey = APatchKeyHelper.readSPSuperKey()
if (superKey.isNullOrBlank())
return
APApplication.superKey = superKey

if(uid == null) {
Log.e(TAG, "package $packageName uninstall, but can't find uid")
} else {
Log.e(TAG, "package $packageName uninstall, uid: $uid")
val packageUid = getUid(intent)
val packageName = getPkg(intent)

val config = PkgConfig.Config(packageName, 1, 0, Natives.Profile(uid, 0, ""));
PkgConfig.changeConfig(config)
}
if (packageUid != null && packageName != null) {
val config =
PkgConfig.Config(packageName, 1, 0, Natives.Profile(packageUid, 0, ""));
PkgConfig.changeConfig(config)
Log.i(TAG, "Package $packageName uninstall, uid: $packageUid")
}
}
}
Expand Down

0 comments on commit aae1651

Please sign in to comment.