AccountAdapter.java

  1package eu.siacs.conversations.ui.adapter;
  2
  3import android.view.LayoutInflater;
  4import android.view.View;
  5import android.view.ViewGroup;
  6import android.widget.ArrayAdapter;
  7import androidx.annotation.NonNull;
  8import androidx.core.graphics.ColorUtils;
  9import androidx.databinding.DataBindingUtil;
 10import com.google.android.material.color.MaterialColors;
 11import eu.siacs.conversations.R;
 12import eu.siacs.conversations.databinding.ItemAccountBinding;
 13import eu.siacs.conversations.entities.Account;
 14import eu.siacs.conversations.ui.XmppActivity;
 15import eu.siacs.conversations.ui.util.AvatarWorkerTask;
 16import eu.siacs.conversations.utils.UIHelper;
 17
 18import java.util.List;
 19
 20public class AccountAdapter extends ArrayAdapter<Account> {
 21
 22    private final XmppActivity activity;
 23    private final boolean showStateButton;
 24
 25    public AccountAdapter(XmppActivity activity, List<Account> objects, boolean showStateButton) {
 26        super(activity, 0, objects);
 27        this.activity = activity;
 28        this.showStateButton = showStateButton;
 29    }
 30
 31    public AccountAdapter(XmppActivity activity, List<Account> objects) {
 32        super(activity, 0, objects);
 33        this.activity = activity;
 34        this.showStateButton = true;
 35    }
 36
 37    @NonNull
 38    @Override
 39    public View getView(int position, View view, @NonNull ViewGroup parent) {
 40        final Account account = getItem(position);
 41        final ViewHolder viewHolder;
 42        if (view == null) {
 43            ItemAccountBinding binding =
 44                    DataBindingUtil.inflate(
 45                            LayoutInflater.from(parent.getContext()),
 46                            R.layout.item_account,
 47                            parent,
 48                            false);
 49            view = binding.getRoot();
 50            viewHolder = new ViewHolder(binding);
 51            view.setTag(viewHolder);
 52        } else {
 53            viewHolder = (ViewHolder) view.getTag();
 54        }
 55        viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toString());
 56        AvatarWorkerTask.loadAvatar(account, viewHolder.binding.accountImage, R.dimen.avatar);
 57        viewHolder.binding.accountStatus.setText(
 58                getContext().getString(account.getStatus().getReadableId()));
 59        switch (account.getStatus()) {
 60            case ONLINE:
 61                viewHolder.binding.accountStatus.setTextColor(
 62                        MaterialColors.getColor(
 63                                viewHolder.binding.accountStatus,
 64                                androidx.appcompat.R.attr.colorPrimary));
 65                break;
 66            case DISABLED:
 67            case LOGGED_OUT:
 68            case CONNECTING:
 69                viewHolder.binding.accountStatus.setTextColor(
 70                        MaterialColors.getColor(
 71                                viewHolder.binding.accountStatus,
 72                                com.google.android.material.R.attr.colorOnSurfaceVariant));
 73                break;
 74            default:
 75                viewHolder.binding.accountStatus.setTextColor(
 76                        MaterialColors.getColor(
 77                                viewHolder.binding.accountStatus,
 78                                androidx.appcompat.R.attr.colorError));
 79                break;
 80        }
 81        if (account.isOnlineAndConnected()) {
 82            viewHolder.binding.verificationIndicator.setVisibility(View.VISIBLE);
 83            if (account.getXmppConnection() != null && account.getXmppConnection().resolverAuthenticated()) {
 84                if (account.getXmppConnection().daneVerified()) {
 85                    viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield_verified);
 86                } else {
 87                    viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield);
 88                }
 89            } else {
 90                viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield_question);
 91            }
 92        } else {
 93            viewHolder.binding.verificationIndicator.setVisibility(View.GONE);
 94        }
 95        final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
 96        viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(null);
 97        viewHolder.binding.tglAccountStatus.setChecked(!isDisabled);
 98        if (this.showStateButton) {
 99            viewHolder.binding.tglAccountStatus.setVisibility(View.VISIBLE);
100        } else {
101            viewHolder.binding.tglAccountStatus.setVisibility(View.GONE);
102        }
103        viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(
104                (compoundButton, b) -> {
105                    if (b == isDisabled && activity instanceof OnTglAccountState tglAccountState) {
106                        tglAccountState.onClickTglAccountState(account, b);
107                    }
108                });
109        if (activity.xmppConnectionService != null && activity.xmppConnectionService.getAccounts().size() > 1) {
110            viewHolder.binding.frame.setBackgroundColor(account.getColor(activity.isDark()));
111        }
112        return view;
113    }
114
115    private static class ViewHolder {
116        private final ItemAccountBinding binding;
117
118        private ViewHolder(ItemAccountBinding binding) {
119            this.binding = binding;
120        }
121    }
122
123    public interface OnTglAccountState {
124        void onClickTglAccountState(Account account, boolean state);
125    }
126}