15b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki/* 25b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * Copyright (C) 2010 The Android Open Source Project 35b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * 45b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * Licensed under the Apache License, Version 2.0 (the "License"); 55b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * you may not use this file except in compliance with the License. 65b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * You may obtain a copy of the License at 75b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * 85b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * http://www.apache.org/licenses/LICENSE-2.0 95b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * 105b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * Unless required by applicable law or agreed to in writing, software 115b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * distributed under the License is distributed on an "AS IS" BASIS, 125b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * See the License for the specific language governing permissions and 145b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * limitations under the License. 155b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki */ 165b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 175b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onukipackage com.android.email; 185b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 195b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onukiimport android.content.Context; 205b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onukiimport android.content.res.Resources; 21290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedyimport android.content.res.TypedArray; 225b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onukiimport android.graphics.Paint; 235b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 245b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki/** 255b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * Helper class to load resources. 265b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki */ 275b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onukipublic class ResourceHelper { 28290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy public final static int UNDEFINED_RESOURCE_ID = -1; 29290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy 305b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private static ResourceHelper sInstance; 315b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private final Context mContext; 325b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private final Resources mResources; 335b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 345b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private final int[] mAccountColors; 355b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private final Paint[] mAccountColorPaints; 36290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy private final TypedArray mAccountColorArray; 375b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 385b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki private ResourceHelper(Context context) { 39e1b4d2cd4a312594507a451f51d8230ef448af12Makoto Onuki mContext = context.getApplicationContext(); 405b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki mResources = mContext.getResources(); 415b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 42290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy mAccountColorArray = mResources.obtainTypedArray(R.array.combined_view_account_colors); 435b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki mAccountColors = mResources.getIntArray(R.array.combined_view_account_colors); 445b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki mAccountColorPaints = new Paint[mAccountColors.length]; 455b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki for (int i = 0; i < mAccountColors.length; i++) { 465b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki Paint p = new Paint(); 475b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki p.setColor(mAccountColors[i]); 485b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki mAccountColorPaints[i] = p; 495b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 505b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 515b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 525b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki public static synchronized ResourceHelper getInstance(Context context) { 535b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki if (sInstance == null) { 545b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki sInstance = new ResourceHelper(context); 555b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 565b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki return sInstance; 575b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 585b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 595b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki /* package */ int getAccountColorIndex(long accountId) { 605b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki // The account ID is 1-based, so -1. 615b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki // Use abs so that it'd work for -1 as well. 625b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki return Math.abs((int) ((accountId - 1) % mAccountColors.length)); 635b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 645b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 655b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki /** 665b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * @return color for an account. 675b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki */ 685b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki public int getAccountColor(long accountId) { 695b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki return mAccountColors[getAccountColorIndex(accountId)]; 705b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 715b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki 725b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki /** 73290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy * @return The resource ID for an account color. 74290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy * Otherwise, {@value #UNDEFINED_RESOURCE_ID} if color was not specified via ID. 75290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy */ 76290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy public int getAccountColorId(long accountId) { 77290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy return mAccountColorArray.getResourceId(getAccountColorIndex(accountId), 78290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy UNDEFINED_RESOURCE_ID); 79290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy } 80290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy 81290fb65595e08680a53ab77f54c21e7c9a380da0Todd Kennedy /** 825b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki * @return {@link Paint} equivalent to {@link #getAccountColor}. 835b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki */ 845b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki public Paint getAccountColorPaint(long accountId) { 855b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki return mAccountColorPaints[getAccountColorIndex(accountId)]; 865b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki } 875b81690de1ea15035ab0539df683acd8e28b0ebdMakoto Onuki} 88