Home | History | Annotate | Download | only in email
      1 /*
      2  * Copyright (C) 2010 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.email;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.content.res.TypedArray;
     22 import android.graphics.Paint;
     23 
     24 /**
     25  * Helper class to load resources.
     26  */
     27 public class ResourceHelper {
     28     public final static int UNDEFINED_RESOURCE_ID = -1;
     29 
     30     private static ResourceHelper sInstance;
     31     private final Context mContext;
     32     private final Resources mResources;
     33 
     34     private final int[] mAccountColors;
     35     private final Paint[] mAccountColorPaints;
     36     private final TypedArray mAccountColorArray;
     37 
     38     private ResourceHelper(Context context) {
     39         mContext = context.getApplicationContext();
     40         mResources = mContext.getResources();
     41 
     42         mAccountColorArray = mResources.obtainTypedArray(R.array.combined_view_account_colors);
     43         mAccountColors = mResources.getIntArray(R.array.combined_view_account_colors);
     44         mAccountColorPaints = new Paint[mAccountColors.length];
     45         for (int i = 0; i < mAccountColors.length; i++) {
     46             Paint p = new Paint();
     47             p.setColor(mAccountColors[i]);
     48             mAccountColorPaints[i] = p;
     49         }
     50     }
     51 
     52     public static synchronized ResourceHelper getInstance(Context context) {
     53         if (sInstance == null) {
     54             sInstance = new ResourceHelper(context);
     55         }
     56         return sInstance;
     57     }
     58 
     59     /* package */ int getAccountColorIndex(long accountId) {
     60         // The account ID is 1-based, so -1.
     61         // Use abs so that it'd work for -1 as well.
     62         return Math.abs((int) ((accountId - 1) % mAccountColors.length));
     63     }
     64 
     65     /**
     66      * @return color for an account.
     67      */
     68     public int getAccountColor(long accountId) {
     69         return mAccountColors[getAccountColorIndex(accountId)];
     70     }
     71 
     72     /**
     73      * @return The resource ID for an account color.
     74      * Otherwise, {@value #UNDEFINED_RESOURCE_ID} if color was not specified via ID.
     75      */
     76     public int getAccountColorId(long accountId) {
     77         return mAccountColorArray.getResourceId(getAccountColorIndex(accountId),
     78                 UNDEFINED_RESOURCE_ID);
     79     }
     80 
     81     /**
     82      * @return {@link Paint} equivalent to {@link #getAccountColor}.
     83      */
     84     public Paint getAccountColorPaint(long accountId) {
     85         return mAccountColorPaints[getAccountColorIndex(accountId)];
     86     }
     87 }
     88