91 lines
2.8 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-09-30 03:04:23 +08:00
import com.topjohnwu.magisk.container.Policy;
import com.topjohnwu.magisk.container.SuLogEntry;
2017-11-06 04:41:23 +08:00
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
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
@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-11-06 04:41:23 +08:00
MagiskManager mm = Utils.getMagiskManager(context);
2017-02-07 02:01:32 +08:00
if (intent == null) return;
2017-01-25 01:23:41 +08:00
mode = intent.getIntExtra("mode", -1);
if (mode < 0) return;
2017-11-06 04:41:23 +08:00
if (mode == Const.Value.NOTIFY_USER_TO_OWNER) {
2017-10-16 00:54:48 +08:00
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-11-06 04:41:23 +08:00
policy = mm.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-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
2017-11-06 04:41:23 +08:00
if (policy.notification && mm.suNotificationType == Const.Value.NOTIFICATION_TOAST) {
MagiskManager.toast(message, Toast.LENGTH_SHORT);
}
2017-02-06 08:16:48 +08:00
2017-11-06 04:41:23 +08:00
if (mode == Const.Value.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-11-06 04:41:23 +08:00
mm.suDB.addLog(log);
}
}
}