View Javadoc
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.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   * Created by The eXo Platform SAS
44   * 
45   * @author MinhTDH MinhTDH@exoplatform.com Jul 31, 2015
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        // Check the image URL format
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          // relative URL => prefix with current server URL
79          imageUrl = AccountSetting.getInstance().getDomainName() + imageUrl;
80        }
81        // add http protocol if the URL has none
82        if (!URLUtil.isNetworkUrl(imageUrl))
83          imageUrl = ExoConnectionUtils.HTTP + imageUrl;
84        // load the image
85        ExoPicasso.picasso(tv.getContext())
86                  .load(imageUrl)
87                  .resize(PLACEHOLDER_SIZE, PLACEHOLDER_SIZE)
88                  .centerInside()
89                  .into(target);
90      }
91      // TODO add loading placeholder
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 }