95 lines
2.9 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk.superuser;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
2017-06-01 00:19:52 +08:00
import android.content.pm.PackageManager;
2017-01-25 01:23:41 +08:00
import android.os.Process;
import android.widget.Toast;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R;
2017-01-28 01:10:50 +08:00
import java.util.Date;
public class SuReceiver extends BroadcastReceiver {
2017-01-28 06:13:07 +08:00
private static final int NO_NOTIFICATION = 0;
private static final int TOAST = 1;
private static final int NOTIFY_NORMAL_LOG = 0;
private static final int NOTIFY_USER_TOASTS = 1;
private static final int NOTIFY_USER_TO_OWNER = 2;
2017-01-28 06:13:07 +08:00
@Override
public void onReceive(Context context, Intent intent) {
int fromUid, toUid, pid, mode;
String command, action;
2017-01-25 01:23:41 +08:00
Policy policy;
2017-02-07 02:01:32 +08:00
MagiskManager magiskManager = (MagiskManager) context.getApplicationContext();
if (intent == null) return;
2017-01-25 01:23:41 +08:00
mode = intent.getIntExtra("mode", -1);
if (mode < 0) return;
if (mode == NOTIFY_USER_TO_OWNER) {
magiskManager.toast(R.string.multiuser_hint_owner_request, Toast.LENGTH_LONG);
return;
}
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
action = intent.getStringExtra("action");
if (action == null) return;
2017-01-25 01:23:41 +08:00
2017-06-01 00:19:52 +08:00
policy = magiskManager.suDB.getPolicy(fromUid);
if (policy == null) {
try {
policy = new Policy(fromUid, context.getPackageManager());
2017-06-01 00:19:52 +08:00
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return;
}
2017-01-25 01:23:41 +08:00
}
2017-05-31 16:31:33 +08:00
magiskManager.initSUConfig();
2017-01-28 06:13:07 +08:00
2017-01-28 01:10:50 +08:00
SuLogEntry log = new SuLogEntry(policy);
2017-02-06 08:16:48 +08:00
String message;
switch (action) {
case "allow":
message = context.getString(R.string.su_allow_toast, policy.appName);
log.action = true;
break;
case "deny":
message = context.getString(R.string.su_deny_toast, policy.appName);
log.action = false;
break;
default:
return;
}
2017-02-06 08:16:48 +08:00
if (policy.notification && magiskManager.suNotificationType == TOAST) {
magiskManager.toast(message, Toast.LENGTH_SHORT);
}
2017-02-06 08:16:48 +08:00
if (mode == NOTIFY_NORMAL_LOG && 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
log.toUid = toUid;
log.fromPid = pid;
log.command = command;
2017-01-28 01:10:50 +08:00
log.date = new Date();
2017-06-01 00:19:52 +08:00
magiskManager.suDB.addLog(log);
}
}
}