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; 23 import android.util.AttributeSet; 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 42 public static final int DRAW_FULL = 0; 43 public static final int DRAW_BORDER = 1; 44 public static final int DRAW_FADED = 2; 45 46 private int mDrawStyle = DRAW_FULL; 47 private float mDefStrokeWidth; 48 private Paint mPaint; 49 50 private static final int DEF_BORDER_WIDTH = 4; 51 52 int mBorderWidth = DEF_BORDER_WIDTH; 53 54 int mColor; 55 ColorChipView(Context context)56 public ColorChipView(Context context) { 57 super(context); 58 init(); 59 } 60 ColorChipView(Context context, AttributeSet attrs)61 public ColorChipView(Context context, AttributeSet attrs) { 62 super(context, attrs); 63 init(); 64 } 65 init()66 private void init() { 67 mPaint = new Paint(); 68 mDefStrokeWidth = mPaint.getStrokeWidth(); 69 mPaint.setStyle(Style.FILL_AND_STROKE); 70 } 71 72 setDrawStyle(int style)73 public void setDrawStyle(int style) { 74 if (style != DRAW_FULL && style != DRAW_BORDER && style != DRAW_FADED) { 75 return; 76 } 77 mDrawStyle = style; 78 invalidate(); 79 } 80 setBorderWidth(int width)81 public void setBorderWidth(int width) { 82 if (width >= 0) { 83 mBorderWidth = width; 84 invalidate(); 85 } 86 } 87 setColor(int color)88 public void setColor(int color) { 89 mColor = color; 90 invalidate(); 91 } 92 93 @Override onDraw(Canvas c)94 public void onDraw(Canvas c) { 95 96 int right = getWidth() - 1; 97 int bottom = getHeight() - 1; 98 mPaint.setColor(mDrawStyle == DRAW_FADED ? 99 Utils.getDeclinedColorFromColor(mColor) : mColor); 100 101 switch (mDrawStyle) { 102 case DRAW_FADED: 103 case DRAW_FULL: 104 mPaint.setStrokeWidth(mDefStrokeWidth); 105 c.drawRect(0, 0, right, bottom, mPaint); 106 break; 107 case DRAW_BORDER: 108 if (mBorderWidth <= 0) { 109 return; 110 } 111 int halfBorderWidth = mBorderWidth / 2; 112 int top = halfBorderWidth; 113 int left = halfBorderWidth; 114 mPaint.setStrokeWidth(mBorderWidth); 115 116 float[] lines = new float[16]; 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; 134 c.drawLines(lines, mPaint); 135 break; 136 } 137 } 138 } 139