blob: 5fba23c34a677bd274bb85384566f0107bb02502 [file] [log] [blame]
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.mail.photomanager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.LruCache;
import android.util.TypedValue;
import com.android.mail.R;
import com.android.mail.photomanager.ContactPhotoManager.DefaultImageProvider;
import com.android.mail.ui.DividedImageCanvas;
import com.google.common.base.Objects;
/**
* LetterTileProvider is an implementation of the DefaultImageProvider. When no
* matching contact photo is found, and there is a supplied displayName or email
* address whose first letter corresponds to an English alphabet letter (or
* number), this method creates a bitmap with the letter in the center of a
* tile. If there is no English alphabet character (or digit), it creates a
* bitmap with the default contact avatar.
*/
public class LetterTileProvider extends DefaultImageProvider {
private Bitmap mDefaultBitmap;
private final LruCache<Integer, Bitmap> mTileBitmapCache;
private static int sTilePaddingBottom;
private static int sTilePaddingLeft;
private static int sTileLetterFontSize = -1;
private static TextPaint sPaint = new TextPaint();
private static int DEFAULT_AVATAR_DRAWABLE = R.drawable.ic_contact_picture;
private static final Pattern ALPHABET = Pattern.compile("^[a-zA-Z]+$");
public LetterTileProvider() {
super();
final float cacheSizeAdjustment =
(MemoryUtils.getTotalMemorySize() >= MemoryUtils.LARGE_RAM_THRESHOLD) ?
1.0f : 0.5f;
final int bitmapCacheSize = (int) (cacheSizeAdjustment * 26);
mTileBitmapCache = new LruCache<Integer, Bitmap>(bitmapCacheSize);
}
@Override
public void applyDefaultImage(String displayName, String address, DividedImageCanvas view,
int extent) {
Bitmap bitmap = null;
// TODO: show just the default gray contact photo for now
if (false) {
final String display = !TextUtils.isEmpty(displayName) ? displayName : address;
final String firstChar = display.substring(0, 1);
// If its a valid english alphabet letter...
if (isLetter(firstChar)) {
final String first = firstChar.toUpperCase();
DividedImageCanvas.Dimensions d = view.getDesiredDimensions(address);
int hash = Objects.hashCode(first, d);
bitmap = mTileBitmapCache.get(hash);
if (bitmap == null) {
// Create bitmap based on the first char
bitmap = Bitmap.createBitmap(d.width, d.height, Bitmap.Config.ARGB_8888);
sPaint.setColor(Color.BLACK);
if (sTileLetterFontSize == -1) {
final Resources res = view.getContext().getResources();
sTileLetterFontSize = res
.getDimensionPixelSize(R.dimen.tile_letter_font_size);
sTilePaddingBottom = res
.getDimensionPixelSize(R.dimen.tile_letter_padding_bottom);
sTilePaddingLeft = res
.getDimensionPixelSize(R.dimen.tile_letter_padding_left);
}
sPaint.setTextSize(sTileLetterFontSize * d.scale);
sPaint.setTypeface(Typeface.DEFAULT);
Canvas c = new Canvas(bitmap);
c.drawColor(Color.GRAY);
c.drawText(first, d.scale * sTilePaddingLeft, d.height
- (sTilePaddingBottom * d.scale), sPaint);
mTileBitmapCache.put(hash, bitmap);
}
}
} else {
if (mDefaultBitmap == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
mDefaultBitmap = BitmapFactory.decodeResource(view.getContext().getResources(),
DEFAULT_AVATAR_DRAWABLE, options);
}
bitmap = mDefaultBitmap;
}
view.addDivisionImage(bitmap, address);
}
private boolean isLetter(String letter) {
Matcher m = ALPHABET.matcher(letter);
return m.matches();
}
}