• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
26 import android.widget.ImageView.ScaleType;
27 import com.android.systemui.R;
28 
29 import java.util.Objects;
30 
31 public class QSIconView extends ViewGroup {
32 
33     protected final View mIcon;
34     protected final int mIconSizePx;
35     protected final int mTilePaddingBelowIconPx;
36     private boolean mAnimationEnabled = true;
37 
QSIconView(Context context)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 
disableAnimation()49     public void disableAnimation() {
50         mAnimationEnabled = false;
51     }
52 
getIconView()53     public View getIconView() {
54         return mIcon;
55     }
56 
57     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)58     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
59         final int w = MeasureSpec.getSize(widthMeasureSpec);
60         final int iconSpec = exactly(mIconSizePx);
61         mIcon.measure(MeasureSpec.makeMeasureSpec(w, getIconMeasureMode()), iconSpec);
62         setMeasuredDimension(w, mIcon.getMeasuredHeight() + mTilePaddingBelowIconPx);
63     }
64 
65     @Override
onLayout(boolean changed, int l, int t, int r, int b)66     protected void onLayout(boolean changed, int l, int t, int r, int b) {
67         final int w = getMeasuredWidth();
68         final int h = getMeasuredHeight();
69         int top = 0;
70         final int iconLeft = (w - mIcon.getMeasuredWidth()) / 2;
71         layout(mIcon, iconLeft, top);
72     }
73 
setIcon(QSTile.State state)74     public void setIcon(QSTile.State state) {
75         setIcon((ImageView) mIcon, state);
76     }
77 
setIcon(ImageView iv, QSTile.State state)78     protected void setIcon(ImageView iv, QSTile.State state) {
79         if (!Objects.equals(state.icon, iv.getTag(R.id.qs_icon_tag))) {
80             Drawable d = state.icon != null
81                     ? iv.isShown() && mAnimationEnabled ? state.icon.getDrawable(mContext)
82                     : state.icon.getInvisibleDrawable(mContext) : null;
83             int padding = state.icon != null ? state.icon.getPadding() : 0;
84             if (d != null && state.autoMirrorDrawable) {
85                 d.setAutoMirrored(true);
86             }
87             iv.setImageDrawable(d);
88             iv.setTag(R.id.qs_icon_tag, state.icon);
89             iv.setPadding(0, padding, 0, padding);
90             if (d instanceof Animatable && iv.isShown()) {
91                 Animatable a = (Animatable) d;
92                 a.start();
93                 if (!iv.isShown()) {
94                     a.stop(); // skip directly to end state
95                 }
96             }
97         }
98         if (state.disabledByPolicy) {
99             iv.setColorFilter(getContext().getColor(R.color.qs_tile_disabled_color));
100         } else {
101             iv.clearColorFilter();
102         }
103     }
104 
getIconMeasureMode()105     protected int getIconMeasureMode() {
106         return MeasureSpec.EXACTLY;
107     }
108 
createIcon()109     protected View createIcon() {
110         final ImageView icon = new ImageView(mContext);
111         icon.setId(android.R.id.icon);
112         icon.setScaleType(ScaleType.FIT_CENTER);
113         return icon;
114     }
115 
exactly(int size)116     protected final int exactly(int size) {
117         return MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
118     }
119 
layout(View child, int left, int top)120     protected final void layout(View child, int left, int top) {
121         child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
122     }
123 }
124