CorporaAdapter.java revision 96c7058210699c82445169048b7c0fdfb16f59ee
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.Corpus;
20import com.android.quicksearchbox.CorpusRanker;
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.List;
31
32/**
33 * Adapter for showing a list of sources in the source selection activity.
34 */
35public class CorporaAdapter extends BaseAdapter {
36
37    private static final String TAG = "CorporaAdapter";
38    private static final boolean DBG = false;
39
40    private final CorpusViewFactory mViewFactory;
41
42    private final CorpusRanker mRanker;
43
44    private final DataSetObserver mCorporaObserver = new CorporaObserver();
45
46    private List<Corpus> mRankedEnabledCorpora;
47
48    private boolean mGridView;
49
50    private CorporaAdapter(CorpusViewFactory viewFactory,
51            CorpusRanker ranker, boolean gridView) {
52        mViewFactory = viewFactory;
53        mRanker = ranker;
54        mGridView = gridView;
55        mRanker.registerDataSetObserver(mCorporaObserver);
56        updateCorpora();
57    }
58
59    public static CorporaAdapter createListAdapter(CorpusViewFactory viewFactory,
60            CorpusRanker ranker) {
61        return new CorporaAdapter(viewFactory, ranker, false);
62    }
63
64    public static CorporaAdapter createGridAdapter(CorpusViewFactory viewFactory,
65            CorpusRanker ranker) {
66        return new CorporaAdapter(viewFactory, ranker, true);
67    }
68
69    private void updateCorpora() {
70        mRankedEnabledCorpora = new ArrayList<Corpus>();
71        for (Corpus corpus : mRanker.getRankedCorpora()) {
72            if (!corpus.isCorpusHidden()) {
73                mRankedEnabledCorpora.add(corpus);
74            }
75        }
76        notifyDataSetChanged();
77    }
78
79    public void close() {
80        mRanker.unregisterDataSetObserver(mCorporaObserver);
81    }
82
83    public int getCount() {
84        return 1 + mRankedEnabledCorpora.size();
85    }
86
87    public Corpus getItem(int position) {
88        if (position == 0) {
89            return null;
90        } else {
91            return mRankedEnabledCorpora.get(position - 1);
92        }
93    }
94
95    public long getItemId(int position) {
96        return position;
97    }
98
99    /**
100     * Gets the position of the given corpus.
101     */
102    public int getCorpusPosition(Corpus corpus) {
103        if (corpus == null) {
104            return 0;
105        }
106        int count = getCount();
107        for (int i = 0; i < count; i++) {
108            if (corpus.equals(getItem(i))) {
109                return i;
110            }
111        }
112        Log.w(TAG, "Corpus not in adapter: " + corpus);
113        return 0;
114    }
115
116    public View getView(int position, View convertView, ViewGroup parent) {
117        CorpusView view = (CorpusView) convertView;
118        if (view == null) {
119            view = createView(parent);
120        }
121        Corpus corpus = getItem(position);
122        Drawable icon;
123        CharSequence label;
124        if (corpus == null) {
125            icon = mViewFactory.getGlobalSearchIcon();
126            label = mViewFactory.getGlobalSearchLabel();
127        } else {
128            icon = corpus.getCorpusIcon();
129            label = corpus.getLabel();
130        }
131        if (DBG) Log.d(TAG, "Binding " + position + ", label=" + label);
132        view.setIcon(icon);
133        view.setLabel(label);
134        return view;
135    }
136
137    protected CorpusView createView(ViewGroup parent) {
138        if (mGridView) {
139            return mViewFactory.createGridCorpusView(parent);
140        } else {
141            return mViewFactory.createListCorpusView(parent);
142        }
143    }
144
145    private class CorporaObserver extends DataSetObserver {
146        @Override
147        public void onChanged() {
148            updateCorpora();
149        }
150
151        @Override
152        public void onInvalidated() {
153            updateCorpora();
154        }
155    }
156
157}
158