2017-01-24 14:19:28 +08:00
|
|
|
package com.topjohnwu.magisk.superuser;
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-01-25 01:23:41 +08:00
|
|
|
import android.os.Process;
|
2017-01-24 14:19:28 +08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.topjohnwu.magisk.R;
|
|
|
|
|
|
|
|
public class SuReceiver extends BroadcastReceiver {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
int fromUid, toUid, pid;
|
|
|
|
String command, action;
|
2017-01-25 01:23:41 +08:00
|
|
|
Policy policy;
|
2017-01-24 14:19:28 +08:00
|
|
|
|
|
|
|
if (intent == null) return;
|
2017-01-25 01:23:41 +08:00
|
|
|
|
2017-01-24 14:19:28 +08:00
|
|
|
fromUid = intent.getIntExtra("from.uid", -1);
|
|
|
|
if (fromUid < 0) return;
|
2017-01-25 01:23:41 +08:00
|
|
|
if (fromUid == Process.myUid()) return; // Don't show anything if it's Magisk Manager
|
|
|
|
|
2017-01-24 14:19:28 +08:00
|
|
|
action = intent.getStringExtra("action");
|
|
|
|
if (action == null) return;
|
2017-01-25 01:23:41 +08:00
|
|
|
|
2017-01-27 01:02:40 +08:00
|
|
|
SuDatabaseHelper suDbHelper = new SuDatabaseHelper(context);
|
|
|
|
policy = suDbHelper.getPolicy(fromUid);
|
2017-01-25 01:23:41 +08:00
|
|
|
if (policy == null) try {
|
|
|
|
policy = new Policy(fromUid, context.getPackageManager());
|
|
|
|
} catch (Throwable throwable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:19:28 +08:00
|
|
|
if (policy.notification) {
|
|
|
|
String message;
|
|
|
|
switch (action) {
|
|
|
|
case "allow":
|
|
|
|
message = context.getString(R.string.su_allow_toast, policy.appName);
|
|
|
|
break;
|
|
|
|
case "deny":
|
|
|
|
message = context.getString(R.string.su_deny_toast, policy.appName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
if (policy.logging) {
|
|
|
|
toUid = intent.getIntExtra("to.uid", -1);
|
|
|
|
if (toUid < 0) return;
|
|
|
|
pid = intent.getIntExtra("pid", -1);
|
|
|
|
if (pid < 0) return;
|
|
|
|
command = intent.getStringExtra("command");
|
|
|
|
if (command == null) return;
|
2017-01-27 01:02:40 +08:00
|
|
|
SuLogEntry log = new SuLogEntry(policy);
|
|
|
|
log.toUid = toUid;
|
|
|
|
log.fromPid = pid;
|
|
|
|
log.command = command;
|
|
|
|
SuLogDatabaseHelper logDbHelper = new SuLogDatabaseHelper(context);
|
|
|
|
logDbHelper.addLog(log);
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|