blob: b56ad76ec2b4d39d3d379d95c590f9d425d28212 [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;
26import com.android.systemui.R;
27
28import java.util.Objects;
29
30public class QSIconView extends ViewGroup {
31
32 private final View mIcon;
33 private final int mIconSizePx;
34 private final int mTilePaddingBelowIconPx;
35
36 public QSIconView(Context context) {
37 super(context);
38
39 final Resources res = context.getResources();
40 mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_tile_icon_size);
41 mTilePaddingBelowIconPx = res.getDimensionPixelSize(R.dimen.qs_tile_padding_below_icon);
42
43 mIcon = createIcon();
44 addView(mIcon);
45 }
46
47 @Override
48 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49 final int w = MeasureSpec.getSize(widthMeasureSpec);
50 final int iconSpec = exactly(mIconSizePx);
51 mIcon.measure(MeasureSpec.makeMeasureSpec(w, getIconMeasureMode()), iconSpec);
52 setMeasuredDimension(w, mIcon.getMeasuredHeight() + mTilePaddingBelowIconPx);
53 }
54
55 @Override
56 protected void onLayout(boolean changed, int l, int t, int r, int b) {
57 final int w = getMeasuredWidth();
58 final int h = getMeasuredHeight();
59 int top = 0;
60 final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2;
61 layout(mIcon, iconLeft, top);
62 }
63
64 public void setIcon(QSTile.State state) {
65 setIcon((ImageView) mIcon, state);
66 }
67
68 protected void setIcon(ImageView iv, QSTile.State state) {
69 if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag))) {
70 Drawable d = state.icon != null ? state.icon.getDrawable(mContext) : null;
71 if (d != null && state.autoMirrorDrawable) {
72 d.setAutoMirrored(true);
73 }
74 iv.setImageDrawable(d);
75 iv.setTag(R.id.qs_icon_tag, state.icon);
76 if (d instanceof Animatable) {
77 Animatable a = (Animatable) d;
78 if (state.icon instanceof QSTile.AnimationIcon && !iv.isShown()) {
79 a.stop(); // skip directly to end state
80 }
81 }
82 }
83
84 }
85
86 protected int getIconMeasureMode() {
87 return MeasureSpec.EXACTLY;
88 }
89
90 protected View createIcon() {
91 final ImageView icon = new ImageView(mContext);
92 icon.setId(android.R.id.icon);
93 icon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
94 return icon;
95 }
96
97 protected static int exactly(int size) {
98 return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
99 }
100
101 protected static void layout(View child, int left, int top) {
102 child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
103 }
104}