John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.systemui.qs; |
| 18 | |
| 19 | import android.content.Context; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 20 | import android.content.res.Configuration; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.content.res.TypedArray; |
| 23 | import android.graphics.Typeface; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 24 | import android.graphics.drawable.Animatable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 26 | import android.graphics.drawable.RippleDrawable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 30 | import android.util.MathUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 31 | import android.util.TypedValue; |
| 32 | import android.view.Gravity; |
| 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
John Spurlock | 83e2848 | 2014-05-18 17:20:05 -0400 | [diff] [blame] | 35 | import android.widget.ImageView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 36 | import android.widget.ImageView.ScaleType; |
| 37 | import android.widget.TextView; |
| 38 | |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 39 | import com.android.systemui.FontSizeUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 40 | import com.android.systemui.R; |
| 41 | import com.android.systemui.qs.QSTile.State; |
| 42 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 43 | import java.util.Objects; |
| 44 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | /** View that represents a standard quick settings tile. **/ |
| 46 | public class QSTileView extends ViewGroup { |
| 47 | private static final Typeface CONDENSED = Typeface.create("sans-serif-condensed", |
| 48 | Typeface.NORMAL); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 49 | |
| 50 | protected final Context mContext; |
| 51 | private final View mIcon; |
| 52 | private final View mDivider; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | private final H mHandler = new H(); |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 54 | private final int mIconSizePx; |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 55 | private final int mTileSpacingPx; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 56 | private int mTilePaddingTopPx; |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 57 | private final int mTilePaddingBelowIconPx; |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 58 | private final int mDualTileVerticalPaddingPx; |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 59 | private final View mTopBackgroundView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 61 | private TextView mLabel; |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 62 | private QSDualTileLabel mDualLabel; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 63 | private boolean mDual; |
| 64 | private OnClickListener mClickPrimary; |
| 65 | private OnClickListener mClickSecondary; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 66 | private OnLongClickListener mLongClick; |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 67 | private Drawable mTileBackground; |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 68 | private RippleDrawable mRipple; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | |
| 70 | public QSTileView(Context context) { |
| 71 | super(context); |
| 72 | |
| 73 | mContext = context; |
| 74 | final Resources res = context.getResources(); |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 75 | mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 76 | mTileSpacingPx = res.getDimensionPixelSize(R.dimen.qs_tile_spacing); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 77 | mTilePaddingBelowIconPx = res.getDimensionPixelSize(R.dimen.qs_tile_padding_below_icon); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 78 | mDualTileVerticalPaddingPx = |
| 79 | res.getDimensionPixelSize(R.dimen.qs_dual_tile_padding_vertical); |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 80 | mTileBackground = newTileBackground(); |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 81 | recreateLabel(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | setClipChildren(false); |
| 83 | |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 84 | mTopBackgroundView = new View(context); |
| 85 | addView(mTopBackgroundView); |
| 86 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 87 | mIcon = createIcon(); |
| 88 | addView(mIcon); |
| 89 | |
| 90 | mDivider = new View(mContext); |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 91 | mDivider.setBackgroundColor(res.getColor(R.color.qs_tile_divider)); |
| 92 | final int dh = res.getDimensionPixelSize(R.dimen.qs_tile_divider_height); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | mDivider.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, dh)); |
| 94 | addView(mDivider); |
| 95 | |
| 96 | setClickable(true); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 97 | |
| 98 | updateTopPadding(); |
| 99 | } |
| 100 | |
| 101 | private void updateTopPadding() { |
| 102 | Resources res = getResources(); |
| 103 | int padding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top); |
| 104 | int largePadding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top_large_text); |
| 105 | float largeFactor = (MathUtils.constrain(getResources().getConfiguration().fontScale, |
| 106 | 1.0f, FontSizeUtils.LARGE_TEXT_SCALE) - 1f) / (FontSizeUtils.LARGE_TEXT_SCALE - 1f); |
| 107 | mTilePaddingTopPx = Math.round((1 - largeFactor) * padding + largeFactor * largePadding); |
| 108 | requestLayout(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onConfigurationChanged(Configuration newConfig) { |
| 113 | super.onConfigurationChanged(newConfig); |
| 114 | updateTopPadding(); |
| 115 | FontSizeUtils.updateFontSize(mLabel, R.dimen.qs_tile_text_size); |
| 116 | if (mDualLabel != null) { |
| 117 | mDualLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 118 | getResources().getDimensionPixelSize(R.dimen.qs_tile_text_size)); |
| 119 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | } |
| 121 | |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 122 | private void recreateLabel() { |
| 123 | CharSequence labelText = null; |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 124 | CharSequence labelDescription = null; |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 125 | if (mLabel != null) { |
| 126 | labelText = mLabel.getText(); |
| 127 | removeView(mLabel); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 128 | mLabel = null; |
| 129 | } |
| 130 | if (mDualLabel != null) { |
| 131 | labelText = mDualLabel.getText(); |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 132 | labelDescription = mLabel.getContentDescription(); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 133 | removeView(mDualLabel); |
| 134 | mDualLabel = null; |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 135 | } |
| 136 | final Resources res = mContext.getResources(); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 137 | if (mDual) { |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 138 | mDualLabel = new QSDualTileLabel(mContext); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 139 | mDualLabel.setId(android.R.id.title); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 140 | mDualLabel.setBackgroundResource(R.drawable.btn_borderless_rect); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 141 | mDualLabel.setFirstLineCaret(res.getDrawable(R.drawable.qs_dual_tile_caret)); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 142 | mDualLabel.setTextColor(res.getColor(R.color.qs_tile_text)); |
John Spurlock | 39076ed | 2014-06-30 20:47:20 -0400 | [diff] [blame] | 143 | mDualLabel.setPadding(0, mDualTileVerticalPaddingPx, 0, mDualTileVerticalPaddingPx); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 144 | mDualLabel.setTypeface(CONDENSED); |
| 145 | mDualLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 146 | res.getDimensionPixelSize(R.dimen.qs_tile_text_size)); |
| 147 | mDualLabel.setClickable(true); |
| 148 | mDualLabel.setOnClickListener(mClickSecondary); |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 149 | mDualLabel.setFocusable(true); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 150 | if (labelText != null) { |
| 151 | mDualLabel.setText(labelText); |
| 152 | } |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 153 | if (labelDescription != null) { |
| 154 | mDualLabel.setContentDescription(labelDescription); |
| 155 | } |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 156 | addView(mDualLabel); |
| 157 | } else { |
| 158 | mLabel = new TextView(mContext); |
| 159 | mLabel.setId(android.R.id.title); |
| 160 | mLabel.setTextColor(res.getColor(R.color.qs_tile_text)); |
| 161 | mLabel.setGravity(Gravity.CENTER_HORIZONTAL); |
| 162 | mLabel.setMinLines(2); |
| 163 | mLabel.setPadding(0, 0, 0, 0); |
| 164 | mLabel.setTypeface(CONDENSED); |
| 165 | mLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 166 | res.getDimensionPixelSize(R.dimen.qs_tile_text_size)); |
| 167 | mLabel.setClickable(false); |
| 168 | if (labelText != null) { |
| 169 | mLabel.setText(labelText); |
| 170 | } |
| 171 | addView(mLabel); |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 172 | } |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 173 | } |
| 174 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 175 | public void setDual(boolean dual) { |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 176 | final boolean changed = dual != mDual; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 177 | mDual = dual; |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 178 | if (changed) { |
| 179 | recreateLabel(); |
| 180 | } |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 181 | if (mTileBackground instanceof RippleDrawable) { |
| 182 | setRipple((RippleDrawable) mTileBackground); |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 183 | } |
| 184 | if (dual) { |
| 185 | mTopBackgroundView.setOnClickListener(mClickPrimary); |
| 186 | setOnClickListener(null); |
| 187 | setClickable(false); |
| 188 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 189 | mTopBackgroundView.setBackground(mTileBackground); |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 190 | } else { |
| 191 | mTopBackgroundView.setOnClickListener(null); |
| 192 | mTopBackgroundView.setClickable(false); |
| 193 | setOnClickListener(mClickPrimary); |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 194 | setOnLongClickListener(mLongClick); |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 195 | setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 196 | setBackground(mTileBackground); |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 197 | } |
| 198 | mTopBackgroundView.setFocusable(dual); |
| 199 | setFocusable(!dual); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 200 | mDivider.setVisibility(dual ? VISIBLE : GONE); |
| 201 | postInvalidate(); |
| 202 | } |
| 203 | |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 204 | private void setRipple(RippleDrawable tileBackground) { |
| 205 | mRipple = tileBackground; |
| 206 | if (getWidth() != 0) { |
| 207 | updateRippleSize(getWidth(), getHeight()); |
| 208 | } |
| 209 | } |
| 210 | |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 211 | public void init(OnClickListener clickPrimary, OnClickListener clickSecondary, |
| 212 | OnLongClickListener longClick) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 213 | mClickPrimary = clickPrimary; |
| 214 | mClickSecondary = clickSecondary; |
John Spurlock | c247b8f | 2014-11-06 23:06:25 -0500 | [diff] [blame] | 215 | mLongClick = longClick; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | protected View createIcon() { |
John Spurlock | 83e2848 | 2014-05-18 17:20:05 -0400 | [diff] [blame] | 219 | final ImageView icon = new ImageView(mContext); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 220 | icon.setId(android.R.id.icon); |
| 221 | icon.setScaleType(ScaleType.CENTER_INSIDE); |
| 222 | return icon; |
| 223 | } |
| 224 | |
John Spurlock | 31798f1 | 2014-11-04 09:41:32 -0500 | [diff] [blame] | 225 | private Drawable newTileBackground() { |
John Spurlock | 856edeb | 2014-06-01 20:36:47 -0400 | [diff] [blame] | 226 | final int[] attrs = new int[] { android.R.attr.selectableItemBackgroundBorderless }; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 227 | final TypedArray ta = mContext.obtainStyledAttributes(attrs); |
| 228 | final Drawable d = ta.getDrawable(0); |
| 229 | ta.recycle(); |
| 230 | return d; |
| 231 | } |
| 232 | |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 233 | private View labelView() { |
| 234 | return mDual ? mDualLabel : mLabel; |
| 235 | } |
| 236 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 237 | @Override |
| 238 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 239 | final int w = MeasureSpec.getSize(widthMeasureSpec); |
| 240 | final int h = MeasureSpec.getSize(heightMeasureSpec); |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 241 | final int iconSpec = exactly(mIconSizePx); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 242 | mIcon.measure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST), iconSpec); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 243 | labelView().measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(h, MeasureSpec.AT_MOST)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 244 | if (mDual) { |
| 245 | mDivider.measure(widthMeasureSpec, exactly(mDivider.getLayoutParams().height)); |
| 246 | } |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 247 | int heightSpec = exactly( |
| 248 | mIconSizePx + mTilePaddingBelowIconPx + mTilePaddingTopPx); |
| 249 | mTopBackgroundView.measure(widthMeasureSpec, heightSpec); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 250 | setMeasuredDimension(w, h); |
| 251 | } |
| 252 | |
| 253 | private static int exactly(int size) { |
| 254 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 259 | final int w = getMeasuredWidth(); |
John Spurlock | 529aa73 | 2014-07-10 22:49:16 -0400 | [diff] [blame] | 260 | final int h = getMeasuredHeight(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 261 | |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 262 | layout(mTopBackgroundView, 0, mTileSpacingPx); |
| 263 | |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 264 | int top = 0; |
| 265 | top += mTileSpacingPx; |
| 266 | top += mTilePaddingTopPx; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 267 | final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2; |
| 268 | layout(mIcon, iconLeft, top); |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 269 | if (mRipple != null) { |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 270 | updateRippleSize(w, h); |
| 271 | |
John Spurlock | 4bf3198 | 2014-05-21 13:04:22 -0400 | [diff] [blame] | 272 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 273 | top = mIcon.getBottom(); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 274 | top += mTilePaddingBelowIconPx; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 275 | if (mDual) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 276 | layout(mDivider, 0, top); |
| 277 | top = mDivider.getBottom(); |
| 278 | } |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 279 | layout(labelView(), 0, top); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Selim Cinek | e32010a | 2014-08-20 23:50:41 +0200 | [diff] [blame] | 282 | private void updateRippleSize(int width, int height) { |
| 283 | // center the touch feedback on the center of the icon, and dial it down a bit |
| 284 | final int cx = width / 2; |
| 285 | final int cy = mDual ? mIcon.getTop() + mIcon.getHeight() / 2 : height / 2; |
| 286 | final int rad = (int)(mIcon.getHeight() * 1.25f); |
| 287 | mRipple.setHotspotBounds(cx - rad, cy - rad, cx + rad, cy + rad); |
| 288 | } |
| 289 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 290 | private static void layout(View child, int left, int top) { |
| 291 | child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight()); |
| 292 | } |
| 293 | |
| 294 | protected void handleStateChanged(QSTile.State state) { |
John Spurlock | 83e2848 | 2014-05-18 17:20:05 -0400 | [diff] [blame] | 295 | if (mIcon instanceof ImageView) { |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 296 | setIcon((ImageView) mIcon, state); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 297 | } |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 298 | if (mDual) { |
| 299 | mDualLabel.setText(state.label); |
Selim Cinek | e5557a9 | 2014-08-15 19:59:23 +0200 | [diff] [blame] | 300 | mDualLabel.setContentDescription(state.dualLabelContentDescription); |
Jason Monk | ec81d38 | 2014-09-18 16:11:09 -0400 | [diff] [blame] | 301 | mTopBackgroundView.setContentDescription(state.contentDescription); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 302 | } else { |
| 303 | mLabel.setText(state.label); |
Jason Monk | ec81d38 | 2014-09-18 16:11:09 -0400 | [diff] [blame] | 304 | setContentDescription(state.contentDescription); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 305 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 306 | } |
| 307 | |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 308 | protected void setIcon(ImageView iv, QSTile.State state) { |
| 309 | if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag))) { |
| 310 | Drawable d = state.icon != null ? state.icon.getDrawable(mContext) : null; |
| 311 | if (d != null && state.autoMirrorDrawable) { |
| 312 | d.setAutoMirrored(true); |
| 313 | } |
| 314 | iv.setImageDrawable(d); |
| 315 | iv.setTag(R.id.qs_icon_tag, state.icon); |
| 316 | if (d instanceof Animatable) { |
| 317 | if (!iv.isShown()) { |
| 318 | ((Animatable) d).stop(); // skip directly to end state |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 324 | public void onStateChanged(QSTile.State state) { |
| 325 | mHandler.obtainMessage(H.STATE_CHANGED, state).sendToTarget(); |
| 326 | } |
| 327 | |
| 328 | private class H extends Handler { |
| 329 | private static final int STATE_CHANGED = 1; |
| 330 | public H() { |
| 331 | super(Looper.getMainLooper()); |
| 332 | } |
| 333 | @Override |
| 334 | public void handleMessage(Message msg) { |
| 335 | if (msg.what == STATE_CHANGED) { |
| 336 | handleStateChanged((State) msg.obj); |
| 337 | } |
| 338 | } |
| 339 | } |
Selim Cinek | ece4a83 | 2014-08-22 15:35:53 +0000 | [diff] [blame] | 340 | } |