SelectCalendarsSyncAdapter.java revision 4acb2fd087308dea146b8b10f5278c59df387680
16b858fc6f8afad95a075595016c5c22547924886Erik/*
26b858fc6f8afad95a075595016c5c22547924886Erik * Copyright (C) 2010 The Android Open Source Project
36b858fc6f8afad95a075595016c5c22547924886Erik *
46b858fc6f8afad95a075595016c5c22547924886Erik * Licensed under the Apache License, Version 2.0 (the "License");
56b858fc6f8afad95a075595016c5c22547924886Erik * you may not use this file except in compliance with the License.
66b858fc6f8afad95a075595016c5c22547924886Erik * You may obtain a copy of the License at
76b858fc6f8afad95a075595016c5c22547924886Erik *
86b858fc6f8afad95a075595016c5c22547924886Erik *      http://www.apache.org/licenses/LICENSE-2.0
96b858fc6f8afad95a075595016c5c22547924886Erik *
106b858fc6f8afad95a075595016c5c22547924886Erik * Unless required by applicable law or agreed to in writing, software
116b858fc6f8afad95a075595016c5c22547924886Erik * distributed under the License is distributed on an "AS IS" BASIS,
126b858fc6f8afad95a075595016c5c22547924886Erik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b858fc6f8afad95a075595016c5c22547924886Erik * See the License for the specific language governing permissions and
146b858fc6f8afad95a075595016c5c22547924886Erik * limitations under the License.
156b858fc6f8afad95a075595016c5c22547924886Erik */
166b858fc6f8afad95a075595016c5c22547924886Erik
176b858fc6f8afad95a075595016c5c22547924886Erikpackage com.android.calendar.selectcalendars;
186b858fc6f8afad95a075595016c5c22547924886Erik
196b858fc6f8afad95a075595016c5c22547924886Erikimport com.android.calendar.R;
204acb2fd087308dea146b8b10f5278c59df387680RoboErikimport com.android.calendar.Utils;
216b858fc6f8afad95a075595016c5c22547924886Erik
226b858fc6f8afad95a075595016c5c22547924886Erikimport android.content.Context;
236b858fc6f8afad95a075595016c5c22547924886Erikimport android.content.res.Resources;
246b858fc6f8afad95a075595016c5c22547924886Erikimport android.database.Cursor;
256b858fc6f8afad95a075595016c5c22547924886Erikimport android.graphics.drawable.shapes.RectShape;
26a7c0390d9c5dd4ff730de505682687fae5f5ced0RoboErikimport android.provider.CalendarContract.Calendars;
276b858fc6f8afad95a075595016c5c22547924886Erikimport android.text.TextUtils;
286b858fc6f8afad95a075595016c5c22547924886Erikimport android.view.LayoutInflater;
296b858fc6f8afad95a075595016c5c22547924886Erikimport android.view.View;
306b858fc6f8afad95a075595016c5c22547924886Erikimport android.view.ViewGroup;
316b858fc6f8afad95a075595016c5c22547924886Erikimport android.widget.BaseAdapter;
326b858fc6f8afad95a075595016c5c22547924886Erikimport android.widget.CheckBox;
336b858fc6f8afad95a075595016c5c22547924886Erikimport android.widget.CompoundButton;
346b858fc6f8afad95a075595016c5c22547924886Erikimport android.widget.ListAdapter;
356b858fc6f8afad95a075595016c5c22547924886Erikimport android.widget.TextView;
366b858fc6f8afad95a075595016c5c22547924886Erik
376b858fc6f8afad95a075595016c5c22547924886Erikimport java.util.HashMap;
386b858fc6f8afad95a075595016c5c22547924886Erik
396b858fc6f8afad95a075595016c5c22547924886Erikpublic class SelectCalendarsSyncAdapter extends BaseAdapter
406b858fc6f8afad95a075595016c5c22547924886Erik        implements ListAdapter, CompoundButton.OnCheckedChangeListener {
416b858fc6f8afad95a075595016c5c22547924886Erik    private static final String TAG = "SelCalsAdapter";
426b858fc6f8afad95a075595016c5c22547924886Erik    private static int COLOR_CHIP_SIZE = 30;
436b858fc6f8afad95a075595016c5c22547924886Erik    private RectShape r = new RectShape();
446b858fc6f8afad95a075595016c5c22547924886Erik
456b858fc6f8afad95a075595016c5c22547924886Erik    private LayoutInflater mInflater;
466b858fc6f8afad95a075595016c5c22547924886Erik    private static final int LAYOUT = R.layout.calendar_sync_item;
476b858fc6f8afad95a075595016c5c22547924886Erik    private CalendarRow[] mData;
48d17780987a421477ae57fe88e48cb2d81c42f517Erik    private HashMap<Long, CalendarRow> mChanges = new HashMap<Long, CalendarRow>();
496b858fc6f8afad95a075595016c5c22547924886Erik    private int mRowCount = 0;
506b858fc6f8afad95a075595016c5c22547924886Erik
516b858fc6f8afad95a075595016c5c22547924886Erik    private int mIdColumn;
526b858fc6f8afad95a075595016c5c22547924886Erik    private int mNameColumn;
536b858fc6f8afad95a075595016c5c22547924886Erik    private int mColorColumn;
546b858fc6f8afad95a075595016c5c22547924886Erik    private int mSyncedColumn;
556b858fc6f8afad95a075595016c5c22547924886Erik
566b858fc6f8afad95a075595016c5c22547924886Erik    private final String mSyncedString;
576b858fc6f8afad95a075595016c5c22547924886Erik    private final String mNotSyncedString;
586b858fc6f8afad95a075595016c5c22547924886Erik
596b858fc6f8afad95a075595016c5c22547924886Erik    public class CalendarRow {
606b858fc6f8afad95a075595016c5c22547924886Erik        long id;
616b858fc6f8afad95a075595016c5c22547924886Erik        String displayName;
626b858fc6f8afad95a075595016c5c22547924886Erik        int color;
636b858fc6f8afad95a075595016c5c22547924886Erik        boolean synced;
64b331a8217f09c2d48960bc192fc1cda1e6a35faeErik        boolean originalSynced;
656b858fc6f8afad95a075595016c5c22547924886Erik    }
666b858fc6f8afad95a075595016c5c22547924886Erik
676b858fc6f8afad95a075595016c5c22547924886Erik    public SelectCalendarsSyncAdapter(Context context, Cursor c) {
686b858fc6f8afad95a075595016c5c22547924886Erik        super();
696b858fc6f8afad95a075595016c5c22547924886Erik        initData(c);
706b858fc6f8afad95a075595016c5c22547924886Erik        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
716b858fc6f8afad95a075595016c5c22547924886Erik        COLOR_CHIP_SIZE *= context.getResources().getDisplayMetrics().density;
726b858fc6f8afad95a075595016c5c22547924886Erik        r.resize(COLOR_CHIP_SIZE, COLOR_CHIP_SIZE);
736b858fc6f8afad95a075595016c5c22547924886Erik        Resources res = context.getResources();
746b858fc6f8afad95a075595016c5c22547924886Erik        mSyncedString = res.getString(R.string.synced);
756b858fc6f8afad95a075595016c5c22547924886Erik        mNotSyncedString = res.getString(R.string.not_synced);
766b858fc6f8afad95a075595016c5c22547924886Erik    }
776b858fc6f8afad95a075595016c5c22547924886Erik
786b858fc6f8afad95a075595016c5c22547924886Erik    private void initData(Cursor c) {
796b858fc6f8afad95a075595016c5c22547924886Erik        if (c == null) {
806b858fc6f8afad95a075595016c5c22547924886Erik            mRowCount = 0;
816b858fc6f8afad95a075595016c5c22547924886Erik            mData = null;
826b858fc6f8afad95a075595016c5c22547924886Erik            return;
836b858fc6f8afad95a075595016c5c22547924886Erik        }
846b858fc6f8afad95a075595016c5c22547924886Erik
856b858fc6f8afad95a075595016c5c22547924886Erik        mIdColumn = c.getColumnIndexOrThrow(Calendars._ID);
86ef2add9145f058ea103ade2870a8ba899b9ed1c9RoboErik        mNameColumn = c.getColumnIndexOrThrow(Calendars.CALENDAR_DISPLAY_NAME);
870e1e62408b96e1532eb6f6a609ae4c817751aaf3RoboErik        mColorColumn = c.getColumnIndexOrThrow(Calendars.CALENDAR_COLOR);
886b858fc6f8afad95a075595016c5c22547924886Erik        mSyncedColumn = c.getColumnIndexOrThrow(Calendars.SYNC_EVENTS);
896b858fc6f8afad95a075595016c5c22547924886Erik
906b858fc6f8afad95a075595016c5c22547924886Erik        mRowCount = c.getCount();
916b858fc6f8afad95a075595016c5c22547924886Erik        mData = new CalendarRow[mRowCount];
926b858fc6f8afad95a075595016c5c22547924886Erik        c.moveToPosition(-1);
936b858fc6f8afad95a075595016c5c22547924886Erik        int p = 0;
946b858fc6f8afad95a075595016c5c22547924886Erik        while (c.moveToNext()) {
95d17780987a421477ae57fe88e48cb2d81c42f517Erik            long id = c.getLong(mIdColumn);
966b858fc6f8afad95a075595016c5c22547924886Erik            mData[p] = new CalendarRow();
97d17780987a421477ae57fe88e48cb2d81c42f517Erik            mData[p].id = id;
986b858fc6f8afad95a075595016c5c22547924886Erik            mData[p].displayName = c.getString(mNameColumn);
996b858fc6f8afad95a075595016c5c22547924886Erik            mData[p].color = c.getInt(mColorColumn);
100d17780987a421477ae57fe88e48cb2d81c42f517Erik            mData[p].originalSynced = c.getInt(mSyncedColumn) != 0;
101d17780987a421477ae57fe88e48cb2d81c42f517Erik            if (mChanges.containsKey(id)) {
102d17780987a421477ae57fe88e48cb2d81c42f517Erik                mData[p].synced = mChanges.get(id).synced;
103d17780987a421477ae57fe88e48cb2d81c42f517Erik            } else {
104d17780987a421477ae57fe88e48cb2d81c42f517Erik                mData[p].synced = mData[p].originalSynced;
105d17780987a421477ae57fe88e48cb2d81c42f517Erik            }
1066b858fc6f8afad95a075595016c5c22547924886Erik            p++;
1076b858fc6f8afad95a075595016c5c22547924886Erik        }
1086b858fc6f8afad95a075595016c5c22547924886Erik    }
1096b858fc6f8afad95a075595016c5c22547924886Erik
1106b858fc6f8afad95a075595016c5c22547924886Erik    public void changeCursor(Cursor c) {
1116b858fc6f8afad95a075595016c5c22547924886Erik        initData(c);
1126b858fc6f8afad95a075595016c5c22547924886Erik        notifyDataSetChanged();
1136b858fc6f8afad95a075595016c5c22547924886Erik    }
1146b858fc6f8afad95a075595016c5c22547924886Erik
1156b858fc6f8afad95a075595016c5c22547924886Erik    public View getView(int position, View convertView, ViewGroup parent) {
1166b858fc6f8afad95a075595016c5c22547924886Erik        if (position >= mRowCount) {
1176b858fc6f8afad95a075595016c5c22547924886Erik            return null;
1186b858fc6f8afad95a075595016c5c22547924886Erik        }
1196b858fc6f8afad95a075595016c5c22547924886Erik        String name = mData[position].displayName;
1206b858fc6f8afad95a075595016c5c22547924886Erik        boolean selected = mData[position].synced;
1214acb2fd087308dea146b8b10f5278c59df387680RoboErik        int color = Utils.getDisplayColorFromColor(mData[position].color);
1226b858fc6f8afad95a075595016c5c22547924886Erik        View view;
1236b858fc6f8afad95a075595016c5c22547924886Erik        if (convertView == null) {
1246b858fc6f8afad95a075595016c5c22547924886Erik            view = mInflater.inflate(LAYOUT, parent, false);
1256b858fc6f8afad95a075595016c5c22547924886Erik        } else {
1266b858fc6f8afad95a075595016c5c22547924886Erik            view = convertView;
1276b858fc6f8afad95a075595016c5c22547924886Erik        }
1286b858fc6f8afad95a075595016c5c22547924886Erik
1296b858fc6f8afad95a075595016c5c22547924886Erik        CheckBox cb = (CheckBox) view.findViewById(R.id.sync);
1306b858fc6f8afad95a075595016c5c22547924886Erik        // This must be set to null in case the view was recycled
1316b858fc6f8afad95a075595016c5c22547924886Erik        cb.setOnCheckedChangeListener(null);
1326b858fc6f8afad95a075595016c5c22547924886Erik        cb.setChecked(selected);
1336b858fc6f8afad95a075595016c5c22547924886Erik        cb.setTag(mData[position]);
1346b858fc6f8afad95a075595016c5c22547924886Erik        cb.setOnCheckedChangeListener(this);
1356b858fc6f8afad95a075595016c5c22547924886Erik
1366b858fc6f8afad95a075595016c5c22547924886Erik        if (selected) {
1376b858fc6f8afad95a075595016c5c22547924886Erik            setText(view, R.id.status, mSyncedString);
1386b858fc6f8afad95a075595016c5c22547924886Erik        } else {
1396b858fc6f8afad95a075595016c5c22547924886Erik            setText(view, R.id.status, mNotSyncedString);
1406b858fc6f8afad95a075595016c5c22547924886Erik        }
1416b858fc6f8afad95a075595016c5c22547924886Erik
1426b858fc6f8afad95a075595016c5c22547924886Erik        View colorView = view.findViewById(R.id.color);
1436b858fc6f8afad95a075595016c5c22547924886Erik
144ec047f19da78f4feaa5b9866846b375b454cd547Erik        colorView.setBackgroundColor(color);
1456b858fc6f8afad95a075595016c5c22547924886Erik
1466b858fc6f8afad95a075595016c5c22547924886Erik        setText(view, R.id.calendar, name);
1476b858fc6f8afad95a075595016c5c22547924886Erik        return view;
1486b858fc6f8afad95a075595016c5c22547924886Erik    }
1496b858fc6f8afad95a075595016c5c22547924886Erik
1506b858fc6f8afad95a075595016c5c22547924886Erik    private static void setText(View view, int id, String text) {
1516b858fc6f8afad95a075595016c5c22547924886Erik        if (TextUtils.isEmpty(text)) {
1526b858fc6f8afad95a075595016c5c22547924886Erik            return;
1536b858fc6f8afad95a075595016c5c22547924886Erik        }
1546b858fc6f8afad95a075595016c5c22547924886Erik        TextView textView = (TextView) view.findViewById(id);
1556b858fc6f8afad95a075595016c5c22547924886Erik        textView.setText(text);
1566b858fc6f8afad95a075595016c5c22547924886Erik    }
1576b858fc6f8afad95a075595016c5c22547924886Erik
1586b858fc6f8afad95a075595016c5c22547924886Erik    public int getCount() {
1596b858fc6f8afad95a075595016c5c22547924886Erik        return mRowCount;
1606b858fc6f8afad95a075595016c5c22547924886Erik    }
1616b858fc6f8afad95a075595016c5c22547924886Erik
1626b858fc6f8afad95a075595016c5c22547924886Erik    public Object getItem(int position) {
1636b858fc6f8afad95a075595016c5c22547924886Erik        if (position >= mRowCount) {
1646b858fc6f8afad95a075595016c5c22547924886Erik            return null;
1656b858fc6f8afad95a075595016c5c22547924886Erik        }
1666b858fc6f8afad95a075595016c5c22547924886Erik        CalendarRow item = mData[position];
1676b858fc6f8afad95a075595016c5c22547924886Erik        return item;
1686b858fc6f8afad95a075595016c5c22547924886Erik    }
1696b858fc6f8afad95a075595016c5c22547924886Erik
1706b858fc6f8afad95a075595016c5c22547924886Erik    public long getItemId(int position) {
1716b858fc6f8afad95a075595016c5c22547924886Erik        if (position >= mRowCount) {
1726b858fc6f8afad95a075595016c5c22547924886Erik            return 0;
1736b858fc6f8afad95a075595016c5c22547924886Erik        }
1746b858fc6f8afad95a075595016c5c22547924886Erik        return mData[position].id;
1756b858fc6f8afad95a075595016c5c22547924886Erik    }
1766b858fc6f8afad95a075595016c5c22547924886Erik
17746c666c2a115d0f8e805db62d3cf741c163cbaa0Erik    @Override
17846c666c2a115d0f8e805db62d3cf741c163cbaa0Erik    public boolean hasStableIds() {
17946c666c2a115d0f8e805db62d3cf741c163cbaa0Erik        return true;
18046c666c2a115d0f8e805db62d3cf741c163cbaa0Erik    }
18146c666c2a115d0f8e805db62d3cf741c163cbaa0Erik
1826b858fc6f8afad95a075595016c5c22547924886Erik    public int getSynced(int position) {
1836b858fc6f8afad95a075595016c5c22547924886Erik        return mData[position].synced ? 1 : 0;
1846b858fc6f8afad95a075595016c5c22547924886Erik    }
1856b858fc6f8afad95a075595016c5c22547924886Erik
1866b858fc6f8afad95a075595016c5c22547924886Erik    @Override
1876b858fc6f8afad95a075595016c5c22547924886Erik    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1886b858fc6f8afad95a075595016c5c22547924886Erik        CalendarRow row = (CalendarRow) buttonView.getTag();
1896b858fc6f8afad95a075595016c5c22547924886Erik        row.synced = isChecked;
190b2f986ce6f535f1d93a498e611455705f7547aecErik        if (isChecked) {
191b2f986ce6f535f1d93a498e611455705f7547aecErik            setText((View) buttonView.getParent(), R.id.status, mSyncedString);
192b2f986ce6f535f1d93a498e611455705f7547aecErik        } else {
193b2f986ce6f535f1d93a498e611455705f7547aecErik            setText((View) buttonView.getParent(), R.id.status, mNotSyncedString);
194b2f986ce6f535f1d93a498e611455705f7547aecErik        }
1956b858fc6f8afad95a075595016c5c22547924886Erik
1966b858fc6f8afad95a075595016c5c22547924886Erik        // There is some data loss in long -> int, but we should never see it in
1976b858fc6f8afad95a075595016c5c22547924886Erik        // practice regarding calendar ids.
198d17780987a421477ae57fe88e48cb2d81c42f517Erik        mChanges.put(row.id, row);
1996b858fc6f8afad95a075595016c5c22547924886Erik    }
2006b858fc6f8afad95a075595016c5c22547924886Erik
201d17780987a421477ae57fe88e48cb2d81c42f517Erik    public HashMap<Long, CalendarRow> getChanges() {
2026b858fc6f8afad95a075595016c5c22547924886Erik        return mChanges;
2036b858fc6f8afad95a075595016c5c22547924886Erik    }
2046b858fc6f8afad95a075595016c5c22547924886Erik}
205