Magisk/app/src/main/java/com/topjohnwu/magisk/ReposAdapter.java
d8ahazard b3ba79a3ba Add Swipe-to-refresh for Modules, Code Cleanup, Stylizations
Moar updates.  Modules now swipe to refresh and indicate properly-ish.
Cleanup minor code stuff.  Colorize icons to match, set global color
variable so it can be changed...
2016-09-12 23:05:04 -05:00

283 lines
11 KiB
Java

package com.topjohnwu.magisk;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
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.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebWindow;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder> {
private ReposFragment reposFragment;
private final List<Repo> mList;
List<Boolean> mExpandedList;
private View viewMain;
private Context context;
private boolean mIsInstalled, mCanUpdate;
private Repo repo;
private ViewHolder mHolder;
public ReposAdapter(ReposFragment reposFragment, List<Repo> list) {
this.reposFragment = reposFragment;
this.mList = list;
Log.d("Magisk", "ReposAdapter: I am alive. I have a list " + list.size());
mExpandedList = new ArrayList<>(mList.size());
for (int i = 0; i < mList.size(); i++) {
mExpandedList.add(false);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
viewMain = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false);
ButterKnife.bind(this, viewMain);
context = parent.getContext();
return new ViewHolder(viewMain);
}
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case R.id.force_reload:
// listModulesDownload.clear();
// new Utils.LoadModules(getActivity(), true).execute();
// break;
// }
//
// return super.onOptionsItemSelected(item);
// }
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
repo = mList.get(position);
mHolder = holder;
mExpandedList = new ArrayList<>(mList.size());
for (int i = 0; i < mList.size(); i++) {
mExpandedList.add(false);
}
SetupViewElements();
}
private void SetupViewElements() {
int mPosition = mHolder.getAdapterPosition();
String titleString;
if (repo.getId() != null) {
if (repo.isCacheModule()) {
titleString = "[Cache] " + repo.getName();
} else {
titleString = repo.getName();
}
mHolder.title.setText(titleString);
mHolder.versionName.setText(repo.getmVersion());
mHolder.description.setText(repo.getDescription());
String authorString = this.context.getResources().getString(R.string.author) + " " + repo.getmAuthor();
mHolder.author.setText(authorString);
mHolder.installedStatus.setText(repo.isInstalled() ? this.context.getResources().getString(R.string.module_installed) : this.context.getResources().getString(R.string.module_not_installed));
if (mExpandedList.get(mPosition)) {
mHolder.expandLayout.setVisibility(View.VISIBLE);
} else {
mHolder.expandLayout.setVisibility(View.GONE);
}
if (repo.isInstalled()) {
mHolder.installedStatus.setTextColor(Color.parseColor("#14AD00"));
mHolder.updateStatus.setText(repo.canUpdate() ? this.context.getResources().getString(R.string.module_update_available) : this.context.getResources().getString(R.string.module_up_to_date));
}
Log.d("Magisk", "ReposAdapter: Setting up info " + repo.getId() + " and " + repo.getDescription() + " and " + repo.getmVersion());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
mCanUpdate = prefs.getBoolean("repo-isInstalled_" + repo.getId(), false);
View.OnClickListener oCl = view -> {
Log.d("Magisk", "Onlick captured, view is " + view.getId());
if (view.getId() == mHolder.updateImage.getId()) {
if (!mIsInstalled | mCanUpdate) {
Utils.DownloadReceiver receiver = new Utils.DownloadReceiver() {
@Override
public void task(File file) {
Log.d("Magisk", "Task firing");
new Utils.FlashZIP(context, repo.getId(), file.toString()).execute();
}
};
String filename = repo.getId().replace(" ", "") + ".zip";
Utils.downloadAndReceive(context, receiver, repo.getmZipUrl(), filename);
} else {
Toast.makeText(context, repo.getId() + " is already installed.", Toast.LENGTH_SHORT).show();
}
}
if (view.getId() == mHolder.changeLog.getId()) {
new WebWindow("Changelog", repo.getmLogText(), this.context);
}
if (view.getId() == mHolder.authorLink.getId()) {
new WebWindow("Donate", repo.getmDonateUrl(), this.context);
}
if (view.getId() == mHolder.supportLink.getId()) {
new WebWindow("Support", repo.getmSupportUrl(), this.context);
}
};
mHolder.changeLog.setOnClickListener(oCl);
mHolder.updateImage.setOnClickListener(oCl);
mHolder.authorLink.setOnClickListener(oCl);
mHolder.supportLink.setOnClickListener(oCl);
if (prefs.contains("repo-isInstalled_" + repo.getId())) {
mIsInstalled = prefs.getBoolean("repo-isInstalled_" + repo.getId(), false);
}
}
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.title)
TextView title;
@BindView(R.id.version_name)
TextView versionName;
@BindView(R.id.description)
TextView description;
@BindView(R.id.author)
TextView author;
@BindView(R.id.installedStatus)
TextView installedStatus;
@BindView(R.id.updateStatus)
TextView updateStatus;
@BindView(R.id.expand_layout)
LinearLayout expandLayout;
@BindView(R.id.update)
ImageView updateImage;
@BindView(R.id.installed)
ImageView installedImage;
@BindView(R.id.changeLog)
ImageView changeLog;
@BindView(R.id.authorLink)
ImageView authorLink;
@BindView(R.id.supportLink)
ImageView supportLink;
private ValueAnimator mAnimator;
private ObjectAnimator animY2;
private ViewHolder holder;
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);
holder = this;
this.expandLayout.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
holder.expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
holder.expandLayout.setVisibility(View.GONE);
holder.expandLayout.measure(widthSpec, heightSpec);
final int holderHeight = holder.expandLayout.getMeasuredHeight();
mAnimator = slideAnimator(0, holderHeight);
animY2 = ObjectAnimator.ofFloat(holder.updateImage, "translationY", holderHeight / 2);
return true;
}
});
viewMain.setOnClickListener(view -> {
int position = getAdapterPosition();
if (mExpandedList.get(position)) {
collapse(holder.expandLayout);
} else {
expand(holder.expandLayout);
}
mExpandedList.set(position, !mExpandedList.get(position));
});
}
private void expand(View view) {
view.setVisibility(View.VISIBLE);
mAnimator.start();
animY2.start();
}
private void collapse(View view) {
int finalHeight = view.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
mAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationEnd(Animator animator) {
view.setVisibility(View.GONE);
}
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
mAnimator.start();
animY2.reverse();
}
private ValueAnimator slideAnimator(int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(valueAnimator -> {
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = expandLayout
.getLayoutParams();
layoutParams.height = value;
expandLayout.setLayoutParams(layoutParams);
});
return animator;
}
}
}