aboutsummaryrefslogtreecommitdiffstats
path: root/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/HighlightQueryCursorAdapter.java
blob: 1c41cbf0349623779fda21436ef9423019a479e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.sufficientlysecure.keychain.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.text.Spannable;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.CursorAdapter;

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


public class HighlightQueryCursorAdapter extends CursorAdapter {

    private String mCurQuery;

    public HighlightQueryCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        mCurQuery = null;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return null;
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {

    }

    public void setSearchQuery(String searchQuery){
        mCurQuery = searchQuery;
    }

    public String getSearchQuery(){
        return mCurQuery;
    }

    protected Spannable highlightSearchKey(String text) {
        Spannable  highlight;
        Pattern pattern;
        Matcher matcher;

        highlight  = Spannable.Factory.getInstance().newSpannable(text);;
        pattern = Pattern.compile("(?i)" + mCurQuery);
        matcher = pattern.matcher(text);
        if (matcher.find()) {
            highlight.setSpan(
                    new ForegroundColorSpan(0xFF33B5E5),
                    matcher.start(),
                    matcher.end(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return highlight;
    }
}