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