1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.utils.image;
20
21 import java.lang.ref.WeakReference;
22
23 import com.squareup.picasso.Picasso.LoadedFrom;
24 import com.squareup.picasso.Target;
25
26 import org.exoplatform.singleton.AccountSetting;
27 import org.exoplatform.utils.ExoConnectionUtils;
28 import org.exoplatform.utils.ExoWebAddress;
29 import org.exoplatform.utils.ExoWebAddress.ParseException;
30 import org.exoplatform.utils.Log;
31 import org.exoplatform.utils.Utils;
32 import org.exoplatform.widget.PicassoTextView;
33
34 import android.graphics.Bitmap;
35 import android.graphics.Canvas;
36 import android.graphics.drawable.BitmapDrawable;
37 import android.graphics.drawable.Drawable;
38 import android.text.Html.ImageGetter;
39 import android.webkit.URLUtil;
40 import android.widget.TextView;
41
42
43
44
45
46
47 public class PicassoImageGetter implements ImageGetter {
48
49 private WeakReference<TextView> mTvRef;
50
51 public PicassoImageGetter(TextView textView) {
52 super();
53 mTvRef = new WeakReference<TextView>(textView);
54 }
55
56 private static final int PLACEHOLDER_SIZE = 300;
57
58 @Override
59 public Drawable getDrawable(String paramString) {
60 final DrawablePlaceHolder ret = new DrawablePlaceHolder();
61 ret.setBounds(0, 0, PLACEHOLDER_SIZE, PLACEHOLDER_SIZE);
62 TextView tv = Utils.getVal(mTvRef);
63 if (tv != null) {
64 PicassoGetterTarget target = new PicassoGetterTarget(tv, ret);
65 if (tv instanceof PicassoTextView) {
66 ((PicassoTextView) tv).addTarget(target);
67 }
68 String imageUrl = paramString;
69
70 ExoWebAddress imageAddr = null;
71 try {
72 imageAddr = new ExoWebAddress(imageUrl);
73 } catch (ParseException e) {
74 if (Log.LOGI)
75 Log.i(getClass().getName(), e.getMessage());
76 }
77 if (imageAddr == null || imageAddr.isRelativeURL()) {
78
79 imageUrl = AccountSetting.getInstance().getDomainName() + imageUrl;
80 }
81
82 if (!URLUtil.isNetworkUrl(imageUrl))
83 imageUrl = ExoConnectionUtils.HTTP + imageUrl;
84
85 ExoPicasso.picasso(tv.getContext())
86 .load(imageUrl)
87 .resize(PLACEHOLDER_SIZE, PLACEHOLDER_SIZE)
88 .centerInside()
89 .into(target);
90 }
91
92 return ret;
93 }
94
95 public static class PicassoGetterTarget implements Target {
96
97 private WeakReference<TextView> mTvRef;
98
99 private WeakReference<DrawablePlaceHolder> mDrawRef;
100
101 public PicassoGetterTarget(TextView textView, DrawablePlaceHolder drawable) {
102 super();
103 mTvRef = new WeakReference<TextView>(textView);
104 mDrawRef = new WeakReference<PicassoImageGetter.DrawablePlaceHolder>(drawable);
105 }
106
107 @Override
108 public void onBitmapFailed(Drawable arg0) {
109 if (Log.LOGD)
110 Log.d(this.getClass().getName(), "Image could not be loaded");
111 }
112
113 @Override
114 public void onBitmapLoaded(Bitmap bm, LoadedFrom arg1) {
115 TextView tv = Utils.getVal(mTvRef);
116 if (tv != null) {
117 DrawablePlaceHolder drawable = Utils.getVal(mDrawRef);
118 if (drawable != null) {
119 BitmapDrawable bd = new BitmapDrawable(tv.getResources(), bm);
120 bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
121 drawable.drawable = bd;
122 drawable.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
123 tv.setText(tv.getText());
124 }
125 }
126 }
127
128 @Override
129 public void onPrepareLoad(Drawable placeholder) {
130 }
131 }
132
133 public static class DrawablePlaceHolder extends BitmapDrawable {
134
135 private Drawable drawable;
136
137 @Override
138 public void draw(final Canvas canvas) {
139 if (drawable != null) {
140 drawable.draw(canvas);
141 }
142 }
143
144 public void setDrawable(Drawable drawable) {
145 this.drawable = drawable;
146 }
147
148 }
149 }