|
|
@@ -0,0 +1,101 @@
|
|
|
+package com.izouma.booster.adapter;
|
|
|
+
|
|
|
+import android.content.pm.PackageInfo;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.graphics.drawable.Drawable;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.loader.content.AsyncTaskLoader;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+
|
|
|
+import com.izouma.booster.data.AppInfo;
|
|
|
+import com.izouma.booster.databinding.ItemAppUninstallBinding;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+public class UninstallAdapter extends RecyclerView.Adapter<UninstallAdapter.UninstallHolder> {
|
|
|
+ private List<AppInfo> appInfoList;
|
|
|
+ private OnCheckedChangeListener onCheckedChangeListener;
|
|
|
+
|
|
|
+ public UninstallAdapter(List<AppInfo> appInfoList) {
|
|
|
+ this.appInfoList = appInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
|
|
|
+ this.onCheckedChangeListener = onCheckedChangeListener;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getChecked() {
|
|
|
+ List<String> checked = new ArrayList<>();
|
|
|
+ for (AppInfo info : appInfoList) {
|
|
|
+ if (info.isChecked()) {
|
|
|
+ checked.add(info.getPackageName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return checked;
|
|
|
+ }
|
|
|
+
|
|
|
+ @NonNull
|
|
|
+ @Override
|
|
|
+ public UninstallHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
+ return new UninstallHolder(ItemAppUninstallBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onBindViewHolder(@NonNull UninstallHolder holder, int position) {
|
|
|
+ AppInfo appInfo = appInfoList.get(position);
|
|
|
+ holder.binding.tvName.setText(appInfo.getLabel());
|
|
|
+ holder.binding.tvDate.setText(appInfo.getInstallDate());
|
|
|
+ holder.binding.checkbox.setChecked(appInfo.isChecked());
|
|
|
+ try {
|
|
|
+ if (appInfo.getIcon() == null) {
|
|
|
+ appInfo.setIcon(holder.binding.getRoot().getContext().getPackageManager().getApplicationIcon(appInfo.getPackageName()));
|
|
|
+ }
|
|
|
+ holder.binding.icon.setImageDrawable(appInfo.getIcon());
|
|
|
+ } catch (PackageManager.NameNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ holder.binding.checkbox.setOnClickListener(v -> {
|
|
|
+ appInfo.setChecked(holder.binding.checkbox.isChecked());
|
|
|
+ if (onCheckedChangeListener != null) {
|
|
|
+ onCheckedChangeListener.onCheckedChange(getChecked());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ holder.itemView.setOnClickListener(v -> {
|
|
|
+ holder.binding.checkbox.setChecked(!holder.binding.checkbox.isChecked());
|
|
|
+ appInfo.setChecked(holder.binding.checkbox.isChecked());
|
|
|
+ if (onCheckedChangeListener != null) {
|
|
|
+ onCheckedChangeListener.onCheckedChange(getChecked());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int getItemCount() {
|
|
|
+ return appInfoList.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class UninstallHolder extends RecyclerView.ViewHolder {
|
|
|
+ private ItemAppUninstallBinding binding;
|
|
|
+
|
|
|
+ public UninstallHolder(ItemAppUninstallBinding binding) {
|
|
|
+ super(binding.getRoot());
|
|
|
+ this.binding = binding;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ItemAppUninstallBinding getBinding() {
|
|
|
+ return binding;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public interface OnCheckedChangeListener {
|
|
|
+ void onCheckedChange(List<String> checked);
|
|
|
+ }
|
|
|
+}
|