220 lines
8.4 KiB
Java
Raw Normal View History

2016-11-08 00:09:08 +08:00
package com.topjohnwu.magisk.adapters;
import android.animation.Animator;
import android.animation.ObjectAnimator;
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;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
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;
2016-12-28 04:48:40 +08:00
import com.topjohnwu.magisk.MainActivity;
2016-11-08 00:09:08 +08:00
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.module.Repo;
2016-11-23 22:38:15 +08:00
import com.topjohnwu.magisk.receivers.RepoDlReceiver;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebWindow;
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;
2016-09-28 14:50:26 +08:00
private View mView;
2016-12-28 04:48:40 +08:00
private Context mContext;
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;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
2016-09-28 14:50:26 +08:00
mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_repo, parent, false);
ButterKnife.bind(this, mView);
2016-12-28 04:48:40 +08:00
mContext = parent.getContext();
2016-09-29 23:24:31 +08:00
2016-09-28 14:50:26 +08:00
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
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) {
2016-12-28 04:48:40 +08:00
holder.author.setText(mContext.getString(R.string.author, author));
2016-09-28 14:50:26 +08:00
}
if (description != null) {
holder.description.setText(description);
}
2016-09-28 14:50:26 +08:00
View.OnClickListener listener = view -> {
if (view.getId() == holder.updateImage.getId()) {
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
2016-12-28 04:48:40 +08:00
MainActivity.alertBuilder
.setTitle(mContext.getString(R.string.repo_install_title, repo.getName()))
.setMessage(mContext.getString(R.string.repo_install_msg, filename))
2016-09-29 23:24:31 +08:00
.setCancelable(true)
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.dlAndReceive(
2016-12-28 04:48:40 +08:00
mContext,
2016-11-23 22:38:15 +08:00
new RepoDlReceiver(),
2016-09-29 23:24:31 +08:00
repo.getZipUrl(),
Utils.getLegalFilename(filename)))
2016-09-29 23:24:31 +08:00
.setNegativeButton(R.string.no_thanks, null)
.show();
2016-09-28 14:50:26 +08:00
}
if ((view.getId() == holder.changeLog.getId()) && (!repo.getLogUrl().equals(""))) {
2016-12-28 04:48:40 +08:00
new WebWindow(mContext.getString(R.string.changelog), repo.getLogUrl(), mContext);
2016-09-28 14:50:26 +08:00
}
if ((view.getId() == holder.authorLink.getId()) && (!repo.getSupportUrl().equals(""))) {
2016-12-28 04:48:40 +08:00
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getDonateUrl())));
}
2016-09-28 14:50:26 +08:00
if ((view.getId() == holder.supportLink.getId()) && (!repo.getSupportUrl().equals(""))) {
2016-12-28 04:48:40 +08:00
mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(repo.getSupportUrl())));
2016-09-28 14:50:26 +08:00
}
};
2016-09-28 14:50:26 +08:00
holder.changeLog.setOnClickListener(listener);
holder.updateImage.setOnClickListener(listener);
holder.authorLink.setOnClickListener(listener);
holder.supportLink.setOnClickListener(listener);
}
@Override
public int getItemCount() {
2016-10-17 10:11:26 +08:00
return mUpdateRepos.size() + mInstalledRepos.size() + mOthersRepos.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
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.installed) ImageView installedImage;
@BindView(R.id.changeLog) ImageView changeLog;
@BindView(R.id.authorLink) ImageView authorLink;
@BindView(R.id.supportLink) ImageView supportLink;
private ValueAnimator mAnimator;
2016-09-12 12:03:02 -05:00
private ObjectAnimator animY2;
private ViewHolder holder;
private boolean expanded = false;
public ViewHolder(View itemView) {
super(itemView);
2016-12-28 04:48:40 +08:00
WindowManager windowmanager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
ButterKnife.bind(this, itemView);
DisplayMetrics dimension = new DisplayMetrics();
windowmanager.getDefaultDisplay().getMetrics(dimension);
2016-09-12 12:03:02 -05:00
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);
2016-09-12 12:03:02 -05:00
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);
2016-09-12 12:03:02 -05:00
return true;
}
});
2016-09-28 14:50:26 +08:00
mView.setOnClickListener(view -> {
if (expanded) {
2016-09-12 12:03:02 -05:00
collapse(holder.expandLayout);
} else {
2016-09-12 12:03:02 -05:00
expand(holder.expandLayout);
}
expanded = !expanded;
});
}
private void expand(View view) {
view.setVisibility(View.VISIBLE);
mAnimator.start();
2016-09-12 12:03:02 -05:00
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
2016-09-28 14:50:26 +08:00
public void onAnimationStart(Animator animator) {}
@Override
2016-09-28 14:50:26 +08:00
public void onAnimationCancel(Animator animator) {}
@Override
2016-09-28 14:50:26 +08:00
public void onAnimationRepeat(Animator animator) {}
});
mAnimator.start();
2016-09-12 12:03:02 -05:00
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;
}
}
}