SignalTileView.java revision af8d6c44f06d2f8baac2c5774a9efdae3fc36797
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 = new ImageView(context);
43        mIn.setImageResource(R.drawable.ic_qs_signal_in);
44        addView(mIn);
45
46        mOut = new ImageView(context);
47        mOut.setImageResource(R.drawable.ic_qs_signal_out);
48        addView(mOut);
49    }
50
51    @Override
52    protected View createIcon() {
53        mIconFrame = new FrameLayout(mContext);
54        mSignal = new ImageView(mContext);
55        mIconFrame.addView(mSignal);
56        mOverlay = new ImageView(mContext);
57        mIconFrame.addView(mOverlay);
58        return mIconFrame;
59    }
60
61    @Override
62    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
63        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
64        int hs = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.EXACTLY);
65        int ws = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.AT_MOST);
66        mIn.measure(ws, hs);
67        mOut.measure(ws, hs);
68    }
69
70    @Override
71    protected void onLayout(boolean changed, int l, int t, int r, int b) {
72        super.onLayout(changed, l, t, r, b);
73        layoutIndicator(mIn);
74        layoutIndicator(mOut);
75    }
76
77    private void layoutIndicator(View indicator) {
78        indicator.layout(
79                mIconFrame.getRight(),
80                mIconFrame.getBottom() - indicator.getMeasuredHeight(),
81                mIconFrame.getRight() + indicator.getMeasuredWidth(),
82                mIconFrame.getBottom());
83    }
84
85    @Override
86    protected void handleStateChanged(QSTile.State state) {
87        super.handleStateChanged(state);
88        final SignalState s = (SignalState) state;
89        mSignal.setImageDrawable(null);  // force refresh
90        mSignal.setImageResource(s.iconId);
91        if (s.overlayIconId > 0) {
92            mOverlay.setVisibility(VISIBLE);
93            mOverlay.setImageDrawable(null);  // force refresh
94            mOverlay.setImageResource(s.overlayIconId);
95        } else {
96            mOverlay.setVisibility(GONE);
97        }
98        setVisibility(mIn, s.activityIn);
99        setVisibility(mOut, s.activityOut);
100    }
101
102    private void setVisibility(View view, boolean visible) {
103        final float newAlpha = visible ? 1 : 0;
104        if (view.getAlpha() != newAlpha) {
105            view.animate()
106                .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION)
107                .alpha(newAlpha)
108                .withLayer()
109                .start();
110        }
111    }
112}