2016-11-08 00:09:08 +08:00
|
|
|
package com.topjohnwu.magisk.adapters;
|
2016-09-11 13:36:58 -05:00
|
|
|
|
|
|
|
import android.content.Context;
|
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.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2016-11-08 00:09:08 +08:00
|
|
|
import com.topjohnwu.magisk.R;
|
2017-02-15 05:24:02 +08:00
|
|
|
import com.topjohnwu.magisk.asyncs.ProcessRepoZip;
|
|
|
|
import com.topjohnwu.magisk.components.AlertDialogBuilder;
|
2017-02-16 05:45:31 +08:00
|
|
|
import com.topjohnwu.magisk.components.MarkDownWindow;
|
2016-09-11 13:36:58 -05:00
|
|
|
import com.topjohnwu.magisk.module.Repo;
|
2017-02-15 05:24:02 +08:00
|
|
|
import com.topjohnwu.magisk.receivers.DownloadReceiver;
|
2016-09-11 13:36:58 -05:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
|
|
|
|
|
|
|
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-02-16 05:45:31 +08:00
|
|
|
private Context mContext;
|
2017-01-06 15:33:31 +08:00
|
|
|
|
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-02-16 05:45:31 +08:00
|
|
|
mContext = parent.getContext();
|
|
|
|
View v = LayoutInflater.from(mContext).inflate(R.layout.list_item_repo, parent, false);
|
2017-01-06 15:33:31 +08:00
|
|
|
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:34:17 +01:00
|
|
|
Repo repo = getItem(position);
|
|
|
|
|
2016-11-09 00:46:26 +08:00
|
|
|
holder.title.setText(repo.getName());
|
2017-01-09 20:38:42 +01:00
|
|
|
holder.versionName.setText(repo.getVersion());
|
2016-09-28 14:50:26 +08:00
|
|
|
String author = repo.getAuthor();
|
2017-02-16 05:45:31 +08:00
|
|
|
holder.author.setText(TextUtils.isEmpty(author) ? null : mContext.getString(R.string.author, author));
|
2017-01-09 20:38:42 +01:00
|
|
|
holder.description.setText(repo.getDescription());
|
2016-09-11 13:36:58 -05:00
|
|
|
|
2017-02-16 05:45:31 +08:00
|
|
|
holder.infoLayout.setOnClickListener(v -> new MarkDownWindow(null, repo.getDetailUrl(), mContext));
|
2017-01-06 15:33:31 +08:00
|
|
|
|
2017-02-16 05:45:31 +08:00
|
|
|
holder.downloadImage.setOnClickListener(v -> {
|
2017-01-06 15:33:31 +08:00
|
|
|
String filename = repo.getName() + "-" + repo.getVersion() + ".zip";
|
2017-02-16 05:45:31 +08:00
|
|
|
new AlertDialogBuilder(mContext)
|
|
|
|
.setTitle(mContext.getString(R.string.repo_install_title, repo.getName()))
|
|
|
|
.setMessage(mContext.getString(R.string.repo_install_msg, filename))
|
2017-01-06 15:33:31 +08:00
|
|
|
.setCancelable(true)
|
2017-02-19 08:05:16 +08:00
|
|
|
.setPositiveButton(R.string.install, (d, i) -> Utils.dlAndReceive(
|
2017-02-16 05:45:31 +08:00
|
|
|
mContext,
|
2017-02-15 05:24:02 +08:00
|
|
|
new DownloadReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onDownloadDone(Uri uri) {
|
2017-02-16 17:50:36 +08:00
|
|
|
new ProcessRepoZip(activity, uri, true).exec();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
repo.getZipUrl(),
|
|
|
|
Utils.getLegalFilename(filename)))
|
|
|
|
.setNeutralButton(R.string.download, (d, i) -> Utils.dlAndReceive(
|
|
|
|
mContext,
|
|
|
|
new DownloadReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onDownloadDone(Uri uri) {
|
|
|
|
new ProcessRepoZip(activity, uri, false).exec();
|
2017-02-15 05:24:02 +08:00
|
|
|
}
|
|
|
|
},
|
2017-01-06 15:33:31 +08:00
|
|
|
repo.getZipUrl(),
|
|
|
|
Utils.getLegalFilename(filename)))
|
|
|
|
.setNegativeButton(R.string.no_thanks, null)
|
|
|
|
.show();
|
|
|
|
});
|
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:34:17 +01:00
|
|
|
private Repo getItem(int position) {
|
|
|
|
if (position >= mUpdateRepos.size()) {
|
|
|
|
position -= mUpdateRepos.size();
|
|
|
|
if (position >= mInstalledRepos.size()) {
|
|
|
|
position -= mInstalledRepos.size();
|
|
|
|
return mOthersRepos.get(position);
|
|
|
|
} else {
|
|
|
|
return mInstalledRepos.get(position);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return mUpdateRepos.get(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2017-02-16 05:45:31 +08:00
|
|
|
@BindView(R.id.info_layout) LinearLayout infoLayout;
|
|
|
|
@BindView(R.id.download) ImageView downloadImage;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|