aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/ui/adapter/KnownHostsAdapter.java
blob: 2b8ee0484b6bd415a73624cd91bd95671898ccd8 (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
package eu.siacs.conversations.ui.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Filter;

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

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

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			ArrayList filteredList = (ArrayList)results.values;
			if (results != null && results.count > 0) {
				clear();
				for (Object c : filteredList) {
					add((String)c);
				}
				notifyDataSetChanged();
			}
		}
	};

	public KnownHostsAdapter(Context context, int viewResourceId,
			List<String> mKnownHosts) {
		super(context, viewResourceId, mKnownHosts);
		domains = new ArrayList<String>(mKnownHosts.size());
		for (String domain : mKnownHosts) {
			domains.add(new String(domain));
		}
	}

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