2014-07-31 13:09:34 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-03-09 09:58:11 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.AsyncTask;
|
2018-02-17 10:18:23 +00:00
|
|
|
import android.support.v7.widget.SwitchCompat;
|
2014-07-31 13:09:34 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
2015-04-13 16:19:40 +00:00
|
|
|
import android.widget.CompoundButton;
|
2014-07-31 13:09:34 +00:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2017-03-09 09:58:11 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
2015-07-20 12:26:29 +00:00
|
|
|
import java.util.List;
|
2017-03-09 09:58:11 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2015-07-20 12:26:29 +00:00
|
|
|
|
2015-08-03 20:58:17 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-07-20 12:26:29 +00:00
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.ui.ManageAccountActivity;
|
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2017-03-09 09:58:11 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2015-07-20 12:26:29 +00:00
|
|
|
|
2014-07-31 13:09:34 +00:00
|
|
|
public class AccountAdapter extends ArrayAdapter<Account> {
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-07-31 13:09:34 +00:00
|
|
|
private XmppActivity activity;
|
2017-12-06 10:18:05 +00:00
|
|
|
private boolean showStateButton;
|
|
|
|
|
|
|
|
public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
|
|
|
|
super(activity, 0, objects);
|
|
|
|
this.activity = activity;
|
|
|
|
this.showStateButton = showStateButton;
|
|
|
|
}
|
2014-08-31 14:28:21 +00:00
|
|
|
|
2014-07-31 13:09:34 +00:00
|
|
|
public AccountAdapter(XmppActivity activity, List<Account> objects) {
|
|
|
|
super(activity, 0, objects);
|
|
|
|
this.activity = activity;
|
2017-12-06 10:18:05 +00:00
|
|
|
this.showStateButton = true;
|
2014-07-31 13:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
2015-04-01 10:14:05 +00:00
|
|
|
final Account account = getItem(position);
|
2014-07-31 13:09:34 +00:00
|
|
|
if (view == null) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) getContext()
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2014-10-26 17:54:10 +00:00
|
|
|
view = inflater.inflate(R.layout.account_row, parent, false);
|
2014-07-31 13:09:34 +00:00
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
TextView jid = view.findViewById(R.id.account_jid);
|
2015-08-03 20:58:17 +00:00
|
|
|
if (Config.DOMAIN_LOCK != null) {
|
|
|
|
jid.setText(account.getJid().getLocalpart());
|
|
|
|
} else {
|
|
|
|
jid.setText(account.getJid().toBareJid().toString());
|
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
TextView statusView = view.findViewById(R.id.account_status);
|
|
|
|
ImageView imageView = view.findViewById(R.id.account_image);
|
2017-03-09 09:58:11 +00:00
|
|
|
loadAvatar(account,imageView);
|
2015-04-01 10:14:05 +00:00
|
|
|
statusView.setText(getContext().getString(account.getStatus().getReadableId()));
|
|
|
|
switch (account.getStatus()) {
|
|
|
|
case ONLINE:
|
|
|
|
statusView.setTextColor(activity.getOnlineColor());
|
|
|
|
break;
|
|
|
|
case DISABLED:
|
|
|
|
case CONNECTING:
|
|
|
|
statusView.setTextColor(activity.getSecondaryTextColor());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
statusView.setTextColor(activity.getWarningTextColor());
|
|
|
|
break;
|
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
final SwitchCompat tglAccountState = view.findViewById(R.id.tgl_account_status);
|
2015-07-17 21:58:53 +00:00
|
|
|
final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
|
2018-02-17 10:18:23 +00:00
|
|
|
tglAccountState.setOnCheckedChangeListener(null);
|
|
|
|
tglAccountState.setChecked(!isDisabled);
|
2017-12-06 10:18:05 +00:00
|
|
|
if (this.showStateButton) {
|
|
|
|
tglAccountState.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
tglAccountState.setVisibility(View.GONE);
|
|
|
|
}
|
2018-02-17 10:18:23 +00:00
|
|
|
tglAccountState.setOnCheckedChangeListener((compoundButton, b) -> {
|
|
|
|
if (b == isDisabled && activity instanceof ManageAccountActivity) {
|
|
|
|
((ManageAccountActivity) activity).onClickTglAccountState(account,b);
|
2015-04-01 10:14:05 +00:00
|
|
|
}
|
|
|
|
});
|
2014-07-31 13:09:34 +00:00
|
|
|
return view;
|
|
|
|
}
|
2017-03-09 09:58:11 +00:00
|
|
|
|
|
|
|
class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> {
|
|
|
|
private final WeakReference<ImageView> imageViewReference;
|
|
|
|
private Account account = null;
|
|
|
|
|
|
|
|
public BitmapWorkerTask(ImageView imageView) {
|
|
|
|
imageViewReference = new WeakReference<>(imageView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(Account... params) {
|
|
|
|
return activity.avatarService().get(params[0], activity.getPixel(48), isCancelled());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap bitmap) {
|
|
|
|
if (bitmap != null && !isCancelled()) {
|
|
|
|
final ImageView imageView = imageViewReference.get();
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadAvatar(Account account, ImageView imageView) {
|
|
|
|
if (cancelPotentialWork(account, imageView)) {
|
|
|
|
final Bitmap bm = activity.avatarService().get(account,activity.getPixel(48),true);
|
|
|
|
if (bm != null) {
|
|
|
|
cancelPotentialWork(account, imageView);
|
|
|
|
imageView.setImageBitmap(bm);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
} else {
|
2017-12-05 13:15:10 +00:00
|
|
|
imageView.setBackgroundColor(UIHelper.getColorForName(account.getJid().toBareJid().toString()));
|
2017-03-09 09:58:11 +00:00
|
|
|
imageView.setImageDrawable(null);
|
|
|
|
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
|
|
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
|
|
|
imageView.setImageDrawable(asyncDrawable);
|
|
|
|
try {
|
|
|
|
task.execute(account);
|
|
|
|
} catch (final RejectedExecutionException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean cancelPotentialWork(Account account, ImageView imageView) {
|
|
|
|
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
|
|
|
|
|
|
|
|
if (bitmapWorkerTask != null) {
|
|
|
|
final Account oldAccount = bitmapWorkerTask.account;
|
|
|
|
if (oldAccount == null || account != oldAccount) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
|
|
|
|
super(res, bitmap);
|
|
|
|
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BitmapWorkerTask getBitmapWorkerTask() {
|
|
|
|
return bitmapWorkerTaskReference.get();
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 13:09:34 +00:00
|
|
|
}
|