John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import android.animation.ValueAnimator; |
| 20 | import android.content.Context; |
| 21 | import android.view.View; |
| 22 | import android.widget.FrameLayout; |
| 23 | import android.widget.ImageView; |
| 24 | |
| 25 | import com.android.systemui.R; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 26 | import com.android.systemui.plugins.qs.QSTile.SignalState; |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 27 | import com.android.systemui.plugins.qs.QSTile.State; |
Jason Monk | 702e2eb | 2017-03-03 16:53:44 -0500 | [diff] [blame] | 28 | import com.android.systemui.qs.tileimpl.QSIconViewImpl; |
Jason Monk | 395617f | 2017-05-05 14:07:58 -0400 | [diff] [blame] | 29 | import com.android.systemui.qs.tileimpl.SlashImageView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | |
| 31 | /** View that represents a custom quick settings tile for displaying signal info (wifi/cell). **/ |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 32 | public class SignalTileView extends QSIconViewImpl { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | private static final long DEFAULT_DURATION = new ValueAnimator().getDuration(); |
| 34 | private static final long SHORT_DURATION = DEFAULT_DURATION / 3; |
| 35 | |
Jason Monk | e5b770e | 2017-03-03 21:49:29 -0500 | [diff] [blame] | 36 | protected FrameLayout mIconFrame; |
| 37 | protected ImageView mSignal; |
Jason Monk | 01df36f | 2017-06-07 13:02:47 -0400 | [diff] [blame] | 38 | private ImageView mOverlay; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | private ImageView mIn; |
| 40 | private ImageView mOut; |
| 41 | |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 42 | private int mWideOverlayIconStartPadding; |
Rohan Shah | 28191fc | 2018-01-16 14:28:55 -0800 | [diff] [blame] | 43 | private int mSignalIndicatorToIconFrameSpacing; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 44 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | public SignalTileView(Context context) { |
| 46 | super(context); |
| 47 | |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 48 | mIn = addTrafficView(R.drawable.ic_qs_signal_in); |
| 49 | mOut = addTrafficView(R.drawable.ic_qs_signal_out); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 50 | |
Rohan Shah | 28191fc | 2018-01-16 14:28:55 -0800 | [diff] [blame] | 51 | setClipChildren(false); |
| 52 | setClipToPadding(false); |
| 53 | |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 54 | mWideOverlayIconStartPadding = context.getResources().getDimensionPixelSize( |
| 55 | R.dimen.wide_type_icon_start_padding_qs); |
Rohan Shah | 28191fc | 2018-01-16 14:28:55 -0800 | [diff] [blame] | 56 | mSignalIndicatorToIconFrameSpacing = context.getResources().getDimensionPixelSize( |
| 57 | R.dimen.signal_indicator_to_icon_frame_spacing); |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 58 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 59 | |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 60 | private ImageView addTrafficView(int icon) { |
| 61 | final ImageView traffic = new ImageView(mContext); |
| 62 | traffic.setImageResource(icon); |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 63 | traffic.setAlpha(0f); |
| 64 | addView(traffic); |
| 65 | return traffic; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected View createIcon() { |
| 70 | mIconFrame = new FrameLayout(mContext); |
Evan Laird | 369ded0 | 2017-09-21 14:11:00 -0400 | [diff] [blame] | 71 | mSignal = createSlashImageView(mContext); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 72 | mIconFrame.addView(mSignal); |
Jason Monk | 01df36f | 2017-06-07 13:02:47 -0400 | [diff] [blame] | 73 | mOverlay = new ImageView(mContext); |
| 74 | mIconFrame.addView(mOverlay, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 75 | return mIconFrame; |
| 76 | } |
| 77 | |
Evan Laird | 369ded0 | 2017-09-21 14:11:00 -0400 | [diff] [blame] | 78 | protected SlashImageView createSlashImageView(Context context) { |
| 79 | return new SlashImageView(context); |
| 80 | } |
| 81 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | @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 Monk | c34befb | 2015-10-07 16:40:02 -0400 | [diff] [blame] | 98 | @Override |
| 99 | protected int getIconMeasureMode() { |
| 100 | return MeasureSpec.AT_MOST; |
| 101 | } |
| 102 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | private void layoutIndicator(View indicator) { |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 104 | boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL; |
| 105 | int left, right; |
| 106 | if (isRtl) { |
Rohan Shah | 28191fc | 2018-01-16 14:28:55 -0800 | [diff] [blame] | 107 | right = getLeft() - mSignalIndicatorToIconFrameSpacing; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 108 | left = right - indicator.getMeasuredWidth(); |
| 109 | } else { |
Rohan Shah | 28191fc | 2018-01-16 14:28:55 -0800 | [diff] [blame] | 110 | left = getRight() + mSignalIndicatorToIconFrameSpacing; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 111 | right = left + indicator.getMeasuredWidth(); |
| 112 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 113 | indicator.layout( |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 114 | left, |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 115 | mIconFrame.getBottom() - indicator.getMeasuredHeight(), |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 116 | right, |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 117 | mIconFrame.getBottom()); |
| 118 | } |
| 119 | |
| 120 | @Override |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 121 | public void setIcon(State state, boolean allowAnimations) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 122 | final SignalState s = (SignalState) state; |
Amin Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 123 | setIcon(mSignal, s, allowAnimations); |
Jason Monk | 01df36f | 2017-06-07 13:02:47 -0400 | [diff] [blame] | 124 | |
| 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 Shaikh | 32d786f | 2018-08-01 17:08:26 -0400 | [diff] [blame] | 136 | final boolean shouldAnimate = allowAnimations && isShown(); |
| 137 | setVisibility(mIn, shouldAnimate, s.activityIn); |
| 138 | setVisibility(mOut, shouldAnimate, s.activityOut); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 139 | } |
| 140 | |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 141 | 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 Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 145 | view.animate() |
| 146 | .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION) |
| 147 | .alpha(newAlpha) |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 148 | .start(); |
John Spurlock | 360e15b | 2014-07-08 18:45:21 -0400 | [diff] [blame] | 149 | } else { |
| 150 | view.setAlpha(newAlpha); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 151 | } |
| 152 | } |
Jorim Jaggi | fbc3f19 | 2014-09-09 15:51:16 +0000 | [diff] [blame] | 153 | } |