1 /* 2 * Copyright (C) 2018 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.example.android.intentplayground; 18 19 import android.content.ComponentName; 20 import androidx.annotation.ColorRes; 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * Assigns colors to given tasks and activities. 26 */ 27 public class ColorManager { 28 private static Map<String, Integer> mActivityColorMap = new HashMap<>(); 29 private static Map<Integer, Integer> mTaskColorMap = new HashMap<>(); 30 private static int[] mColors = new int[] { 31 R.color.md_red_500, 32 R.color.md_pink_500, 33 R.color.md_purple_500, 34 R.color.md_deep_purple_500, 35 R.color.md_indigo_500, 36 R.color.md_blue_500, 37 R.color.md_light_blue_500, 38 R.color.md_cyan_500, 39 R.color.md_green_500, 40 R.color.md_light_green_500, 41 R.color.md_lime_500, 42 R.color.md_yellow_500, 43 R.color.md_amber_500, 44 R.color.md_orange_500, 45 R.color.md_deep_orange_500, 46 R.color.md_brown_500, 47 R.color.md_blue_grey_500 48 }; 49 private static int mLastTaskColor = -1; 50 private static int mLastActivityColor = -1; 51 52 /** 53 * Retrieves the assigned color for the given activity. 54 * @param activity The activity to retrieve a color for. 55 * @return The corresponding color for this activity. 56 */ getColorForActivity(ComponentName activity)57 public static @ColorRes int getColorForActivity(ComponentName activity) { 58 String className = activity.getClassName(); 59 if (mActivityColorMap.containsKey(className)) { 60 return mActivityColorMap.get(className); 61 } else { 62 int newColor = nextActivityColor(); 63 mActivityColorMap.put(className, newColor); 64 return newColor; 65 } 66 } 67 68 /** 69 * Retrieves the assigned color for the given task. 70 * @param taskPersistentId The ID of the task to retrieve a color for. 71 * @return The corresponding color for this task. 72 */ getColorForTask(int taskPersistentId)73 public static @ColorRes int getColorForTask(int taskPersistentId) { 74 if (mTaskColorMap.containsKey(taskPersistentId)) { 75 return mTaskColorMap.get(taskPersistentId); 76 } else { 77 int newColor = nextTaskColor(); 78 mTaskColorMap.put(taskPersistentId, newColor); 79 return newColor; 80 } 81 } 82 nextTaskColor()83 private static @ColorRes int nextTaskColor() { 84 return mColors[++mLastTaskColor % mColors.length]; 85 } 86 nextActivityColor()87 private static @ColorRes int nextActivityColor() { 88 return mColors[++mLastActivityColor % mColors.length]; 89 } 90 } 91