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 if (account == null) {
56 return view;
57 }
58 viewHolder.binding.accountJid.setText(account.getJid().asBareJid().toString());
59 AvatarWorkerTask.loadAvatar(account, viewHolder.binding.accountImage, R.dimen.avatar);
60 final var status = account.getStatus();
61 if (account.isServiceOutage()) {
62 final var sos = account.getServiceOutageStatus();
63 if (sos != null && sos.isPlanned()) {
64 viewHolder.binding.accountStatus.setText(
65 R.string.account_status_service_outage_scheduled);
66 } else {
67 viewHolder.binding.accountStatus.setText(
68 R.string.account_status_service_outage_known);
69 }
70 } else {
71 viewHolder.binding.accountStatus.setText(status.getReadableId());
72 }
73 switch (status) {
74 case ONLINE:
75 viewHolder.binding.accountStatus.setTextColor(
76 MaterialColors.getColor(
77 viewHolder.binding.accountStatus,
78 androidx.appcompat.R.attr.colorPrimary));
79 break;
80 case DISABLED:
81 case LOGGED_OUT:
82 case CONNECTING:
83 viewHolder.binding.accountStatus.setTextColor(
84 MaterialColors.getColor(
85 viewHolder.binding.accountStatus,
86 com.google.android.material.R.attr.colorOnSurfaceVariant));
87 break;
88 default:
89 viewHolder.binding.accountStatus.setTextColor(
90 MaterialColors.getColor(
91 viewHolder.binding.accountStatus,
92 androidx.appcompat.R.attr.colorError));
93 break;
94 }
95 if (account.isOnlineAndConnected()) {
96 viewHolder.binding.verificationIndicator.setVisibility(View.VISIBLE);
97 if (account.getXmppConnection() != null && account.getXmppConnection().resolverAuthenticated()) {
98 if (account.getXmppConnection().daneVerified()) {
99 viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield_verified);
100 } else {
101 viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield);
102 }
103 } else {
104 viewHolder.binding.verificationIndicator.setImageResource(R.drawable.shield_question);
105 }
106 } else {
107 viewHolder.binding.verificationIndicator.setVisibility(View.GONE);
108 }
109 final boolean isDisabled = (account.getStatus() == Account.State.DISABLED);
110 viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(null);
111 viewHolder.binding.tglAccountStatus.setChecked(!isDisabled);
112 if (this.showStateButton) {
113 viewHolder.binding.tglAccountStatus.setVisibility(View.VISIBLE);
114 } else {
115 viewHolder.binding.tglAccountStatus.setVisibility(View.GONE);
116 }
117 viewHolder.binding.tglAccountStatus.setOnCheckedChangeListener(
118 (compoundButton, b) -> {
119 if (b == isDisabled && activity instanceof OnTglAccountState tglAccountState) {
120 tglAccountState.onClickTglAccountState(account, b);
121 }
122 });
123 if (activity.xmppConnectionService != null && activity.xmppConnectionService.getAccounts().size() > 1) {
124 viewHolder.binding.frame.setBackgroundColor(account.getColor(activity.isDark()));
125 }
126 return view;
127 }
128
129 private static class ViewHolder {
130 private final ItemAccountBinding binding;
131
132 private ViewHolder(ItemAccountBinding binding) {
133 this.binding = binding;
134 }
135 }
136
137 public interface OnTglAccountState {
138 void onClickTglAccountState(Account account, boolean state);
139 }
140}