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.drawable.Drawable;
22import android.view.View;
23import android.widget.FrameLayout;
24import android.widget.ImageView;
25
26import com.android.systemui.R;
27import com.android.systemui.plugins.qs.QSTile;
28import com.android.systemui.plugins.qs.QSTile.SignalState;
29import com.android.systemui.qs.tileimpl.QSIconViewImpl;
30import com.android.systemui.qs.tileimpl.SlashImageView;
31
32/** View that represents a custom quick settings tile for displaying signal info (wifi/cell). **/
33public class SignalTileView extends QSIconViewImpl {
34    private static final long DEFAULT_DURATION = new ValueAnimator().getDuration();
35    private static final long SHORT_DURATION = DEFAULT_DURATION / 3;
36
37    protected FrameLayout mIconFrame;
38    protected ImageView mSignal;
39    private ImageView mOverlay;
40    private ImageView mIn;
41    private ImageView mOut;
42
43    private int mWideOverlayIconStartPadding;
44
45    public SignalTileView(Context context) {
46        super(context);
47
48        mIn = addTrafficView(R.drawable.ic_qs_signal_in);
49        mOut = addTrafficView(R.drawable.ic_qs_signal_out);
50
51        mWideOverlayIconStartPadding = context.getResources().getDimensionPixelSize(
52                R.dimen.wide_type_icon_start_padding_qs);
53    }
54
55    private ImageView addTrafficView(int icon) {
56        final ImageView traffic = new ImageView(mContext);
57        traffic.setImageResource(icon);
58        traffic.setAlpha(0f);
59        addView(traffic);
60        return traffic;
61    }
62
63    @Override
64    protected View createIcon() {
65        mIconFrame = new FrameLayout(mContext);
66        mSignal = createSlashImageView(mContext);
67        mIconFrame.addView(mSignal);
68        mOverlay = new ImageView(mContext);
69        mIconFrame.addView(mOverlay, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
70        return mIconFrame;
71    }
72
73    protected SlashImageView createSlashImageView(Context context) {
74        return new SlashImageView(context);
75    }
76
77    @Override
78    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
79        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
80        int hs = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.EXACTLY);
81        int ws = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.AT_MOST);
82        mIn.measure(ws, hs);
83        mOut.measure(ws, hs);
84    }
85
86    @Override
87    protected void onLayout(boolean changed, int l, int t, int r, int b) {
88        super.onLayout(changed, l, t, r, b);
89        layoutIndicator(mIn);
90        layoutIndicator(mOut);
91    }
92
93    @Override
94    protected int getIconMeasureMode() {
95        return MeasureSpec.AT_MOST;
96    }
97
98    private void layoutIndicator(View indicator) {
99        boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
100        int left, right;
101        if (isRtl) {
102            right = mIconFrame.getLeft();
103            left = right - indicator.getMeasuredWidth();
104        } else {
105            left = mIconFrame.getRight();
106            right = left + indicator.getMeasuredWidth();
107        }
108        indicator.layout(
109                left,
110                mIconFrame.getBottom() - indicator.getMeasuredHeight(),
111                right,
112                mIconFrame.getBottom());
113    }
114
115    @Override
116    public void setIcon(QSTile.State state) {
117        final SignalState s = (SignalState) state;
118        setIcon(mSignal, s);
119
120        if (s.overlayIconId > 0) {
121            mOverlay.setVisibility(VISIBLE);
122            mOverlay.setImageResource(s.overlayIconId);
123        } else {
124            mOverlay.setVisibility(GONE);
125        }
126        if (s.overlayIconId > 0 && s.isOverlayIconWide) {
127            mSignal.setPaddingRelative(mWideOverlayIconStartPadding, 0, 0, 0);
128        } else {
129            mSignal.setPaddingRelative(0, 0, 0, 0);
130        }
131        final boolean shown = isShown();
132        setVisibility(mIn, shown, s.activityIn);
133        setVisibility(mOut, shown, s.activityOut);
134    }
135
136    private void setVisibility(View view, boolean shown, boolean visible) {
137        final float newAlpha = shown && visible ? 1 : 0;
138        if (view.getAlpha() == newAlpha) return;
139        if (shown) {
140            view.animate()
141                .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION)
142                .alpha(newAlpha)
143                .start();
144        } else {
145            view.setAlpha(newAlpha);
146        }
147    }
148}
149