com.none.tom.simplerssreader.utils.SearchUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.utils.SearchUtils.java

Source

// Copyright (c) 2017-2018, Tom Geiselmann
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.none.tom.simplerssreader.utils;

import android.app.Activity;
import android.text.Spannable;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;

import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

public class SearchUtils {
    public static LinkedHashMap<Integer, Integer> getIndicesForQuery(final String str, final String query) {
        final LinkedHashMap<Integer, Integer> indices = new LinkedHashMap<>();

        int start = StringUtils.indexOfIgnoreCase(str, query);

        indices.put(start, start + query.length());

        for (++start; (start = StringUtils.indexOfIgnoreCase(str, query, start)) > 0; start++) {
            indices.put(start, start + query.length());
        }

        return indices;
    }

    public static void setSearchResultsSpan(final Activity activity, final SearchResults results, final Spannable s,
            final int colorHighlightCurrent, final int colorHighlight, final int colorFont, final int position) {
        final int index = results.columnIndexOf(position);

        int i = 0;

        for (final Map.Entry<Integer, Integer> entry : results.getRowIndicesAt(index).entrySet()) {
            final int start = entry.getKey();
            final int end = entry.getValue();

            final BackgroundColorSpan span;

            if (results.isCurrent(index, i)) {
                span = new BackgroundColorSpan(colorHighlightCurrent);
            } else {
                span = new BackgroundColorSpan(colorHighlight);
            }

            s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            if (ThemeUtils.isDarkThemeSelected(activity)) {
                s.setSpan(new ForegroundColorSpan(colorFont), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            i++;
        }
    }
}