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

Commit 65791348 authored by Danny Baumann's avatar Danny Baumann Committed by Adnan Begovic
Browse files

Highlight search string match.

Change-Id: I72c0380512028674c91f287325f221a686a4821d
parent 99c23f54
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/search_match_nonhighlight_foreground"
            android:gravity="center_vertical"/>

        <TextView
+3 −1
Original line number Diff line number Diff line
@@ -29,7 +29,9 @@
    <color name="memory_user_light">#479392</color>
    <color name="memory_user_dark">#316665</color>

    <color name="search_pref_highlight_background">@android:color/white</color>
    <color name="search_pref_highlight_background">#ffffffff</color>
    <color name="search_match_highlight_foreground">#fffefefe</color>
    <color name="search_match_nonhighlight_foreground">#c0bebebe</color>

    <color name="crypt_keeper_clock_background">#ff9a9a9a</color>
    <color name="crypt_keeper_clock_foreground">#ff666666</color>
+40 −3
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceActivity.Header;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@@ -44,6 +46,7 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
    private Context mContext;
    private Resources mResources;
    private Drawable mDefaultIcon;
    private int mMatchHighlightColor;

    public static class SearchInfo {
        public Header header;
@@ -53,6 +56,9 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
        public int iconRes;
        public int parentTitle;
        public String key;

        private int mMatchStart;
        private int mMatchEnd;
    }

    public SettingsSearchFilterAdapter(Context context, int resourceId,
@@ -64,6 +70,7 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
        mResId = resourceId;
        mResources = mContext.getResources();
        mDefaultIcon = mResources.getDrawable(R.drawable.default_search_icon);
        mMatchHighlightColor = mResources.getColor(R.color.search_match_highlight_foreground);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
@@ -95,8 +102,16 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
                holder.imageView.setImageDrawable(d);
            }
            if (holder.titleView != null) {
                if (info.mMatchStart >= 0 && info.mMatchEnd >= 0) {
                    SpannableStringBuilder titleSpan = new SpannableStringBuilder(info.title);
                    ForegroundColorSpan span = new ForegroundColorSpan(mMatchHighlightColor);
                    titleSpan.setSpan(span, info.mMatchStart, info.mMatchEnd,
                            SpannableStringBuilder.SPAN_INCLUSIVE_EXCLUSIVE);
                    holder.titleView.setText(titleSpan);
                } else {
                    holder.titleView.setText(info.title);
                }
            }
            if (holder.parentView != null) {
                if (info.parentTitle != 0) {
                    holder.parentView.setText(info.parentTitle);
@@ -159,8 +174,30 @@ public class SettingsSearchFilterAdapter extends ArrayAdapter<SearchInfo> implem
                    SearchInfo item = mOriginalValues.get(i);
                    String title = item.title.toString().toLowerCase();
                    String filteredTitle = removeNonAlphaNumeric(title);
                    if (title.contains(actualConstraint) ||
                            filteredTitle.contains(filteredConstraint)) {

                    item.mMatchStart = -1;
                    item.mMatchEnd = -1;

                    int pos = filteredTitle.indexOf(filteredConstraint);
                    if (pos != -1) {
                        int unfilteredLen = title.length();
                        int filteredLen = filteredTitle.length();
                        int constraintLen = filteredConstraint.length();
                        for (int ufIndex = pos, fIndex = pos;
                                ufIndex < unfilteredLen && fIndex < filteredLen; ufIndex++) {
                            if (title.charAt(ufIndex) != filteredTitle.charAt(fIndex)) {
                                continue;
                            }
                            if (fIndex == pos) {
                                item.mMatchStart = ufIndex;
                            }
                            if (fIndex == pos + constraintLen - 1) {
                                item.mMatchEnd = ufIndex + 1;
                                break;
                            }
                            fIndex++;
                        }

                        newValues.add(item);
                    }
                }