Home | History | Annotate | Download | only in calendar
      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     int mDrawStyle = DRAW_FULL;
     47 
     48     private static final int DEF_BORDER_WIDTH = 4;
     49 
     50     int mBorderWidth = DEF_BORDER_WIDTH;
     51 
     52     int mColor;
     53 
     54     public ColorChipView(Context context) {
     55         super(context);
     56     }
     57 
     58     public ColorChipView(Context context, AttributeSet attrs) {
     59         super(context, attrs);
     60     }
     61 
     62     public void setDrawStyle(int style) {
     63         if (style != DRAW_FULL && style != DRAW_BORDER && style != DRAW_FADED) {
     64             return;
     65         }
     66         mDrawStyle = style;
     67         invalidate();
     68     }
     69 
     70     public void setBorderWidth(int width) {
     71         if (width >= 0) {
     72             mBorderWidth = width;
     73             invalidate();
     74         }
     75     }
     76 
     77     public void setColor(int color) {
     78         mColor = color;
     79         invalidate();
     80     }
     81 
     82     @Override
     83     public void onDraw(Canvas c) {
     84 
     85         int right = getWidth() - 1;
     86         int bottom = getHeight() - 1;
     87         Paint p = new Paint();
     88         p.setColor(mDrawStyle == DRAW_FADED ? Utils.getDeclinedColorFromColor(mColor) : mColor);
     89         p.setStyle(Style.FILL_AND_STROKE);
     90 
     91         switch (mDrawStyle) {
     92             case DRAW_FADED:
     93             case DRAW_FULL:
     94                 c.drawRect(0, 0, right, bottom, p);
     95                 break;
     96             case DRAW_BORDER:
     97                 if (mBorderWidth <= 0) {
     98                     return;
     99                 }
    100                 int halfBorderWidth = mBorderWidth / 2;
    101                 int top = halfBorderWidth;
    102                 int left = halfBorderWidth;
    103                 p.setStrokeWidth(mBorderWidth);
    104 
    105                 float[] lines = new float[16];
    106                 int ptr = 0;
    107                 lines [ptr++] = 0;
    108                 lines [ptr++] = top;
    109                 lines [ptr++] = right;
    110                 lines [ptr++] = top;
    111                 lines [ptr++] = 0;
    112                 lines [ptr++] = bottom - halfBorderWidth;
    113                 lines [ptr++] = right;
    114                 lines [ptr++] = bottom - halfBorderWidth;
    115                 lines [ptr++] = left;
    116                 lines [ptr++] = 0;
    117                 lines [ptr++] = left;
    118                 lines [ptr++] = bottom;
    119                 lines [ptr++] = right - halfBorderWidth;
    120                 lines [ptr++] = 0;
    121                 lines [ptr++] = right - halfBorderWidth;
    122                 lines [ptr++] = bottom;
    123                 c.drawLines(lines, p);
    124                 break;
    125         }
    126     }
    127 }
    128