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