summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/CalendarColorPickerDialog.java
blob: e72d1962d082d3f366ac4fb2ceb80f4df325b491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calendar;

import android.app.Activity;
import android.app.Dialog;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Calendars;
import android.provider.CalendarContract.Colors;
import android.util.SparseIntArray;

import com.android.colorpicker.ColorPickerDialog;
import com.android.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
import com.android.colorpicker.HsvColorComparator;

import java.util.ArrayList;
import java.util.Arrays;

public class CalendarColorPickerDialog extends ColorPickerDialog {

    private static final int NUM_COLUMNS = 4;

    private static final String KEY_CALENDAR_ID = "calendar_id";
    private static final String KEY_COLOR_KEYS = "color_keys";

    private static final int TOKEN_QUERY_CALENDARS = 1 << 1;
    private static final int TOKEN_QUERY_COLORS = 1 << 2;

    static final String[] CALENDARS_PROJECTION = new String[] {
            Calendars.ACCOUNT_NAME,
            Calendars.ACCOUNT_TYPE,
            Calendars.CALENDAR_COLOR
    };

    static final int CALENDARS_INDEX_ACCOUNT_NAME = 0;
    static final int CALENDARS_INDEX_ACCOUNT_TYPE = 1;
    static final int CALENDARS_INDEX_CALENDAR_COLOR = 2;

    static final String[] COLORS_PROJECTION = new String[] {
            Colors.COLOR,
            Colors.COLOR_KEY
    };

    static final String COLORS_WHERE = Colors.ACCOUNT_NAME + "=? AND " + Colors.ACCOUNT_TYPE +
            "=? AND " + Colors.COLOR_TYPE + "=" + Colors.TYPE_CALENDAR;

    public static final int COLORS_INDEX_COLOR = 0;
    public static final int COLORS_INDEX_COLOR_KEY = 1;


    private QueryService mService;
    private SparseIntArray mColorKeyMap = new SparseIntArray();
    private long mCalendarId;

    private class QueryService extends AsyncQueryService {

        private QueryService(Context context) {
            super(context);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            // If the query didn't return a cursor for some reason return
            if (cursor == null) {
                return;
            }

            // If the Activity is finishing, then close the cursor.
            // Otherwise, use the new cursor in the adapter.
            final Activity activity = getActivity();
            if (activity == null || activity.isFinishing()) {
                cursor.close();
                return;
            }

            switch (token) {
                case TOKEN_QUERY_CALENDARS:
                    if (!cursor.moveToFirst()) {
                        cursor.close();
                        dismiss();
                        break;
                    }
                    mSelectedColor = Utils.getDisplayColorFromColor(
                            cursor.getInt(CALENDARS_INDEX_CALENDAR_COLOR));
                    Uri uri = Colors.CONTENT_URI;
                    String[] args = new String[] {
                            cursor.getString(CALENDARS_INDEX_ACCOUNT_NAME),
                            cursor.getString(CALENDARS_INDEX_ACCOUNT_TYPE) };
                    cursor.close();
                    startQuery(TOKEN_QUERY_COLORS, null, uri, COLORS_PROJECTION, COLORS_WHERE,
                            args, null);
                    break;
                case TOKEN_QUERY_COLORS:
                    if (!cursor.moveToFirst()) {
                        cursor.close();
                        dismiss();
                        break;
                    }
                    mColorKeyMap.clear();
                    ArrayList<Integer> colors = new ArrayList<Integer>();
                    do
                    {
                        int colorKey = cursor.getInt(COLORS_INDEX_COLOR_KEY);
                        int rawColor = cursor.getInt(COLORS_INDEX_COLOR);
                        int displayColor = Utils.getDisplayColorFromColor(rawColor);
                        mColorKeyMap.put(displayColor, colorKey);
                        colors.add(displayColor);
                    } while (cursor.moveToNext());
                    Integer[] colorsToSort = colors.toArray(new Integer[colors.size()]);
                    Arrays.sort(colorsToSort, new HsvColorComparator());
                    mColors = new int[colorsToSort.length];
                    for (int i = 0; i < mColors.length; i++) {
                        mColors[i] = colorsToSort[i];
                    }
                    showPaletteView();
                    cursor.close();
                    break;
            }
        }
    }

    private class OnCalendarColorSelectedListener implements OnColorSelectedListener {

        @Override
        public void onColorSelected(int color) {
            if (color == mSelectedColor || mService == null) {
                return;
            }

            ContentValues values = new ContentValues();
            values.put(Calendars.CALENDAR_COLOR_KEY, mColorKeyMap.get(color));
            mService.startUpdate(mService.getNextToken(), null, ContentUris.withAppendedId(
                    Calendars.CONTENT_URI, mCalendarId), values, null, null, Utils.UNDO_DELAY);
        }
    }

    public CalendarColorPickerDialog() {
        // Empty constructor required for dialog fragments.
    }

    public static CalendarColorPickerDialog newInstance(long calendarId, boolean isTablet) {
        CalendarColorPickerDialog ret = new CalendarColorPickerDialog();
        ret.setArguments(R.string.calendar_color_picker_dialog_title, NUM_COLUMNS,
                isTablet ? SIZE_LARGE : SIZE_SMALL);
        ret.setCalendarId(calendarId);
        return ret;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(KEY_CALENDAR_ID, mCalendarId);
        saveColorKeys(outState);
    }

    private void saveColorKeys(Bundle outState) {
        // No color keys to save, so just return
        if (mColors == null) {
            return;
        }
        int[] colorKeys = new int[mColors.length];
        for (int i = 0; i < mColors.length; i++) {
            colorKeys[i] = mColorKeyMap.get(mColors[i]);
        }
        outState.putIntArray(KEY_COLOR_KEYS, colorKeys);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mCalendarId = savedInstanceState.getLong(KEY_CALENDAR_ID);
            retrieveColorKeys(savedInstanceState);
        }
        setOnColorSelectedListener(new OnCalendarColorSelectedListener());
    }

    private void retrieveColorKeys(Bundle savedInstanceState) {
        int[] colorKeys = savedInstanceState.getIntArray(KEY_COLOR_KEYS);
        if (mColors != null && colorKeys != null) {
            for (int i = 0; i < mColors.length; i++) {
                mColorKeyMap.put(mColors[i], colorKeys[i]);
            }
        }
    }

    @Override
    public void setColors(int[] colors) {
        throw new IllegalStateException("Must call setCalendarId() to update calendar colors");
    }

    @Override
    public void setColors(int[] colors, int selectedColor) {
        throw new IllegalStateException("Must call setCalendarId() to update calendar colors");
    }

    public void setCalendarId(long calendarId) {
        if (calendarId != mCalendarId) {
            mCalendarId = calendarId;
            startQuery();
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        mService = new QueryService(getActivity());
        if (mColors == null) {
            startQuery();
        }
        return dialog;
    }

    private void startQuery() {
        if (mService != null) {
            showProgressBarView();
            mService.startQuery(TOKEN_QUERY_CALENDARS, null,
                    ContentUris.withAppendedId(Calendars.CONTENT_URI, mCalendarId),
                    CALENDARS_PROJECTION, null, null, null);
        }
    }
}