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