Use recyclerview to show Magisk logs

This commit is contained in:
topjohnwu 2018-12-30 22:15:00 +08:00
parent 44b969e0b6
commit bf7d6ddcb2
4 changed files with 101 additions and 54 deletions

View File

@ -0,0 +1,75 @@
package com.topjohnwu.magisk.adapters;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.topjohnwu.magisk.R;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
public class MagiskLogAdapter extends RecyclerView.Adapter<MagiskLogAdapter.ViewHolder> {
private List<String> mList;
private int txtWidth = -1;
public MagiskLogAdapter(List<String> list) {
mList = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_magisk_log, parent, false);
ViewHolder vh = new ViewHolder(v);
if (txtWidth < 0) {
int max = 0;
String maxStr = "";
for (String s : mList) {
int len = s.length();
if (len > max) {
max = len;
maxStr = s;
}
}
vh.txt.setText(maxStr);
vh.txt.measure(0, 0);
txtWidth = vh.txt.getMeasuredWidth();
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) parent.getContext()).getWindowManager()
.getDefaultDisplay().getMetrics(displayMetrics);
if (txtWidth < displayMetrics.widthPixels)
txtWidth = displayMetrics.widthPixels;
}
vh.txt.getLayoutParams().width = txtWidth;
return vh;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.txt.setText(mList.get(position));
}
@Override
public int getItemCount() {
return mList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.txt) TextView txt;
public ViewHolder(@NonNull View v) {
super(v);
new MagiskLogAdapter$ViewHolder_ViewBinding(this, itemView);
}
}
}

View File

@ -2,22 +2,18 @@ package com.topjohnwu.magisk.fragments;
import android.Manifest;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import com.google.android.material.snackbar.Snackbar;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.adapters.MagiskLogAdapter;
import com.topjohnwu.magisk.components.BaseFragment;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.superuser.Shell;
@ -27,14 +23,12 @@ import java.io.IOException;
import java.util.Calendar;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
public class MagiskLogFragment extends BaseFragment {
@BindView(R.id.txtLog) TextView txtLog;
@BindView(R.id.svLog) ScrollView svLog;
@BindView(R.id.hsvLog) HorizontalScrollView hsvLog;
@BindView(R.id.progressBar) ProgressBar progressBar;
@BindView(R.id.recyclerView) RecyclerView rv;
@Nullable
@Override
@ -42,7 +36,6 @@ public class MagiskLogFragment extends BaseFragment {
View view = inflater.inflate(R.layout.fragment_magisk_log, container, false);
unbinder = new MagiskLogFragment_ViewBinding(this, view);
setHasOptionsMenu(true);
txtLog.setTextIsSelectable(true);
return view;
}
@ -82,13 +75,7 @@ public class MagiskLogFragment extends BaseFragment {
private void readLogs() {
Shell.su("cat " + Const.MAGISK_LOG + " | tail -n 5000").submit(result -> {
progressBar.setVisibility(View.GONE);
if (result.getOut().isEmpty())
txtLog.setText(R.string.log_is_empty);
else
txtLog.setText(TextUtils.join("\n", result.getOut()));
svLog.postDelayed(() -> svLog.fullScroll(ScrollView.FOCUS_DOWN), 100);
hsvLog.postDelayed(() -> hsvLog.fullScroll(ScrollView.FOCUS_LEFT), 100);
rv.setAdapter(new MagiskLogAdapter(result.getOut()));
});
}
@ -107,12 +94,11 @@ public class MagiskLogFragment extends BaseFragment {
}
Shell.su("cat " + Const.MAGISK_LOG + " > " + logFile)
.submit(result ->
SnackbarMaker.make(txtLog, logFile.getPath(), Snackbar.LENGTH_SHORT).show());
SnackbarMaker.make(rv, logFile.getPath(), Snackbar.LENGTH_SHORT).show());
}
private void clearLogs() {
Shell.su("echo -n > " + Const.MAGISK_LOG).submit();
txtLog.setText(R.string.log_is_empty);
SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
SnackbarMaker.make(rv, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show();
}
}

View File

@ -1,37 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/svLog"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/hsvLog"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingStart="8dp"
android:paddingEnd="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ScrollViewSize">
<TextView
android:id="@+id/txtLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:padding="8dp"
android:textIsSelectable="true"
android:textSize="10sp" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
</HorizontalScrollView>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:textSize="10sp" />