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