blob: 546f8c3c95af5359596558b2fc3a6686dea15418 [file] [log] [blame]
Jason Monkdc35dcb2015-12-04 16:36:15 -05001/*
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
17package com.android.systemui.qs;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.drawable.Animatable;
22import android.graphics.drawable.Drawable;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageView;
Jason Monkeae7c312016-02-04 13:00:48 -050026import android.widget.ImageView.ScaleType;
Jason Monkdc35dcb2015-12-04 16:36:15 -050027import com.android.systemui.R;
28
29import java.util.Objects;
30
31public class QSIconView extends ViewGroup {
32
33 private final View mIcon;
34 private final int mIconSizePx;
35 private final int mTilePaddingBelowIconPx;
Jason Monk1aec93f2016-03-01 09:39:30 -050036 private boolean mAnimationEnabled = true;
Jason Monkdc35dcb2015-12-04 16:36:15 -050037
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 Monk1aec93f2016-03-01 09:39:30 -050049 public void disableAnimation() {
50 mAnimationEnabled = false;
51 }
52
Jason Monkdc35dcb2015-12-04 16:36:15 -050053 @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 Monk1aec93f2016-03-01 09:39:30 -050076 Drawable d = state.icon != null
77 ? iv.isShown() && mAnimationEnabled ? state.icon.getDrawable(mContext)
78 : state.icon.getInvisibleDrawable(mContext) : null;
Jason Monkb53b6c52016-02-24 17:25:49 -050079 int padding = state.icon != null ? state.icon.getPadding() : null;
Jason Monkdc35dcb2015-12-04 16:36:15 -050080 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 Monkb53b6c52016-02-24 17:25:49 -050085 iv.setPadding(0, padding, 0, padding);
Jason Monk1aec93f2016-03-01 09:39:30 -050086 if (d instanceof Animatable && iv.isShown()) {
Jason Monkdc35dcb2015-12-04 16:36:15 -050087 Animatable a = (Animatable) d;
Jason Monk66239fb2015-12-21 14:27:00 -050088 a.start();
89 if (!iv.isShown()) {
Jason Monkdc35dcb2015-12-04 16:36:15 -050090 a.stop(); // skip directly to end state
91 }
92 }
93 }
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000094 if (state.disabledByPolicy) {
95 iv.setColorFilter(getContext().getColor(R.color.qs_tile_disabled_color));
96 } else {
97 iv.clearColorFilter();
98 }
Jason Monkdc35dcb2015-12-04 16:36:15 -050099 }
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 Monkeae7c312016-02-04 13:00:48 -0500108 icon.setScaleType(ScaleType.FIT_CENTER);
Jason Monkdc35dcb2015-12-04 16:36:15 -0500109 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}