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; |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 21 | import android.graphics.ColorFilter; |
| 22 | import android.graphics.LightingColorFilter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 23 | import android.view.View; |
| 24 | import android.widget.FrameLayout; |
| 25 | import android.widget.ImageView; |
| 26 | |
| 27 | import com.android.systemui.R; |
| 28 | import com.android.systemui.qs.QSTile.SignalState; |
| 29 | |
| 30 | /** View that represents a custom quick settings tile for displaying signal info (wifi/cell). **/ |
| 31 | public final class SignalTileView extends QSTileView { |
| 32 | private static final long DEFAULT_DURATION = new ValueAnimator().getDuration(); |
| 33 | private static final long SHORT_DURATION = DEFAULT_DURATION / 3; |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 34 | private static final ColorFilter FILTER = new LightingColorFilter(0xffffffff, 0xff283034); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | |
| 36 | private FrameLayout mIconFrame; |
| 37 | private ImageView mSignal; |
| 38 | private ImageView mOverlay; |
| 39 | private ImageView mIn; |
| 40 | private ImageView mOut; |
| 41 | |
| 42 | public SignalTileView(Context context) { |
| 43 | super(context); |
| 44 | |
| 45 | mIn = new ImageView(context); |
| 46 | mIn.setImageResource(R.drawable.ic_qs_signal_in); |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 47 | mIn.setColorFilter(FILTER); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 48 | addView(mIn); |
| 49 | |
| 50 | mOut = new ImageView(context); |
| 51 | mOut.setImageResource(R.drawable.ic_qs_signal_out); |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 52 | mOut.setColorFilter(FILTER); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | addView(mOut); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected View createIcon() { |
| 58 | mIconFrame = new FrameLayout(mContext); |
| 59 | mSignal = new ImageView(mContext); |
| 60 | mIconFrame.addView(mSignal); |
| 61 | mOverlay = new ImageView(mContext); |
| 62 | mIconFrame.addView(mOverlay); |
| 63 | return mIconFrame; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 68 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 69 | int hs = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.EXACTLY); |
| 70 | int ws = MeasureSpec.makeMeasureSpec(mIconFrame.getMeasuredHeight(), MeasureSpec.AT_MOST); |
| 71 | mIn.measure(ws, hs); |
| 72 | mOut.measure(ws, hs); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 77 | super.onLayout(changed, l, t, r, b); |
| 78 | layoutIndicator(mIn); |
| 79 | layoutIndicator(mOut); |
| 80 | } |
| 81 | |
| 82 | private void layoutIndicator(View indicator) { |
| 83 | indicator.layout( |
| 84 | mIconFrame.getRight(), |
| 85 | mIconFrame.getBottom() - indicator.getMeasuredHeight(), |
| 86 | mIconFrame.getRight() + indicator.getMeasuredWidth(), |
| 87 | mIconFrame.getBottom()); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void handleStateChanged(QSTile.State state) { |
| 92 | super.handleStateChanged(state); |
| 93 | final SignalState s = (SignalState) state; |
| 94 | mSignal.setImageDrawable(null); // force refresh |
| 95 | mSignal.setImageResource(s.iconId); |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 96 | mSignal.setColorFilter(FILTER); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 97 | if (s.overlayIconId > 0) { |
| 98 | mOverlay.setVisibility(VISIBLE); |
| 99 | mOverlay.setImageDrawable(null); // force refresh |
| 100 | mOverlay.setImageResource(s.overlayIconId); |
John Spurlock | faa3262 | 2014-06-04 01:38:34 -0400 | [diff] [blame] | 101 | mOverlay.setColorFilter(FILTER); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 102 | } else { |
| 103 | mOverlay.setVisibility(GONE); |
| 104 | } |
| 105 | setVisibility(mIn, s.activityIn); |
| 106 | setVisibility(mOut, s.activityOut); |
| 107 | } |
| 108 | |
| 109 | private void setVisibility(View view, boolean visible) { |
| 110 | final float newAlpha = visible ? 1 : 0; |
| 111 | if (view.getAlpha() != newAlpha) { |
| 112 | view.animate() |
| 113 | .setDuration(visible ? SHORT_DURATION : DEFAULT_DURATION) |
| 114 | .alpha(newAlpha) |
| 115 | .withLayer() |
| 116 | .start(); |
| 117 | } |
| 118 | } |
| 119 | } |