diff --git a/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java b/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java index ea29e5f8e..eef4e01c6 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java @@ -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 { - - private int mode; - private File targetFile; - - LogManager() { - super(MagiskLogFragment.this.getActivity()); - } - - @Override - protected Object doInBackground(Object... params) { - mode = (int) params[0]; - switch (mode) { - case 0: - StringBuilder builder = new StringBuilder(); - CallbackList logs = new CallbackList() { - @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: - 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(); - String filename = String.format(Locale.US, - "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 list = new CallbackList() { - @Override - public void onAddElement(String s) { - try { - out.write(s); - out.write("\n"); - } catch (IOException ignored) {} - } - }; - Shell.Sync.su(list, "cat " + Const.MAGISK_LOG); - } catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; + public void readLogs() { + Shell.Async.su(new Shell.Async.Callback() { + @Override + public void onTaskResult(@Nullable List out, @Nullable List 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); } - return null; - } - @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; + @Override + public void onTaskError(@NonNull Throwable throwable) { + txtLog.setText(R.string.log_is_empty); } - } + }, "cat " + Const.MAGISK_LOG + " | tail -n 5000"); + } - void read() { - exec(0); - } + public void saveLogs() { + Calendar now = Calendar.getInstance(); + 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)); - void clear() { - exec(1); + File targetFile = new File(Const.EXTERNAL_PATH + "/logs", filename); + targetFile.getParentFile().mkdirs(); + try { + targetFile.createNewFile(); + } catch (IOException e) { + return; } + Shell.Async.su(new Shell.Async.Callback() { + @Override + public void onTaskResult(@Nullable List out, @Nullable List err) { + SnackbarMaker.make(txtLog, targetFile.getPath(), Snackbar.LENGTH_SHORT).show(); + } - void save() { - exec(2); - } + @Override + public void onTaskError(@NonNull Throwable throwable) {} + }, "cat " + Const.MAGISK_LOG + " > " + targetFile); + } + + public void clearLogs() { + Shell.Async.su("echo -n > " + Const.MAGISK_LOG); + SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show(); } }