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