1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package de.pixart.messenger.ui.adapter;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;
import de.pixart.messenger.R;
import de.pixart.messenger.databinding.AccountRowBinding;
import de.pixart.messenger.services.AvatarService;
import de.pixart.messenger.services.ImportBackupService;
import de.pixart.messenger.utils.BackupFileHeader;
import de.pixart.messenger.utils.UIHelper;
import rocks.xmpp.addr.Jid;
public class BackupFileAdapter extends RecyclerView.Adapter<BackupFileAdapter.BackupFileViewHolder> {
private OnItemClickedListener listener;
private final List<ImportBackupService.BackupFile> files = new ArrayList<>();
@NonNull
@Override
public BackupFileViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new BackupFileViewHolder(DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.account_row, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull BackupFileViewHolder backupFileViewHolder, int position) {
final ImportBackupService.BackupFile backupFile = files.get(position);
final BackupFileHeader header = backupFile.getHeader();
backupFileViewHolder.binding.accountJid.setText(header.getJid().asBareJid().toString());
backupFileViewHolder.binding.accountStatus.setText(String.format("%s · %s", header.getApp(), DateUtils.formatDateTime(backupFileViewHolder.binding.getRoot().getContext(), header.getTimestamp(), DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_TIME)));
backupFileViewHolder.binding.tglAccountStatus.setVisibility(View.GONE);
backupFileViewHolder.binding.getRoot().setOnClickListener(v -> {
if (listener != null) {
listener.onClick(backupFile);
}
});
loadAvatar(header.getJid(), backupFileViewHolder.binding.accountImage);
}
@Override
public int getItemCount() {
return files.size();
}
public void setFiles(List<ImportBackupService.BackupFile> files) {
this.files.clear();
this.files.addAll(files);
notifyDataSetChanged();
}
public void setOnItemClickedListener(OnItemClickedListener listener) {
this.listener = listener;
}
static class BackupFileViewHolder extends RecyclerView.ViewHolder {
private final AccountRowBinding binding;
BackupFileViewHolder(AccountRowBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
public interface OnItemClickedListener {
void onClick(ImportBackupService.BackupFile backupFile);
}
static class BitmapWorkerTask extends AsyncTask<Jid, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private Jid jid = null;
private final int size;
BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<>(imageView);
DisplayMetrics metrics = imageView.getContext().getResources().getDisplayMetrics();
this.size = ((int) (48 * metrics.density));
}
@Override
protected Bitmap doInBackground(Jid... params) {
this.jid = params[0];
return AvatarService.get(this.jid, size);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && !isCancelled()) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setBackgroundColor(0x00000000);
}
}
}
}
private void loadAvatar(Jid jid, ImageView imageView) {
if (cancelPotentialWork(jid, imageView)) {
imageView.setBackgroundColor(UIHelper.getColorForName(jid.asBareJid().toString()));
imageView.setImageDrawable(null);
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(imageView.getContext().getResources(), null, task);
imageView.setImageDrawable(asyncDrawable);
try {
task.execute(jid);
} catch (final RejectedExecutionException ignored) {
}
}
}
private static boolean cancelPotentialWork(Jid jid, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final Jid oldJid = bitmapWorkerTask.jid;
if (oldJid == null || jid != oldJid) {
bitmapWorkerTask.cancel(true);
} else {
return false;
}
}
return true;
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
}
BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
}
|