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.CompoundButton;
 15import android.widget.ImageView;
 16import android.widget.TextView;
 17
 18import java.lang.ref.WeakReference;
 19import java.util.List;
 20import java.util.concurrent.RejectedExecutionException;
 21
 22import eu.siacs.conversations.Config;
 23import eu.siacs.conversations.R;
 24import eu.siacs.conversations.entities.Account;
 25import eu.siacs.conversations.ui.ManageAccountActivity;
 26import eu.siacs.conversations.ui.XmppActivity;
 27import eu.siacs.conversations.utils.UIHelper;
 28
 29public class AccountAdapter extends ArrayAdapter<Account> {
 30
 31	private XmppActivity activity;
 32	private boolean showStateButton;
 33
 34	public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
 35		super(activity, 0, objects);
 36		this.activity = activity;
 37		this.showStateButton = showStateButton;
 38	}
 39
 40	public AccountAdapter(XmppActivity activity, List<Account> objects) {
 41		super(activity, 0, objects);
 42		this.activity = activity;
 43		this.showStateButton = true;
 44	}
 45
 46	@Override
 47	public View getView(int position, View view, ViewGroup parent) {
 48		final Account account = getItem(position);
 49		if (view == null) {
 50			LayoutInflater inflater = (LayoutInflater) getContext()
 51					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 52			view = inflater.inflate(R.layout.account_row, parent, false);
 53		}
 54		TextView jid = view.findViewById(R.id.account_jid);
 55		if (Config.DOMAIN_LOCK != null) {
 56			jid.setText(account.getJid().getLocalpart());
 57		} else {
 58			jid.setText(account.getJid().toBareJid().toString());
 59		}
 60		TextView statusView = view.findViewById(R.id.account_status);
 61		ImageView imageView = view.findViewById(R.id.account_image);
 62		loadAvatar(account,imageView);
 63		statusView.setText(getContext().getString(account.getStatus().getReadableId()));
 64		switch (account.getStatus()) {
 65			case ONLINE:
 66				statusView.setTextColor(activity.getOnlineColor());
 67				break;
 68			case DISABLED:
 69			case CONNECTING:
 70				statusView.setTextColor(activity.getSecondaryTextColor());
 71				break;
 72			default:
 73				statusView.setTextColor(activity.getWarningTextColor());
 74				break;
 75		}
 76		final SwitchCompat tglAccountState = view.findViewById(R.id.tgl_account_status);
 77		final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
 78		tglAccountState.setOnCheckedChangeListener(null);
 79		tglAccountState.setChecked(!isDisabled);
 80		if (this.showStateButton) {
 81			tglAccountState.setVisibility(View.VISIBLE);
 82		} else {
 83			tglAccountState.setVisibility(View.GONE);
 84		}
 85		tglAccountState.setOnCheckedChangeListener((compoundButton, b) -> {
 86			if (b == isDisabled && activity instanceof ManageAccountActivity) {
 87				((ManageAccountActivity) activity).onClickTglAccountState(account,b);
 88			}
 89		});
 90		return view;
 91	}
 92
 93	class BitmapWorkerTask extends AsyncTask<Account, Void, Bitmap> {
 94		private final WeakReference<ImageView> imageViewReference;
 95		private Account account = null;
 96
 97		public BitmapWorkerTask(ImageView imageView) {
 98			imageViewReference = new WeakReference<>(imageView);
 99		}
100
101		@Override
102		protected Bitmap doInBackground(Account... params) {
103			return activity.avatarService().get(params[0], 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().toBareJid().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	public static boolean cancelPotentialWork(Account account, ImageView imageView) {
140		final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
141
142		if (bitmapWorkerTask != null) {
143			final Account oldAccount = bitmapWorkerTask.account;
144			if (oldAccount == null || account != oldAccount) {
145				bitmapWorkerTask.cancel(true);
146			} else {
147				return false;
148			}
149		}
150		return true;
151	}
152
153	private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
154		if (imageView != null) {
155			final Drawable drawable = imageView.getDrawable();
156			if (drawable instanceof AsyncDrawable) {
157				final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
158				return asyncDrawable.getBitmapWorkerTask();
159			}
160		}
161		return null;
162	}
163
164	static class AsyncDrawable extends BitmapDrawable {
165		private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
166
167		public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
168			super(res, bitmap);
169			bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
170		}
171
172		public BitmapWorkerTask getBitmapWorkerTask() {
173			return bitmapWorkerTaskReference.get();
174		}
175	}
176}