1 /*
2  * Copyright (C) 2013 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.selectcalendars;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.provider.CalendarContract.Colors;
22 
23 import com.android.calendar.AsyncQueryService;
24 
25 import java.util.HashSet;
26 
27 /**
28  * CalendarColorCache queries the provider and stores the account identifiers (name and type)
29  * of the accounts which contain optional calendar colors, and thus should allow for the
30  * user to choose calendar colors.
31  */
32 public class CalendarColorCache {
33 
34     private HashSet<String> mCache = new HashSet<String>();
35 
36     private static final String SEPARATOR = "::";
37 
38     private AsyncQueryService mService;
39     private OnCalendarColorsLoadedListener mListener;
40 
41     private StringBuffer mStringBuffer = new StringBuffer();
42 
43     private static String[] PROJECTION = new String[] {Colors.ACCOUNT_NAME, Colors.ACCOUNT_TYPE };
44 
45     /**
46      * Interface which provides callback after provider query of calendar colors.
47      */
48     public interface OnCalendarColorsLoadedListener {
49 
50         /**
51          * Callback after the set of accounts with additional calendar colors are loaded.
52          */
onCalendarColorsLoaded()53         void onCalendarColorsLoaded();
54     }
55 
CalendarColorCache(Context context, OnCalendarColorsLoadedListener listener)56     public CalendarColorCache(Context context, OnCalendarColorsLoadedListener listener) {
57         mListener = listener;
58         mService = new AsyncQueryService(context) {
59 
60             @Override
61             public void onQueryComplete(int token, Object cookie, Cursor c) {
62                 if (c == null) {
63                     return;
64                 }
65                 if (c.moveToFirst()) {
66                     clear();
67                     do {
68                         insert(c.getString(0), c.getString(1));
69                     } while (c.moveToNext());
70                     mListener.onCalendarColorsLoaded();
71                 }
72                 if (c != null) {
73                     c.close();
74                 }
75             }
76         };
77         mService.startQuery(0, null, Colors.CONTENT_URI, PROJECTION,
78                 Colors.COLOR_TYPE + "=" + Colors.TYPE_CALENDAR, null, null);
79     }
80 
81     /**
82      * Inserts a specified account into the set.
83      */
insert(String accountName, String accountType)84     private void insert(String accountName, String accountType) {
85         mCache.add(generateKey(accountName, accountType));
86     }
87 
88     /**
89      * Does a set lookup to determine if a specified account has more optional calendar colors.
90      */
hasColors(String accountName, String accountType)91     public boolean hasColors(String accountName, String accountType) {
92         return mCache.contains(generateKey(accountName, accountType));
93     }
94 
95     /**
96      * Clears the cached set.
97      */
clear()98     private void clear() {
99         mCache.clear();
100     }
101 
102     /**
103      * Generates a single key based on account name and account type for map lookup/insertion.
104      */
generateKey(String accountName, String accountType)105     private String generateKey(String accountName, String accountType) {
106         mStringBuffer.setLength(0);
107         return mStringBuffer.append(accountName).append(SEPARATOR).append(accountType).toString();
108     }
109 }
110