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