package de.thedevstack.conversationsplus.ui.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.Switch; import android.widget.TextView; import java.util.List; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.ConversationsPlusColors; import de.thedevstack.conversationsplus.R; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.services.avatar.AvatarCache; import de.thedevstack.conversationsplus.services.avatar.AvatarService; import de.thedevstack.conversationsplus.ui.ManageAccountActivity; import de.thedevstack.conversationsplus.ui.XmppActivity; public class AccountAdapter extends ArrayAdapter { private XmppActivity activity; public AccountAdapter(XmppActivity activity, List objects) { super(activity, 0, objects); this.activity = activity; } @Override public View getView(int position, View view, ViewGroup parent) { final Account account = getItem(position); if (view == null) { LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.account_row, parent, false); } TextView jid = (TextView) view.findViewById(R.id.account_jid); if (Config.DOMAIN_LOCK != null) { jid.setText(account.getJid().getLocalpart()); } else { jid.setText(account.getJid().toBareJid().toString()); } TextView statusView = (TextView) view.findViewById(R.id.account_status); ImageView imageView = (ImageView) view.findViewById(R.id.account_image); imageView.setImageBitmap(AvatarCache.get(account, activity.getPixel(48))); statusView.setText(getContext().getString(account.getStatus().getReadableId())); switch (account.getStatus()) { case ONLINE: statusView.setTextColor(ConversationsPlusColors.online()); break; case DISABLED: case CONNECTING: statusView.setTextColor(ConversationsPlusColors.secondaryText()); break; default: statusView.setTextColor(ConversationsPlusColors.warning()); break; } final Switch tglAccountState = (Switch) view.findViewById(R.id.tgl_account_status); final boolean isDisabled = (account.getStatus() == Account.State.DISABLED); tglAccountState.setChecked(!isDisabled); tglAccountState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { // Condition compoundButton.isPressed() added because of http://stackoverflow.com/a/28219410 if (compoundButton.isPressed() && b == isDisabled && activity instanceof ManageAccountActivity) { ((ManageAccountActivity) activity).onClickTglAccountState(account, b); } } }); return view; } }