Rewrite Magisk log fragment

This commit is contained in:
topjohnwu 2018-07-04 17:59:16 +08:00
parent cf58545a45
commit 316ec98e0f

View File

@ -2,6 +2,7 @@ package com.topjohnwu.magisk;
import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
@ -15,20 +16,18 @@ import android.widget.HorizontalScrollView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.superuser.CallbackList;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell;
import com.topjohnwu.superuser.ShellUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -49,11 +48,7 @@ public class MagiskLogFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_magisk_log, container, false);
unbinder = ButterKnife.bind(this, view);
setHasOptionsMenu(true);
txtLog.setTextIsSelectable(true);
new LogManager().read();
return view;
}
@ -66,7 +61,7 @@ public class MagiskLogFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
new LogManager().read();
readLogs();
}
@Override
@ -84,120 +79,67 @@ public class MagiskLogFragment extends Fragment {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_refresh:
new LogManager().read();
readLogs();
return true;
case R.id.menu_save:
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
() -> new LogManager().save());
runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, this::saveLogs);
return true;
case R.id.menu_clear:
new LogManager().clear();
clearLogs();
return true;
default:
return true;
}
}
private class LogManager extends ParallelTask<Object, Void, Object> {
private int mode;
private File targetFile;
LogManager() {
super(MagiskLogFragment.this.getActivity());
public void readLogs() {
Shell.Async.su(new Shell.Async.Callback() {
@Override
public void onTaskResult(@Nullable List<String> out, @Nullable List<String> err) {
progressBar.setVisibility(View.GONE);
if (ShellUtils.isValidOutput(out)) {
txtLog.setText(TextUtils.join("\n", out));
} else {
txtLog.setText(R.string.log_is_empty);
}
svLog.postDelayed(() -> svLog.fullScroll(ScrollView.FOCUS_DOWN), 100);
hsvLog.postDelayed(() -> hsvLog.fullScroll(ScrollView.FOCUS_LEFT), 100);
}
@Override
protected Object doInBackground(Object... params) {
mode = (int) params[0];
switch (mode) {
case 0:
StringBuilder builder = new StringBuilder();
CallbackList<String> logs = new CallbackList<String>() {
@Override
public void onAddElement(String s) {
builder.append(s).append('\n');
public void onTaskError(@NonNull Throwable throwable) {
txtLog.setText(R.string.log_is_empty);
}
}, "cat " + Const.MAGISK_LOG + " | tail -n 5000");
}
};
Shell.Sync.su(logs, "cat " + Const.MAGISK_LOG + " | tail -n 5000");
return builder;
case 1:
Shell.Async.su("echo -n > " + Const.MAGISK_LOG);
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
return "";
case 2:
public void saveLogs() {
Calendar now = Calendar.getInstance();
String filename = String.format(Locale.US,
"magisk_log_%04d%02d%02d_%02d%02d%02d.log",
String filename = Utils.fmt("magisk_log_%04d%02d%02d_%02d%02d%02d.log",
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
targetFile = new File(Const.EXTERNAL_PATH + "/logs", filename);
if ((!targetFile.getParentFile().exists() && !targetFile.getParentFile().mkdirs())
|| (targetFile.exists() && !targetFile.delete())) {
return false;
}
try (FileWriter out = new FileWriter(targetFile)) {
CallbackList<String> list = new CallbackList<String>() {
@Override
public void onAddElement(String s) {
File targetFile = new File(Const.EXTERNAL_PATH + "/logs", filename);
targetFile.getParentFile().mkdirs();
try {
out.write(s);
out.write("\n");
} catch (IOException ignored) {}
}
};
Shell.Sync.su(list, "cat " + Const.MAGISK_LOG);
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
return;
}
return true;
}
return null;
Shell.Async.su(new Shell.Async.Callback() {
@Override
public void onTaskResult(@Nullable List<String> out, @Nullable List<String> err) {
SnackbarMaker.make(txtLog, targetFile.getPath(), Snackbar.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(Object o) {
if (o == null) return;
switch (mode) {
case 0:
case 1:
CharSequence llog = (CharSequence) o;
progressBar.setVisibility(View.GONE);
if (TextUtils.isEmpty(llog))
txtLog.setText(R.string.log_is_empty);
else
txtLog.setText(llog);
svLog.postDelayed(() -> svLog.fullScroll(ScrollView.FOCUS_DOWN), 100);
hsvLog.postDelayed(() -> hsvLog.fullScroll(ScrollView.FOCUS_LEFT), 100);
break;
case 2:
boolean bool = (boolean) o;
if (bool) {
MagiskManager.toast(targetFile.getPath(), Toast.LENGTH_LONG);
} else {
MagiskManager.toast(R.string.logs_save_failed, Toast.LENGTH_LONG);
}
break;
}
public void onTaskError(@NonNull Throwable throwable) {}
}, "cat " + Const.MAGISK_LOG + " > " + targetFile);
}
void read() {
exec(0);
}
void clear() {
exec(1);
}
void save() {
exec(2);
}
public void clearLogs() {
Shell.Async.su("echo -n > " + Const.MAGISK_LOG);
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
}
}