blob: 59f7a800fe4cd3b8266df28252ba1626c73a501e [file] [log] [blame]
Michael Jurka3cd0a592011-08-16 12:40:30 -07001/*
2 * Copyright (C) 2011 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.recent;
18
Michael Jurka3cd0a592011-08-16 12:40:30 -070019import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Canvas;
22import android.graphics.LinearGradient;
23import android.graphics.Matrix;
24import android.graphics.Paint;
25import android.graphics.Shader;
Michael Jurka3cd0a592011-08-16 12:40:30 -070026import android.util.AttributeSet;
27import android.view.View;
28import android.view.ViewConfiguration;
Michael Jurka3cd0a592011-08-16 12:40:30 -070029import android.widget.LinearLayout;
30
31import com.android.systemui.R;
32
Michael Jurka4a6bc552013-09-17 17:42:54 +020033public class FadedEdgeDrawHelper {
Michael Jurka3cd0a592011-08-16 12:40:30 -070034 public static final boolean OPTIMIZE_SW_RENDERED_RECENTS = true;
35 public static final boolean USE_DARK_FADE_IN_HW_ACCELERATED_MODE = true;
36 private View mScrollView;
Michael Jurka3cd0a592011-08-16 12:40:30 -070037
Michael Jurka3cd0a592011-08-16 12:40:30 -070038 private int mFadingEdgeLength;
Michael Jurka3cd0a592011-08-16 12:40:30 -070039 private boolean mIsVertical;
Michael Jurka3cd0a592011-08-16 12:40:30 -070040 private boolean mSoftwareRendered = false;
Michael Jurka31a6fb32013-03-20 16:44:13 +010041 private Paint mBlackPaint;
42 private Paint mFadePaint;
43 private Matrix mFadeMatrix;
44 private LinearGradient mFade;
Michael Jurka3cd0a592011-08-16 12:40:30 -070045
Michael Jurka4a6bc552013-09-17 17:42:54 +020046 public static FadedEdgeDrawHelper create(Context context,
Michael Jurka3cd0a592011-08-16 12:40:30 -070047 AttributeSet attrs, View scrollView, boolean isVertical) {
48 boolean isTablet = context.getResources().
49 getBoolean(R.bool.config_recents_interface_for_tablets);
50 if (!isTablet && (OPTIMIZE_SW_RENDERED_RECENTS || USE_DARK_FADE_IN_HW_ACCELERATED_MODE)) {
Michael Jurka4a6bc552013-09-17 17:42:54 +020051 return new FadedEdgeDrawHelper(context, attrs, scrollView, isVertical);
Michael Jurka3cd0a592011-08-16 12:40:30 -070052 } else {
53 return null;
54 }
55 }
56
Michael Jurka4a6bc552013-09-17 17:42:54 +020057 public FadedEdgeDrawHelper(Context context,
Michael Jurka3cd0a592011-08-16 12:40:30 -070058 AttributeSet attrs, View scrollView, boolean isVertical) {
59 mScrollView = scrollView;
Michael Jurka3cd0a592011-08-16 12:40:30 -070060 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View);
61 mFadingEdgeLength = a.getDimensionPixelSize(android.R.styleable.View_fadingEdgeLength,
62 ViewConfiguration.get(context).getScaledFadingEdgeLength());
63 mIsVertical = isVertical;
64 }
65
66 public void onAttachedToWindowCallback(
Michael Jurka4a6bc552013-09-17 17:42:54 +020067 LinearLayout layout, boolean hardwareAccelerated) {
Michael Jurka3cd0a592011-08-16 12:40:30 -070068 mSoftwareRendered = !hardwareAccelerated;
69 if ((mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS)
70 || USE_DARK_FADE_IN_HW_ACCELERATED_MODE) {
71 mScrollView.setVerticalFadingEdgeEnabled(false);
72 mScrollView.setHorizontalFadingEdgeEnabled(false);
73 }
Michael Jurka3cd0a592011-08-16 12:40:30 -070074 }
75
76 public void addViewCallback(View newLinearLayoutChild) {
77 if (mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS) {
Michael Jurka261277e2012-06-01 04:06:45 -070078 final RecentsPanelView.ViewHolder holder =
79 (RecentsPanelView.ViewHolder) newLinearLayoutChild.getTag();
80 holder.labelView.setDrawingCacheEnabled(true);
81 holder.labelView.buildDrawingCache();
Michael Jurka3cd0a592011-08-16 12:40:30 -070082 }
83 }
84
85 public void drawCallback(Canvas canvas,
86 int left, int right, int top, int bottom, int scrollX, int scrollY,
87 float topFadingEdgeStrength, float bottomFadingEdgeStrength,
Michael Jurka31a6fb32013-03-20 16:44:13 +010088 float leftFadingEdgeStrength, float rightFadingEdgeStrength, int mPaddingTop) {
Michael Jurka3cd0a592011-08-16 12:40:30 -070089
90 if ((mSoftwareRendered && OPTIMIZE_SW_RENDERED_RECENTS)
91 || USE_DARK_FADE_IN_HW_ACCELERATED_MODE) {
Michael Jurka31a6fb32013-03-20 16:44:13 +010092 if (mFadePaint == null) {
93 mFadePaint = new Paint();
94 mFadeMatrix = new Matrix();
95 // use use a height of 1, and then wack the matrix each time we
96 // actually use it.
97 mFade = new LinearGradient(0, 0, 0, 1, 0xCC000000, 0, Shader.TileMode.CLAMP);
98 // PULL OUT THIS CONSTANT
99 mFadePaint.setShader(mFade);
100 }
Michael Jurka3cd0a592011-08-16 12:40:30 -0700101
102 // draw the fade effect
103 boolean drawTop = false;
104 boolean drawBottom = false;
105 boolean drawLeft = false;
106 boolean drawRight = false;
107
108 float topFadeStrength = 0.0f;
109 float bottomFadeStrength = 0.0f;
110 float leftFadeStrength = 0.0f;
111 float rightFadeStrength = 0.0f;
112
113 final float fadeHeight = mFadingEdgeLength;
114 int length = (int) fadeHeight;
115
116 // clip the fade length if top and bottom fades overlap
117 // overlapping fades produce odd-looking artifacts
118 if (mIsVertical && (top + length > bottom - length)) {
119 length = (bottom - top) / 2;
120 }
121
122 // also clip horizontal fades if necessary
123 if (!mIsVertical && (left + length > right - length)) {
124 length = (right - left) / 2;
125 }
126
127 if (mIsVertical) {
128 topFadeStrength = Math.max(0.0f, Math.min(1.0f, topFadingEdgeStrength));
129 drawTop = topFadeStrength * fadeHeight > 1.0f;
130 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, bottomFadingEdgeStrength));
131 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
132 }
133
134 if (!mIsVertical) {
135 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, leftFadingEdgeStrength));
136 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
137 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, rightFadingEdgeStrength));
138 drawRight = rightFadeStrength * fadeHeight > 1.0f;
139 }
140
141 if (drawTop) {
Michael Jurka31a6fb32013-03-20 16:44:13 +0100142 mFadeMatrix.setScale(1, fadeHeight * topFadeStrength);
143 mFadeMatrix.postTranslate(left, top);
144 mFade.setLocalMatrix(mFadeMatrix);
Leon Scroggins IIIab879832014-07-09 16:44:35 -0400145 mFadePaint.setShader(mFade);
Michael Jurka31a6fb32013-03-20 16:44:13 +0100146 canvas.drawRect(left, top, right, top + length, mFadePaint);
147
148 if (mBlackPaint == null) {
149 // Draw under the status bar at the top
150 mBlackPaint = new Paint();
151 mBlackPaint.setColor(0xFF000000);
152 }
153 canvas.drawRect(left, top - mPaddingTop, right, top, mBlackPaint);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700154 }
155
156 if (drawBottom) {
Michael Jurka31a6fb32013-03-20 16:44:13 +0100157 mFadeMatrix.setScale(1, fadeHeight * bottomFadeStrength);
158 mFadeMatrix.postRotate(180);
159 mFadeMatrix.postTranslate(left, bottom);
160 mFade.setLocalMatrix(mFadeMatrix);
Leon Scroggins IIIab879832014-07-09 16:44:35 -0400161 mFadePaint.setShader(mFade);
Michael Jurka31a6fb32013-03-20 16:44:13 +0100162 canvas.drawRect(left, bottom - length, right, bottom, mFadePaint);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700163 }
164
165 if (drawLeft) {
Michael Jurka31a6fb32013-03-20 16:44:13 +0100166 mFadeMatrix.setScale(1, fadeHeight * leftFadeStrength);
167 mFadeMatrix.postRotate(-90);
168 mFadeMatrix.postTranslate(left, top);
169 mFade.setLocalMatrix(mFadeMatrix);
Leon Scroggins IIIab879832014-07-09 16:44:35 -0400170 mFadePaint.setShader(mFade);
Michael Jurka31a6fb32013-03-20 16:44:13 +0100171 canvas.drawRect(left, top, left + length, bottom, mFadePaint);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700172 }
173
174 if (drawRight) {
Michael Jurka31a6fb32013-03-20 16:44:13 +0100175 mFadeMatrix.setScale(1, fadeHeight * rightFadeStrength);
176 mFadeMatrix.postRotate(90);
177 mFadeMatrix.postTranslate(right, top);
178 mFade.setLocalMatrix(mFadeMatrix);
Leon Scroggins IIIab879832014-07-09 16:44:35 -0400179 mFadePaint.setShader(mFade);
Michael Jurka31a6fb32013-03-20 16:44:13 +0100180 canvas.drawRect(right - length, top, right, bottom, mFadePaint);
Michael Jurka3cd0a592011-08-16 12:40:30 -0700181 }
182 }
183 }
184
Michael Jurka4a6bc552013-09-17 17:42:54 +0200185 public int getVerticalFadingEdgeLength() {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700186 return mFadingEdgeLength;
187 }
188
Michael Jurka4a6bc552013-09-17 17:42:54 +0200189 public int getHorizontalFadingEdgeLength() {
Michael Jurka3cd0a592011-08-16 12:40:30 -0700190 return mFadingEdgeLength;
191 }
192
Michael Jurka3cd0a592011-08-16 12:40:30 -0700193}