1 /* 2 * Copyright (C) 2015 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 package com.android.messaging.datamodel.media; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.graphics.Bitmap; 21 import android.graphics.Canvas; 22 import android.graphics.Color; 23 import android.graphics.Paint; 24 import android.graphics.PorterDuff; 25 import android.graphics.PorterDuffColorFilter; 26 import android.graphics.Rect; 27 import android.graphics.Typeface; 28 import android.graphics.drawable.BitmapDrawable; 29 import android.media.ExifInterface; 30 import android.text.TextUtils; 31 32 import com.android.messaging.R; 33 import com.android.messaging.util.Assert; 34 import com.android.messaging.util.AvatarUriUtil; 35 36 import java.io.IOException; 37 import java.util.List; 38 39 public class SimSelectorAvatarRequest extends AvatarRequest { 40 private static Bitmap sRegularSimIcon; 41 SimSelectorAvatarRequest(final Context context, final AvatarRequestDescriptor descriptor)42 public SimSelectorAvatarRequest(final Context context, 43 final AvatarRequestDescriptor descriptor) { 44 super(context, descriptor); 45 } 46 47 /** 48 * {@inheritDoc} 49 */ 50 @Override loadMediaInternal(List<MediaRequest<ImageResource>> chainedTasks)51 protected ImageResource loadMediaInternal(List<MediaRequest<ImageResource>> chainedTasks) 52 throws IOException { 53 Assert.isNotMainThread(); 54 final String avatarType = AvatarUriUtil.getAvatarType(mDescriptor.uri); 55 if (AvatarUriUtil.TYPE_SIM_SELECTOR_URI.equals(avatarType)){ 56 final int width = mDescriptor.desiredWidth; 57 final int height = mDescriptor.desiredHeight; 58 final String identifier = AvatarUriUtil.getIdentifier(mDescriptor.uri); 59 final boolean simSelected = AvatarUriUtil.getSimSelected(mDescriptor.uri); 60 final int simColor = AvatarUriUtil.getSimColor(mDescriptor.uri); 61 final boolean incoming = AvatarUriUtil.getSimIncoming(mDescriptor.uri); 62 return renderSimAvatarInternal(identifier, width, height, simColor, simSelected, 63 incoming); 64 } 65 return super.loadMediaInternal(chainedTasks); 66 } 67 renderSimAvatarInternal(final String identifier, final int width, final int height, final int subColor, final boolean selected, final boolean incoming)68 private ImageResource renderSimAvatarInternal(final String identifier, final int width, 69 final int height, final int subColor, final boolean selected, final boolean incoming) { 70 final Resources resources = mContext.getResources(); 71 final float halfWidth = width / 2; 72 final float halfHeight = height / 2; 73 final int minOfWidthAndHeight = Math.min(width, height); 74 final int backgroundColor = selected ? subColor : Color.WHITE; 75 final int textColor = selected ? subColor : Color.WHITE; 76 final int simColor = selected ? Color.WHITE : subColor; 77 final Bitmap bitmap = getBitmapPool().createOrReuseBitmap(width, height, backgroundColor); 78 final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 79 final Canvas canvas = new Canvas(bitmap); 80 81 if (sRegularSimIcon == null) { 82 final BitmapDrawable regularSim = (BitmapDrawable) mContext.getResources() 83 .getDrawable(R.drawable.ic_sim_card_send); 84 sRegularSimIcon = regularSim.getBitmap(); 85 } 86 87 paint.setColorFilter(new PorterDuffColorFilter(simColor, PorterDuff.Mode.SRC_ATOP)); 88 paint.setAlpha(0xff); 89 canvas.drawBitmap(sRegularSimIcon, halfWidth - sRegularSimIcon.getWidth() / 2, 90 halfHeight - sRegularSimIcon.getHeight() / 2, paint); 91 paint.setColorFilter(null); 92 paint.setAlpha(0xff); 93 94 if (!TextUtils.isEmpty(identifier)) { 95 paint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL)); 96 paint.setColor(textColor); 97 final float letterToTileRatio = 98 resources.getFraction(R.dimen.sim_identifier_to_tile_ratio, 1, 1); 99 paint.setTextSize(letterToTileRatio * minOfWidthAndHeight); 100 101 final String firstCharString = identifier.substring(0, 1).toUpperCase(); 102 final Rect textBound = new Rect(); 103 paint.getTextBounds(firstCharString, 0, 1, textBound); 104 105 final float xOffset = halfWidth - textBound.centerX(); 106 final float yOffset = halfHeight - textBound.centerY(); 107 canvas.drawText(firstCharString, xOffset, yOffset, paint); 108 } 109 110 return new DecodedImageResource(getKey(), bitmap, ExifInterface.ORIENTATION_NORMAL); 111 } 112 113 @Override getCacheId()114 public int getCacheId() { 115 return BugleMediaCacheManager.AVATAR_IMAGE_CACHE; 116 } 117 } 118