2016-11-08 00:09:08 +08:00
|
|
|
package com.topjohnwu.magisk.adapters;
|
2016-09-11 13:36:58 -05:00
|
|
|
|
|
|
|
import android.animation.Animator;
|
2016-09-11 22:44:24 -05:00
|
|
|
import android.animation.ObjectAnimator;
|
2016-09-11 13:36:58 -05:00
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
import android.content.Context;
|
2016-09-29 23:24:31 +08:00
|
|
|
import android.content.Intent;
|
2016-09-27 15:51:38 +08:00
|
|
|
import android.net.Uri;
|
2016-09-11 13:36:58 -05:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-01-09 20:08:37 +01:00
|
|
|
import android.text.TextUtils;
|
2016-09-11 13:36:58 -05:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.ViewTreeObserver;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-11-08 00:09:08 +08:00
|
|
|
import com.topjohnwu.magisk.R;
|
2016-09-11 13:36:58 -05:00
|
|
|
import com.topjohnwu.magisk.module.Repo;
|
2016-11-23 22:38:15 +08:00
|
|
|
import com.topjohnwu.magisk.receivers.RepoDlReceiver;
|
2016-09-11 13:36:58 -05:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
import com.topjohnwu.magisk.utils.WebWindow;
|
|
|
|
|
2017-01-06 15:33:31 +08:00
|
|
|
import java.util.HashSet;
|
2016-09-11 13:36:58 -05:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
|
|
|
|
public class ReposAdapter extends RecyclerView.Adapter<ReposAdapter.ViewHolder> {
|
|
|
|
|
2016-10-17 10:11:26 +08:00
|
|
|
private List<Repo> mUpdateRepos, mInstalledRepos, mOthersRepos;
|
2017-01-06 15:33:31 +08:00
|
|
|
private HashSet<Repo> expandList = new HashSet<>();
|
|
|
|
|
2016-10-17 10:11:26 +08:00
|
|
|
public ReposAdapter(List<Repo> update, List<Repo> installed, List<Repo> others) {
|
|
|
|
mUpdateRepos = update;
|
|
|
|
mInstalledRepos = installed;
|
|
|
|
mOthersRepos = others;
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
2017-01-06 15:33:31 +08:00
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false);
|
|
|
|
ButterKnife.bind(this, v);
|
|
|
|
return new ViewHolder(v);
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(final ViewHolder holder, int position) {
|
2017-01-09 20:26:11 +01:00
|
|
|
Context context = holder.itemView.getContext();
|
2016-10-17 10:11:26 +08:00
|
|
|
final Repo repo;
|
|
|
|
if (position >= mUpdateRepos.size()) {
|
|
|
|
position -= mUpdateRepos.size();
|
|
|
|
if (position >= mInstalledRepos.size()) {
|
|
|
|
position -= mInstalledRepos.size();
|
|
|
|
repo = mOthersRepos.get(position);
|
|
|
|
} else {
|
|
|
|
repo = mInstalledRepos.get(position);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
repo = mUpdateRepos.get(position);
|
|
|
|
}
|
2016-11-09 00:46:26 +08:00
|
|
|
holder.title.setText(repo.getName());
|
2016-09-28 14:50:26 +08:00
|
|
|
String author = repo.getAuthor();
|
|
|
|
String versionName = repo.getVersion();
|
|
|
|
String description = repo.getDescription();
|
|
|
|
if (versionName != null) {
|
|
|
|
holder.versionName.setText(versionName);
|
|
|
|
}
|
|
|
|
if (author != null) {
|
2017-01-09 20:26:11 +01:00
|
|
|
holder.author.setText(context.getString(R.string.author, author));
|
2016-09-28 14:50:26 +08:00
|
|
|
}
|
|
|
|
if (description != null) {
|
|
|
|
holder.description.setText(description);
|
|
|
|
}
|
2016-09-11 13:36:58 -05:00
|
|
|
|
2017-01-06 15:33:31 +08:00
|
|
|
holder.setExpanded(expandList.contains(repo));
|
|
|
|
|
|
|
|
holder.itemView.setOnClickListener(view -> {
|
|
|
|
if (holder.mExpanded) {
|
|
|
|
holder.collapse();
|
|
|
|
expandList.remove(repo);
|
|
|
|
} else {
|
|
|
|
holder.expand();
|
|
|
|
expandList.add(repo);
|
2016-09-28 14:50:26 +08:00
|
|
|
}
|
2017-01-06 15:33:31 +08:00
|
|
|
});
|
|
|
|
holder.changeLog.setOnClickListener(view -> {
|
2017-01-09 20:08:37 +01:00
|
|
|
if (!TextUtils.isEmpty(repo.getLogUrl())) {
|
2017-01-09 20:26:11 +01:00
|
|
|
new WebWindow(context.getString(R.string.changelog), repo.getLogUrl(), context);
|
2016-09-28 14:50:26 +08:00
|
|
|
}
|
2017-01-06 15:33:31 +08:00
|
|
|
});
|
|
|
|
holder.updateImage.setOnClickListener(view -> {
|
|
|
|
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
|
2017-01-09 20:26:11 +01:00
|
|
|
Utils.getAlertDialogBuilder(context)
|
|
|
|
.setTitle(context.getString(R.string.repo_install_title, repo.getName()))
|
|
|
|
.setMessage(context.getString(R.string.repo_install_msg, filename))
|
2017-01-06 15:33:31 +08:00
|
|
|
.setCancelable(true)
|
|
|
|
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive(
|
2017-01-09 20:26:11 +01:00
|
|
|
context,
|
2017-01-06 15:33:31 +08:00
|
|
|
new RepoDlReceiver(),
|
|
|
|
repo.getZipUrl(),
|
|
|
|
Utils.getLegalFilename(filename)))
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.show();
|
|
|
|
});
|
|
|
|
holder.authorLink.setOnClickListener(view -> {
|
2017-01-09 20:08:37 +01:00
|
|
|
if (!TextUtils.isEmpty(repo.getDonateUrl())) {
|
2017-01-09 20:26:11 +01:00
|
|
|
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getDonateUrl())));
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
2017-01-06 15:33:31 +08:00
|
|
|
});
|
|
|
|
holder.supportLink.setOnClickListener(view -> {
|
2017-01-09 20:08:37 +01:00
|
|
|
if (!TextUtils.isEmpty(repo.getSupportUrl())) {
|
2017-01-09 20:26:11 +01:00
|
|
|
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getSupportUrl())));
|
2016-09-28 14:50:26 +08:00
|
|
|
}
|
2017-01-06 15:33:31 +08:00
|
|
|
});
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
2016-10-17 10:11:26 +08:00
|
|
|
return mUpdateRepos.size() + mInstalledRepos.size() + mOthersRepos.size();
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-09 20:26:11 +01:00
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
2016-09-11 13:36:58 -05:00
|
|
|
|
2016-09-28 14:50:26 +08:00
|
|
|
@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.expand_layout) LinearLayout expandLayout;
|
|
|
|
@BindView(R.id.update) ImageView updateImage;
|
|
|
|
@BindView(R.id.changeLog) ImageView changeLog;
|
|
|
|
@BindView(R.id.authorLink) ImageView authorLink;
|
|
|
|
@BindView(R.id.supportLink) ImageView supportLink;
|
|
|
|
|
2016-09-11 13:36:58 -05:00
|
|
|
private ValueAnimator mAnimator;
|
2016-09-12 12:03:02 -05:00
|
|
|
private ObjectAnimator animY2;
|
2017-01-06 15:33:31 +08:00
|
|
|
private boolean mExpanded = false;
|
2017-01-09 20:26:11 +01:00
|
|
|
private static int expandHeight = 0;
|
2016-09-29 02:05:53 +08:00
|
|
|
|
2017-01-09 20:26:11 +01:00
|
|
|
ViewHolder(View itemView) {
|
2016-09-11 13:36:58 -05:00
|
|
|
super(itemView);
|
|
|
|
ButterKnife.bind(this, itemView);
|
2017-01-06 15:33:31 +08:00
|
|
|
expandLayout.getViewTreeObserver().addOnPreDrawListener(
|
2016-09-11 13:36:58 -05:00
|
|
|
new ViewTreeObserver.OnPreDrawListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreDraw() {
|
2017-01-06 15:33:31 +08:00
|
|
|
if (expandHeight == 0) {
|
|
|
|
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
expandLayout.measure(widthSpec, heightSpec);
|
|
|
|
expandHeight = expandLayout.getMeasuredHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
expandLayout.getViewTreeObserver().removeOnPreDrawListener(this);
|
|
|
|
expandLayout.setVisibility(View.GONE);
|
|
|
|
mAnimator = slideAnimator(0, expandHeight);
|
|
|
|
animY2 = ObjectAnimator.ofFloat(updateImage, "translationY", expandHeight / 2);
|
2016-09-11 13:36:58 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2017-01-06 15:33:31 +08:00
|
|
|
}
|
2016-09-11 13:36:58 -05:00
|
|
|
|
2017-01-06 15:33:31 +08:00
|
|
|
private void setExpanded(boolean expanded) {
|
|
|
|
mExpanded = expanded;
|
|
|
|
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
|
|
|
|
layoutParams.height = expanded ? expandHeight : 0;
|
|
|
|
expandLayout.setLayoutParams(layoutParams);
|
|
|
|
expandLayout.setVisibility(expanded ? View.VISIBLE : View.GONE);
|
2017-01-07 03:18:47 +08:00
|
|
|
if (expanded) {
|
|
|
|
updateImage.setTranslationY(expandHeight / 2);
|
|
|
|
} else {
|
|
|
|
updateImage.setTranslationY(0);
|
|
|
|
}
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-06 15:33:31 +08:00
|
|
|
private void expand() {
|
|
|
|
expandLayout.setVisibility(View.VISIBLE);
|
2016-09-11 13:36:58 -05:00
|
|
|
mAnimator.start();
|
2016-09-12 12:03:02 -05:00
|
|
|
animY2.start();
|
2017-01-06 15:33:31 +08:00
|
|
|
mExpanded = true;
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-06 15:33:31 +08:00
|
|
|
private void collapse() {
|
|
|
|
if (!mExpanded) return;
|
|
|
|
int finalHeight = expandLayout.getHeight();
|
2016-09-11 13:36:58 -05:00
|
|
|
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);
|
|
|
|
mAnimator.addListener(new Animator.AnimatorListener() {
|
|
|
|
@Override
|
|
|
|
public void onAnimationEnd(Animator animator) {
|
2017-01-06 15:33:31 +08:00
|
|
|
expandLayout.setVisibility(View.GONE);
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-09-28 14:50:26 +08:00
|
|
|
public void onAnimationStart(Animator animator) {}
|
2016-09-11 13:36:58 -05:00
|
|
|
|
|
|
|
@Override
|
2016-09-28 14:50:26 +08:00
|
|
|
public void onAnimationCancel(Animator animator) {}
|
2016-09-11 13:36:58 -05:00
|
|
|
|
|
|
|
@Override
|
2016-09-28 14:50:26 +08:00
|
|
|
public void onAnimationRepeat(Animator animator) {}
|
2016-09-11 13:36:58 -05:00
|
|
|
});
|
|
|
|
mAnimator.start();
|
2016-09-12 12:03:02 -05:00
|
|
|
animY2.reverse();
|
2017-01-06 15:33:31 +08:00
|
|
|
mExpanded = false;
|
2016-09-11 13:36:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private ValueAnimator slideAnimator(int start, int end) {
|
|
|
|
|
|
|
|
ValueAnimator animator = ValueAnimator.ofInt(start, end);
|
|
|
|
|
|
|
|
animator.addUpdateListener(valueAnimator -> {
|
|
|
|
int value = (Integer) valueAnimator.getAnimatedValue();
|
2017-01-06 15:33:31 +08:00
|
|
|
ViewGroup.LayoutParams layoutParams = expandLayout.getLayoutParams();
|
2016-09-11 13:36:58 -05:00
|
|
|
layoutParams.height = value;
|
|
|
|
expandLayout.setLayoutParams(layoutParams);
|
|
|
|
});
|
|
|
|
return animator;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|