Pankaj Garg | 07b2fd9 | 2015-07-15 12:07:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | package com.android.browser; |
| 30 | |
| 31 | import android.content.Context; |
| 32 | import android.content.res.Resources; |
| 33 | import android.content.res.TypedArray; |
| 34 | import android.graphics.Canvas; |
| 35 | import android.graphics.Paint; |
| 36 | import android.graphics.Rect; |
| 37 | import android.util.AttributeSet; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewGroup; |
| 40 | import android.widget.TextView; |
| 41 | |
| 42 | public class FolderTileView extends ViewGroup { |
| 43 | |
| 44 | // created in the constructor |
| 45 | private View mInnerLayout; |
| 46 | private TextView mTextView; |
| 47 | private TextView mLabelView; |
| 48 | private int mPaddingLeft = 0; |
| 49 | private int mPaddingTop = 0; |
| 50 | private int mPaddingRight = 0; |
| 51 | private int mPaddingBottom = 0; |
| 52 | |
| 53 | // runtime params set on Layout |
| 54 | private int mCurrentWidth; |
| 55 | private int mCurrentHeight; |
| 56 | |
| 57 | // static objects, to be recycled amongst instances (this is an optimization) |
| 58 | private static Paint sBackgroundPaint; |
| 59 | private String mText; |
| 60 | private String mLabel; |
| 61 | |
| 62 | |
| 63 | /* XML constructors */ |
| 64 | |
| 65 | public FolderTileView(Context context) { |
| 66 | super(context); |
| 67 | xmlInit(null, 0); |
| 68 | } |
| 69 | |
| 70 | public FolderTileView(Context context, AttributeSet attrs) { |
| 71 | super(context, attrs); |
| 72 | xmlInit(attrs, 0); |
| 73 | } |
| 74 | |
| 75 | public FolderTileView(Context context, AttributeSet attrs, int defStyle) { |
| 76 | super(context, attrs, defStyle); |
| 77 | xmlInit(attrs, defStyle); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /* Programmatic Constructors */ |
| 82 | |
| 83 | public FolderTileView(Context context, String text, String label) { |
| 84 | super(context); |
| 85 | mText = text; |
| 86 | mLabel = label; |
| 87 | init(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Replaces the main text of the bookmark tile |
| 93 | */ |
| 94 | public void setText(String text) { |
| 95 | mText = text; |
| 96 | if (mTextView != null) |
| 97 | mTextView.setText(mText); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Replaces the subtitle, for example "32 items" |
| 102 | */ |
| 103 | public void setLabel(String label) { |
| 104 | mLabel = label; |
| 105 | if (mLabelView != null) |
| 106 | mLabelView.setText(mLabel); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /* private stuff ahead */ |
| 111 | |
| 112 | private void xmlInit(AttributeSet attrs, int defStyle) { |
| 113 | // load attributes |
| 114 | final TypedArray a = getContext().obtainStyledAttributes(attrs, |
| 115 | R.styleable.FolderTileView, defStyle, 0); |
| 116 | |
| 117 | // saves the text for later |
| 118 | setText(a.getString(R.styleable.FolderTileView_android_text)); |
| 119 | |
| 120 | // saves the label for later |
| 121 | setLabel(a.getString(R.styleable.FolderTileView_android_label)); |
| 122 | |
| 123 | // delete attribute resolution |
| 124 | a.recycle(); |
| 125 | |
| 126 | // proceed with real initialization |
| 127 | init(); |
| 128 | } |
| 129 | |
| 130 | private void init() { |
| 131 | // create new Views for us from the XML (and automatically add them) |
| 132 | inflate(getContext(), R.layout.folder_tile_view, this); |
| 133 | |
| 134 | // we make the assumption that the XML file will always have 1 and only 1 child |
| 135 | mInnerLayout = getChildAt(0); |
| 136 | mInnerLayout.setVisibility(View.VISIBLE); |
| 137 | |
| 138 | // reference objects |
| 139 | mTextView = (TextView) mInnerLayout.findViewById(android.R.id.text1); |
| 140 | if (mText != null && !mText.isEmpty()) |
| 141 | mTextView.setText(mText); |
| 142 | mLabelView = (TextView) mInnerLayout.findViewById(android.R.id.text2); |
| 143 | if (mLabel != null && !mLabel.isEmpty()) |
| 144 | mLabelView.setText(mLabel); |
| 145 | |
| 146 | // load the common statics, also for the SiteTileView if needed |
| 147 | final Resources resources = getResources(); |
| 148 | SiteTileView.ensureCommonLoaded(resources); |
| 149 | ensureCommonLoaded(resources); |
| 150 | |
| 151 | // get the padding rect from the Tile View (to stay synced in size) |
| 152 | final Rect padding = SiteTileView.getBackgroundDrawablePadding(); |
| 153 | mPaddingLeft = padding.left; |
| 154 | mPaddingTop = padding.top; |
| 155 | mPaddingRight = padding.right; |
| 156 | mPaddingBottom = padding.bottom; |
| 157 | |
| 158 | // we'll draw our background (usually ViewGroups don't) |
| 159 | setWillNotDraw(false); |
| 160 | } |
| 161 | |
| 162 | private static void ensureCommonLoaded(Resources r) { |
| 163 | if (sBackgroundPaint != null) |
| 164 | return; |
| 165 | |
| 166 | // shared tiles background paint |
| 167 | sBackgroundPaint = new Paint(); |
| 168 | sBackgroundPaint.setColor(r.getColor(R.color.FolderTileBackground)); |
| 169 | sBackgroundPaint.setAntiAlias(true); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 174 | // layout the xml inflated contents |
| 175 | if (mInnerLayout != null) { |
| 176 | final int desiredWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 177 | final int desiredHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 178 | if (desiredHeight > 0 || desiredHeight > 0) |
| 179 | mInnerLayout.measure( |
| 180 | MeasureSpec.EXACTLY | desiredWidth - mPaddingLeft - mPaddingRight, |
| 181 | MeasureSpec.EXACTLY | desiredHeight - mPaddingTop - mPaddingBottom); |
| 182 | else |
| 183 | mInnerLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| 184 | } |
| 185 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 190 | // update current params |
| 191 | mCurrentWidth = right - left; |
| 192 | mCurrentHeight = bottom - top; |
| 193 | |
| 194 | // layout the inflated XML layout using the same Padding as the SiteTileView |
| 195 | if (mInnerLayout != null) |
| 196 | mInnerLayout.layout(mPaddingLeft, mPaddingTop, |
| 197 | mCurrentWidth - mPaddingRight, mCurrentHeight - mPaddingBottom); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | protected void onDraw(Canvas canvas) { |
| 202 | super.onDraw(canvas); |
| 203 | |
| 204 | final int left = mPaddingLeft; |
| 205 | final int top = mPaddingTop; |
| 206 | final int right = mCurrentWidth - mPaddingRight; |
| 207 | final int bottom = mCurrentHeight - mPaddingBottom; |
| 208 | |
| 209 | // draw the background rectangle |
| 210 | float roundedRadius = SiteTileView.sRoundedRadius; |
| 211 | if (roundedRadius >= 1.) { |
| 212 | SiteTileView.sRectF.set(left, top, right, bottom); |
| 213 | canvas.drawRoundRect(SiteTileView.sRectF, roundedRadius, roundedRadius, sBackgroundPaint); |
| 214 | } else |
| 215 | canvas.drawRect(left, top, right, bottom, sBackgroundPaint); |
| 216 | } |
| 217 | |
| 218 | } |