blob: f314b32c60a55de3450ae0d1d6c78df23de935e0 [file] [log] [blame]
Michael Jurka8c920dd2011-01-20 14:16:56 -08001/*
2 * Copyright (C) 2008 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.launcher2;
18
19import com.android.launcher.R;
20
21import android.content.Context;
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.Rect;
27import android.graphics.Bitmap.Config;
28import android.graphics.PorterDuff.Mode;
Michael Jurkacf6125c2011-01-28 15:20:01 -080029import android.util.AttributeSet;
Michael Jurka8c920dd2011-01-20 14:16:56 -080030import android.view.View;
31import android.view.ViewGroup;
32
33// This class caches the drawing of this View's children in a bitmap when the scale factor
34// falls below a certain size. Only used by CellLayout, but in a separate class to keep cache
35// logic separate from the other logic in CellLayout
36public class CachedViewGroup extends ViewGroup implements VisibilityChangedListener {
37 static final String TAG = "CachedViewGroup";
38
39 private Bitmap mCache;
40 private Canvas mCacheCanvas;
41 private Rect mCacheRect;
42 private Paint mCachePaint;
43
44 private boolean mIsCacheEnabled = true;
45 private boolean mDisableCacheUpdates = false;
46 private boolean mForceCacheUpdate = false;
47 private boolean isUpdatingCache = false;
48 private boolean mIsCacheDirty = true;
49 private float mBitmapCacheScale;
50 private float mMaxScaleForUsingBitmapCache;
51
52 private Rect mBackgroundRect;
53
54 public CachedViewGroup(Context context) {
55 super(context);
Michael Jurkacf6125c2011-01-28 15:20:01 -080056 init();
57 }
58
59 public CachedViewGroup(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
61 init();
62 }
63
64 private void init() {
Michael Jurka8c920dd2011-01-20 14:16:56 -080065 mBackgroundRect = new Rect();
66 mCacheRect = new Rect();
67 final Resources res = getResources();
68 mBitmapCacheScale =
69 res.getInteger(R.integer.config_workspaceScreenBitmapCacheScale) / 100.0f;
70 mMaxScaleForUsingBitmapCache =
71 res.getInteger(R.integer.config_maxScaleForUsingWorkspaceScreenBitmapCache) / 100.0f;
72 mCacheCanvas = new Canvas();
73 }
74
75 @Override
76 protected void onLayout(boolean changed, int l, int t, int r, int b) {
77 // sub-classes (namely CellLayout) will need to implement this
78 prepareCacheBitmap();
79 invalidateCache();
80 }
81
82 private void invalidateIfNeeded() {
83 if (mIsCacheDirty) {
84 // Force a redraw to update the cache if it's dirty
85 invalidate();
86 }
87 }
88
89 public void enableCache() {
90 mIsCacheEnabled = true;
91 invalidateIfNeeded();
92 }
93
94 public void disableCache() {
95 mIsCacheEnabled = false;
96 }
97
98 public void disableCacheUpdates() {
99 mDisableCacheUpdates = true;
100 // Force just one update before we enter a period of no cache updates
101 mForceCacheUpdate = true;
102 }
103
104 public void enableCacheUpdates() {
105 mDisableCacheUpdates = false;
106 invalidateIfNeeded();
107 }
108
109 private void invalidateCache() {
110 mIsCacheDirty = true;
111 invalidate();
112 }
113
114 public void receiveVisibilityChangedMessage(View v) {
115 invalidateCache();
116 }
117
118 private void prepareCacheBitmap() {
119 if (mCache == null) {
120 mCache = Bitmap.createBitmap((int) (getWidth() * mBitmapCacheScale),
121 (int) (getHeight() * mBitmapCacheScale), Config.ARGB_8888);
122
123 mCachePaint = new Paint();
124 mCachePaint.setFilterBitmap(true);
125 mCacheCanvas.setBitmap(mCache);
126 mCacheCanvas.scale(mBitmapCacheScale, mBitmapCacheScale);
127 }
128 }
129
130
131 public void updateCache() {
132 mCacheCanvas.drawColor(0, Mode.CLEAR);
133
134 float alpha = getAlpha();
135 setAlpha(1.0f);
136 isUpdatingCache = true;
Michael Jurkacf6125c2011-01-28 15:20:01 -0800137 drawChildren(mCacheCanvas);
Michael Jurka8c920dd2011-01-20 14:16:56 -0800138 isUpdatingCache = false;
139 setAlpha(alpha);
140
141 mIsCacheDirty = false;
142 }
143
Michael Jurka8c920dd2011-01-20 14:16:56 -0800144 public void drawChildren(Canvas canvas) {
145 super.dispatchDraw(canvas);
146 }
147
148 @Override
149 public void removeAllViews() {
150 super.removeAllViews();
151 invalidateCache();
152 }
153
154 @Override
155 public void removeAllViewsInLayout() {
156 super.removeAllViewsInLayout();
157 invalidateCache();
158 }
159
Michael Jurka8c920dd2011-01-20 14:16:56 -0800160 @Override
161 public void removeView(View view) {
162 super.removeView(view);
163 invalidateCache();
164 }
165
166 @Override
167 public void removeViewAt(int index) {
168 super.removeViewAt(index);
169 invalidateCache();
170 }
171
172 @Override
173 public void removeViewInLayout(View view) {
174 super.removeViewInLayout(view);
175 invalidateCache();
176 }
177
178 @Override
179 public void removeViews(int start, int count) {
180 super.removeViews(start, count);
181 invalidateCache();
182 }
183
184 @Override
185 public void removeViewsInLayout(int start, int count) {
186 super.removeViewsInLayout(start, count);
187 invalidateCache();
188 }
189
190 @Override
191 public void dispatchDraw(Canvas canvas) {
192 final int count = getChildCount();
193
194 boolean useBitmapCache = false;
195 if (!isUpdatingCache) {
196 if (!mIsCacheDirty) {
197 // Check if one of the children (an icon or widget) is dirty
198 for (int i = 0; i < count; i++) {
199 final View child = getChildAt(i);
200 if (child.isDirty()) {
201 mIsCacheDirty = true;
202 break;
203 }
204 }
205 }
206
207 useBitmapCache = mIsCacheEnabled && getScaleX() < mMaxScaleForUsingBitmapCache;
208 if (mForceCacheUpdate ||
209 (useBitmapCache && !mDisableCacheUpdates)) {
210 // Sometimes we force a cache update-- this is used to make sure the cache will look as
211 // up-to-date as possible right when we disable cache updates
212 if (mIsCacheDirty) {
213 updateCache();
214 }
215 mForceCacheUpdate = false;
216 }
217 }
218
219 if (useBitmapCache) {
220 mCachePaint.setAlpha((int)(255*getAlpha()));
221 canvas.drawBitmap(mCache, mCacheRect, mBackgroundRect, mCachePaint);
222 } else {
223 super.dispatchDraw(canvas);
224 }
225 }
226
227 @Override
228 public void addView(View child, int index, ViewGroup.LayoutParams params) {
229 super.addView(child, index, params);
230
231 // invalidate the cache to have it reflect the new item
232 invalidateCache();
233
234 if (child instanceof VisibilityChangedBroadcaster) {
235 VisibilityChangedBroadcaster v = (VisibilityChangedBroadcaster) child;
236 v.setVisibilityChangedListener(this);
237 }
238 }
239
240
241 @Override
242 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
243 super.onSizeChanged(w, h, oldw, oldh);
244 mBackgroundRect.set(0, 0, w, h);
245 mCacheRect.set(0, 0, (int) (mBitmapCacheScale * w), (int) (mBitmapCacheScale * h));
246 mCache = null;
247 prepareCacheBitmap();
248 invalidateCache();
249 }
250}
251
252
253//Custom interfaces used to listen to "visibility changed" events of *children* of Views. Avoided
254//using "onVisibilityChanged" in the names because there's a method of that name in framework
255//(which can only can be used to listen to ancestors' "visibility changed" events)
256interface VisibilityChangedBroadcaster {
257 public void setVisibilityChangedListener(VisibilityChangedListener listener);
258}
259
260interface VisibilityChangedListener {
261 public void receiveVisibilityChangedMessage(View v);
262}