89 lines
2.8 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk.utils;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Process;
import android.widget.Toast;
2019-01-30 03:10:12 -05:00
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.Config;
2019-04-10 18:09:41 -04:00
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.model.entity.Policy;
import com.topjohnwu.magisk.model.entity.SuLogEntry;
import java.util.Date;
2019-04-10 18:09:41 -04:00
public class SuLogger {
2019-04-10 18:09:41 -04:00
public static void handleLogs(Intent intent) {
int fromUid = intent.getIntExtra("from.uid", -1);
if (fromUid < 0) return;
if (fromUid == Process.myUid()) return;
App app = App.self;
PackageManager pm = app.getPackageManager();
2018-10-27 22:06:24 -04:00
Policy policy;
boolean notify;
Bundle data = intent.getExtras();
if (data.containsKey("notify")) {
notify = data.getBoolean("notify");
try {
2018-10-27 22:06:24 -04:00
policy = new Policy(fromUid, pm);
} catch (PackageManager.NameNotFoundException e) {
return;
}
2018-10-27 22:06:24 -04:00
} else {
// Doesn't report whether notify or not, check database ourselves
policy = app.getDB().getPolicy(fromUid);
2018-10-27 22:06:24 -04:00
if (policy == null)
return;
notify = policy.notification;
}
policy.policy = data.getInt("policy", -1);
if (policy.policy < 0)
return;
2018-10-27 22:06:24 -04:00
if (notify)
handleNotify(policy);
SuLogEntry log = new SuLogEntry(policy);
2018-10-27 22:06:24 -04:00
int toUid = intent.getIntExtra("to.uid", -1);
if (toUid < 0) return;
int pid = intent.getIntExtra("pid", -1);
if (pid < 0) return;
String command = intent.getStringExtra("command");
if (command == null) return;
log.toUid = toUid;
log.fromPid = pid;
log.command = command;
log.date = new Date();
app.getDB().addLog(log);
2018-10-27 22:06:24 -04:00
}
2019-04-10 18:09:41 -04:00
private static void handleNotify(Policy policy) {
2019-01-21 15:49:03 -05:00
if (policy.notification &&
2019-04-10 18:09:41 -04:00
(int) Config.get(Config.Key.SU_NOTIFICATION) == Config.Value.NOTIFICATION_TOAST) {
Utils.toast(App.self.getString(policy.policy == Policy.ALLOW ?
R.string.su_allow_toast : R.string.su_deny_toast, policy.appName),
Toast.LENGTH_SHORT);
}
2018-10-27 22:06:24 -04:00
}
2019-04-10 18:09:41 -04:00
public static void handleNotify(Intent intent) {
2018-10-27 22:06:24 -04:00
int fromUid = intent.getIntExtra("from.uid", -1);
if (fromUid < 0) return;
if (fromUid == Process.myUid()) return;
try {
Policy policy = new Policy(fromUid, App.self.getPackageManager());
2018-10-27 22:06:24 -04:00
policy.policy = intent.getIntExtra("policy", -1);
if (policy.policy >= 0)
handleNotify(policy);
} catch (PackageManager.NameNotFoundException ignored) {}
}
}