CorporaAdapter.java revision 22da5e41bec9a5d36388c1e6f99bca392c3ac63e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.quicksearchbox.ui;
18
19import com.android.quicksearchbox.Corpora;
20import com.android.quicksearchbox.Corpus;
21
22import android.database.DataSetObserver;
23import android.graphics.drawable.Drawable;
24import android.util.Log;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.BaseAdapter;
28
29import java.util.ArrayList;
30import java.util.Collections;
31import java.util.Comparator;
32import java.util.List;
33
34/**
35 * Adapter for showing a list of sources in the source selection activity.
36 */
37public class CorporaAdapter extends BaseAdapter {
38
39    private static final String TAG = "CorporaAdapter";
40    private static final boolean DBG = false;
41
42    private final CorpusViewFactory mViewFactory;
43
44    private final Corpora mCorpora;
45
46    private final boolean mGridView;
47
48    private final DataSetObserver mCorporaObserver = new CorporaObserver();
49
50    private List<Corpus> mSortedCorpora;
51
52    public CorporaAdapter(CorpusViewFactory viewFactory,
53            Corpora corpora, boolean gridView) {
54        mViewFactory = viewFactory;
55        mCorpora = corpora;
56        mGridView = gridView;
57        mCorpora.registerDataSetObserver(mCorporaObserver);
58        updateCorpora();
59    }
60
61    private void updateCorpora() {
62        List<Corpus> enabledCorpora = mCorpora.getEnabledCorpora();
63        ArrayList<Corpus> sorted = new ArrayList<Corpus>(enabledCorpora.size());
64        for (Corpus corpus : enabledCorpora) {
65            if (!corpus.isCorpusHidden()) {
66                sorted.add(corpus);
67            }
68        }
69        Collections.sort(sorted, new CorpusComparator());
70        mSortedCorpora = sorted;
71        notifyDataSetChanged();
72    }
73
74    private static class CorpusComparator implements Comparator<Corpus> {
75        public int compare(Corpus corpus1, Corpus corpus2) {
76            // Comparing a corpus against itself
77            if (corpus1 == corpus2) return 0;
78            // Web always comes first
79            if (corpus1.isWebCorpus()) return -1;
80            if (corpus2.isWebCorpus()) return 1;
81            // Alphabetically by name
82            return corpus1.getLabel().toString().compareTo(corpus2.getLabel().toString());
83        }
84    }
85
86    public void close() {
87        mCorpora.unregisterDataSetObserver(mCorporaObserver);
88    }
89
90    public int getCount() {
91        return 1 + (mSortedCorpora == null ? 0 : mSortedCorpora.size());
92    }
93
94    public Corpus getItem(int position) {
95        if (position == 0) {
96            return null;
97        } else {
98            return mSortedCorpora.get(position - 1);
99        }
100    }
101
102    public long getItemId(int position) {
103        return position;
104    }
105
106    /**
107     * Gets the position of the given corpus.
108     */
109    public int getCorpusPosition(Corpus corpus) {
110        if (corpus == null) {
111            return 0;
112        }
113        int count = getCount();
114        for (int i = 0; i < count; i++) {
115            if (corpus.equals(getItem(i))) {
116                return i;
117            }
118        }
119        Log.w(TAG, "Corpus not in adapter: " + corpus);
120        return 0;
121    }
122
123    public View getView(int position, View convertView, ViewGroup parent) {
124        CorpusView view = (CorpusView) convertView;
125        if (view == null) {
126            view = createView(parent);
127        }
128        Corpus corpus = getItem(position);
129        Drawable icon;
130        CharSequence label;
131        if (corpus == null) {
132            icon = mViewFactory.getGlobalSearchIcon();
133            label = mViewFactory.getGlobalSearchLabel();
134        } else {
135            icon = corpus.getCorpusIcon();
136            label = corpus.getLabel();
137        }
138        if (DBG) Log.d(TAG, "Binding " + position + ", label=" + label);
139        view.setIcon(icon);
140        view.setLabel(label);
141        return view;
142    }
143
144    protected CorpusView createView(ViewGroup parent) {
145        if (mGridView) {
146            return mViewFactory.createGridCorpusView(parent);
147        } else {
148            return mViewFactory.createListCorpusView(parent);
149        }
150    }
151
152    private class CorporaObserver extends DataSetObserver {
153        @Override
154        public void onChanged() {
155            updateCorpora();
156        }
157
158        @Override
159        public void onInvalidated() {
160            updateCorpora();
161        }
162    }
163
164}
165