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