1 /*
2 * Copyright (C) 2003-2015 eXo Platform SAS.
3 *
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this software; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18 */
19 package org.exoplatform.widget;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.exoplatform.utils.image.ExoPicasso;
25
26 import com.squareup.picasso.Target;
27
28 import android.content.Context;
29 import android.util.AttributeSet;
30 import android.widget.TextView;
31
32 /**
33 * Created by The eXo Platform SAS for use as target to load bitmap by Picasso
34 * lib, auto cancel request when detached
35 *
36 * @author MinhTDH MinhTDH@exoplatform.com Jul 31, 2015
37 */
38 public class PicassoTextView extends TextView {
39
40 private List<Target> mTargets;
41
42 public PicassoTextView(Context context, AttributeSet attrs, int defStyle) {
43 super(context, attrs, defStyle);
44 }
45
46 public PicassoTextView(Context context, AttributeSet attrs) {
47 super(context, attrs);
48 }
49
50 public PicassoTextView(Context context) {
51 super(context);
52 }
53
54 @Override
55 protected void onDetachedFromWindow() {
56 if (mTargets != null) {
57 for (Target target : mTargets) {
58 if (target != null) {
59 ExoPicasso.picasso(getContext()).cancelRequest(target);
60 }
61 }
62 }
63 super.onDetachedFromWindow();
64 }
65
66 public void addTarget(Target target) {
67 if (mTargets == null) {
68 mTargets = new ArrayList<Target>();
69 }
70 mTargets.add(target);
71 }
72 }