SelectCalendarsSimpleAdapter.java revision 07816aec18f3da7f233dc3075f573295b19e5c45
1/*
2 * Copyright (C) 2010 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.calendar.selectcalendars;
18
19import com.android.calendar.R;
20
21import android.content.Context;
22import android.content.res.Configuration;
23import android.content.res.Resources;
24import android.database.Cursor;
25import android.graphics.drawable.Drawable;
26import android.provider.Calendar.Calendars;
27import android.text.TextUtils;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31import android.widget.BaseAdapter;
32import android.widget.ListAdapter;
33import android.widget.RelativeLayout;
34import android.widget.TextView;
35
36public class SelectCalendarsSimpleAdapter extends BaseAdapter implements ListAdapter {
37    private static final String TAG = "SelectCalendarsAdapter";
38    private static int SELECTED_COLOR_CHIP_SIZE = 16;
39    private static int UNSELECTED_COLOR_CHIP_SIZE = 10;
40    private static int COLOR_CHIP_LEFT_MARGIN = 20;
41    private static int COLOR_CHIP_RIGHT_MARGIN = 8;
42    private static int COLOR_CHIP_TOP_OFFSET = 5;
43
44    private Drawable[] mBackgrounds = new Drawable[16];
45
46    private static final int SELECTED_UNDER_NORMAL = 0;
47    private static final int BOTTOM_SELECTED_UNDER_NORMAL = 1;
48    private static final int BOTTOM_SELECTED_UNDER_SELECTED = 2;
49    private static final int SELECTED_UNDER_SELECTED = 3;
50    private static final int NORMAL_UNDER_NORMAL = 4;
51    private static final int BOTTOM_NORMAL_UNDER_NORMAL = 5;
52    private static final int BOTTOM_NORMAL_UNDER_SELECTED = 6;
53    private static final int NORMAL_UNDER_SELECTED = 7;
54    private static final int IS_SELECTED = 1 << 0;
55    private static final int IS_TOP = 1 << 1;
56    private static final int IS_BOTTOM = 1 << 2;
57    private static final int IS_BELOW_SELECTED = 1 << 3;
58
59
60    private LayoutInflater mInflater;
61    private int mLayout;
62    private int mOrientation;
63    private CalendarRow[] mData;
64    private Cursor mCursor;
65    private int mRowCount = 0;
66
67    private int mIdColumn;
68    private int mNameColumn;
69    private int mColorColumn;
70    private int mSelectedColumn;
71    private float mScale = 0;
72
73    private class CalendarRow {
74        long id;
75        String displayName;
76        int color;
77        boolean selected;
78    }
79
80    public SelectCalendarsSimpleAdapter(Context context, int layout, Cursor c) {
81        super();
82        mLayout = layout;
83        mOrientation = context.getResources().getConfiguration().orientation;
84        initData(c);
85        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
86        Resources res = context.getResources();
87        if (mScale == 0) {
88            mScale = res.getDisplayMetrics().density;
89            SELECTED_COLOR_CHIP_SIZE *= mScale;
90            UNSELECTED_COLOR_CHIP_SIZE *= mScale;
91            COLOR_CHIP_LEFT_MARGIN *= mScale;
92            COLOR_CHIP_RIGHT_MARGIN *= mScale;
93            COLOR_CHIP_TOP_OFFSET *= mScale;
94        }
95        initBackgrounds(res);
96    }
97
98    /**
99     * Sets up the background drawables for the calendars list
100     *
101     * @param res The context's resources
102     */
103    private void initBackgrounds(Resources res) {
104        mBackgrounds[0] = res.getDrawable(R.drawable.calname_unselected_holo_light);
105        mBackgrounds[IS_TOP] = mBackgrounds[0];
106
107        mBackgrounds[IS_SELECTED] = res.getDrawable(
108                R.drawable.calname_select_underunselected_holo_light);
109        mBackgrounds[IS_SELECTED | IS_TOP] = mBackgrounds[IS_SELECTED];
110
111        mBackgrounds[IS_SELECTED | IS_BOTTOM] = res.getDrawable(
112                R.drawable.calname_bottom_select_underunselected_holo_light);
113        mBackgrounds[IS_SELECTED | IS_TOP | IS_BOTTOM] = mBackgrounds[IS_SELECTED | IS_BOTTOM];
114
115        mBackgrounds[IS_SELECTED | IS_BOTTOM | IS_BELOW_SELECTED] =
116                res.getDrawable(R.drawable.calname_bottom_select_underselect_holo_light);
117        mBackgrounds[IS_SELECTED | IS_TOP | IS_BOTTOM | IS_BELOW_SELECTED] = mBackgrounds[
118                IS_SELECTED | IS_BOTTOM | IS_BELOW_SELECTED];
119
120        mBackgrounds[IS_SELECTED | IS_BELOW_SELECTED] =
121                res.getDrawable(R.drawable.calname_select_underselect_holo_light);
122        mBackgrounds[IS_SELECTED | IS_TOP | IS_BELOW_SELECTED] = mBackgrounds[IS_SELECTED
123                | IS_BELOW_SELECTED];
124
125        mBackgrounds[IS_BOTTOM] = res.getDrawable(R.drawable.calname_bottom_unselected_holo_light);
126        mBackgrounds[IS_TOP | IS_BOTTOM] = mBackgrounds[IS_BOTTOM];
127
128        mBackgrounds[IS_BOTTOM | IS_BELOW_SELECTED] = res.getDrawable(
129                R.drawable.calname_bottom_unselected_underselect_holo_light);
130        mBackgrounds[IS_TOP | IS_BOTTOM | IS_BELOW_SELECTED] = mBackgrounds[IS_BOTTOM
131                | IS_BELOW_SELECTED];
132
133        mBackgrounds[IS_BELOW_SELECTED] = res.getDrawable(
134                R.drawable.calname_unselected_underselect_holo_light);
135        mBackgrounds[IS_TOP | IS_BELOW_SELECTED] = mBackgrounds[IS_BELOW_SELECTED];
136    }
137
138    private void initData(Cursor c) {
139        if (mCursor != null && c != mCursor) {
140            mCursor.close();
141        }
142        if (c == null) {
143            mCursor = c;
144            mRowCount = 0;
145            mData = null;
146            return;
147        }
148        // TODO create a broadcast listener for ACTION_PROVIDER_CHANGED to update the cursor
149        mCursor = c;
150        mIdColumn = c.getColumnIndexOrThrow(Calendars._ID);
151        mNameColumn = c.getColumnIndexOrThrow(Calendars.DISPLAY_NAME);
152        mColorColumn = c.getColumnIndexOrThrow(Calendars.COLOR);
153        mSelectedColumn = c.getColumnIndexOrThrow(Calendars.SELECTED);
154
155        mRowCount = c.getCount();
156        mData = new CalendarRow[(c.getCount() + 2)];
157        c.moveToPosition(-1);
158        int p = 0;
159        while (c.moveToNext()) {
160            mData[p] = new CalendarRow();
161            mData[p].id = c.getLong(mIdColumn);
162            mData[p].displayName = c.getString(mNameColumn);
163            mData[p].color = c.getInt(mColorColumn);
164            mData[p].selected = c.getInt(mSelectedColumn) != 0;
165            p++;
166        }
167    }
168
169    public void changeCursor(Cursor c) {
170        initData(c);
171        notifyDataSetChanged();
172    }
173
174    public View getView(int position, View convertView, ViewGroup parent) {
175        if (position >= mRowCount) {
176            return null;
177        }
178        String name = mData[position].displayName;
179        boolean selected = mData[position].selected;
180        Drawable bg = getBackground(position, selected);
181
182
183        int color = mData[position].color;
184        View view;
185        if (convertView == null) {
186            view = mInflater.inflate(mLayout, parent, false);
187        } else {
188            view = convertView;
189        }
190
191        View colorView = view.findViewById(R.id.color);
192        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
193                SELECTED_COLOR_CHIP_SIZE, SELECTED_COLOR_CHIP_SIZE);
194        params.leftMargin = COLOR_CHIP_LEFT_MARGIN;
195        params.rightMargin = COLOR_CHIP_RIGHT_MARGIN;
196        // This offset is needed because the assets include the bottom of the
197        // previous item
198        params.topMargin = COLOR_CHIP_TOP_OFFSET;
199        if (!selected) {
200            params.height = UNSELECTED_COLOR_CHIP_SIZE;
201            params.width = UNSELECTED_COLOR_CHIP_SIZE;
202            params.leftMargin += (SELECTED_COLOR_CHIP_SIZE - UNSELECTED_COLOR_CHIP_SIZE) / 2;
203            params.topMargin += (SELECTED_COLOR_CHIP_SIZE - UNSELECTED_COLOR_CHIP_SIZE) / 2;
204        }
205        colorView.setLayoutParams(params);
206        colorView.setBackgroundColor(color);
207
208        setText(view, R.id.calendar, name);
209        view.setBackgroundDrawable(bg);
210        view.invalidate();
211        return view;
212    }
213
214    /**
215     * @param position position of the calendar item
216     * @param selected whether it is selected or not
217     * @return the drawable to use for this view
218     */
219    protected Drawable getBackground(int position, boolean selected) {
220        int bg;
221        bg = selected ? IS_SELECTED : 0;
222        bg |= position == 0 ? IS_TOP : 0;
223        bg |= position == mData.length - 1 ? IS_BOTTOM : 0;
224        bg |= ((position == 0 && mOrientation != Configuration.ORIENTATION_LANDSCAPE && selected)
225                || (position > 0 && mData[position - 1].selected)) ? IS_BELOW_SELECTED : 0;
226        return mBackgrounds[bg];
227    }
228
229    private static void setText(View view, int id, String text) {
230        if (TextUtils.isEmpty(text)) {
231            return;
232        }
233        TextView textView = (TextView) view.findViewById(id);
234        textView.setText(text);
235    }
236
237    public int getCount() {
238        return mRowCount;
239    }
240
241    public Object getItem(int position) {
242        if (position >= mRowCount) {
243            return null;
244        }
245        CalendarRow item = mData[position];
246        return item;
247    }
248
249    public long getItemId(int position) {
250        if (position >= mRowCount) {
251            return 0;
252        }
253        return mData[position].id;
254    }
255
256    public void setVisible(int position, int visible) {
257        mData[position].selected = visible != 0;
258        notifyDataSetChanged();
259    }
260
261    public int getVisible(int position) {
262        return mData[position].selected ? 1 : 0;
263    }
264
265}
266