1 // Copyright 2011 Google Inc. 2 // All Rights Reserved. 3 4 package com.android.mms.ui; 5 6 import android.content.Context; 7 import android.content.res.Resources; 8 import android.graphics.Canvas; 9 import android.graphics.drawable.Drawable; 10 import android.util.AttributeSet; 11 import android.widget.ImageView; 12 import android.widget.QuickContactBadge; 13 14 import com.android.mms.R; 15 16 public class QuickContactDivot extends QuickContactBadge implements Divot{ 17 private Drawable mDrawable; 18 private int mDrawableIntrinsicWidth; 19 private int mDrawableIntrinsicHeight; 20 private int mPosition; 21 22 // The screen density. Multiple this by dips to get pixels. 23 private float mDensity; 24 QuickContactDivot(Context context, AttributeSet attrs, int defStyle)25 public QuickContactDivot(Context context, AttributeSet attrs, int defStyle) { 26 super(context, attrs, defStyle); 27 initialize(attrs); 28 } 29 QuickContactDivot(Context context, AttributeSet attrs)30 public QuickContactDivot(Context context, AttributeSet attrs) { 31 super(context, attrs); 32 initialize(attrs); 33 } 34 QuickContactDivot(Context context)35 public QuickContactDivot(Context context) { 36 super(context); 37 initialize(null); 38 } 39 initialize(AttributeSet attrs)40 private void initialize(AttributeSet attrs) { 41 if (attrs != null) { 42 mPosition = attrs.getAttributeListValue(null, "position", sPositionChoices, -1); 43 } 44 45 Resources r = getContext().getResources(); 46 mDensity = r.getDisplayMetrics().density; 47 48 setDrawable(); 49 } 50 setDrawable()51 private void setDrawable() { 52 Resources r = getContext().getResources(); 53 54 switch (mPosition) { 55 case LEFT_UPPER: 56 case LEFT_MIDDLE: 57 case LEFT_LOWER: 58 mDrawable = r.getDrawable(R.drawable.msg_bubble_right); 59 break; 60 61 case RIGHT_UPPER: 62 case RIGHT_MIDDLE: 63 case RIGHT_LOWER: 64 mDrawable = r.getDrawable(R.drawable.msg_bubble_left); 65 break; 66 67 // case TOP_LEFT: 68 // case TOP_MIDDLE: 69 // case TOP_RIGHT: 70 // mDrawable = r.getDrawable(R.drawable.msg_bubble_bottom); 71 // break; 72 // 73 // case BOTTOM_LEFT: 74 // case BOTTOM_MIDDLE: 75 // case BOTTOM_RIGHT: 76 // mDrawable = r.getDrawable(R.drawable.msg_bubble_top); 77 // break; 78 } 79 mDrawableIntrinsicWidth = mDrawable.getIntrinsicWidth(); 80 mDrawableIntrinsicHeight = mDrawable.getIntrinsicHeight(); 81 } 82 83 @Override onDraw(Canvas c)84 public void onDraw(Canvas c) { 85 super.onDraw(c); 86 c.save(); 87 computeBounds(c); 88 mDrawable.draw(c); 89 c.restore(); 90 } 91 setPosition(int position)92 public void setPosition(int position) { 93 mPosition = position; 94 setDrawable(); 95 invalidate(); 96 } 97 getPosition()98 public int getPosition() { 99 return mPosition; 100 } 101 getCloseOffset()102 public float getCloseOffset() { 103 return CORNER_OFFSET * mDensity; // multiply by density to get pixels 104 } 105 asImageView()106 public ImageView asImageView() { 107 return this; 108 } 109 assignContactFromEmail(String emailAddress)110 public void assignContactFromEmail(String emailAddress) { 111 assignContactFromEmail(emailAddress, true); 112 } 113 getFarOffset()114 public float getFarOffset() { 115 return getCloseOffset() + mDrawableIntrinsicHeight; 116 } 117 computeBounds(Canvas c)118 private void computeBounds(Canvas c) { 119 final int left = 0; 120 final int top = 0; 121 final int right = getWidth(); 122 final int middle = right / 2; 123 final int bottom = getHeight(); 124 125 final int cornerOffset = (int) getCloseOffset(); 126 127 switch (mPosition) { 128 case RIGHT_UPPER: 129 mDrawable.setBounds( 130 right - mDrawableIntrinsicWidth, 131 top + cornerOffset, 132 right, 133 top + cornerOffset + mDrawableIntrinsicHeight); 134 break; 135 136 case LEFT_UPPER: 137 mDrawable.setBounds( 138 left, 139 top + cornerOffset, 140 left + mDrawableIntrinsicWidth, 141 top + cornerOffset + mDrawableIntrinsicHeight); 142 break; 143 144 case BOTTOM_MIDDLE: 145 int halfWidth = mDrawableIntrinsicWidth / 2; 146 mDrawable.setBounds( 147 (int)(middle - halfWidth), 148 (int)(bottom - mDrawableIntrinsicHeight), 149 (int)(middle + halfWidth), 150 (int)(bottom)); 151 152 break; 153 } 154 } 155 156 } 157