Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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; |
| 20 | import android.content.res.Resources; |
| 21 | import android.graphics.drawable.Animatable; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.ImageView; |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 26 | import android.widget.ImageView.ScaleType; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 27 | import com.android.systemui.R; |
| 28 | |
| 29 | import java.util.Objects; |
| 30 | |
| 31 | public class QSIconView extends ViewGroup { |
| 32 | |
| 33 | private final View mIcon; |
| 34 | private final int mIconSizePx; |
| 35 | private final int mTilePaddingBelowIconPx; |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 36 | private boolean mAnimationEnabled = true; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 37 | |
| 38 | public QSIconView(Context context) { |
| 39 | super(context); |
| 40 | |
| 41 | final Resources res = context.getResources(); |
| 42 | mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size); |
| 43 | mTilePaddingBelowIconPx = res.getDimensionPixelSize(R.dimen.qs_tile_padding_below_icon); |
| 44 | |
| 45 | mIcon = createIcon(); |
| 46 | addView(mIcon); |
| 47 | } |
| 48 | |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 49 | public void disableAnimation() { |
| 50 | mAnimationEnabled = false; |
| 51 | } |
| 52 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 53 | @Override |
| 54 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 55 | final int w = MeasureSpec.getSize(widthMeasureSpec); |
| 56 | final int iconSpec = exactly(mIconSizePx); |
| 57 | mIcon.measure(MeasureSpec.makeMeasureSpec(w, getIconMeasureMode()), iconSpec); |
| 58 | setMeasuredDimension(w, mIcon.getMeasuredHeight() + mTilePaddingBelowIconPx); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 63 | final int w = getMeasuredWidth(); |
| 64 | final int h = getMeasuredHeight(); |
| 65 | int top = 0; |
| 66 | final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2; |
| 67 | layout(mIcon, iconLeft, top); |
| 68 | } |
| 69 | |
| 70 | public void setIcon(QSTile.State state) { |
| 71 | setIcon((ImageView) mIcon, state); |
| 72 | } |
| 73 | |
| 74 | protected void setIcon(ImageView iv, QSTile.State state) { |
| 75 | if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag))) { |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 76 | Drawable d = state.icon != null |
| 77 | ? iv.isShown() && mAnimationEnabled ? state.icon.getDrawable(mContext) |
| 78 | : state.icon.getInvisibleDrawable(mContext) : null; |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 79 | int padding = state.icon != null ? state.icon.getPadding() : null; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 80 | if (d != null && state.autoMirrorDrawable) { |
| 81 | d.setAutoMirrored(true); |
| 82 | } |
| 83 | iv.setImageDrawable(d); |
| 84 | iv.setTag(R.id.qs_icon_tag, state.icon); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 85 | iv.setPadding(0, padding, 0, padding); |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 86 | if (d instanceof Animatable && iv.isShown()) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 87 | Animatable a = (Animatable) d; |
Jason Monk | 66239fb | 2015-12-21 14:27:00 -0500 | [diff] [blame] | 88 | a.start(); |
| 89 | if (!iv.isShown()) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 90 | a.stop(); // skip directly to end state |
| 91 | } |
| 92 | } |
| 93 | } |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 94 | if (state.disabledByPolicy) { |
| 95 | iv.setColorFilter(getContext().getColor(R.color.qs_tile_disabled_color)); |
| 96 | } else { |
| 97 | iv.clearColorFilter(); |
| 98 | } |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | protected int getIconMeasureMode() { |
| 102 | return MeasureSpec.EXACTLY; |
| 103 | } |
| 104 | |
| 105 | protected View createIcon() { |
| 106 | final ImageView icon = new ImageView(mContext); |
| 107 | icon.setId(android.R.id.icon); |
Jason Monk | eae7c31 | 2016-02-04 13:00:48 -0500 | [diff] [blame] | 108 | icon.setScaleType(ScaleType.FIT_CENTER); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 109 | return icon; |
| 110 | } |
| 111 | |
| 112 | protected static int exactly(int size) { |
| 113 | return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); |
| 114 | } |
| 115 | |
| 116 | protected static void layout(View child, int left, int top) { |
| 117 | child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight()); |
| 118 | } |
| 119 | } |