|
| 1 | +package com.huntmix.secbutton; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.graphics.drawable.Drawable; |
| 7 | +import android.os.Build; |
| 8 | +import android.util.Log; |
| 9 | +import android.view.LayoutInflater; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.widget.CheckBox; |
| 13 | +import android.widget.ImageView; |
| 14 | +import android.widget.RelativeLayout; |
| 15 | +import android.widget.TextView; |
| 16 | + |
| 17 | +import androidx.annotation.RequiresApi; |
| 18 | +import androidx.recyclerview.widget.RecyclerView; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> { |
| 24 | + ; |
| 25 | + |
| 26 | + public Context justcontext; |
| 27 | + public String result = ""; |
| 28 | + private ArrayList<GetInfo> data; |
| 29 | + List selected = new ArrayList(); |
| 30 | + ; |
| 31 | + |
| 32 | + public Adapter(Context context, ArrayList<GetInfo> list) { |
| 33 | + justcontext = context; |
| 34 | + data = list; |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + public static class ViewHolder extends RecyclerView.ViewHolder { |
| 42 | + |
| 43 | + public TextView nameofapk; |
| 44 | + public TextView nameofpkg; |
| 45 | + public ImageView iconofapp; |
| 46 | + public CheckBox selector; |
| 47 | + public RelativeLayout fullitem; |
| 48 | + |
| 49 | + public ViewHolder(View v) { |
| 50 | + super(v); |
| 51 | + // Get the widgets reference from custom layout |
| 52 | + nameofapk = (TextView) v.findViewById(R.id.nameofapp); |
| 53 | + nameofpkg = (TextView) v.findViewById(R.id.apkpkg); |
| 54 | + iconofapp = (ImageView) v.findViewById(R.id.iconofapp); |
| 55 | + selector = (CheckBox) v.findViewById(R.id.selector); |
| 56 | + fullitem = (RelativeLayout) v.findViewById(R.id.item); |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 63 | + |
| 64 | + View v = LayoutInflater.from(justcontext).inflate(R.layout.apkitem, parent, false); |
| 65 | + ViewHolder vh = new ViewHolder(v); |
| 66 | + return vh; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onBindViewHolder(ViewHolder holder, final int position) { |
| 71 | + |
| 72 | + // Get pkg |
| 73 | + final String packageName = data.get(position).getAppPackage(); |
| 74 | + |
| 75 | + // Get thicon |
| 76 | + Drawable icon = data.get(position).getAppIcon(); |
| 77 | + |
| 78 | + // Get label |
| 79 | + String label = data.get(position).getAppName(); |
| 80 | + |
| 81 | + // Set label |
| 82 | + holder.nameofapk.setText(label); |
| 83 | + |
| 84 | + // Set pkg |
| 85 | + holder.nameofpkg.setText(packageName); |
| 86 | + |
| 87 | + // Set icon |
| 88 | + holder.iconofapp.setImageDrawable(icon); |
| 89 | + |
| 90 | + holder.selector.setChecked(data.get(position).isSelected()); |
| 91 | +//onclick set checkbox state and append pkg to list with writing to db |
| 92 | + holder.fullitem.setOnClickListener(new View.OnClickListener() { |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + @RequiresApi(api = Build.VERSION_CODES.O) |
| 97 | + @Override |
| 98 | + public void onClick(View v) { |
| 99 | + TinyDB tinydb = new TinyDB(justcontext); |
| 100 | + data.get(position).setSelected(!data.get(position).isSelected()); |
| 101 | + Adapter.this.notifyDataSetChanged(); |
| 102 | + String pkg = packageName; |
| 103 | + if (selected.contains(pkg)){ |
| 104 | + selected.remove(pkg); |
| 105 | + }else{ |
| 106 | + selected.add(pkg); |
| 107 | + } |
| 108 | + tinydb.putListString("list", (ArrayList<String>) selected); |
| 109 | + Log.e("pkg", String.valueOf(selected)); |
| 110 | + |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + }); |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public int getItemCount() { |
| 123 | + // Count the installed apps |
| 124 | + return data.size(); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +} |
0 commit comments