Add Superuser logging UI

This commit is contained in:
topjohnwu
2017-01-28 01:10:50 +08:00
parent ca9334b2df
commit cbb32f82eb
23 changed files with 594 additions and 71 deletions
@@ -2,13 +2,20 @@ package com.topjohnwu.magisk.superuser;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
public class SuLogEntry {
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SuLogEntry implements Parcelable {
public int fromUid, toUid, fromPid;
public String packageName, appName, command;
public boolean action;
public long time;
public Date date;
public SuLogEntry(Policy policy) {
fromUid = policy.uid;
@@ -24,7 +31,7 @@ public class SuLogEntry {
appName = c.getString(c.getColumnIndex("app_name"));
command = c.getString(c.getColumnIndex("command"));
action = c.getInt(c.getColumnIndex("action")) != 0;
time = c.getLong(c.getColumnIndex("until"));
date = new Date(c.getLong(c.getColumnIndex("time")) * 1000);
}
public ContentValues getContentValues() {
@@ -35,8 +42,57 @@ public class SuLogEntry {
values.put("from_pid", fromPid);
values.put("command", command);
values.put("to_uid", toUid);
values.put("action", action);
values.put("time", time);
values.put("action", action ? 1 : 0);
values.put("time", date.getTime() / 1000);
return values;
}
public String getDateString() {
return DateFormat.getDateInstance(DateFormat.MEDIUM).format(date);
}
public String getTimeString() {
return new SimpleDateFormat("h:mm a", Locale.US).format(date);
}
public static final Creator<SuLogEntry> CREATOR = new Creator<SuLogEntry>() {
@Override
public SuLogEntry createFromParcel(Parcel in) {
return new SuLogEntry(in);
}
@Override
public SuLogEntry[] newArray(int size) {
return new SuLogEntry[size];
}
};
protected SuLogEntry(Parcel in) {
fromUid = in.readInt();
toUid = in.readInt();
fromPid = in.readInt();
packageName = in.readString();
appName = in.readString();
command = in.readString();
action = in.readByte() != 0;
date = new Date(in.readLong());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(fromUid);
dest.writeInt(toUid);
dest.writeInt(fromPid);
dest.writeString(packageName);
dest.writeString(appName);
dest.writeString(command);
dest.writeByte((byte) (action ? 1 : 0));
dest.writeLong(date.getTime());
}
}
@@ -8,6 +8,8 @@ import android.widget.Toast;
import com.topjohnwu.magisk.R;
import java.util.Date;
public class SuReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
@@ -32,14 +34,18 @@ public class SuReceiver extends BroadcastReceiver {
return;
}
SuLogEntry log = new SuLogEntry(policy);
if (policy.notification) {
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;
@@ -53,10 +59,10 @@ public class SuReceiver extends BroadcastReceiver {
if (pid < 0) return;
command = intent.getStringExtra("command");
if (command == null) return;
SuLogEntry log = new SuLogEntry(policy);
log.toUid = toUid;
log.fromPid = pid;
log.command = command;
log.date = new Date();
SuLogDatabaseHelper logDbHelper = new SuLogDatabaseHelper(context);
logDbHelper.addLog(log);
}