package com.example.modifier.adapter; import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.PackageManager; import android.util.Log; import android.view.LayoutInflater; import android.view.ViewGroup; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.example.modifier.model.Backup; import com.example.modifier.Global; import com.example.modifier.Utils; import com.example.modifier.databinding.ItemBackupBinding; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import java.io.File; import java.text.SimpleDateFormat; import java.util.List; public class BackupAdapter extends RecyclerView.Adapter { private Context context; private List backups; public BackupAdapter(Context context, List backups) { this.context = context; this.backups = backups; } @NonNull @Override public BackupAdapter.BackupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { ItemBackupBinding binding = ItemBackupBinding.inflate(LayoutInflater.from(context), parent, false); return new BackupViewHolder(binding); } @SuppressLint("DefaultLocale") @Override public void onBindViewHolder(@NonNull BackupAdapter.BackupViewHolder holder, int position) { Backup backup = backups.get(position); holder.binding.tvNumber.setText(backup.getConfig().getNumber()); holder.binding.tvTime.setText(SimpleDateFormat.getDateTimeInstance().format(backup.getDate())); holder.binding.tvInfo.setText(String.format("MCC: %s, MNC: %s, Country: %s", backup.getConfig().getMcc(), backup.getConfig().getMcc(), backup.getConfig().getMcc())); holder.binding.btnRestore.setOnClickListener(v -> { new MaterialAlertDialogBuilder(context) .setTitle("Restore backup") .setMessage("Are you sure you want to restore this backup?") .setPositiveButton("Yes", (dialog, which) -> { new Thread(() -> { try { PackageManager packageManager = context.getPackageManager(); int uid = packageManager.getApplicationInfo("com.google.android.apps.messaging", 0).uid; int gmsUid = packageManager.getApplicationInfo("com.google.android.gms", 0).uid; Global.suspend(false, true, true); Global.save(backup.getConfig(), false); Utils.runAsRoot("pm clear com.google.android.apps.messaging", "cp -rf " + backup.getPath() + "/data/* /data/data/com.google.android.apps.messaging/", "chown -R " + uid + ":" + uid + " /data/data/com.google.android.apps.messaging/*", "cp -rf " + backup.getPath() + "/gmsData/* /data/data/com.google.android.gms/", "chown -R " + gmsUid + ":" + gmsUid + " /data/data/com.google.android.gms/*"); Global.unsuspend(false, true, true); } catch (Exception e) { e.printStackTrace(); } }).start(); }) .setNegativeButton("No", null) .show(); }); holder.binding.btnDel.setOnClickListener(v -> { new MaterialAlertDialogBuilder(context) .setTitle("Delete backup") .setMessage("Are you sure you want to delete this backup?") .setPositiveButton("Yes", (dialog, which) -> { File file = new File(backup.getPath()); if (file.exists()) { try { Utils.runAsRoot("rm -rf " + backup.getPath()); backups.remove(backup); notifyItemRemoved(position); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); } } else { Log.e("BackupAdapter", "File not found"); } }) .setNegativeButton("No", null) .show(); }); } @Override public int getItemCount() { return backups.size(); } public class BackupViewHolder extends RecyclerView.ViewHolder { ItemBackupBinding binding; public BackupViewHolder(@NonNull ItemBackupBinding binding) { super(binding.getRoot()); this.binding = binding; } } }