Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 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.calendar; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.Paint; |
| 22 | import android.graphics.Paint.Style; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * A custom view for a color chip for an event that can be drawn differently |
| 31 | * accroding to the event's status. |
| 32 | * |
| 33 | */ |
| 34 | public class ColorChipView extends View { |
| 35 | |
| 36 | private static final String TAG = "ColorChipView"; |
| 37 | // Style of drawing |
| 38 | // Full rectangle for accepted events |
| 39 | // Border for tentative events |
| 40 | // Cross-hatched with 50% transparency for declined events |
| 41 | |
Isaac Katzenelson | 980d530 | 2011-06-21 18:35:00 -0700 | [diff] [blame] | 42 | public static final int DRAW_FULL = 0; |
| 43 | public static final int DRAW_BORDER = 1; |
Michael Chan | aeae98b | 2011-09-09 13:06:43 -0700 | [diff] [blame] | 44 | public static final int DRAW_FADED = 2; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 45 | |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 46 | private int mDrawStyle = DRAW_FULL; |
| 47 | private float mDefStrokeWidth; |
| 48 | private Paint mPaint; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 49 | |
Isaac Katzenelson | 980d530 | 2011-06-21 18:35:00 -0700 | [diff] [blame] | 50 | private static final int DEF_BORDER_WIDTH = 4; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 51 | |
| 52 | int mBorderWidth = DEF_BORDER_WIDTH; |
| 53 | |
| 54 | int mColor; |
| 55 | |
| 56 | public ColorChipView(Context context) { |
| 57 | super(context); |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 58 | init(); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public ColorChipView(Context context, AttributeSet attrs) { |
| 62 | super(context, attrs); |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 63 | init(); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 66 | private void init() { |
| 67 | mPaint = new Paint(); |
| 68 | mDefStrokeWidth = mPaint.getStrokeWidth(); |
| 69 | mPaint.setStyle(Style.FILL_AND_STROKE); |
| 70 | } |
| 71 | |
| 72 | |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 73 | public void setDrawStyle(int style) { |
Michael Chan | aeae98b | 2011-09-09 13:06:43 -0700 | [diff] [blame] | 74 | if (style != DRAW_FULL && style != DRAW_BORDER && style != DRAW_FADED) { |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | mDrawStyle = style; |
Isaac Katzenelson | d002bd2 | 2011-06-15 18:59:59 -0700 | [diff] [blame] | 78 | invalidate(); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public void setBorderWidth(int width) { |
| 82 | if (width >= 0) { |
| 83 | mBorderWidth = width; |
Isaac Katzenelson | d002bd2 | 2011-06-15 18:59:59 -0700 | [diff] [blame] | 84 | invalidate(); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | public void setColor(int color) { |
| 89 | mColor = color; |
Isaac Katzenelson | d002bd2 | 2011-06-15 18:59:59 -0700 | [diff] [blame] | 90 | invalidate(); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onDraw(Canvas c) { |
| 95 | |
| 96 | int right = getWidth() - 1; |
| 97 | int bottom = getHeight() - 1; |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 98 | mPaint.setColor(mDrawStyle == DRAW_FADED ? |
| 99 | Utils.getDeclinedColorFromColor(mColor) : mColor); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 100 | |
| 101 | switch (mDrawStyle) { |
Michael Chan | aeae98b | 2011-09-09 13:06:43 -0700 | [diff] [blame] | 102 | case DRAW_FADED: |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 103 | case DRAW_FULL: |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 104 | mPaint.setStrokeWidth(mDefStrokeWidth); |
| 105 | c.drawRect(0, 0, right, bottom, mPaint); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 106 | break; |
| 107 | case DRAW_BORDER: |
| 108 | if (mBorderWidth <= 0) { |
| 109 | return; |
| 110 | } |
Isaac Katzenelson | 980d530 | 2011-06-21 18:35:00 -0700 | [diff] [blame] | 111 | int halfBorderWidth = mBorderWidth / 2; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 112 | int top = halfBorderWidth; |
| 113 | int left = halfBorderWidth; |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 114 | mPaint.setStrokeWidth(mBorderWidth); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 115 | |
Isaac Katzenelson | 980d530 | 2011-06-21 18:35:00 -0700 | [diff] [blame] | 116 | float[] lines = new float[16]; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 117 | int ptr = 0; |
| 118 | lines [ptr++] = 0; |
| 119 | lines [ptr++] = top; |
| 120 | lines [ptr++] = right; |
| 121 | lines [ptr++] = top; |
| 122 | lines [ptr++] = 0; |
| 123 | lines [ptr++] = bottom - halfBorderWidth; |
| 124 | lines [ptr++] = right; |
| 125 | lines [ptr++] = bottom - halfBorderWidth; |
| 126 | lines [ptr++] = left; |
| 127 | lines [ptr++] = 0; |
| 128 | lines [ptr++] = left; |
| 129 | lines [ptr++] = bottom; |
| 130 | lines [ptr++] = right - halfBorderWidth; |
| 131 | lines [ptr++] = 0; |
| 132 | lines [ptr++] = right - halfBorderWidth; |
| 133 | lines [ptr++] = bottom; |
Isaac Katzenelson | e35e485 | 2012-03-07 15:00:55 -0800 | [diff] [blame] | 134 | c.drawLines(lines, mPaint); |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 135 | break; |
Isaac Katzenelson | 0560799 | 2011-06-15 13:43:19 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |