1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.wallpapers;
import android.app.WallpaperColors;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RecordingCanvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Trace;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.dagger.qualifiers.LongRunning;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.util.concurrency.DelayableExecutor;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import javax.inject.Inject;
/**
* Default built-in wallpaper that simply shows a static image.
*/
@SuppressWarnings({"UnusedDeclaration"})
public class ImageWallpaper extends WallpaperService {
private static final String TAG = ImageWallpaper.class.getSimpleName();
private static final boolean DEBUG = false;
// keep track of the number of pages of the launcher for local color extraction purposes
private volatile int mPages = 1;
private boolean mPagesComputed = false;
private final UserTracker mUserTracker;
// used for most tasks (call canvas.drawBitmap, load/unload the bitmap)
@LongRunning
private final DelayableExecutor mLongExecutor;
// wait at least this duration before unloading the bitmap
private static final int DELAY_UNLOAD_BITMAP = 2000;
@Inject
public ImageWallpaper(@LongRunning DelayableExecutor longExecutor, UserTracker userTracker) {
super();
mLongExecutor = longExecutor;
mUserTracker = userTracker;
}
@Override
public Engine onCreateEngine() {
return new CanvasEngine();
}
class CanvasEngine extends WallpaperService.Engine implements DisplayListener {
private WallpaperManager mWallpaperManager;
private final WallpaperLocalColorExtractor mWallpaperLocalColorExtractor;
private SurfaceHolder mSurfaceHolder;
@VisibleForTesting
static final int MIN_SURFACE_WIDTH = 128;
@VisibleForTesting
static final int MIN_SURFACE_HEIGHT = 128;
private Bitmap mBitmap;
private boolean mWideColorGamut = false;
/*
* Counter to unload the bitmap as soon as possible.
* Before any bitmap operation, this is incremented.
* After an operation completion, this is decremented (synchronously),
* and if the count is 0, unload the bitmap
*/
private int mBitmapUsages = 0;
private final Object mLock = new Object();
CanvasEngine() {
super();
setFixedSizeAllowed(true);
setShowForAllUsers(true);
mWallpaperLocalColorExtractor = new WallpaperLocalColorExtractor(
mLongExecutor,
new WallpaperLocalColorExtractor.WallpaperLocalColorExtractorCallback() {
@Override
public void onColorsProcessed(List<RectF> regions,
List<WallpaperColors> colors) {
CanvasEngine.this.onColorsProcessed(regions, colors);
}
@Override
public void onMiniBitmapUpdated() {
CanvasEngine.this.onMiniBitmapUpdated();
}
@Override
public void onActivated() {
setOffsetNotificationsEnabled(true);
}
@Override
public void onDeactivated() {
setOffsetNotificationsEnabled(false);
}
});
// if the number of pages is already computed, transmit it to the color extractor
if (mPagesComputed) {
mWallpaperLocalColorExtractor.onPageChanged(mPages);
}
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
Trace.beginSection("ImageWallpaper.CanvasEngine#onCreate");
if (DEBUG) {
Log.d(TAG, "onCreate");
}
mWallpaperManager = getDisplayContext().getSystemService(WallpaperManager.class);
mSurfaceHolder = surfaceHolder;
Rect dimensions = mWallpaperManager.peekBitmapDimensions();
int width = Math.max(MIN_SURFACE_WIDTH, dimensions.width());
int height = Math.max(MIN_SURFACE_HEIGHT, dimensions.height());
mSurfaceHolder.setFixedSize(width, height);
getDisplayContext().getSystemService(DisplayManager.class)
.registerDisplayListener(this, null);
getDisplaySizeAndUpdateColorExtractor();
Trace.endSection();
}
@Override
public void onDestroy() {
getDisplayContext().getSystemService(DisplayManager.class)
.unregisterDisplayListener(this);
mWallpaperLocalColorExtractor.cleanUp();
}
@Override
public boolean shouldZoomOutWallpaper() {
return true;
}
@Override
public boolean shouldWaitForEngineShown() {
return true;
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (DEBUG) {
Log.d(TAG, "onSurfaceChanged: width=" + width + ", height=" + height);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
if (DEBUG) {
Log.i(TAG, "onSurfaceDestroyed");
}
mSurfaceHolder = null;
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
if (DEBUG) {
Log.i(TAG, "onSurfaceCreated");
}
}
@Override
public void onSurfaceRedrawNeeded(SurfaceHolder holder) {
if (DEBUG) {
Log.d(TAG, "onSurfaceRedrawNeeded");
}
drawFrame();
}
private void drawFrame() {
mLongExecutor.execute(this::drawFrameSynchronized);
}
private void drawFrameSynchronized() {
synchronized (mLock) {
drawFrameInternal();
}
}
private void drawFrameInternal() {
if (mSurfaceHolder == null) {
Log.e(TAG, "attempt to draw a frame without a valid surface");
return;
}
// load the wallpaper if not already done
if (!isBitmapLoaded()) {
loadWallpaperAndDrawFrameInternal();
} else {
mBitmapUsages++;
drawFrameOnCanvas(mBitmap);
reportEngineShown(false);
unloadBitmapIfNotUsedInternal();
}
}
@VisibleForTesting
void drawFrameOnCanvas(Bitmap bitmap) {
Trace.beginSection("ImageWallpaper.CanvasEngine#drawFrame");
Surface surface = mSurfaceHolder.getSurface();
Canvas canvas = null;
try {
canvas = mWideColorGamut
? surface.lockHardwareWideColorGamutCanvas()
: surface.lockHardwareCanvas();
} catch (IllegalStateException e) {
Log.w(TAG, "Unable to lock canvas", e);
}
if (canvas != null) {
Rect dest = mSurfaceHolder.getSurfaceFrame();
try {
canvas.drawBitmap(bitmap, null, dest, null);
} finally {
surface.unlockCanvasAndPost(canvas);
}
}
Trace.endSection();
}
@VisibleForTesting
boolean isBitmapLoaded() {
return mBitmap != null && !mBitmap.isRecycled();
}
private void unloadBitmapIfNotUsed() {
mLongExecutor.execute(this::unloadBitmapIfNotUsedSynchronized);
}
private void unloadBitmapIfNotUsedSynchronized() {
synchronized (mLock) {
unloadBitmapIfNotUsedInternal();
}
}
private void unloadBitmapIfNotUsedInternal() {
mBitmapUsages -= 1;
if (mBitmapUsages <= 0) {
mBitmapUsages = 0;
unloadBitmapInternal();
}
}
private void unloadBitmapInternal() {
Trace.beginSection("ImageWallpaper.CanvasEngine#unloadBitmap");
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = null;
final Surface surface = getSurfaceHolder().getSurface();
surface.hwuiDestroy();
mWallpaperManager.forgetLoadedWallpaper();
Trace.endSection();
}
private void loadWallpaperAndDrawFrameInternal() {
Trace.beginSection("ImageWallpaper.CanvasEngine#loadWallpaper");
boolean loadSuccess = false;
Bitmap bitmap;
try {
bitmap = mWallpaperManager.getBitmapAsUser(mUserTracker.getUserId(), false);
if (bitmap != null
&& bitmap.getByteCount() > RecordingCanvas.MAX_BITMAP_SIZE) {
throw new RuntimeException("Wallpaper is too large to draw!");
}
} catch (RuntimeException | OutOfMemoryError exception) {
// Note that if we do fail at this, and the default wallpaper can't
// be loaded, we will go into a cycle. Don't do a build where the
// default wallpaper can't be loaded.
Log.w(TAG, "Unable to load wallpaper!", exception);
mWallpaperManager.clearWallpaper(
WallpaperManager.FLAG_SYSTEM, mUserTracker.getUserId());
try {
bitmap = mWallpaperManager.getBitmapAsUser(mUserTracker.getUserId(), false);
} catch (RuntimeException | OutOfMemoryError e) {
Log.w(TAG, "Unable to load default wallpaper!", e);
bitmap = null;
}
}
if (bitmap == null) {
Log.w(TAG, "Could not load bitmap");
} else if (bitmap.isRecycled()) {
Log.e(TAG, "Attempt to load a recycled bitmap");
} else if (mBitmap == bitmap) {
Log.e(TAG, "Loaded a bitmap that was already loaded");
} else {
// at this point, loading is done correctly.
loadSuccess = true;
// recycle the previously loaded bitmap
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = bitmap;
mWideColorGamut = mWallpaperManager.wallpaperSupportsWcg(
WallpaperManager.FLAG_SYSTEM);
// +2 usages for the color extraction and the delayed unload.
mBitmapUsages += 2;
recomputeColorExtractorMiniBitmap();
drawFrameInternal();
/*
* after loading, the bitmap will be unloaded after all these conditions:
* - the frame is redrawn
* - the mini bitmap from color extractor is recomputed
* - the DELAY_UNLOAD_BITMAP has passed
*/
mLongExecutor.executeDelayed(
this::unloadBitmapIfNotUsedSynchronized, DELAY_UNLOAD_BITMAP);
}
// even if the bitmap cannot be loaded, call reportEngineShown
if (!loadSuccess) reportEngineShown(false);
Trace.endSection();
}
private void onColorsProcessed(List<RectF> regions, List<WallpaperColors> colors) {
try {
notifyLocalColorsChanged(regions, colors);
} catch (RuntimeException e) {
Log.e(TAG, e.getMessage(), e);
}
}
@VisibleForTesting
void recomputeColorExtractorMiniBitmap() {
mWallpaperLocalColorExtractor.onBitmapChanged(mBitmap);
}
@VisibleForTesting
void onMiniBitmapUpdated() {
unloadBitmapIfNotUsed();
}
@Override
public boolean supportsLocalColorExtraction() {
return true;
}
@Override
public void addLocalColorsAreas(@NonNull List<RectF> regions) {
// this call will activate the offset notifications
// if no colors were being processed before
mWallpaperLocalColorExtractor.addLocalColorsAreas(regions);
}
@Override
public void removeLocalColorsAreas(@NonNull List<RectF> regions) {
// this call will deactivate the offset notifications
// if we are no longer processing colors
mWallpaperLocalColorExtractor.removeLocalColorAreas(regions);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xOffsetStep, float yOffsetStep,
int xPixelOffset, int yPixelOffset) {
final int pages;
if (xOffsetStep > 0 && xOffsetStep <= 1) {
pages = Math.round(1 / xOffsetStep) + 1;
} else {
pages = 1;
}
if (pages != mPages || !mPagesComputed) {
mPages = pages;
mPagesComputed = true;
mWallpaperLocalColorExtractor.onPageChanged(mPages);
}
}
@Override
public void onDisplayAdded(int displayId) {
}
@Override
public void onDisplayRemoved(int displayId) {
}
@Override
public void onDisplayChanged(int displayId) {
// changes the display in the color extractor
// the new display dimensions will be used in the next color computation
if (displayId == getDisplayContext().getDisplayId()) {
getDisplaySizeAndUpdateColorExtractor();
}
}
private void getDisplaySizeAndUpdateColorExtractor() {
Rect window = getDisplayContext()
.getSystemService(WindowManager.class)
.getCurrentWindowMetrics()
.getBounds();
mWallpaperLocalColorExtractor.setDisplayDimensions(window.width(), window.height());
}
@Override
protected void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) {
super.dump(prefix, fd, out, args);
out.print(prefix); out.print("Engine="); out.println(this);
out.print(prefix); out.print("valid surface=");
out.println(getSurfaceHolder() != null && getSurfaceHolder().getSurface() != null
? getSurfaceHolder().getSurface().isValid()
: "null");
out.print(prefix); out.print("surface frame=");
out.println(getSurfaceHolder() != null ? getSurfaceHolder().getSurfaceFrame() : "null");
out.print(prefix); out.print("bitmap=");
out.println(mBitmap == null ? "null"
: mBitmap.isRecycled() ? "recycled"
: mBitmap.getWidth() + "x" + mBitmap.getHeight());
mWallpaperLocalColorExtractor.dump(prefix, fd, out, args);
}
}
}
|