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