Home | History | Annotate | Download | only in widget
      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.settings.widget;
     18 
     19 import static com.android.settings.DataUsageSummary.formatDateRange;
     20 
     21 import android.content.Context;
     22 import android.content.res.Resources;
     23 import android.content.res.TypedArray;
     24 import android.graphics.Canvas;
     25 import android.graphics.Color;
     26 import android.graphics.Paint;
     27 import android.graphics.drawable.Drawable;
     28 import android.text.Layout;
     29 import android.text.StaticLayout;
     30 import android.text.TextPaint;
     31 import android.util.AttributeSet;
     32 import android.util.TypedValue;
     33 import android.view.View;
     34 
     35 import com.android.internal.util.Preconditions;
     36 import com.android.settings.R;
     37 
     38 /**
     39  * Background of {@link ChartView} that renders grid lines as requested by
     40  * {@link ChartAxis#getTickPoints()}.
     41  */
     42 public class ChartGridView extends View {
     43 
     44     private ChartAxis mHoriz;
     45     private ChartAxis mVert;
     46 
     47     private Drawable mPrimary;
     48     private Drawable mSecondary;
     49     private Drawable mBorder;
     50     private int mLabelColor;
     51 
     52     private Layout mLayoutStart;
     53     private Layout mLayoutEnd;
     54 
     55     public ChartGridView(Context context) {
     56         this(context, null, 0);
     57     }
     58 
     59     public ChartGridView(Context context, AttributeSet attrs) {
     60         this(context, attrs, 0);
     61     }
     62 
     63     public ChartGridView(Context context, AttributeSet attrs, int defStyle) {
     64         super(context, attrs, defStyle);
     65 
     66         setWillNotDraw(false);
     67 
     68         final TypedArray a = context.obtainStyledAttributes(
     69                 attrs, R.styleable.ChartGridView, defStyle, 0);
     70 
     71         mPrimary = a.getDrawable(R.styleable.ChartGridView_primaryDrawable);
     72         mSecondary = a.getDrawable(R.styleable.ChartGridView_secondaryDrawable);
     73         mBorder = a.getDrawable(R.styleable.ChartGridView_borderDrawable);
     74         mLabelColor = a.getColor(R.styleable.ChartGridView_labelColor, Color.RED);
     75 
     76         a.recycle();
     77     }
     78 
     79     void init(ChartAxis horiz, ChartAxis vert) {
     80         mHoriz = Preconditions.checkNotNull(horiz, "missing horiz");
     81         mVert = Preconditions.checkNotNull(vert, "missing vert");
     82     }
     83 
     84     void setBounds(long start, long end) {
     85         final Context context = getContext();
     86         mLayoutStart = makeLayout(formatDateRange(context, start, start));
     87         mLayoutEnd = makeLayout(formatDateRange(context, end, end));
     88         invalidate();
     89     }
     90 
     91     @Override
     92     protected void onDraw(Canvas canvas) {
     93         final int width = getWidth();
     94         final int height = getHeight();
     95 
     96         final Drawable secondary = mSecondary;
     97         final int secondaryHeight = mSecondary.getIntrinsicHeight();
     98 
     99         final float[] vertTicks = mVert.getTickPoints();
    100         for (float y : vertTicks) {
    101             final int bottom = (int) Math.min(y + secondaryHeight, height);
    102             secondary.setBounds(0, (int) y, width, bottom);
    103             secondary.draw(canvas);
    104         }
    105 
    106         final Drawable primary = mPrimary;
    107         final int primaryWidth = mPrimary.getIntrinsicWidth();
    108         final int primaryHeight = mPrimary.getIntrinsicHeight();
    109 
    110         final float[] horizTicks = mHoriz.getTickPoints();
    111         for (float x : horizTicks) {
    112             final int right = (int) Math.min(x + primaryWidth, width);
    113             primary.setBounds((int) x, 0, right, height);
    114             primary.draw(canvas);
    115         }
    116 
    117         mBorder.setBounds(0, 0, width, height);
    118         mBorder.draw(canvas);
    119 
    120         final int padding = mLayoutStart != null ? mLayoutStart.getHeight() / 8 : 0;
    121 
    122         final Layout start = mLayoutStart;
    123         if (start != null) {
    124             canvas.save();
    125             canvas.translate(0, height + padding);
    126             start.draw(canvas);
    127             canvas.restore();
    128         }
    129 
    130         final Layout end = mLayoutEnd;
    131         if (end != null) {
    132             canvas.save();
    133             canvas.translate(width - end.getWidth(), height + padding);
    134             end.draw(canvas);
    135             canvas.restore();
    136         }
    137     }
    138 
    139     private Layout makeLayout(CharSequence text) {
    140         final Resources res = getResources();
    141         final TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    142         paint.density = res.getDisplayMetrics().density;
    143         paint.setCompatibilityScaling(res.getCompatibilityInfo().applicationScale);
    144         paint.setColor(mLabelColor);
    145         paint.setTextSize(
    146                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, res.getDisplayMetrics()));
    147 
    148         return new StaticLayout(text, paint,
    149                 (int) Math.ceil(Layout.getDesiredWidth(text, paint)),
    150                 Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
    151     }
    152 
    153 }
    154