Implement logging and bug fixes

This commit is contained in:
topjohnwu 2017-01-27 01:02:40 +08:00
parent a5c0411be0
commit 959ed7f866
7 changed files with 120 additions and 8 deletions

View File

@ -46,6 +46,8 @@ public class Global {
public static boolean isDarkTheme; public static boolean isDarkTheme;
public static boolean shellLogging; public static boolean shellLogging;
public static boolean devLogging; public static boolean devLogging;
public static int suRequestTimeout = 10;
public static int suLogTimeout = 14;
} }
@ -59,7 +61,7 @@ public class Global {
static void updateMagiskInfo() { static void updateMagiskInfo() {
List<String> ret = Shell.sh("getprop magisk.version"); List<String> ret = Shell.sh("getprop magisk.version");
if (Utils.isValidShellResponse(ret)) { if (!Utils.isValidShellResponse(ret)) {
Info.magiskVersion = -1; Info.magiskVersion = -1;
} else { } else {
try { try {

View File

@ -45,7 +45,7 @@ public class Policy {
values.put("uid", uid); values.put("uid", uid);
values.put("package_name", packageName); values.put("package_name", packageName);
values.put("app_name", appName); values.put("app_name", appName);
values.put("policy",policy); values.put("policy", policy);
values.put("until", until); values.put("until", until);
values.put("logging", logging ? 1 : 0); values.put("logging", logging ? 1 : 0);
values.put("notification", notification ? 1 : 0); values.put("notification", notification ? 1 : 0);

View File

@ -62,7 +62,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
Policy policy; Policy policy;
// Clear outdated policies // Clear outdated policies
db.delete(TABLE_NAME, "until > 0 and until < ?", new String[] { String.valueOf(System.currentTimeMillis()) }); db.delete(TABLE_NAME, "until > 0 and until < ?", new String[] { String.valueOf(System.currentTimeMillis()) });
try (Cursor c = db.query(TABLE_NAME, null, null, null, null, null, "app_name")) { try (Cursor c = db.query(TABLE_NAME, null, null, null, null, null, "app_name ASC")) {
while (c.moveToNext()) { while (c.moveToNext()) {
policy = new Policy(c); policy = new Policy(c);
// Package is uninstalled // Package is uninstalled

View File

@ -0,0 +1,62 @@
package com.topjohnwu.magisk.superuser;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.topjohnwu.magisk.Global;
import java.util.ArrayList;
import java.util.List;
public class SuLogDatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VER = 1;
private static final String TABLE_NAME = "logs";
public SuLogDatabaseHelper(Context context) {
super(context, "sulog.db", null, DATABASE_VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " +
"to_uid INT, action INT, time INT, command TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Currently new database, no upgrading
}
public void addLog(SuLogEntry log) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, log.getContentValues());
db.close();
}
public List<SuLogEntry> getLogList() {
return getLogList(null);
}
public List<SuLogEntry> getLogList(int uid) {
return getLogList("uid=" + uid);
}
public List<SuLogEntry> getLogList(String selection) {
List<SuLogEntry> ret = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase();
// Clear outdated logs
db.delete(TABLE_NAME, "time < ?", new String[] { String.valueOf(
System.currentTimeMillis() / 1000 - Global.Configs.suLogTimeout * 86400) });
try (Cursor c = db.query(TABLE_NAME, null, selection, null, null, null, "time DESC")) {
while (c.moveToNext())
ret.add(new SuLogEntry(c));
}
db.close();
return ret;
}
}

View File

@ -0,0 +1,42 @@
package com.topjohnwu.magisk.superuser;
import android.content.ContentValues;
import android.database.Cursor;
public class SuLogEntry {
public int fromUid, toUid, fromPid;
public String packageName, appName, command;
public boolean action;
public long time;
public SuLogEntry(Policy policy) {
fromUid = policy.uid;
packageName = policy.packageName;
appName = policy.appName;
}
public SuLogEntry(Cursor c) {
fromUid = c.getInt(c.getColumnIndex("from_uid"));
fromPid = c.getInt(c.getColumnIndex("from_pid"));
toUid = c.getInt(c.getColumnIndex("to_uid"));
packageName = c.getString(c.getColumnIndex("package_name"));
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"));
}
public ContentValues getContentValues() {
ContentValues values = new ContentValues();
values.put("from_uid", fromUid);
values.put("package_name", packageName);
values.put("app_name", appName);
values.put("from_pid", fromPid);
values.put("command", command);
values.put("to_uid", toUid);
values.put("action", action);
values.put("time", time);
return values;
}
}

View File

@ -24,8 +24,8 @@ public class SuReceiver extends BroadcastReceiver {
action = intent.getStringExtra("action"); action = intent.getStringExtra("action");
if (action == null) return; if (action == null) return;
SuDatabaseHelper dbHelper = new SuDatabaseHelper(context); SuDatabaseHelper suDbHelper = new SuDatabaseHelper(context);
policy = dbHelper.getPolicy(fromUid); policy = suDbHelper.getPolicy(fromUid);
if (policy == null) try { if (policy == null) try {
policy = new Policy(fromUid, context.getPackageManager()); policy = new Policy(fromUid, context.getPackageManager());
} catch (Throwable throwable) { } catch (Throwable throwable) {
@ -53,7 +53,12 @@ public class SuReceiver extends BroadcastReceiver {
if (pid < 0) return; if (pid < 0) return;
command = intent.getStringExtra("command"); command = intent.getStringExtra("command");
if (command == null) return; if (command == null) return;
// TODO: Place info into logs SuLogEntry log = new SuLogEntry(policy);
log.toUid = toUid;
log.fromPid = pid;
log.command = command;
SuLogDatabaseHelper logDbHelper = new SuLogDatabaseHelper(context);
logDbHelper.addLog(log);
} }
} }
} }

View File

@ -18,6 +18,7 @@ import android.widget.LinearLayout;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import com.topjohnwu.magisk.Global;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import java.io.DataInputStream; import java.io.DataInputStream;
@ -46,7 +47,7 @@ public class SuRequestActivity extends AppCompatActivity {
private PackageManager pm; private PackageManager pm;
private PackageInfo info; private PackageInfo info;
private int uid, countdown = 10; private int uid;
private String appName, packageName; private String appName, packageName;
private CountDownTimer timer; private CountDownTimer timer;
@ -73,7 +74,7 @@ public class SuRequestActivity extends AppCompatActivity {
appNameView.setText(appName); appNameView.setText(appName);
packageNameView.setText(packageName); packageNameView.setText(packageName);
timer = new CountDownTimer(countdown * 1000, 1000) { timer = new CountDownTimer(Global.Configs.suRequestTimeout * 1000, 1000) {
@Override @Override
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
deny_btn.setText(getString(R.string.deny, "(" + millisUntilFinished / 1000 + ")")); deny_btn.setText(getString(R.string.deny, "(" + millisUntilFinished / 1000 + ")"));