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

Commit d2d33d85 authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Enhance global note search highlight

parent 9a95143f
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.persistence;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.Html;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.NonNull;
@@ -23,6 +25,7 @@ import it.niedermann.owncloud.notes.model.Category;
import it.niedermann.owncloud.notes.model.DBNote;
import it.niedermann.owncloud.notes.model.Item;
import it.niedermann.owncloud.notes.model.SectionItem;
import it.niedermann.owncloud.notes.util.DisplayUtils;
import it.niedermann.owncloud.notes.util.NoteUtil;

public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
@@ -32,6 +35,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
    private final Category category;
    private final CharSequence searchQuery;
    private final long accountId;
    private final int searchForeground;
    private final int searchBackground;

    public LoadNotesListTask(long accountId, @NonNull Context context, @NonNull NotesLoadedListener callback, @NonNull Category category, @Nullable CharSequence searchQuery) {
        this.context = context;
@@ -39,6 +44,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
        this.category = category;
        this.searchQuery = searchQuery;
        this.accountId = accountId;
        this.searchBackground = context.getResources().getColor(R.color.bg_highlighted);
        this.searchForeground = DisplayUtils.getForeground(Integer.toHexString(this.searchBackground)) ? Color.WHITE : context.getResources().getColor(R.color.primary);
    }

    @Override
@@ -59,8 +66,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
            SpannableString spannableString = new SpannableString(dbNote.getTitle());
            Matcher matcher = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE).matcher(spannableString);
            while (matcher.find()) {
                spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.primary_dark)),
                        matcher.start(), matcher.end(), 0);
                spannableString.setSpan(new ForegroundColorSpan(searchForeground), matcher.start(), matcher.end(), 0);
                spannableString.setSpan(new BackgroundColorSpan(searchBackground), matcher.start(), matcher.end(), 0);
            }

            dbNote.setTitle(Html.toHtml(spannableString));
@@ -68,8 +75,8 @@ public class LoadNotesListTask extends AsyncTask<Void, Void, List<Item>> {
            spannableString = new SpannableString(dbNote.getExcerpt());
            matcher = Pattern.compile("(" + searchQuery + ")", Pattern.CASE_INSENSITIVE).matcher(spannableString);
            while (matcher.find()) {
                spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.primary_dark)),
                        matcher.start(), matcher.end(), 0);
                spannableString.setSpan(new ForegroundColorSpan(searchForeground), matcher.start(), matcher.end(), 0);
                spannableString.setSpan(new BackgroundColorSpan(searchBackground), matcher.start(), matcher.end(), 0);
            }

            dbNote.setExcerpt(Html.toHtml(spannableString));
+8 −10
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.MetricAffectingSpan;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

@@ -84,7 +83,7 @@ public class DisplayUtils {
        @Override
        public void updateDrawState(TextPaint tp) {
            tp.bgColor = current ? bgColorPrimary : bgColorSecondary;
            tp.setColor(current ? getForeground(Integer.toHexString(tp.bgColor)) : bgColorPrimary);
            tp.setColor(current ? (getForeground(Integer.toHexString(tp.bgColor)) ? Color.WHITE : Color.BLACK) : bgColorPrimary);
            tp.setFakeBoldText(true);
        }

@@ -92,14 +91,13 @@ public class DisplayUtils {
        public void updateMeasureState(@NonNull TextPaint tp) {
            tp.setFakeBoldText(true);
        }
    }

        private static @ColorInt
        int getForeground(String backgroundColorHex) {
    public static boolean getForeground(String backgroundColorHex) {
        return ((float) (
                0.2126 * Integer.valueOf(backgroundColorHex.substring(1, 3), 16)
                        + 0.7152 * Integer.valueOf(backgroundColorHex.substring(3, 5), 16)
                        + 0.0722 * Integer.valueOf(backgroundColorHex.substring(5, 7), 16)
            ) < 140) ? Color.WHITE : Color.BLACK;
        }
        ) < 140);
    }
}