Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2013 Square, Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
michael@0 | 5 | * you may not use this file except in compliance with the License. |
michael@0 | 6 | * You may obtain a copy of the License at |
michael@0 | 7 | * |
michael@0 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 9 | * |
michael@0 | 10 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 13 | * See the License for the specific language governing permissions and |
michael@0 | 14 | * limitations under the License. |
michael@0 | 15 | */ |
michael@0 | 16 | package com.squareup.picasso; |
michael@0 | 17 | |
michael@0 | 18 | import android.view.ViewTreeObserver; |
michael@0 | 19 | import android.widget.ImageView; |
michael@0 | 20 | import java.lang.ref.WeakReference; |
michael@0 | 21 | |
michael@0 | 22 | class DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener { |
michael@0 | 23 | |
michael@0 | 24 | final RequestCreator creator; |
michael@0 | 25 | final WeakReference<ImageView> target; |
michael@0 | 26 | Callback callback; |
michael@0 | 27 | |
michael@0 | 28 | DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback) { |
michael@0 | 29 | this.creator = creator; |
michael@0 | 30 | this.target = new WeakReference<ImageView>(target); |
michael@0 | 31 | this.callback = callback; |
michael@0 | 32 | target.getViewTreeObserver().addOnPreDrawListener(this); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | @Override public boolean onPreDraw() { |
michael@0 | 36 | ImageView target = this.target.get(); |
michael@0 | 37 | if (target == null) { |
michael@0 | 38 | return true; |
michael@0 | 39 | } |
michael@0 | 40 | ViewTreeObserver vto = target.getViewTreeObserver(); |
michael@0 | 41 | if (!vto.isAlive()) { |
michael@0 | 42 | return true; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | int width = target.getMeasuredWidth(); |
michael@0 | 46 | int height = target.getMeasuredHeight(); |
michael@0 | 47 | |
michael@0 | 48 | if (width <= 0 || height <= 0) { |
michael@0 | 49 | return true; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | vto.removeOnPreDrawListener(this); |
michael@0 | 53 | |
michael@0 | 54 | this.creator.unfit().resize(width, height).into(target, callback); |
michael@0 | 55 | return true; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | void cancel() { |
michael@0 | 59 | callback = null; |
michael@0 | 60 | ImageView target = this.target.get(); |
michael@0 | 61 | if (target == null) { |
michael@0 | 62 | return; |
michael@0 | 63 | } |
michael@0 | 64 | ViewTreeObserver vto = target.getViewTreeObserver(); |
michael@0 | 65 | if (!vto.isAlive()) { |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | vto.removeOnPreDrawListener(this); |
michael@0 | 69 | } |
michael@0 | 70 | } |