Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b71b4619 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

compose: highlight query in dropdown text

parent 32fae731
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -2,10 +2,14 @@ package com.fsck.k9.activity;


import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -25,6 +29,7 @@ import com.fsck.k9.view.RecipientSelectView.RecipientCryptoStatus;
public class RecipientAdapter extends BaseAdapter implements Filterable {
    private final Context context;
    private List<Recipient> recipients;
    private String highlight;


    public RecipientAdapter(Context context) {
@@ -37,6 +42,10 @@ public class RecipientAdapter extends BaseAdapter implements Filterable {
        notifyDataSetChanged();
    }

    public void setHighlight(String highlight) {
        this.highlight = highlight;
    }

    @Override
    public int getCount() {
        return recipients == null ? 0 : recipients.size();
@@ -76,10 +85,10 @@ public class RecipientAdapter extends BaseAdapter implements Filterable {
    public void bindView(View view, Recipient recipient) {
        RecipientTokenHolder holder = (RecipientTokenHolder) view.getTag();

        holder.name.setText(recipient.getDisplayNameOrUnknown(context));
        holder.name.setText(highlightText(recipient.getDisplayNameOrUnknown(context)));

        String address = recipient.address.getAddress();
        holder.email.setText(address);
        holder.email.setText(highlightText(address));

        setContactPhotoOrPlaceholder(context, holder.photo, recipient);

@@ -165,4 +174,23 @@ public class RecipientAdapter extends BaseAdapter implements Filterable {
            cryptoStatus = (ImageView) view.findViewById(R.id.contact_crypto_status);
        }
    }

    public Spannable highlightText(String text) {
        Spannable highlightedSpannable = Spannable.Factory.getInstance().newSpannable(text);

        if (highlight == null) {
            return highlightedSpannable;
        }

        Pattern pattern = Pattern.compile(highlight, Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            highlightedSpannable.setSpan(
                    new ForegroundColorSpan(context.getResources().getColor(android.R.color.holo_blue_light)),
                    matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        return highlightedSpannable;
    }

}
+2 −0
Original line number Diff line number Diff line
@@ -287,6 +287,7 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> implem
        switch (id) {
            case LOADER_ID_FILTERING: {
                String query = args != null && args.containsKey(ARG_QUERY) ? args.getString(ARG_QUERY) : "";
                adapter.setHighlight(query);
                return new RecipientLoader(getContext(), cryptoProvider, query);
            }
            case LOADER_ID_ALTERNATES: {
@@ -321,6 +322,7 @@ public class RecipientSelectView extends TokenCompleteTextView<Recipient> implem
    @Override
    public void onLoaderReset(Loader<List<Recipient>> loader) {
        if (loader.getId() == LOADER_ID_FILTERING) {
            adapter.setHighlight(null);
            adapter.setRecipients(null);
        }
    }