blob: daf0933a6b1f7be88fef89d5fb406b6226783a8d [file] [log] [blame]
Isaac Katzenelson05607992011-06-15 13:43:19 -07001/*
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
17package com.android.calendar;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.Paint;
22import android.graphics.Paint.Style;
Isaac Katzenelson05607992011-06-15 13:43:19 -070023import android.util.AttributeSet;
Isaac Katzenelson05607992011-06-15 13:43:19 -070024import 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 */
34public 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 Katzenelson980d5302011-06-21 18:35:00 -070042 public static final int DRAW_FULL = 0;
43 public static final int DRAW_BORDER = 1;
Michael Chanaeae98b2011-09-09 13:06:43 -070044 public static final int DRAW_FADED = 2;
Isaac Katzenelson05607992011-06-15 13:43:19 -070045
Isaac Katzenelsone35e4852012-03-07 15:00:55 -080046 private int mDrawStyle = DRAW_FULL;
47 private float mDefStrokeWidth;
48 private Paint mPaint;
Isaac Katzenelson05607992011-06-15 13:43:19 -070049
Isaac Katzenelson980d5302011-06-21 18:35:00 -070050 private static final int DEF_BORDER_WIDTH = 4;
Isaac Katzenelson05607992011-06-15 13:43:19 -070051
52 int mBorderWidth = DEF_BORDER_WIDTH;
53
54 int mColor;
55
56 public ColorChipView(Context context) {
57 super(context);
Isaac Katzenelsone35e4852012-03-07 15:00:55 -080058 init();
Isaac Katzenelson05607992011-06-15 13:43:19 -070059 }
60
61 public ColorChipView(Context context, AttributeSet attrs) {
62 super(context, attrs);
Isaac Katzenelsone35e4852012-03-07 15:00:55 -080063 init();
Isaac Katzenelson05607992011-06-15 13:43:19 -070064 }
65
Isaac Katzenelsone35e4852012-03-07 15:00:55 -080066 private void init() {
67 mPaint = new Paint();
68 mDefStrokeWidth = mPaint.getStrokeWidth();
69 mPaint.setStyle(Style.FILL_AND_STROKE);
70 }
71
72
Isaac Katzenelson05607992011-06-15 13:43:19 -070073 public void setDrawStyle(int style) {
Michael Chanaeae98b2011-09-09 13:06:43 -070074 if (style != DRAW_FULL && style != DRAW_BORDER && style != DRAW_FADED) {
Isaac Katzenelson05607992011-06-15 13:43:19 -070075 return;
76 }
77 mDrawStyle = style;
Isaac Katzenelsond002bd22011-06-15 18:59:59 -070078 invalidate();
Isaac Katzenelson05607992011-06-15 13:43:19 -070079 }
80
81 public void setBorderWidth(int width) {
82 if (width >= 0) {
83 mBorderWidth = width;
Isaac Katzenelsond002bd22011-06-15 18:59:59 -070084 invalidate();
Isaac Katzenelson05607992011-06-15 13:43:19 -070085 }
86 }
87
88 public void setColor(int color) {
89 mColor = color;
Isaac Katzenelsond002bd22011-06-15 18:59:59 -070090 invalidate();
Isaac Katzenelson05607992011-06-15 13:43:19 -070091 }
92
93 @Override
94 public void onDraw(Canvas c) {
95
96 int right = getWidth() - 1;
97 int bottom = getHeight() - 1;
Isaac Katzenelsone35e4852012-03-07 15:00:55 -080098 mPaint.setColor(mDrawStyle == DRAW_FADED ?
99 Utils.getDeclinedColorFromColor(mColor) : mColor);
Isaac Katzenelson05607992011-06-15 13:43:19 -0700100
101 switch (mDrawStyle) {
Michael Chanaeae98b2011-09-09 13:06:43 -0700102 case DRAW_FADED:
Isaac Katzenelson05607992011-06-15 13:43:19 -0700103 case DRAW_FULL:
Isaac Katzenelsone35e4852012-03-07 15:00:55 -0800104 mPaint.setStrokeWidth(mDefStrokeWidth);
105 c.drawRect(0, 0, right, bottom, mPaint);
Isaac Katzenelson05607992011-06-15 13:43:19 -0700106 break;
107 case DRAW_BORDER:
108 if (mBorderWidth <= 0) {
109 return;
110 }
Isaac Katzenelson980d5302011-06-21 18:35:00 -0700111 int halfBorderWidth = mBorderWidth / 2;
Isaac Katzenelson05607992011-06-15 13:43:19 -0700112 int top = halfBorderWidth;
113 int left = halfBorderWidth;
Isaac Katzenelsone35e4852012-03-07 15:00:55 -0800114 mPaint.setStrokeWidth(mBorderWidth);
Isaac Katzenelson05607992011-06-15 13:43:19 -0700115
Isaac Katzenelson980d5302011-06-21 18:35:00 -0700116 float[] lines = new float[16];
Isaac Katzenelson05607992011-06-15 13:43:19 -0700117 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 Katzenelsone35e4852012-03-07 15:00:55 -0800134 c.drawLines(lines, mPaint);
Isaac Katzenelson05607992011-06-15 13:43:19 -0700135 break;
Isaac Katzenelson05607992011-06-15 13:43:19 -0700136 }
137 }
138}