1e5a8f94f935248dea9e515199151472eebe5ca4cErik/*
2e5a8f94f935248dea9e515199151472eebe5ca4cErik * Copyright (C) 2010 The Android Open Source Project
3e5a8f94f935248dea9e515199151472eebe5ca4cErik *
4e5a8f94f935248dea9e515199151472eebe5ca4cErik * Licensed under the Apache License, Version 2.0 (the "License");
5e5a8f94f935248dea9e515199151472eebe5ca4cErik * you may not use this file except in compliance with the License.
6e5a8f94f935248dea9e515199151472eebe5ca4cErik * You may obtain a copy of the License at
7e5a8f94f935248dea9e515199151472eebe5ca4cErik *
8e5a8f94f935248dea9e515199151472eebe5ca4cErik *      http://www.apache.org/licenses/LICENSE-2.0
9e5a8f94f935248dea9e515199151472eebe5ca4cErik *
10e5a8f94f935248dea9e515199151472eebe5ca4cErik * Unless required by applicable law or agreed to in writing, software
11e5a8f94f935248dea9e515199151472eebe5ca4cErik * distributed under the License is distributed on an "AS IS" BASIS,
12e5a8f94f935248dea9e515199151472eebe5ca4cErik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e5a8f94f935248dea9e515199151472eebe5ca4cErik * See the License for the specific language governing permissions and
14e5a8f94f935248dea9e515199151472eebe5ca4cErik * limitations under the License.
15e5a8f94f935248dea9e515199151472eebe5ca4cErik */
16e5a8f94f935248dea9e515199151472eebe5ca4cErik
179a3cb14e28536e4133dddbe952f47189fe344ec1Mason Tangpackage com.android.calendar.selectcalendars;
18e5a8f94f935248dea9e515199151472eebe5ca4cErik
19e5a8f94f935248dea9e515199151472eebe5ca4cErikimport com.android.calendar.R;
204acb2fd087308dea146b8b10f5278c59df387680RoboErikimport com.android.calendar.Utils;
21e5a8f94f935248dea9e515199151472eebe5ca4cErik
22e5a8f94f935248dea9e515199151472eebe5ca4cErikimport android.content.Context;
23189eaf7c678d0bd8218d667c9aa41ea640859acfErikimport android.content.res.Configuration;
24ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErikimport android.content.res.Resources;
25e5a8f94f935248dea9e515199151472eebe5ca4cErikimport android.database.Cursor;
26ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErikimport android.graphics.drawable.Drawable;
27a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Calendars;
28e5a8f94f935248dea9e515199151472eebe5ca4cErikimport android.text.TextUtils;
29975940b44da39c68805df6d08012374f3fa35969Erikimport android.view.LayoutInflater;
30e5a8f94f935248dea9e515199151472eebe5ca4cErikimport android.view.View;
31975940b44da39c68805df6d08012374f3fa35969Erikimport android.view.ViewGroup;
322aeb8d988aa4b65d3402374832613ab977e009dcMichael Chanimport android.view.ViewGroup.LayoutParams;
33975940b44da39c68805df6d08012374f3fa35969Erikimport android.widget.BaseAdapter;
342aeb8d988aa4b65d3402374832613ab977e009dcMichael Chanimport android.widget.CheckBox;
35975940b44da39c68805df6d08012374f3fa35969Erikimport android.widget.ListAdapter;
36ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErikimport android.widget.RelativeLayout;
37e5a8f94f935248dea9e515199151472eebe5ca4cErikimport android.widget.TextView;
38e5a8f94f935248dea9e515199151472eebe5ca4cErik
39975940b44da39c68805df6d08012374f3fa35969Erikpublic class SelectCalendarsSimpleAdapter extends BaseAdapter implements ListAdapter {
40ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static final String TAG = "SelectCalendarsAdapter";
41ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static int SELECTED_COLOR_CHIP_SIZE = 16;
42ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static int UNSELECTED_COLOR_CHIP_SIZE = 10;
43ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static int COLOR_CHIP_LEFT_MARGIN = 20;
44ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static int COLOR_CHIP_RIGHT_MARGIN = 8;
45ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static int COLOR_CHIP_TOP_OFFSET = 5;
46d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik    private static int BOTTOM_ITEM_HEIGHT = 64;
47d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik    private static int NORMAL_ITEM_HEIGHT = 48;
48ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik
49ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static final int IS_SELECTED = 1 << 0;
50ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static final int IS_TOP = 1 << 1;
51ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static final int IS_BOTTOM = 1 << 2;
52ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    private static final int IS_BELOW_SELECTED = 1 << 3;
53ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik
54975940b44da39c68805df6d08012374f3fa35969Erik
55975940b44da39c68805df6d08012374f3fa35969Erik    private LayoutInflater mInflater;
562aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    Resources mRes;
57975940b44da39c68805df6d08012374f3fa35969Erik    private int mLayout;
58189eaf7c678d0bd8218d667c9aa41ea640859acfErik    private int mOrientation;
59975940b44da39c68805df6d08012374f3fa35969Erik    private CalendarRow[] mData;
60975940b44da39c68805df6d08012374f3fa35969Erik    private Cursor mCursor;
61975940b44da39c68805df6d08012374f3fa35969Erik    private int mRowCount = 0;
62975940b44da39c68805df6d08012374f3fa35969Erik
63975940b44da39c68805df6d08012374f3fa35969Erik    private int mIdColumn;
64975940b44da39c68805df6d08012374f3fa35969Erik    private int mNameColumn;
65975940b44da39c68805df6d08012374f3fa35969Erik    private int mColorColumn;
66950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden    private int mVisibleColumn;
672aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private int mOwnerAccountColumn;
68b6f23cbf75cdac02a5e88c5804eb7199ea51efa3mac_wang    private static float mScale = 0;
692aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private int mColorCalendarVisible;
702aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private int mColorCalendarHidden;
712aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private int mColorCalendarSecondaryVisible;
722aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private int mColorCalendarSecondaryHidden;
73975940b44da39c68805df6d08012374f3fa35969Erik
74975940b44da39c68805df6d08012374f3fa35969Erik    private class CalendarRow {
75975940b44da39c68805df6d08012374f3fa35969Erik        long id;
76975940b44da39c68805df6d08012374f3fa35969Erik        String displayName;
772aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        String ownerAccount;
78975940b44da39c68805df6d08012374f3fa35969Erik        int color;
79975940b44da39c68805df6d08012374f3fa35969Erik        boolean selected;
80975940b44da39c68805df6d08012374f3fa35969Erik    }
81e5a8f94f935248dea9e515199151472eebe5ca4cErik
82e5a8f94f935248dea9e515199151472eebe5ca4cErik    public SelectCalendarsSimpleAdapter(Context context, int layout, Cursor c) {
83975940b44da39c68805df6d08012374f3fa35969Erik        super();
84975940b44da39c68805df6d08012374f3fa35969Erik        mLayout = layout;
85189eaf7c678d0bd8218d667c9aa41ea640859acfErik        mOrientation = context.getResources().getConfiguration().orientation;
86975940b44da39c68805df6d08012374f3fa35969Erik        initData(c);
87975940b44da39c68805df6d08012374f3fa35969Erik        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
882aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mRes = context.getResources();
892aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mColorCalendarVisible = mRes.getColor(R.color.calendar_visible);
902aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mColorCalendarHidden = mRes.getColor(R.color.calendar_hidden);
912aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mColorCalendarSecondaryVisible = mRes.getColor(R.color.calendar_secondary_visible);
922aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mColorCalendarSecondaryHidden = mRes.getColor(R.color.calendar_secondary_hidden);
932aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
94ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        if (mScale == 0) {
952aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mScale = mRes.getDisplayMetrics().density;
96ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik            SELECTED_COLOR_CHIP_SIZE *= mScale;
97ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik            UNSELECTED_COLOR_CHIP_SIZE *= mScale;
98ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik            COLOR_CHIP_LEFT_MARGIN *= mScale;
99ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik            COLOR_CHIP_RIGHT_MARGIN *= mScale;
100ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik            COLOR_CHIP_TOP_OFFSET *= mScale;
101d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            BOTTOM_ITEM_HEIGHT *= mScale;
102d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            NORMAL_ITEM_HEIGHT *= mScale;
103ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        }
104ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    }
105ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik
1062aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan    private static class TabletCalendarItemBackgrounds {
107d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik        static private int[] mBackgrounds = null;
1082aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
1092aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        /**
1102aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan         * Sets up the background drawables for the calendars list
1112aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan         *
1122aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan         * @param res The context's resources
1132aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan         */
114d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik        static int[] getBackgrounds() {
1152aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            // Not thread safe. Ok if called only from main thread
1162aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            if (mBackgrounds != null) {
1172aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                return mBackgrounds;
1182aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            }
1192aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
120d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds = new int[16];
1212aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
122d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[0] = R.drawable.calname_unselected;
1232aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
124d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_SELECTED] = R.drawable.calname_select_underunselected;
1252aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
126d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_SELECTED | IS_BOTTOM] =
127d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik                    R.drawable.calname_bottom_select_underunselected;
1282aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
1292aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mBackgrounds[IS_SELECTED | IS_BOTTOM | IS_BELOW_SELECTED] =
130d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik                    R.drawable.calname_bottom_select_underselect;
1312aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mBackgrounds[IS_SELECTED | IS_TOP | IS_BOTTOM | IS_BELOW_SELECTED] = mBackgrounds[
1322aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    IS_SELECTED | IS_BOTTOM | IS_BELOW_SELECTED];
1334a7580153fb14680612e53a2e02e06b1c68b0197RoboErik            mBackgrounds[IS_SELECTED | IS_TOP | IS_BOTTOM] = mBackgrounds[IS_SELECTED | IS_BOTTOM
1344a7580153fb14680612e53a2e02e06b1c68b0197RoboErik                    | IS_BELOW_SELECTED];
1352aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
136d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_SELECTED | IS_BELOW_SELECTED] = R.drawable.calname_select_underselect;
1372aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mBackgrounds[IS_SELECTED | IS_TOP | IS_BELOW_SELECTED] = mBackgrounds[IS_SELECTED
1382aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    | IS_BELOW_SELECTED];
139d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_SELECTED | IS_TOP] = mBackgrounds[IS_SELECTED | IS_BELOW_SELECTED];
1402aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
141d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_BOTTOM] = R.drawable.calname_bottom_unselected;
1422aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
143d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_BOTTOM | IS_BELOW_SELECTED] =
144d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik                    R.drawable.calname_bottom_unselected_underselect;
1452aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mBackgrounds[IS_TOP | IS_BOTTOM | IS_BELOW_SELECTED] = mBackgrounds[IS_BOTTOM
1462aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    | IS_BELOW_SELECTED];
1474a7580153fb14680612e53a2e02e06b1c68b0197RoboErik            mBackgrounds[IS_TOP | IS_BOTTOM] = mBackgrounds[IS_BOTTOM | IS_BELOW_SELECTED];
1482aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
149d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_BELOW_SELECTED] = R.drawable.calname_unselected_underselect;
1502aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mBackgrounds[IS_TOP | IS_BELOW_SELECTED] = mBackgrounds[IS_BELOW_SELECTED];
151d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            mBackgrounds[IS_TOP] = mBackgrounds[IS_BELOW_SELECTED];
1522aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            return mBackgrounds;
1532aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        }
154975940b44da39c68805df6d08012374f3fa35969Erik    }
155975940b44da39c68805df6d08012374f3fa35969Erik
156975940b44da39c68805df6d08012374f3fa35969Erik    private void initData(Cursor c) {
157975940b44da39c68805df6d08012374f3fa35969Erik        if (mCursor != null && c != mCursor) {
158975940b44da39c68805df6d08012374f3fa35969Erik            mCursor.close();
159975940b44da39c68805df6d08012374f3fa35969Erik        }
160975940b44da39c68805df6d08012374f3fa35969Erik        if (c == null) {
161975940b44da39c68805df6d08012374f3fa35969Erik            mCursor = c;
162975940b44da39c68805df6d08012374f3fa35969Erik            mRowCount = 0;
163975940b44da39c68805df6d08012374f3fa35969Erik            mData = null;
164975940b44da39c68805df6d08012374f3fa35969Erik            return;
165975940b44da39c68805df6d08012374f3fa35969Erik        }
166975940b44da39c68805df6d08012374f3fa35969Erik        // TODO create a broadcast listener for ACTION_PROVIDER_CHANGED to update the cursor
167975940b44da39c68805df6d08012374f3fa35969Erik        mCursor = c;
168975940b44da39c68805df6d08012374f3fa35969Erik        mIdColumn = c.getColumnIndexOrThrow(Calendars._ID);
169ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik        mNameColumn = c.getColumnIndexOrThrow(Calendars.CALENDAR_DISPLAY_NAME);
1700e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik        mColorColumn = c.getColumnIndexOrThrow(Calendars.CALENDAR_COLOR);
171950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden        mVisibleColumn = c.getColumnIndexOrThrow(Calendars.VISIBLE);
1722aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        mOwnerAccountColumn = c.getColumnIndexOrThrow(Calendars.OWNER_ACCOUNT);
173975940b44da39c68805df6d08012374f3fa35969Erik
174975940b44da39c68805df6d08012374f3fa35969Erik        mRowCount = c.getCount();
175d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik        mData = new CalendarRow[(c.getCount())];
176975940b44da39c68805df6d08012374f3fa35969Erik        c.moveToPosition(-1);
177975940b44da39c68805df6d08012374f3fa35969Erik        int p = 0;
178975940b44da39c68805df6d08012374f3fa35969Erik        while (c.moveToNext()) {
179975940b44da39c68805df6d08012374f3fa35969Erik            mData[p] = new CalendarRow();
180975940b44da39c68805df6d08012374f3fa35969Erik            mData[p].id = c.getLong(mIdColumn);
181975940b44da39c68805df6d08012374f3fa35969Erik            mData[p].displayName = c.getString(mNameColumn);
182975940b44da39c68805df6d08012374f3fa35969Erik            mData[p].color = c.getInt(mColorColumn);
183950e8a615f3dcca0b61b2eb8f8de72ecf6825d4bAndy McFadden            mData[p].selected = c.getInt(mVisibleColumn) != 0;
1842aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            mData[p].ownerAccount = c.getString(mOwnerAccountColumn);
185975940b44da39c68805df6d08012374f3fa35969Erik            p++;
186975940b44da39c68805df6d08012374f3fa35969Erik        }
187975940b44da39c68805df6d08012374f3fa35969Erik    }
188975940b44da39c68805df6d08012374f3fa35969Erik
189975940b44da39c68805df6d08012374f3fa35969Erik    public void changeCursor(Cursor c) {
190975940b44da39c68805df6d08012374f3fa35969Erik        initData(c);
191975940b44da39c68805df6d08012374f3fa35969Erik        notifyDataSetChanged();
192e5a8f94f935248dea9e515199151472eebe5ca4cErik    }
193e5a8f94f935248dea9e515199151472eebe5ca4cErik
194975940b44da39c68805df6d08012374f3fa35969Erik    public View getView(int position, View convertView, ViewGroup parent) {
195975940b44da39c68805df6d08012374f3fa35969Erik        if (position >= mRowCount) {
196975940b44da39c68805df6d08012374f3fa35969Erik            return null;
197975940b44da39c68805df6d08012374f3fa35969Erik        }
198975940b44da39c68805df6d08012374f3fa35969Erik        String name = mData[position].displayName;
199975940b44da39c68805df6d08012374f3fa35969Erik        boolean selected = mData[position].selected;
200ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik
2014acb2fd087308dea146b8b10f5278c59df387680RoboErik        int color = Utils.getDisplayColorFromColor(mData[position].color);
202975940b44da39c68805df6d08012374f3fa35969Erik        View view;
203975940b44da39c68805df6d08012374f3fa35969Erik        if (convertView == null) {
204975940b44da39c68805df6d08012374f3fa35969Erik            view = mInflater.inflate(mLayout, parent, false);
205975940b44da39c68805df6d08012374f3fa35969Erik        } else {
206975940b44da39c68805df6d08012374f3fa35969Erik            view = convertView;
207975940b44da39c68805df6d08012374f3fa35969Erik        }
208975940b44da39c68805df6d08012374f3fa35969Erik
2092aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        TextView calendarName = (TextView) view.findViewById(R.id.calendar);
2102aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        calendarName.setText(name);
2112aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
212975940b44da39c68805df6d08012374f3fa35969Erik        View colorView = view.findViewById(R.id.color);
213ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        colorView.setBackgroundColor(color);
2143937a47bc5a1afb9701e1d0565fae31f5800590dErik
2152aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        CheckBox syncCheckBox = (CheckBox) view.findViewById(R.id.sync);
2162aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        if (syncCheckBox != null) {
2172aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            // Full screen layout
2182aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            syncCheckBox.setChecked(selected);
2192aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
2202aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            int textColor;
2212aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            if (selected) {
2222aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                textColor = mColorCalendarVisible;
2232aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            } else {
2242aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                textColor = mColorCalendarHidden;
2252aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            }
2262aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            calendarName.setTextColor(textColor);
2272aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            LayoutParams layoutParam = calendarName.getLayoutParams();
2282aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
2292aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            TextView secondaryText = (TextView) view.findViewById(R.id.status);
2302aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            if (!TextUtils.isEmpty(mData[position].ownerAccount)
2312aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    && !mData[position].ownerAccount.equals(name)
2322aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    && !mData[position].ownerAccount.endsWith("calendar.google.com")) {
2332aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                int secondaryColor;
2342aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                if (selected) {
2352aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    secondaryColor = mColorCalendarSecondaryVisible;
2362aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                } else {
2372aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    secondaryColor = mColorCalendarSecondaryHidden;
2382aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                }
2392aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                secondaryText.setText(mData[position].ownerAccount);
2402aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                secondaryText.setTextColor(secondaryColor);
2412aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                secondaryText.setVisibility(View.VISIBLE);
2422aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                layoutParam.height = LayoutParams.WRAP_CONTENT;
2432aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            } else {
2442aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                secondaryText.setVisibility(View.GONE);
2452aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                layoutParam.height = LayoutParams.MATCH_PARENT;
2462aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            }
2472aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
2482aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            calendarName.setLayoutParams(layoutParam);
2492aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
2502aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        } else {
2512aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            // Tablet layout
2522aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
2532aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                    SELECTED_COLOR_CHIP_SIZE, SELECTED_COLOR_CHIP_SIZE);
2542aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            params.leftMargin = COLOR_CHIP_LEFT_MARGIN;
2552aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            params.rightMargin = COLOR_CHIP_RIGHT_MARGIN;
2562aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            // This offset is needed because the assets include the bottom of the
2572aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            // previous item
2582aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            params.topMargin = COLOR_CHIP_TOP_OFFSET;
2592aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            if (!selected) {
2602aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                params.height = UNSELECTED_COLOR_CHIP_SIZE;
2612aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                params.width = UNSELECTED_COLOR_CHIP_SIZE;
2622aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                params.leftMargin += (SELECTED_COLOR_CHIP_SIZE - UNSELECTED_COLOR_CHIP_SIZE) / 2;
2632aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan                params.topMargin += (SELECTED_COLOR_CHIP_SIZE - UNSELECTED_COLOR_CHIP_SIZE) / 2;
2642aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            }
2652aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            colorView.setLayoutParams(params);
2662aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan
2672aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            Drawable bg = getBackground(position, selected);
2682aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan            view.setBackgroundDrawable(bg);
269d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            ViewGroup.LayoutParams newParams = view.getLayoutParams();
270d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            if (position == mData.length - 1) {
271d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik                newParams.height = BOTTOM_ITEM_HEIGHT;
272d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            } else {
273d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik                newParams.height = NORMAL_ITEM_HEIGHT;
274d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            }
275d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik            view.setLayoutParams(newParams);
276f9cb58df1deab65ad426d7997fe3e3fdfeef2850RoboErik            CheckBox visibleCheckBox = (CheckBox) view.findViewById(R.id.visible_check_box);
277f9cb58df1deab65ad426d7997fe3e3fdfeef2850RoboErik            if (visibleCheckBox != null) {
278f9cb58df1deab65ad426d7997fe3e3fdfeef2850RoboErik                visibleCheckBox.setChecked(selected);
279f9cb58df1deab65ad426d7997fe3e3fdfeef2850RoboErik            }
2802aeb8d988aa4b65d3402374832613ab977e009dcMichael Chan        }
281ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        view.invalidate();
282975940b44da39c68805df6d08012374f3fa35969Erik        return view;
283e5a8f94f935248dea9e515199151472eebe5ca4cErik    }
284e5a8f94f935248dea9e515199151472eebe5ca4cErik
285ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    /**
286ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik     * @param position position of the calendar item
287ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik     * @param selected whether it is selected or not
288ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik     * @return the drawable to use for this view
289ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik     */
290ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    protected Drawable getBackground(int position, boolean selected) {
291ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        int bg;
292ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        bg = selected ? IS_SELECTED : 0;
2934a7580153fb14680612e53a2e02e06b1c68b0197RoboErik        bg |= (position == 0 && mOrientation == Configuration.ORIENTATION_LANDSCAPE) ? IS_TOP : 0;
294ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik        bg |= position == mData.length - 1 ? IS_BOTTOM : 0;
2954a7580153fb14680612e53a2e02e06b1c68b0197RoboErik        bg |= (position > 0 && mData[position - 1].selected) ? IS_BELOW_SELECTED : 0;
296d22ab07ccb2ee402cd57597eedf9105051af5c89RoboErik        return mRes.getDrawable(TabletCalendarItemBackgrounds.getBackgrounds()[bg]);
297ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik    }
298ae6215db9f10bae4baa53774b5b9cd2ddc3f066eErik
299975940b44da39c68805df6d08012374f3fa35969Erik    public int getCount() {
300975940b44da39c68805df6d08012374f3fa35969Erik        return mRowCount;
301975940b44da39c68805df6d08012374f3fa35969Erik    }
302975940b44da39c68805df6d08012374f3fa35969Erik
303975940b44da39c68805df6d08012374f3fa35969Erik    public Object getItem(int position) {
304975940b44da39c68805df6d08012374f3fa35969Erik        if (position >= mRowCount) {
305975940b44da39c68805df6d08012374f3fa35969Erik            return null;
306975940b44da39c68805df6d08012374f3fa35969Erik        }
307975940b44da39c68805df6d08012374f3fa35969Erik        CalendarRow item = mData[position];
308975940b44da39c68805df6d08012374f3fa35969Erik        return item;
309975940b44da39c68805df6d08012374f3fa35969Erik    }
310975940b44da39c68805df6d08012374f3fa35969Erik
311975940b44da39c68805df6d08012374f3fa35969Erik    public long getItemId(int position) {
312975940b44da39c68805df6d08012374f3fa35969Erik        if (position >= mRowCount) {
313975940b44da39c68805df6d08012374f3fa35969Erik            return 0;
314975940b44da39c68805df6d08012374f3fa35969Erik        }
315975940b44da39c68805df6d08012374f3fa35969Erik        return mData[position].id;
316975940b44da39c68805df6d08012374f3fa35969Erik    }
317975940b44da39c68805df6d08012374f3fa35969Erik
318975940b44da39c68805df6d08012374f3fa35969Erik    public void setVisible(int position, int visible) {
319975940b44da39c68805df6d08012374f3fa35969Erik        mData[position].selected = visible != 0;
320975940b44da39c68805df6d08012374f3fa35969Erik        notifyDataSetChanged();
321975940b44da39c68805df6d08012374f3fa35969Erik    }
322975940b44da39c68805df6d08012374f3fa35969Erik
323975940b44da39c68805df6d08012374f3fa35969Erik    public int getVisible(int position) {
324975940b44da39c68805df6d08012374f3fa35969Erik        return mData[position].selected ? 1 : 0;
325975940b44da39c68805df6d08012374f3fa35969Erik    }
326975940b44da39c68805df6d08012374f3fa35969Erik
3278d1b2fd416920c5462189ad38c2b4da96ac26b87RoboErik    @Override
3288d1b2fd416920c5462189ad38c2b4da96ac26b87RoboErik    public boolean hasStableIds() {
3298d1b2fd416920c5462189ad38c2b4da96ac26b87RoboErik        return true;
3308d1b2fd416920c5462189ad38c2b4da96ac26b87RoboErik    }
331e5a8f94f935248dea9e515199151472eebe5ca4cErik}
332