SignalTileView.java revision 899f439a29ab1e609e7ba2aea3e9de3afef35c50
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
17package com.android.systemui.qs;
18
19import android.animation.ValueAnimator;
20import android.content.Context;
21import android.graphics.ColorFilter;
22import android.graphics.LightingColorFilter;
23import android.view.View;
24import android.widget.FrameLayout;
25import android.widget.ImageView;
26
27import com.android.systemui.R;
28import com.android.systemui.qs.QSTile.SignalState;
29
30/** View that represents a custom quick settings tile for displaying signal info (wifi/cell). **/
31public final class SignalTileView extends QSTileView {
32    private static final long DEFAULT_DURATION = new ValueAnimator().getDuration();
33    private static final long SHORT_DURATION = DEFAULT_DURATION / 3;
34    private static final ColorFilter FILTER = new LightingColorFilter(0xffffffff, 0xff283034);
35
36    private FrameLayout mIconFrame;
37    private ImageView mSignal;
38    private ImageView mOverlay;
39    private ImageView mIn;
40    private ImageView mOut;
41
42    public SignalTileView(Context context) {
43        super(context);
44
45        mIn = new ImageView(context);
46        mIn.setImageResource(R.drawable.ic_qs_signal_in);
47        mIn.setColorFilter(FILTER);
48        addView(mIn);
49
50        mOut = new ImageView(context);
51        mOut.setImageResource(R.drawable.ic_qs_signal_out);
52        mOut.setColorFilter(FILTER);
53        addView(mOut);
54    }
55
56    @Override
57    protected View createIcon() {
58        mIconFrame = new FrameLayout(mContext);
59        mSignal = new ImageView(mContext);
60        mIconFrame.addView(mSignal);
61        mOverlay = new ImageView(mContext);
62        mIconFrame.addView(mOverlay);
63        return mIconFrame;
64    }
65
66    @Override
67    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
68        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
69        int hs = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.EXACTLY);
70        int ws = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.AT_MOST);
71        mIn.measure(ws, hs);
72        mOut.measure(ws, hs);
73    }
74
75    @Override
76    protected void onLayout(boolean changed, int l, int t, int r, int b) {
77        super.onLayout(changed, l, t, r, b);
78        layoutIndicator(mIn);
79        layoutIndicator(mOut);
80    }
81
82    private void layoutIndicator(View indicator) {
83        indicator.layout(
84                mIconFrame.getRight(),
85                mIconFrame.getBottom() - indicator.getMeasuredHeight(),
86                mIconFrame.getRight() + indicator.getMeasuredWidth(),
87                mIconFrame.getBottom());
88    }
89
90    @Override
91    protected void handleStateChanged(QSTile.State state) {
92        super.handleStateChanged(state);
93        final SignalState s = (SignalState) state;
94        mSignal.setImageDrawable(null);  // force refresh
95        mSignal.setImageResource(s.iconId);
96        mSignal.setColorFilter(s.filter ? FILTER : null);
97        if (s.overlayIconId > 0) {
98            mOverlay.setVisibility(VISIBLE);
99            mOverlay.setImageDrawable(null);  // force refresh
100            mOverlay.setImageResource(s.overlayIconId);
101            mOverlay.setColorFilter(s.filter ? FILTER : null);
102        } else {
103            mOverlay.setVisibility(GONE);
104        }
105        setVisibility(mIn, s.activityIn);
106        setVisibility(mOut, s.activityOut);
107    }
108
109    private void setVisibility(View view, boolean visible) {
110        final float newAlpha = visible ? 1 : 0;
111        if (view.getAlpha() != newAlpha) {
112            view.animate()
113                .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION)
114                .alpha(newAlpha)
115                .withLayer()
116                .start();
117        }
118    }
119}