summaryrefslogtreecommitdiff
path: root/src/com/android/bitmap/drawable/ExtendedBitmapDrawable.java
blob: 6f52061ad827ea36a1e9012691bd2b6a8c356a05 (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
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
/*
 * Copyright (C) 2013 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.bitmap.drawable;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.animation.LinearInterpolator;

import com.android.bitmap.BitmapCache;
import com.android.bitmap.DecodeAggregator;
import com.android.bitmap.DecodeTask;
import com.android.bitmap.R;
import com.android.bitmap.RequestKey;
import com.android.bitmap.RequestKey.FileDescriptorFactory;
import com.android.bitmap.ReusableBitmap;
import com.android.bitmap.util.Trace;

/**
 * This class encapsulates all functionality needed to display a single image bitmap,
 * including request creation/cancelling, data unbinding and re-binding, and fancy animations
 * to draw upon state changes.
 * <p>
 * The actual bitmap decode work is handled by {@link DecodeTask}.
 * TODO: have this class extend from BasicBitmapDrawable
 */
public class ExtendedBitmapDrawable extends BasicBitmapDrawable implements
    Runnable, Parallaxable, DecodeAggregator.Callback {

    // Ordered display.
    private DecodeAggregator mDecodeAggregator;
    
    // Parallax.
    private float mParallaxFraction = 0.5f;
    private float mParallaxSpeedMultiplier;
    private static final float DECODE_VERTICAL_CENTER = 1f / 3;

    // Placeholder and progress.
    private static final int LOAD_STATE_UNINITIALIZED = 0;
    private static final int LOAD_STATE_NOT_YET_LOADED = 1;
    private static final int LOAD_STATE_LOADING = 2;
    private static final int LOAD_STATE_LOADED = 3;
    private static final int LOAD_STATE_FAILED = 4;
    private int mLoadState = LOAD_STATE_UNINITIALIZED;
    private Placeholder mPlaceholder;
    private Progress mProgress;
    private int mProgressDelayMs;
    private final Handler mHandler = new Handler();

    public static final boolean DEBUG = false;
    public static final String TAG = ExtendedBitmapDrawable.class.getSimpleName();

    public ExtendedBitmapDrawable(final Resources res, final BitmapCache cache,
            final boolean limitDensity, final DecodeAggregator decodeAggregator,
            final Drawable placeholder, final Drawable progress) {
        super(res, cache, limitDensity);

        // Ordered display.
        this.mDecodeAggregator = decodeAggregator;

        // Placeholder and progress.
        final int fadeOutDurationMs = res.getInteger(R.integer.bitmap_fade_animation_duration);
        final int tileColor = res.getColor(R.color.bitmap_placeholder_background_color);
        mProgressDelayMs = res.getInteger(R.integer.bitmap_progress_animation_delay);

        int placeholderSize = res.getDimensionPixelSize(R.dimen.placeholder_size);
        mPlaceholder = new Placeholder(placeholder.getConstantState().newDrawable(res), res,
                placeholderSize, placeholderSize, fadeOutDurationMs, tileColor);
        mPlaceholder.setCallback(this);

        int progressBarSize = res.getDimensionPixelSize(R.dimen.progress_bar_size);
        mProgress = new Progress(progress.getConstantState().newDrawable(res), res,
                progressBarSize, progressBarSize, fadeOutDurationMs, tileColor);
        mProgress.setCallback(this);
    }

    @Override
    public void setParallaxFraction(float fraction) {
        mParallaxFraction = fraction;
        invalidateSelf();
    }

    public void setParallaxSpeedMultiplier(final float parallaxSpeedMultiplier) {
        mParallaxSpeedMultiplier = parallaxSpeedMultiplier;
        invalidateSelf();
    }

    /**
     * This sets the drawable to the failed state, which remove all animations from the placeholder.
     * This is different from unbinding to the uninitialized state, where we expect animations.
     */
    public void showStaticPlaceholder() {
        setLoadState(LOAD_STATE_FAILED);
    }

    @Override
    protected void setImage(final RequestKey key) {
        if (mCurrKey != null && mCurrKey.equals(key)) {
            return;
        }

        if (mCurrKey != null && mDecodeAggregator != null) {
            mDecodeAggregator.forget(mCurrKey);
        }

        mHandler.removeCallbacks(this);
        // start from a clean slate on every bind
        // this allows the initial transition to be specially instantaneous, so e.g. a cache hit
        // doesn't unnecessarily trigger a fade-in
        setLoadState(LOAD_STATE_UNINITIALIZED);
        if (key == null) {
            setLoadState(LOAD_STATE_FAILED);
        }

        super.setImage(key);
    }

    @Override
    protected void setBitmap(ReusableBitmap bmp) {
        setLoadState((bmp != null) ? LOAD_STATE_LOADED : LOAD_STATE_FAILED);

        super.setBitmap(bmp);
    }

    @Override
    protected void decode(final FileDescriptorFactory factory) {
        boolean executeStateChange = shouldExecuteStateChange();
        if (executeStateChange) {
            setLoadState(LOAD_STATE_NOT_YET_LOADED);
        }

        super.decode(factory);
    }

    protected boolean shouldExecuteStateChange() {
        // TODO: AttachmentDrawable should override this method to match prev and curr request keys.
        return /* opts.stateChanges */ true;
    }

    @Override
    public float getDrawVerticalCenter() {
        return mParallaxFraction;
    }

    @Override
    protected float getDrawVerticalOffsetMultiplier() {
        return mParallaxSpeedMultiplier;
    }

    @Override
    protected float getDecodeVerticalCenter() {
        return DECODE_VERTICAL_CENTER;
    }

    @Override
    public void draw(final Canvas canvas) {
        final Rect bounds = getBounds();
        if (bounds.isEmpty()) {
            return;
        }

        super.draw(canvas);

        // Draw the two possible overlay layers in reverse-priority order.
        // (each layer will no-op the draw when appropriate)
        // This ordering means cross-fade transitions are just fade-outs of each layer.
        mProgress.draw(canvas);
        mPlaceholder.draw(canvas);
    }

    @Override
    public void setAlpha(int alpha) {
        final int old = mPaint.getAlpha();
        super.setAlpha(alpha);
        mPlaceholder.setAlpha(alpha);
        mProgress.setAlpha(alpha);
        if (alpha != old) {
            invalidateSelf();
        }
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        super.setColorFilter(cf);
        mPlaceholder.setColorFilter(cf);
        mProgress.setColorFilter(cf);
        invalidateSelf();
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);

        mPlaceholder.setBounds(bounds);
        mProgress.setBounds(bounds);
    }

    @Override
    public void onDecodeBegin(final RequestKey key) {
        if (mDecodeAggregator != null) {
            mDecodeAggregator.expect(key, this);
        } else {
            onBecomeFirstExpected(key);
        }
        super.onDecodeBegin(key);
    }

    @Override
    public void onBecomeFirstExpected(final RequestKey key) {
        if (!key.equals(mCurrKey)) {
            return;
        }
        // normally, we'd transition to the LOADING state now, but we want to delay that a bit
        // to minimize excess occurrences of the rotating spinner
        mHandler.postDelayed(this, mProgressDelayMs);
    }

    @Override
    public void run() {
        if (mLoadState == LOAD_STATE_NOT_YET_LOADED) {
            setLoadState(LOAD_STATE_LOADING);
        }
    }

    @Override
    public void onDecodeComplete(final RequestKey key, final ReusableBitmap result) {
        if (mDecodeAggregator != null) {
            mDecodeAggregator.execute(key, new Runnable() {
                @Override
                public void run() {
                    ExtendedBitmapDrawable.super.onDecodeComplete(key, result);
                }

                @Override
                public String toString() {
                    return "DONE";
                }
            });
        } else {
            super.onDecodeComplete(key, result);
        }
    }

    @Override
    public void onDecodeCancel(final RequestKey key) {
        if (mDecodeAggregator != null) {
            mDecodeAggregator.forget(key);
        }
        super.onDecodeCancel(key);
    }

    /**
     * Each attachment gets its own placeholder and progress indicator, to be shown, hidden,
     * and animated based on Drawable#setVisible() changes, which are in turn driven by
     * setLoadState().
     */
    private void setLoadState(int loadState) {
        if (DEBUG) {
            Log.v(TAG, String.format("IN setLoadState. old=%s new=%s key=%s this=%s",
                    mLoadState, loadState, mCurrKey, this));
        }
        if (mLoadState == loadState) {
            if (DEBUG) {
                Log.v(TAG, "OUT no-op setLoadState");
            }
            return;
        }

        Trace.beginSection("set load state");
        switch (loadState) {
            // This state differs from LOADED in that the subsequent state transition away from
            // UNINITIALIZED will not have a fancy transition. This allows list item binds to
            // cached data to take immediate effect without unnecessary whizzery.
            case LOAD_STATE_UNINITIALIZED:
                mPlaceholder.reset();
                mProgress.reset();
                break;
            case LOAD_STATE_NOT_YET_LOADED:
                mPlaceholder.setPulseEnabled(true);
                mPlaceholder.setVisible(true);
                mProgress.setVisible(false);
                break;
            case LOAD_STATE_LOADING:
                mPlaceholder.setVisible(false);
                mProgress.setVisible(true);
                break;
            case LOAD_STATE_LOADED:
                mPlaceholder.setVisible(false);
                mProgress.setVisible(false);
                break;
            case LOAD_STATE_FAILED:
                mPlaceholder.setPulseEnabled(false);
                mPlaceholder.setVisible(true);
                mProgress.setVisible(false);
                break;
        }
        Trace.endSection();

        mLoadState = loadState;
        boolean placeholderVisible = mPlaceholder != null && mPlaceholder.isVisible();
        boolean progressVisible = mProgress != null && mProgress.isVisible();

        if (DEBUG) {
            Log.v(TAG, String.format("OUT stateful setLoadState. new=%s placeholder=%s progress=%s",
                    loadState, placeholderVisible, progressVisible));
        }
    }

    private static class Placeholder extends TileDrawable {

        private final ValueAnimator mPulseAnimator;
        private boolean mPulseEnabled = true;
        private float mPulseAlphaFraction = 1f;

        public Placeholder(Drawable placeholder, Resources res,
                int placeholderWidth, int placeholderHeight, int fadeOutDurationMs,
                int tileColor) {
            super(placeholder, placeholderWidth, placeholderHeight, tileColor, fadeOutDurationMs);
            mPulseAnimator = ValueAnimator.ofInt(55, 255)
                    .setDuration(res.getInteger(R.integer.bitmap_placeholder_animation_duration));
            mPulseAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mPulseAnimator.setRepeatMode(ValueAnimator.REVERSE);
            mPulseAnimator.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mPulseAlphaFraction = ((Integer) animation.getAnimatedValue()) / 255f;
                    setInnerAlpha(getCurrentAlpha());
                }
            });
            mFadeOutAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    stopPulsing();
                }
            });
        }

        @Override
        public void setInnerAlpha(final int alpha) {
            super.setInnerAlpha((int) (alpha * mPulseAlphaFraction));
        }

        public void setPulseEnabled(boolean enabled) {
            mPulseEnabled = enabled;
            if (!mPulseEnabled) {
                stopPulsing();
            }
        }

        private void stopPulsing() {
            if (mPulseAnimator != null) {
                mPulseAnimator.cancel();
                mPulseAlphaFraction = 1f;
                setInnerAlpha(getCurrentAlpha());
            }
        }

        @Override
        public boolean setVisible(boolean visible) {
            final boolean changed = super.setVisible(visible);
            if (changed) {
                if (isVisible()) {
                    // start
                    if (mPulseAnimator != null && mPulseEnabled) {
                        mPulseAnimator.start();
                    }
                } else {
                    // can't cancel the pulsing yet-- wait for the fade-out animation to end
                    // one exception: if alpha is already zero, there is no fade-out, so stop now
                    if (getCurrentAlpha() == 0) {
                        stopPulsing();
                    }
                }
            }
            return changed;
        }

    }

    private static class Progress extends TileDrawable {

        private final ValueAnimator mRotateAnimator;

        public Progress(Drawable progress, Resources res,
                int progressBarWidth, int progressBarHeight, int fadeOutDurationMs,
                int tileColor) {
            super(progress, progressBarWidth, progressBarHeight, tileColor, fadeOutDurationMs);

            mRotateAnimator = ValueAnimator.ofInt(0, 10000)
                    .setDuration(res.getInteger(R.integer.bitmap_progress_animation_duration));
            mRotateAnimator.setInterpolator(new LinearInterpolator());
            mRotateAnimator.setRepeatCount(ValueAnimator.INFINITE);
            mRotateAnimator.addUpdateListener(new AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    setLevel((Integer) animation.getAnimatedValue());
                }
            });
            mFadeOutAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    if (mRotateAnimator != null) {
                        mRotateAnimator.cancel();
                    }
                }
            });
        }

        @Override
        public boolean setVisible(boolean visible) {
            final boolean changed = super.setVisible(visible);
            if (changed) {
                if (isVisible()) {
                    if (mRotateAnimator != null) {
                        mRotateAnimator.start();
                    }
                } else {
                    // can't cancel the rotate yet-- wait for the fade-out animation to end
                    // one exception: if alpha is already zero, there is no fade-out, so stop now
                    if (getCurrentAlpha() == 0 && mRotateAnimator != null) {
                        mRotateAnimator.cancel();
                    }
                }
            }
            return changed;
        }

    }
}