diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java b/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java index b967cfc65..bce29e8e8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/Policy.java @@ -1,6 +1,8 @@ package com.topjohnwu.magisk.superuser; import android.content.ContentValues; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.database.Cursor; @@ -12,6 +14,17 @@ public class Policy { public Policy() {} + public Policy(int uid, PackageManager pm) throws Throwable { + String[] pkgs = pm.getPackagesForUid(uid); + if (pkgs != null && pkgs.length > 0) { + PackageInfo info = pm.getPackageInfo(pkgs[0], 0); + packageName = pkgs[0]; + appName = info.applicationInfo.loadLabel(pm).toString(); + logging = true; + notification = true; + } else throw new Throwable(); + } + public Policy(Cursor c) { uid = c.getInt(c.getColumnIndex("uid")); packageName = c.getString(c.getColumnIndex("package_name")); diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java index a45a560f9..c0fced9fd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.Process; import android.widget.Toast; import com.topjohnwu.magisk.R; @@ -13,15 +14,25 @@ public class SuReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { int fromUid, toUid, pid; String command, action; + Policy policy; if (intent == null) return; + fromUid = intent.getIntExtra("from.uid", -1); if (fromUid < 0) return; + if (fromUid == Process.myUid()) return; // Don't show anything if it's Magisk Manager + action = intent.getStringExtra("action"); if (action == null) return; + SuDatabaseHelper dbHelper = new SuDatabaseHelper(context); - Policy policy = dbHelper.getPolicy(fromUid); - if (policy == null) return; + policy = dbHelper.getPolicy(fromUid); + if (policy == null) try { + policy = new Policy(fromUid, context.getPackageManager()); + } catch (Throwable throwable) { + return; + } + if (policy.notification) { String message; switch (action) { diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java index ba8809ba1..9ee97b1d3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuRequestActivity.java @@ -71,7 +71,7 @@ public class SuRequestActivity extends AppCompatActivity { packageNameView.setText(packageName); grant_btn.setOnClickListener(v -> handleAction(true, timeoutList[timeout.getSelectedItemPosition()])); - deny_btn.setOnClickListener(v -> handleAction(false, 0)); + deny_btn.setOnClickListener(v -> handleAction(false, timeoutList[timeout.getSelectedItemPosition()])); } void handleAction(boolean action, int timeout) {