Fix toasts and timeouts

This commit is contained in:
topjohnwu 2017-01-25 01:23:41 +08:00
parent ad131854ca
commit 151a153dc9
3 changed files with 27 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package com.topjohnwu.magisk.superuser; package com.topjohnwu.magisk.superuser;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
@ -12,6 +14,17 @@ public class Policy {
public 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) { public Policy(Cursor c) {
uid = c.getInt(c.getColumnIndex("uid")); uid = c.getInt(c.getColumnIndex("uid"));
packageName = c.getString(c.getColumnIndex("package_name")); packageName = c.getString(c.getColumnIndex("package_name"));

View File

@ -4,6 +4,7 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Process;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
@ -13,15 +14,25 @@ public class SuReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
int fromUid, toUid, pid; int fromUid, toUid, pid;
String command, action; String command, action;
Policy policy;
if (intent == null) return; if (intent == null) return;
fromUid = intent.getIntExtra("from.uid", -1); fromUid = intent.getIntExtra("from.uid", -1);
if (fromUid < 0) return; if (fromUid < 0) return;
if (fromUid == Process.myUid()) return; // Don't show anything if it's Magisk Manager
action = intent.getStringExtra("action"); action = intent.getStringExtra("action");
if (action == null) return; if (action == null) return;
SuDatabaseHelper dbHelper = new SuDatabaseHelper(context); SuDatabaseHelper dbHelper = new SuDatabaseHelper(context);
Policy policy = dbHelper.getPolicy(fromUid); policy = dbHelper.getPolicy(fromUid);
if (policy == null) return; if (policy == null) try {
policy = new Policy(fromUid, context.getPackageManager());
} catch (Throwable throwable) {
return;
}
if (policy.notification) { if (policy.notification) {
String message; String message;
switch (action) { switch (action) {

View File

@ -71,7 +71,7 @@ public class SuRequestActivity extends AppCompatActivity {
packageNameView.setText(packageName); packageNameView.setText(packageName);
grant_btn.setOnClickListener(v -> handleAction(true, timeoutList[timeout.getSelectedItemPosition()])); 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) { void handleAction(boolean action, int timeout) {