aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/adapter/KnownHostsAdapter.java
blob: 2e0bfccc6128fa0a53bc3e19917362da9bb73863 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package de.pixart.messenger.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;

public class KnownHostsAdapter extends ArrayAdapter<String> {
    private ArrayList<String> domains;
    private Filter domainFilter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            final ArrayList<String> suggestions = new ArrayList<>();
            final String[] split = constraint == null ? new String[0] : constraint.toString().split("@");
            if (split.length == 1) {
                final String local = split[0].toLowerCase(Locale.ENGLISH);
                for (String domain : domains) {
                    suggestions.add(local + "@" + domain);
                }
            } else if (split.length == 2) {
                final String localPart = split[0].toLowerCase(Locale.ENGLISH);
                final String domainPart = split[1].toLowerCase(Locale.ENGLISH);
                if (domains.contains(domainPart)) {
                    return new FilterResults();
                }
                for (String domain : domains) {
                    if (domain.contains(domainPart)) {
                        suggestions.add(localPart + "@" + domain);
                    }
                }
            } else {
                return new FilterResults();
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList filteredList = (ArrayList) results.values;
            if (results.count > 0) {
                clear();
                addAll(filteredList);
                notifyDataSetChanged();
            }
        }
    };

    public KnownHostsAdapter(Context context, int viewResourceId, Collection<String> mKnownHosts) {
        super(context, viewResourceId, new ArrayList<>());
        domains = new ArrayList<>(mKnownHosts);
    }

    public KnownHostsAdapter(Context context, int viewResourceId) {
        super(context, viewResourceId, new ArrayList<>());
        domains = new ArrayList<>();
    }

    public void refresh(Collection<String> knownHosts) {
        domains = new ArrayList<>(knownHosts);
        notifyDataSetChanged();
    }

    @Override
    @NonNull
    public Filter getFilter() {
        return domainFilter;
    }
}