2017-01-28 01:10:50 +08:00
|
|
|
package com.topjohnwu.magisk.adapters;
|
|
|
|
|
2019-01-24 15:15:31 -05:00
|
|
|
import android.content.Context;
|
2017-01-28 01:10:50 +08:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.animation.Animation;
|
|
|
|
import android.view.animation.RotateAnimation;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2018-12-13 04:35:50 -05:00
|
|
|
import com.topjohnwu.core.container.SuLogEntry;
|
|
|
|
import com.topjohnwu.core.database.MagiskDB;
|
2017-01-28 01:10:50 +08:00
|
|
|
import com.topjohnwu.magisk.R;
|
2017-08-29 04:10:04 +08:00
|
|
|
import com.topjohnwu.magisk.components.ExpandableView;
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
import java.util.Collections;
|
2017-01-28 01:10:50 +08:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
2017-01-29 20:50:58 +01:00
|
|
|
import java.util.Set;
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2018-09-10 02:27:45 -04:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2018-10-16 21:00:01 -04:00
|
|
|
import butterknife.BindView;
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
public class SuLogAdapter extends SectionedAdapter<SuLogAdapter.SectionHolder, SuLogAdapter.LogViewHolder> {
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2018-10-27 15:08:11 -04:00
|
|
|
private List<List<SuLogEntry>> logEntries;
|
2017-07-24 00:34:34 +08:00
|
|
|
private Set<Integer> itemExpanded, sectionExpanded;
|
2018-10-27 15:08:11 -04:00
|
|
|
private MagiskDB suDB;
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2018-10-27 15:08:11 -04:00
|
|
|
public SuLogAdapter(MagiskDB db) {
|
2017-07-24 00:34:34 +08:00
|
|
|
suDB = db;
|
2018-10-27 15:08:11 -04:00
|
|
|
logEntries = Collections.emptyList();
|
2017-07-24 00:34:34 +08:00
|
|
|
sectionExpanded = new HashSet<>();
|
|
|
|
itemExpanded = new HashSet<>();
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public int getSectionCount() {
|
2018-10-27 15:08:11 -04:00
|
|
|
return logEntries.size();
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public int getItemCount(int section) {
|
2018-10-27 15:08:11 -04:00
|
|
|
return sectionExpanded.contains(section) ? logEntries.get(section).size() : 0;
|
2017-07-24 00:34:34 +08:00
|
|
|
}
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public SectionHolder onCreateSectionViewHolder(ViewGroup parent) {
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_sulog_group, parent, false);
|
|
|
|
return new SectionHolder(v);
|
|
|
|
}
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public LogViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_sulog, parent, false);
|
|
|
|
return new LogViewHolder(v);
|
|
|
|
}
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public void onBindSectionViewHolder(SectionHolder holder, int section) {
|
2018-10-27 15:08:11 -04:00
|
|
|
SuLogEntry entry = logEntries.get(section).get(0);
|
2017-07-24 00:34:34 +08:00
|
|
|
holder.arrow.setRotation(sectionExpanded.contains(section) ? 180 : 0);
|
|
|
|
holder.itemView.setOnClickListener(v -> {
|
|
|
|
RotateAnimation rotate;
|
|
|
|
if (sectionExpanded.contains(section)) {
|
|
|
|
holder.arrow.setRotation(0);
|
|
|
|
rotate = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
|
|
|
|
sectionExpanded.remove(section);
|
2018-10-27 15:08:11 -04:00
|
|
|
notifyItemRangeRemoved(getItemPosition(section, 0), logEntries.get(section).size());
|
2017-07-24 00:34:34 +08:00
|
|
|
} else {
|
|
|
|
rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
|
|
|
|
sectionExpanded.add(section);
|
2018-10-27 15:08:11 -04:00
|
|
|
notifyItemRangeInserted(getItemPosition(section, 0), logEntries.get(section).size());
|
2017-07-24 00:34:34 +08:00
|
|
|
}
|
|
|
|
rotate.setDuration(300);
|
|
|
|
rotate.setFillAfter(true);
|
|
|
|
holder.arrow.setAnimation(rotate);
|
|
|
|
});
|
|
|
|
holder.date.setText(entry.getDateString());
|
|
|
|
}
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
|
|
|
public void onBindItemViewHolder(LogViewHolder holder, int section, int position) {
|
2018-10-27 15:08:11 -04:00
|
|
|
SuLogEntry entry = logEntries.get(section).get(position);
|
|
|
|
int realIdx = getItemPosition(section, position);
|
|
|
|
holder.setExpanded(itemExpanded.contains(realIdx));
|
2017-07-24 00:34:34 +08:00
|
|
|
holder.itemView.setOnClickListener(view -> {
|
2017-08-29 04:10:04 +08:00
|
|
|
if (holder.isExpanded()) {
|
2017-07-24 00:34:34 +08:00
|
|
|
holder.collapse();
|
2018-10-27 15:08:11 -04:00
|
|
|
itemExpanded.remove(realIdx);
|
2017-07-24 00:34:34 +08:00
|
|
|
} else {
|
2017-02-21 03:30:37 +08:00
|
|
|
holder.expand();
|
2018-10-27 15:08:11 -04:00
|
|
|
itemExpanded.add(realIdx);
|
2017-02-21 03:30:37 +08:00
|
|
|
}
|
2017-07-24 00:34:34 +08:00
|
|
|
});
|
2019-01-24 15:15:31 -05:00
|
|
|
Context context = holder.itemView.getContext();
|
2017-07-24 00:34:34 +08:00
|
|
|
holder.appName.setText(entry.appName);
|
|
|
|
holder.action.setText(entry.action ? R.string.grant : R.string.deny);
|
2019-01-24 15:15:31 -05:00
|
|
|
holder.pid.setText(context.getString(R.string.pid, entry.fromPid));
|
|
|
|
holder.uid.setText(context.getString(R.string.target_uid, entry.toUid));
|
|
|
|
holder.command.setText(context.getString(R.string.command, entry.command));
|
2017-07-24 00:34:34 +08:00
|
|
|
holder.time.setText(entry.getTimeString());
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
public void notifyDBChanged() {
|
2018-10-27 15:08:11 -04:00
|
|
|
logEntries = suDB.getLogs();
|
2017-07-24 00:34:34 +08:00
|
|
|
itemExpanded.clear();
|
|
|
|
sectionExpanded.clear();
|
|
|
|
sectionExpanded.add(0);
|
|
|
|
notifyDataSetChanged();
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
|
2018-10-16 21:00:01 -04:00
|
|
|
static class SectionHolder extends RecyclerView.ViewHolder {
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2018-10-16 21:00:01 -04:00
|
|
|
@BindView(R.id.date) TextView date;
|
|
|
|
@BindView(R.id.arrow) ImageView arrow;
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
SectionHolder(View itemView) {
|
2017-01-28 01:10:50 +08:00
|
|
|
super(itemView);
|
2018-10-16 21:00:01 -04:00
|
|
|
new SuLogAdapter$SectionHolder_ViewBinding(this, itemView);
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 21:00:01 -04:00
|
|
|
static class LogViewHolder extends RecyclerView.ViewHolder implements ExpandableView {
|
2017-06-20 17:55:33 +08:00
|
|
|
|
2018-10-16 21:00:01 -04:00
|
|
|
@BindView(R.id.app_name) TextView appName;
|
|
|
|
@BindView(R.id.action) TextView action;
|
|
|
|
@BindView(R.id.time) TextView time;
|
2019-01-24 15:15:31 -05:00
|
|
|
@BindView(R.id.pid) TextView pid;
|
|
|
|
@BindView(R.id.uid) TextView uid;
|
|
|
|
@BindView(R.id.cmd) TextView command;
|
2018-10-16 21:00:01 -04:00
|
|
|
@BindView(R.id.expand_layout) ViewGroup expandLayout;
|
2017-08-29 04:10:04 +08:00
|
|
|
|
|
|
|
private Container container = new Container();
|
2017-01-28 01:10:50 +08:00
|
|
|
|
2017-06-20 17:55:33 +08:00
|
|
|
LogViewHolder(View itemView) {
|
2017-01-28 01:10:50 +08:00
|
|
|
super(itemView);
|
2018-10-16 21:00:01 -04:00
|
|
|
new SuLogAdapter$LogViewHolder_ViewBinding(this, itemView);
|
2017-08-29 04:10:04 +08:00
|
|
|
container.expandLayout = expandLayout;
|
|
|
|
setupExpandable();
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
@Override
|
2017-08-29 04:10:04 +08:00
|
|
|
public Container getContainer() {
|
|
|
|
return container;
|
2017-01-28 01:10:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|