AccountAdapter.java

  1package eu.siacs.conversations.ui.adapter;
  2
  3import android.content.Context;
  4import android.content.res.Resources;
  5import android.graphics.Bitmap;
  6import android.graphics.drawable.BitmapDrawable;
  7import android.graphics.drawable.Drawable;
  8import android.os.AsyncTask;
  9import android.support.v7.widget.SwitchCompat;
 10import android.view.LayoutInflater;
 11import android.view.View;
 12import android.view.ViewGroup;
 13import android.widget.ArrayAdapter;
 14import android.widget.ImageView;
 15import android.widget.TextView;
 16
 17import java.lang.ref.WeakReference;
 18import java.util.List;
 19import java.util.concurrent.RejectedExecutionException;
 20
 21import eu.siacs.conversations.Config;
 22import eu.siacs.conversations.R;
 23import eu.siacs.conversations.entities.Account;
 24import eu.siacs.conversations.ui.XmppActivity;
 25import eu.siacs.conversations.ui.util.StyledAttributes;
 26import eu.siacs.conversations.utils.UIHelper;
 27
 28public class AccountAdapter extends ArrayAdapter<Account> {
 29
 30	private XmppActivity activity;
 31	private boolean showStateButton;
 32
 33	public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
 34		super(activity, 0, objects);
 35		this.activity = activity;
 36		this.showStateButton = showStateButton;
 37	}
 38
 39	public AccountAdapter(XmppActivity activity, List<Account> objects) {
 40		super(activity, 0, objects);
 41		this.activity = activity;
 42		this.showStateButton = true;
 43	}
 44
 45	@Override
 46	public View getView(int position, View view, ViewGroup parent) {
 47		final Account account = getItem(position);
 48		if (view == null) {
 49			LayoutInflater inflater = (LayoutInflater) getContext()
 50					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 51			view = inflater.inflate(R.layout.account_row, parent, false);
 52		}
 53		TextView jid = view.findViewById(R.id.account_jid);
 54		if (Config.DOMAIN_LOCK != null) {
 55			jid.setText(account.getJid().getLocal());
 56		} else {
 57			jid.setText(account.getJid().asBareJid().toString());
 58		}
 59		TextView statusView = view.findViewById(R.id.account_status);
 60		ImageView imageView = view.findViewById(R.id.account_image);
 61		loadAvatar(account, imageView);
 62		statusView.setText(getContext().getString(account.getStatus().getReadableId()));
 63		switch (account.getStatus()) {
 64			case ONLINE:
 65				statusView.setTextColor(StyledAttributes.getColor(activity, R.attr.TextColorOnline));
 66				break;
 67			case DISABLED:
 68			case CONNECTING:
 69				statusView.setTextColor(StyledAttributes.getColor(activity, android.R.attr.textColorSecondary));
 70				break;
 71			default:
 72				statusView.setTextColor(StyledAttributes.getColor(activity, R.attr.TextColorError));
 73				break;
 74		}
 75		final SwitchCompat tglAccountState = view.findViewById(R.id.tgl_account_status);
 76		final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
 77		tglAccountState.setOnCheckedChangeListener(null);
 78		tglAccountState.setChecked(!isDisabled);
 79		if (this.showStateButton) {
 80			tglAccountState.setVisibility(View.VISIBLE);
 81		} else {
 82			tglAccountState.setVisibility(View.GONE);
 83		}
 84		tglAccountState.setOnCheckedChangeListener((compoundButton, b) -> {
 85			if (b == isDisabled && activity instanceof OnTglAccountState) {
 86				((OnTglAccountState) activity).onClickTglAccountState(account, b);
 87			}
 88		});
 89		return view;
 90	}
 91
 92	class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> {
 93		private final WeakReference<ImageView> imageViewReference;
 94		private Account account = null;
 95
 96		public BitmapWorkerTask(ImageView imageView) {
 97			imageViewReference = new WeakReference<>(imageView);
 98		}
 99
100		@Override
101		protected Bitmap doInBackground(Account... params) {
102			this.account = params[0];
103			return activity.avatarService().get(this.account, activity.getPixel(48), isCancelled());
104		}
105
106		@Override
107		protected void onPostExecute(Bitmap bitmap) {
108			if (bitmap != null && !isCancelled()) {
109				final ImageView imageView = imageViewReference.get();
110				if (imageView != null) {
111					imageView.setImageBitmap(bitmap);
112					imageView.setBackgroundColor(0x00000000);
113				}
114			}
115		}
116	}
117
118	public void loadAvatar(Account account, ImageView imageView) {
119		if (cancelPotentialWork(account, imageView)) {
120			final Bitmap bm = activity.avatarService().get(account, activity.getPixel(48), true);
121			if (bm != null) {
122				cancelPotentialWork(account, imageView);
123				imageView.setImageBitmap(bm);
124				imageView.setBackgroundColor(0x00000000);
125			} else {
126				imageView.setBackgroundColor(UIHelper.getColorForName(account.getJid().asBareJid().toString()));
127				imageView.setImageDrawable(null);
128				final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
129				final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
130				imageView.setImageDrawable(asyncDrawable);
131				try {
132					task.execute(account);
133				} catch (final RejectedExecutionException ignored) {
134				}
135			}
136		}
137	}
138
139
140	public interface OnTglAccountState {
141		void onClickTglAccountState(Account account, boolean state);
142	}
143
144	public static boolean cancelPotentialWork(Account account, ImageView imageView) {
145		final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
146
147		if (bitmapWorkerTask != null) {
148			final Account oldAccount = bitmapWorkerTask.account;
149			if (oldAccount == null || account != oldAccount) {
150				bitmapWorkerTask.cancel(true);
151			} else {
152				return false;
153			}
154		}
155		return true;
156	}
157
158	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
159		if (imageView != null) {
160			final Drawable drawable = imageView.getDrawable();
161			if (drawable instanceof AsyncDrawable) {
162				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
163				return asyncDrawable.getBitmapWorkerTask();
164			}
165		}
166		return null;
167	}
168
169	static class AsyncDrawable extends BitmapDrawable {
170		private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
171
172		public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
173			super(res, bitmap);
174			bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
175		}
176
177		public BitmapWorkerTask getBitmapWorkerTask() {
178			return bitmapWorkerTaskReference.get();
179		}
180	}
181}