CorporaAdapter.java revision 26d85260316193a1aeff5d407d4f32f5297be012
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;
21import com.android.quicksearchbox.CorpusRanker;
22
23import android.database.DataSetObserver;
24import android.graphics.drawable.Drawable;
25import android.util.Log;
26import android.view.View;
27import android.view.ViewGroup;
28import android.widget.BaseAdapter;
29
30import java.util.ArrayList;
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 = true;
39
40    private final CorpusViewFactory mViewFactory;
41
42    private final Corpora mCorpora;
43
44    private final CorpusRanker mRanker;
45
46    private final DataSetObserver mCorporaObserver = new CorporaObserver();
47
48    private ArrayList<Corpus> mRankedEnabledCorpora;
49
50    private boolean mGridView;
51
52    private CorporaAdapter(CorpusViewFactory viewFactory, Corpora corpora,
53            CorpusRanker ranker, boolean gridView) {
54        mViewFactory = viewFactory;
55        mCorpora = corpora;
56        mRanker = ranker;
57        mGridView = gridView;
58        mCorpora.registerDataSetObserver(mCorporaObserver);
59        updateCorpora();
60    }
61
62    public static CorporaAdapter createListAdapter(CorpusViewFactory viewFactory, Corpora corpora,
63            CorpusRanker ranker) {
64        return new CorporaAdapter(viewFactory, corpora, ranker, false);
65    }
66
67    public static CorporaAdapter createGridAdapter(CorpusViewFactory viewFactory, Corpora corpora,
68            CorpusRanker ranker) {
69        return new CorporaAdapter(viewFactory, corpora, ranker, true);
70    }
71
72    private void updateCorpora() {
73        mRankedEnabledCorpora = mRanker.rankCorpora(mCorpora.getEnabledCorpora());
74        notifyDataSetChanged();
75    }
76
77    public void close() {
78        mCorpora.unregisterDataSetObserver(mCorporaObserver);
79    }
80
81    public int getCount() {
82        return 1 + mRankedEnabledCorpora.size();
83    }
84
85    public Corpus getItem(int position) {
86        if (position == 0) {
87            return null;
88        } else {
89            return mRankedEnabledCorpora.get(position - 1);
90        }
91    }
92
93    public long getItemId(int position) {
94        return position;
95    }
96
97    public View getView(int position, View convertView, ViewGroup parent) {
98        CorpusView view = (CorpusView) convertView;
99        if (view == null) {
100            view = createView(parent);
101        }
102        Corpus corpus = getItem(position);
103        Drawable icon;
104        CharSequence label;
105        if (corpus == null) {
106            icon = mViewFactory.getGlobalSearchIcon();
107            label = mViewFactory.getGlobalSearchLabel();
108        } else {
109            icon = corpus.getCorpusIcon();
110            label = corpus.getLabel();
111        }
112        if (DBG) Log.d(TAG, "Binding " + position + ", label=" + label);
113        view.setIcon(icon);
114        view.setLabel(label);
115        return view;
116    }
117
118    protected CorpusView createView(ViewGroup parent) {
119        if (mGridView) {
120            return mViewFactory.createGridCorpusView(parent);
121        } else {
122            return mViewFactory.createListCorpusView(parent);
123        }
124    }
125
126    private class CorporaObserver extends DataSetObserver {
127        @Override
128        public void onChanged() {
129            updateCorpora();
130        }
131
132        @Override
133        public void onInvalidated() {
134            updateCorpora();
135        }
136    }
137}
138