Home | History | Annotate | Download | only in bitmap
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 package com.android.mail.bitmap;
     18 
     19 import android.content.res.Resources;
     20 import android.content.res.TypedArray;
     21 
     22 import com.android.mail.R;
     23 
     24 public interface ColorPicker {
     25     /**
     26      * Returns the color to use for the given email address.
     27      * This method should return the same output for the same input.
     28      * @param email The normalized email address.
     29      * @return The color value in the format {@code 0xAARRGGBB}.
     30      */
     31     public int pickColor(final String email);
     32 
     33     /**
     34      * A simple implementation of a {@link ColorPicker}.
     35      */
     36     public class PaletteColorPicker implements ColorPicker {
     37         /**
     38          * The palette of colors, inflated from {@code R.array.letter_tile_colors}.
     39          */
     40         private static TypedArray sColors;
     41 
     42         /**
     43          * Cached value of {@code sColors.length()}.
     44          */
     45         private static int sColorCount;
     46 
     47         /**
     48          * Default color returned if the one chosen from {@code R.array.letter_tile_colors} is
     49          * a {@link android.content.res.ColorStateList}.
     50          */
     51         private static int sDefaultColor;
     52 
     53         public PaletteColorPicker(Resources res) {
     54             if (sColors == null) {
     55                 sColors = res.obtainTypedArray(R.array.letter_tile_colors);
     56                 sColorCount = sColors.length();
     57                 sDefaultColor = res.getColor(R.color.letter_tile_default_color);
     58             }
     59         }
     60 
     61         @Override
     62         public int pickColor(final String email) {
     63             final int color = Math.abs(email.hashCode()) % sColorCount;
     64             return sColors.getColor(color, sDefaultColor);
     65         }
     66     }
     67 }
     68