Magisk/app/src/main/java/com/topjohnwu/magisk/ModulesAdapter.java

149 lines
5.3 KiB
Java
Raw Normal View History

2016-09-18 14:56:12 +00:00
package com.topjohnwu.magisk;
import android.content.Context;
2016-09-27 14:57:20 +00:00
import android.support.design.widget.Snackbar;
2016-09-18 14:56:12 +00:00
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ModulesAdapter extends RecyclerView.Adapter<ModulesAdapter.ViewHolder> {
private final List<Module> mList;
2016-09-27 14:57:20 +00:00
private View mView;
2016-09-18 14:56:12 +00:00
private Context context;
2016-09-27 14:57:20 +00:00
private Utils.ItemClickListener chboxListener, deleteBtnListener, unDeleteBtnListener;
public ModulesAdapter(List<Module> list) {
mList = list;
chboxListener = (chk, position) -> {
// On Checkbox change listener
CheckBox chbox = (CheckBox) chk;
if (!chbox.isChecked()) {
mList.get(position).createDisableFile();
Snackbar.make(mView, R.string.disable_file_created, Snackbar.LENGTH_SHORT).show();
} else {
mList.get(position).removeDisableFile();
Snackbar.make(mView, R.string.disable_file_removed, Snackbar.LENGTH_SHORT).show();
}
};
deleteBtnListener = (deleteBtn, position) -> {
// On delete button click listener
mList.get(position).createRemoveFile();
Snackbar.make(mView, R.string.remove_file_created, Snackbar.LENGTH_SHORT).show();
};
unDeleteBtnListener = (undeleteBtn, position) -> {
// On undelete button click listener
mList.get(position).deleteRemoveFile();
Snackbar.make(mView, R.string.remove_file_deleted, Snackbar.LENGTH_SHORT).show();
};
2016-09-18 14:56:12 +00:00
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2016-09-27 14:57:20 +00:00
mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_module, parent, false);
2016-09-18 14:56:12 +00:00
context = parent.getContext();
2016-09-27 14:57:20 +00:00
ButterKnife.bind(this, mView);
return new ViewHolder(mView);
2016-09-18 14:56:12 +00:00
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Module module = mList.get(position);
2016-09-27 07:51:38 +00:00
if (module.isCache()) {
2016-09-18 14:56:12 +00:00
holder.title.setText("[Cache] " + module.getName());
2016-09-27 07:51:38 +00:00
} else {
2016-09-18 14:56:12 +00:00
holder.title.setText(module.getName());
}
2016-09-27 07:51:38 +00:00
String author = module.getAuthor();
String versionName = module.getVersion();
String description = module.getDescription();
if (versionName != null) {
holder.versionName.setText(versionName);
}
if (author != null) {
holder.author.setText(context.getString(R.string.author, author));
}
if (description != null) {
holder.description.setText(description);
2016-09-18 14:56:12 +00:00
}
holder.checkBox.setChecked(module.isEnabled());
holder.checkBox.setOnCheckedChangeListener((compoundButton, b) -> chboxListener.onItemClick(compoundButton, holder.getAdapterPosition()));
holder.delete.setOnClickListener(view -> {
if (module.willBeRemoved()) {
unDeleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition());
} else {
deleteBtnListener.onItemClick(holder.delete, holder.getAdapterPosition());
}
updateDeleteButton(holder, module);
});
2016-09-28 19:37:57 +00:00
if (module.isUpdated()) {
holder.notice.setVisibility(View.VISIBLE);
holder.notice.setText(R.string.update_file_created);
holder.delete.setEnabled(false);
} else {
updateDeleteButton(holder, module);
}
2016-09-18 14:56:12 +00:00
}
private void updateDeleteButton(ViewHolder holder, Module module) {
2016-09-28 19:37:57 +00:00
holder.notice.setVisibility(module.willBeRemoved() ? View.VISIBLE : View.GONE);
2016-09-18 14:56:12 +00:00
if (module.willBeRemoved()) {
holder.delete.setImageResource(R.drawable.ic_undelete);
} else {
holder.delete.setImageResource(R.drawable.ic_delete);
}
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
2016-09-27 07:51:38 +00:00
@BindView(R.id.title) TextView title;
@BindView(R.id.version_name) TextView versionName;
@BindView(R.id.description) TextView description;
2016-09-28 19:37:57 +00:00
@BindView(R.id.notice) TextView notice;
2016-09-27 07:51:38 +00:00
@BindView(R.id.checkbox) CheckBox checkBox;
@BindView(R.id.author) TextView author;
@BindView(R.id.delete) ImageView delete;
2016-09-18 14:56:12 +00:00
public ViewHolder(View itemView) {
super(itemView);
WindowManager windowmanager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
ButterKnife.bind(this, itemView);
DisplayMetrics dimension = new DisplayMetrics();
windowmanager.getDefaultDisplay().getMetrics(dimension);
if (!Shell.rootAccess()) {
checkBox.setEnabled(false);
delete.setEnabled(false);
}
}
}
}