OSDN Git Service

Merge commit 'goog/readonly-korg-master' into merge_korg_master
[android-x86/packages-apps-Calendar.git] / src / com / android / calendar / SelectCalendarsAdapter.java
1 /*
2  * Copyright (C) 2007 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
17 package com.android.calendar;
18
19 import android.content.ContentResolver;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.graphics.drawable.Drawable;
25 import android.graphics.drawable.GradientDrawable;
26 import android.net.Uri;
27 import android.provider.Calendar.Calendars;
28 import android.text.TextUtils;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.widget.CheckBox;
33 import android.widget.CompoundButton;
34 import android.widget.CursorAdapter;
35 import android.widget.TextView;
36
37 public class SelectCalendarsAdapter extends CursorAdapter {
38
39     private static final int CLEAR_ALPHA_MASK = 0x00FFFFFF;
40     private static final int HIGH_ALPHA = 255 << 24;
41     private static final int MED_ALPHA = 180 << 24;
42     private static final int LOW_ALPHA = 150 << 24;
43
44     /* The corner should be rounded on the top right and bottom right */
45     private static final float[] CORNERS = new float[] {0, 0, 5, 5, 5, 5, 0, 0};
46
47     private static final String TAG = "Calendar";
48
49     private final LayoutInflater mInflater;
50     private final ContentResolver mResolver;
51     private final ContentValues mValues = new ContentValues();
52     private Boolean mIsChecked[] = null;
53     private static final Boolean CHECKED = true;
54     private static final Boolean UNCHECKED = false;
55
56     private class CheckBoxListener implements CheckBox.OnCheckedChangeListener {
57         private final long mCalendarId;
58         private final int mPosition;
59
60         private CheckBoxListener(long calendarId, int position) {
61             mPosition = position;
62             mCalendarId = calendarId;
63         }
64         
65         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
66             Uri uri = ContentUris.withAppendedId(Calendars.CONTENT_URI, mCalendarId);
67             mValues.clear();
68             int checked = isChecked ? 1 : 0;
69             mValues.put(Calendars.SELECTED, checked);
70             mResolver.update(uri, mValues, null, null);
71             mIsChecked[mPosition] = isChecked ? CHECKED : UNCHECKED;
72         }
73     }
74
75     private void updateIsCheckedArray(int cursorCount) {
76         mIsChecked = new Boolean[cursorCount];
77     }
78
79     public SelectCalendarsAdapter(Context context, Cursor cursor) {
80         super(context, cursor);
81         mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
82         mResolver = context.getContentResolver();
83         updateIsCheckedArray(cursor.getCount());
84     }
85
86     @Override
87     public View newView(Context context, Cursor cursor, ViewGroup parent) {
88         return mInflater.inflate(R.layout.calendar_item, parent, false);
89     }
90
91     @Override
92     public void bindView(View view, Context context, Cursor cursor) {
93         int idColumn = cursor.getColumnIndexOrThrow(Calendars._ID);
94         int nameColumn = cursor.getColumnIndexOrThrow(Calendars.DISPLAY_NAME);
95         int selectedColumn = cursor.getColumnIndexOrThrow(Calendars.SELECTED);
96         int colorColumn = cursor.getColumnIndexOrThrow(Calendars.COLOR);
97         view.findViewById(R.id.color).setBackgroundDrawable(getColorChip(cursor.getInt(colorColumn)));
98         setText(view, R.id.calendar, cursor.getString(nameColumn));
99         CheckBox box = (CheckBox) view.findViewById(R.id.checkbox);
100         long id = cursor.getLong(idColumn);
101
102         // Update mIsChecked array is needed
103         int cursorCount = cursor.getCount();
104         if (cursorCount != mIsChecked.length) {
105             updateIsCheckedArray(cursorCount);
106         }
107
108         // If the value hasn't changed, read from cursor; otherwise, read from mIsChecked array.
109         boolean checked;
110         int position = cursor.getPosition();
111         if (mIsChecked[position] == null) {
112             checked = cursor.getInt(selectedColumn) != 0;
113         } else {
114             checked = (mIsChecked[position] == CHECKED);
115         }
116
117         box.setOnCheckedChangeListener(null);
118         box.setChecked(checked);
119         box.setOnCheckedChangeListener(new CheckBoxListener(id, position));
120     }
121
122     private static void setText(View view, int id, String text) {
123         if (TextUtils.isEmpty(text)) {
124             return;
125         }
126         TextView textView = (TextView) view.findViewById(id);
127         textView.setText(text);
128     }
129     
130     private Drawable getColorChip(int color) {
131         
132         /*
133          * We want the color chip to have a nice gradient using
134          * the color of the calendar. To do this we use a GradientDrawable.
135          * The color supplied has an alpha of FF so we first do:
136          * color & 0x00FFFFFF
137          * to clear the alpha. Then we add our alpha to it.
138          * We use 3 colors to get a step effect where it starts off very
139          * light and quickly becomes dark and then a slow transition to
140          * be even darker.
141          */
142         color &= CLEAR_ALPHA_MASK;
143         int startColor = color | HIGH_ALPHA;
144         int middleColor = color | MED_ALPHA;
145         int endColor = color | LOW_ALPHA;
146         int[] colors = new int[] {startColor, middleColor, endColor};
147         GradientDrawable d = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
148         d.setCornerRadii(CORNERS);
149         return d;
150     }
151 }