blob: ce90fc107b82de7aeefd7bf49b57699565d40dff [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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;
Jason Monk702e2eb2017-03-03 16:53:44 -050026import com.android.systemui.plugins.qs.QSTile.SignalState;
Amin Shaikh32d786f2018-08-01 17:08:26 -040027import com.android.systemui.plugins.qs.QSTile.State;
Jason Monk702e2eb2017-03-03 16:53:44 -050028import com.android.systemui.qs.tileimpl.QSIconViewImpl;
Jason Monk395617f2017-05-05 14:07:58 -040029import com.android.systemui.qs.tileimpl.SlashImageView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030
31/** View that represents a custom quick settings tile for displaying signal info (wifi/cell). **/
Jason Monke5b770e2017-03-03 21:49:29 -050032public class SignalTileView extends QSIconViewImpl {
John Spurlockaf8d6c42014-05-07 17:49:08 -040033 private static final long DEFAULT_DURATION = new ValueAnimator().getDuration();
34 private static final long SHORT_DURATION = DEFAULT_DURATION / 3;
35
Jason Monke5b770e2017-03-03 21:49:29 -050036 protected FrameLayout mIconFrame;
37 protected ImageView mSignal;
Jason Monk01df36f2017-06-07 13:02:47 -040038 private ImageView mOverlay;
John Spurlockaf8d6c42014-05-07 17:49:08 -040039 private ImageView mIn;
40 private ImageView mOut;
41
Jorim Jaggic737b9b2014-09-08 23:57:20 +020042 private int mWideOverlayIconStartPadding;
Rohan Shah28191fc2018-01-16 14:28:55 -080043 private int mSignalIndicatorToIconFrameSpacing;
Jorim Jaggic737b9b2014-09-08 23:57:20 +020044
John Spurlockaf8d6c42014-05-07 17:49:08 -040045 public SignalTileView(Context context) {
46 super(context);
47
John Spurlock360e15b2014-07-08 18:45:21 -040048 mIn = addTrafficView(R.drawable.ic_qs_signal_in);
49 mOut = addTrafficView(R.drawable.ic_qs_signal_out);
Jorim Jaggic737b9b2014-09-08 23:57:20 +020050
Rohan Shah28191fc2018-01-16 14:28:55 -080051 setClipChildren(false);
52 setClipToPadding(false);
53
Jorim Jaggic737b9b2014-09-08 23:57:20 +020054 mWideOverlayIconStartPadding = context.getResources().getDimensionPixelSize(
55 R.dimen.wide_type_icon_start_padding_qs);
Rohan Shah28191fc2018-01-16 14:28:55 -080056 mSignalIndicatorToIconFrameSpacing = context.getResources().getDimensionPixelSize(
57 R.dimen.signal_indicator_to_icon_frame_spacing);
John Spurlock360e15b2014-07-08 18:45:21 -040058 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040059
John Spurlock360e15b2014-07-08 18:45:21 -040060 private ImageView addTrafficView(int icon) {
61 final ImageView traffic = new ImageView(mContext);
62 traffic.setImageResource(icon);
John Spurlock360e15b2014-07-08 18:45:21 -040063 traffic.setAlpha(0f);
64 addView(traffic);
65 return traffic;
John Spurlockaf8d6c42014-05-07 17:49:08 -040066 }
67
68 @Override
69 protected View createIcon() {
70 mIconFrame = new FrameLayout(mContext);
Evan Laird369ded02017-09-21 14:11:00 -040071 mSignal = createSlashImageView(mContext);
John Spurlockaf8d6c42014-05-07 17:49:08 -040072 mIconFrame.addView(mSignal);
Jason Monk01df36f2017-06-07 13:02:47 -040073 mOverlay = new ImageView(mContext);
74 mIconFrame.addView(mOverlay, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
John Spurlockaf8d6c42014-05-07 17:49:08 -040075 return mIconFrame;
76 }
77
Evan Laird369ded02017-09-21 14:11:00 -040078 protected SlashImageView createSlashImageView(Context context) {
79 return new SlashImageView(context);
80 }
81
John Spurlockaf8d6c42014-05-07 17:49:08 -040082 @Override
83 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
84 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
85 int hs = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.EXACTLY);
86 int ws = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.AT_MOST);
87 mIn.measure(ws, hs);
88 mOut.measure(ws, hs);
89 }
90
91 @Override
92 protected void onLayout(boolean changed, int l, int t, int r, int b) {
93 super.onLayout(changed, l, t, r, b);
94 layoutIndicator(mIn);
95 layoutIndicator(mOut);
96 }
97
Jason Monkc34befb2015-10-07 16:40:02 -040098 @Override
99 protected int getIconMeasureMode() {
100 return MeasureSpec.AT_MOST;
101 }
102
John Spurlockaf8d6c42014-05-07 17:49:08 -0400103 private void layoutIndicator(View indicator) {
Selim Cinek06d3bca2014-08-26 17:29:20 +0200104 boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
105 int left, right;
106 if (isRtl) {
Rohan Shah28191fc2018-01-16 14:28:55 -0800107 right = getLeft() - mSignalIndicatorToIconFrameSpacing;
Selim Cinek06d3bca2014-08-26 17:29:20 +0200108 left = right - indicator.getMeasuredWidth();
109 } else {
Rohan Shah28191fc2018-01-16 14:28:55 -0800110 left = getRight() + mSignalIndicatorToIconFrameSpacing;
Selim Cinek06d3bca2014-08-26 17:29:20 +0200111 right = left + indicator.getMeasuredWidth();
112 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400113 indicator.layout(
Selim Cinek06d3bca2014-08-26 17:29:20 +0200114 left,
John Spurlockaf8d6c42014-05-07 17:49:08 -0400115 mIconFrame.getBottom() - indicator.getMeasuredHeight(),
Selim Cinek06d3bca2014-08-26 17:29:20 +0200116 right,
John Spurlockaf8d6c42014-05-07 17:49:08 -0400117 mIconFrame.getBottom());
118 }
119
120 @Override
Amin Shaikh32d786f2018-08-01 17:08:26 -0400121 public void setIcon(State state, boolean allowAnimations) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400122 final SignalState s = (SignalState) state;
Amin Shaikh32d786f2018-08-01 17:08:26 -0400123 setIcon(mSignal, s, allowAnimations);
Jason Monk01df36f2017-06-07 13:02:47 -0400124
125 if (s.overlayIconId > 0) {
126 mOverlay.setVisibility(VISIBLE);
127 mOverlay.setImageResource(s.overlayIconId);
128 } else {
129 mOverlay.setVisibility(GONE);
130 }
131 if (s.overlayIconId > 0 && s.isOverlayIconWide) {
132 mSignal.setPaddingRelative(mWideOverlayIconStartPadding, 0, 0, 0);
133 } else {
134 mSignal.setPaddingRelative(0, 0, 0, 0);
135 }
Amin Shaikh32d786f2018-08-01 17:08:26 -0400136 final boolean shouldAnimate = allowAnimations && isShown();
137 setVisibility(mIn, shouldAnimate, s.activityIn);
138 setVisibility(mOut, shouldAnimate, s.activityOut);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400139 }
140
John Spurlock360e15b2014-07-08 18:45:21 -0400141 private void setVisibility(View view, boolean shown, boolean visible) {
142 final float newAlpha = shown && visible ? 1 : 0;
143 if (view.getAlpha() == newAlpha) return;
144 if (shown) {
John Spurlockaf8d6c42014-05-07 17:49:08 -0400145 view.animate()
146 .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION)
147 .alpha(newAlpha)
John Spurlockaf8d6c42014-05-07 17:49:08 -0400148 .start();
John Spurlock360e15b2014-07-08 18:45:21 -0400149 } else {
150 view.setAlpha(newAlpha);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400151 }
152 }
Jorim Jaggifbc3f192014-09-09 15:51:16 +0000153}