summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/asset/Asset.java
blob: dc4a5e6fd10f7cb5ff955762cca252ee37bd9f89 (plain)
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
/*
 * Copyright (C) 2017 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.wallpaper.asset;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.android.wallpaper.module.BitmapCropper;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.util.ScreenSizeCalculator;
import com.android.wallpaper.util.WallpaperCropUtils;

import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

/**
 * Interface representing an image asset.
 */
public abstract class Asset {

    /**
     * Creates and returns a placeholder Drawable instance sized exactly to the target ImageView and
     * filled completely with pixels of the provided placeholder color.
     */
    protected static Drawable getPlaceholderDrawable(
            Context context, ImageView imageView, int placeholderColor) {
        Point imageViewDimensions = getViewDimensions(imageView);
        Bitmap placeholderBitmap =
                Bitmap.createBitmap(imageViewDimensions.x, imageViewDimensions.y, Config.ARGB_8888);
        placeholderBitmap.eraseColor(placeholderColor);
        return new BitmapDrawable(context.getResources(), placeholderBitmap);
    }

    /**
     * Returns the visible height and width in pixels of the provided ImageView, or if it hasn't
     * been laid out yet, then gets the absolute value of the layout params.
     */
    private static Point getViewDimensions(View view) {
        int width = view.getWidth() > 0 ? view.getWidth() : Math.abs(view.getLayoutParams().width);
        int height = view.getHeight() > 0 ? view.getHeight()
                : Math.abs(view.getLayoutParams().height);

        return new Point(width, height);
    }

    /**
     * Decodes a bitmap sized for the destination view's dimensions off the main UI thread.
     *
     * @param targetWidth  Width of target view in physical pixels.
     * @param targetHeight Height of target view in physical pixels.
     * @param receiver     Called with the decoded bitmap or null if there was an error decoding the
     *                     bitmap.
     */
    public abstract void decodeBitmap(int targetWidth, int targetHeight, BitmapReceiver receiver);

    /**
     * Decodes and downscales a bitmap region off the main UI thread.
     *
     * @param rect         Rect representing the crop region in terms of the original image's
     *                     resolution.
     * @param targetWidth  Width of target view in physical pixels.
     * @param targetHeight Height of target view in physical pixels.
     * @param receiver     Called with the decoded bitmap region or null if there was an error
     *                     decoding the bitmap region.
     */
    public abstract void decodeBitmapRegion(Rect rect, int targetWidth, int targetHeight,
            BitmapReceiver receiver);

    /**
     * Calculates the raw dimensions of the asset at its original resolution off the main UI thread.
     * Avoids decoding the entire bitmap if possible to conserve memory.
     *
     * @param activity Activity in which this decoding request is made. Allows for early termination
     *                 of fetching image data and/or decoding to a bitmap. May be null, in which
     *                 case the request is made in the application context instead.
     * @param receiver Called with the decoded raw dimensions of the whole image or null if there
     *                 was an error decoding the dimensions.
     */
    public abstract void decodeRawDimensions(@Nullable Activity activity,
            DimensionsReceiver receiver);

    /**
     * Returns whether this asset has access to a separate, lower fidelity source of image data
     * (that may be able to be loaded more quickly to simulate progressive loading).
     */
    public boolean hasLowResDataSource() {
        return false;
    }

    /**
     * Loads the asset from the separate low resolution data source (if there is one) into the
     * provided ImageView with the placeholder color and bitmap transformation.
     *
     * @param transformation Bitmap transformation that can transform the thumbnail image
     *                       post-decoding.
     */
    public void loadLowResDrawable(Activity activity, ImageView imageView, int placeholderColor,
            BitmapTransformation transformation) {
        // No op
    }

    /**
     * Returns whether the asset supports rendering tile regions at varying pixel densities.
     */
    public abstract boolean supportsTiling();

    /**
     * Loads a Drawable for this asset into the provided ImageView. While waiting for the image to
     * load, first loads a ColorDrawable based on the provided placeholder color.
     *
     * @param context          Activity hosting the ImageView.
     * @param imageView        ImageView which is the target view of this asset.
     * @param placeholderColor Color of placeholder set to ImageView while waiting for image to
     *                         load.
     */
    public void loadDrawable(final Context context, final ImageView imageView,
            int placeholderColor) {
        // Transition from a placeholder ColorDrawable to the decoded bitmap when the ImageView in
        // question is empty.
        final boolean needsTransition = imageView.getDrawable() == null;
        final Drawable placeholderDrawable = new ColorDrawable(placeholderColor);
        if (needsTransition) {
            imageView.setImageDrawable(placeholderDrawable);
        }

        // Set requested height and width to the either the actual height and width of the view in
        // pixels, or if it hasn't been laid out yet, then to the absolute value of the layout
        // params.
        int width = imageView.getWidth() > 0
                ? imageView.getWidth()
                : Math.abs(imageView.getLayoutParams().width);
        int height = imageView.getHeight() > 0
                ? imageView.getHeight()
                : Math.abs(imageView.getLayoutParams().height);

        decodeBitmap(width, height, new BitmapReceiver() {
            @Override
            public void onBitmapDecoded(Bitmap bitmap) {
                if (!needsTransition) {
                    imageView.setImageBitmap(bitmap);
                    return;
                }

                Resources resources = context.getResources();

                Drawable[] layers = new Drawable[2];
                layers[0] = placeholderDrawable;
                layers[1] = new BitmapDrawable(resources, bitmap);

                TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
                transitionDrawable.setCrossFadeEnabled(true);

                imageView.setImageDrawable(transitionDrawable);
                transitionDrawable.startTransition(resources.getInteger(
                        android.R.integer.config_shortAnimTime));
            }
        });
    }

    /**
     * Loads a Drawable for this asset into the provided ImageView, providing a crossfade transition
     * with the given duration from the Drawable previously set on the ImageView.
     *
     * @param context                  Activity hosting the ImageView.
     * @param imageView                ImageView which is the target view of this asset.
     * @param transitionDurationMillis Duration of the crossfade, in milliseconds.
     * @param drawableLoadedListener   Listener called once the transition has begun.
     * @param placeholderColor         Color of the placeholder if the provided ImageView is empty
     *                                 before the
     */
    public void loadDrawableWithTransition(
            final Context context,
            final ImageView imageView,
            final int transitionDurationMillis,
            @Nullable final DrawableLoadedListener drawableLoadedListener,
            int placeholderColor) {
        Point imageViewDimensions = getViewDimensions(imageView);

        // Transition from a placeholder ColorDrawable to the decoded bitmap when the ImageView in
        // question is empty.
        boolean needsPlaceholder = imageView.getDrawable() == null;
        if (needsPlaceholder) {
            imageView.setImageDrawable(
                    getPlaceholderDrawable(context, imageView, placeholderColor));
        }

        decodeBitmap(imageViewDimensions.x, imageViewDimensions.y, new BitmapReceiver() {
            @Override
            public void onBitmapDecoded(Bitmap bitmap) {
                final Resources resources = context.getResources();

                new CenterCropBitmapTask(bitmap, imageView, new BitmapReceiver() {
                    @Override
                    public void onBitmapDecoded(@Nullable Bitmap newBitmap) {
                        Drawable[] layers = new Drawable[2];
                        Drawable existingDrawable = imageView.getDrawable();

                        if (existingDrawable instanceof TransitionDrawable) {
                            // Take only the second layer in the existing TransitionDrawable so
                            // we don't keep
                            // around a reference to older layers which are no longer shown (this
                            // way we avoid a
                            // memory leak).
                            TransitionDrawable existingTransitionDrawable =
                                    (TransitionDrawable) existingDrawable;
                            int id = existingTransitionDrawable.getId(1);
                            layers[0] = existingTransitionDrawable.findDrawableByLayerId(id);
                        } else {
                            layers[0] = existingDrawable;
                        }
                        layers[1] = new BitmapDrawable(resources, newBitmap);

                        TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
                        transitionDrawable.setCrossFadeEnabled(true);

                        imageView.setImageDrawable(transitionDrawable);
                        transitionDrawable.startTransition(transitionDurationMillis);

                        if (drawableLoadedListener != null) {
                            drawableLoadedListener.onDrawableLoaded();
                        }
                    }
                }).execute();
            }
        });
    }

    /**
     * Loads the image for this asset into the provided ImageView which is used for the preview.
     * While waiting for the image to load, first loads a ColorDrawable based on the provided
     * placeholder color.
     *
     * @param activity         Activity hosting the ImageView.
     * @param imageView        ImageView which is the target view of this asset.
     * @param placeholderColor Color of placeholder set to ImageView while waiting for image to
     *                         load.
     */
    public void loadPreviewImage(Activity activity, ImageView imageView, int placeholderColor) {
        boolean needsTransition = imageView.getDrawable() == null;
        Drawable placeholderDrawable = new ColorDrawable(placeholderColor);
        if (needsTransition) {
            imageView.setImageDrawable(placeholderDrawable);
        }

        decodeRawDimensions(activity, dimensions -> {
            if (dimensions == null) {
                loadDrawable(activity, imageView, placeholderColor);
                return;
            }

            Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
            Point screenSize = ScreenSizeCalculator.getInstance().getScreenSize(defaultDisplay);
            Rect visibleRawWallpaperRect =
                    WallpaperCropUtils.calculateVisibleRect(dimensions, screenSize);
            adjustCropRect(activity, dimensions, visibleRawWallpaperRect);

            BitmapCropper bitmapCropper = InjectorProvider.getInjector().getBitmapCropper();
            bitmapCropper.cropAndScaleBitmap(this, /* scale= */ 1f, visibleRawWallpaperRect,
                    new BitmapCropper.Callback() {
                        @Override
                        public void onBitmapCropped(Bitmap croppedBitmap) {
                            // Since the size of the cropped bitmap may not exactly the same with
                            // image view(maybe has 1px or 2px difference),
                            // so set CENTER_CROP to let the bitmap to fit the image view.
                            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            if (!needsTransition) {
                                imageView.setImageBitmap(croppedBitmap);
                                return;
                            }

                            Resources resources = activity.getResources();

                            Drawable[] layers = new Drawable[2];
                            layers[0] = placeholderDrawable;
                            layers[1] = new BitmapDrawable(resources, croppedBitmap);

                            TransitionDrawable transitionDrawable = new TransitionDrawable(layers);
                            transitionDrawable.setCrossFadeEnabled(true);

                            imageView.setImageDrawable(transitionDrawable);
                            transitionDrawable.startTransition(resources.getInteger(
                                    android.R.integer.config_shortAnimTime));
                        }

                        @Override
                        public void onError(@Nullable Throwable e) {

                        }
                    });
        });
    }

    /**
     * Interface for receiving decoded Bitmaps.
     */
    public interface BitmapReceiver {

        /**
         * Called with a decoded Bitmap object or null if there was an error decoding the bitmap.
         */
        void onBitmapDecoded(@Nullable Bitmap bitmap);
    }

    /**
     * Interface for receiving raw asset dimensions.
     */
    public interface DimensionsReceiver {

        /**
         * Called with raw dimensions of asset or null if the asset is unable to decode the raw
         * dimensions.
         *
         * @param dimensions Dimensions as a Point where width is represented by "x" and height by
         *                   "y".
         */
        void onDimensionsDecoded(@Nullable Point dimensions);
    }

    /**
     * Interface for being notified when a drawable has been loaded.
     */
    public interface DrawableLoadedListener {
        void onDrawableLoaded();
    }

    protected void adjustCropRect(Context context, Point assetDimensions, Rect cropRect) {
        WallpaperCropUtils.adjustCropRect(context, cropRect, true /* zoomIn */);
    }

    /**
     * Custom AsyncTask which returns a copy of the given bitmap which is center cropped and scaled
     * to fit in the given ImageView.
     */
    public static class CenterCropBitmapTask extends AsyncTask<Void, Void, Bitmap> {

        private Bitmap mBitmap;
        private BitmapReceiver mBitmapReceiver;

        private int mImageViewWidth;
        private int mImageViewHeight;

        public CenterCropBitmapTask(Bitmap bitmap, View view,
                BitmapReceiver bitmapReceiver) {
            mBitmap = bitmap;
            mBitmapReceiver = bitmapReceiver;

            Point imageViewDimensions = getViewDimensions(view);

            mImageViewWidth = imageViewDimensions.x;
            mImageViewHeight = imageViewDimensions.y;
        }

        @Override
        protected Bitmap doInBackground(Void... unused) {
            int measuredWidth = mImageViewWidth;
            int measuredHeight = mImageViewHeight;

            int bitmapWidth = mBitmap.getWidth();
            int bitmapHeight = mBitmap.getHeight();

            float scale = Math.min(
                    (float) bitmapWidth / measuredWidth,
                    (float) bitmapHeight / measuredHeight);

            Bitmap scaledBitmap = Bitmap.createScaledBitmap(
                    mBitmap, Math.round(bitmapWidth / scale), Math.round(bitmapHeight / scale),
                    true);

            int horizontalGutterPx = Math.max(0, (scaledBitmap.getWidth() - measuredWidth) / 2);
            int verticalGutterPx = Math.max(0, (scaledBitmap.getHeight() - measuredHeight) / 2);

            return Bitmap.createBitmap(
                    scaledBitmap,
                    horizontalGutterPx,
                    verticalGutterPx,
                    scaledBitmap.getWidth() - (2 * horizontalGutterPx),
                    scaledBitmap.getHeight() - (2 * verticalGutterPx));
        }

        @Override
        protected void onPostExecute(Bitmap newBitmap) {
            mBitmapReceiver.onBitmapDecoded(newBitmap);
        }
    }
}