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.event; 18 19 import java.io.Serializable; 20 import java.util.ArrayList; 21 import java.util.Arrays; 22 import java.util.Comparator; 23 import java.util.HashMap; 24 import java.util.Map; 25 26 /** 27 * A cache for event colors and event color keys stored based upon calendar account name and type. 28 */ 29 public class EventColorCache implements Serializable { 30 31 private static final long serialVersionUID = 2L; 32 33 private static final String SEPARATOR = "::"; 34 35 private Map<String, ArrayList<Integer>> mColorPaletteMap; 36 private Map<String, Integer> mColorKeyMap; 37 EventColorCache()38 public EventColorCache() { 39 mColorPaletteMap = new HashMap<String, ArrayList<Integer>>(); 40 mColorKeyMap = new HashMap<String, Integer>(); 41 } 42 43 /** 44 * Inserts a color into the cache. 45 */ insertColor(String accountName, String accountType, int displayColor, int colorKey)46 public void insertColor(String accountName, String accountType, int displayColor, 47 int colorKey) { 48 mColorKeyMap.put(createKey(accountName, accountType, displayColor), colorKey); 49 String key = createKey(accountName, accountType); 50 ArrayList<Integer> colorPalette; 51 if ((colorPalette = mColorPaletteMap.get(key)) == null) { 52 colorPalette = new ArrayList<Integer>(); 53 } 54 colorPalette.add(displayColor); 55 mColorPaletteMap.put(key, colorPalette); 56 } 57 58 /** 59 * Retrieve an array of colors for a specific account name and type. 60 */ getColorArray(String accountName, String accountType)61 public int[] getColorArray(String accountName, String accountType) { 62 ArrayList<Integer> colors = mColorPaletteMap.get(createKey(accountName, accountType)); 63 if (colors == null) { 64 return null; 65 } 66 int[] ret = new int[colors.size()]; 67 for (int i = 0; i < ret.length; i++) { 68 ret[i] = colors.get(i); 69 } 70 return ret; 71 } 72 73 /** 74 * Retrieve an event color's unique key based on account name, type, and color. 75 */ getColorKey(String accountName, String accountType, int displayColor)76 public int getColorKey(String accountName, String accountType, int displayColor) { 77 return mColorKeyMap.get(createKey(accountName, accountType, displayColor)); 78 } 79 80 /** 81 * Sorts the arrays of colors based on a comparator. 82 */ sortPalettes(Comparator<Integer> comparator)83 public void sortPalettes(Comparator<Integer> comparator) { 84 for (String key : mColorPaletteMap.keySet()) { 85 ArrayList<Integer> palette = mColorPaletteMap.get(key); 86 Integer[] sortedColors = new Integer[palette.size()]; 87 Arrays.sort(palette.toArray(sortedColors), comparator); 88 palette.clear(); 89 for (Integer color : sortedColors) { 90 palette.add(color); 91 } 92 mColorPaletteMap.put(key, palette); 93 } 94 } 95 createKey(String accountName, String accountType)96 private String createKey(String accountName, String accountType) { 97 return new StringBuilder().append(accountName) 98 .append(SEPARATOR) 99 .append(accountType) 100 .toString(); 101 } 102 createKey(String accountName, String accountType, int displayColor)103 private String createKey(String accountName, String accountType, int displayColor) { 104 return new StringBuilder(createKey(accountName, accountType)) 105 .append(SEPARATOR) 106 .append(displayColor) 107 .toString(); 108 } 109 } 110