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;
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"));

View File

@ -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) {

View File

@ -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) {