View Javadoc
1   /*
2    * Copyright (C) 2003-2014 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.ui.social;
20  
21  import java.net.URLConnection;
22  import java.util.Locale;
23  
24  import org.exoplatform.R;
25  import org.exoplatform.model.SocialActivityInfo;
26  import org.exoplatform.utils.ExoConstants;
27  import org.exoplatform.utils.ExoDocumentUtils;
28  import org.exoplatform.utils.ExoUtils;
29  import org.exoplatform.utils.SocialActivityUtil;
30  import org.exoplatform.utils.image.EmptyImageGetter;
31  import org.exoplatform.utils.image.ExoPicasso;
32  import org.exoplatform.utils.image.RoundedCornersTranformer;
33  import org.exoplatform.widget.StandardArrayAdapter;
34  
35  import android.content.Context;
36  import android.content.res.Resources;
37  import android.graphics.Color;
38  import android.net.Uri;
39  import android.text.Html;
40  import android.view.View;
41  import android.view.View.OnClickListener;
42  import android.widget.Button;
43  import android.widget.ImageView;
44  import android.widget.LinearLayout;
45  import android.widget.TextView;
46  
47  /**
48   * Represents activity item on activity stream
49   */
50  public class SocialActivityStreamItem {
51    private static final String FONT_COLOR     = "#696969";
52  
53    /**
54     * We are not on the Home screen
55     */
56    private final boolean       IS_HOME_STREAM = false;
57  
58    public LinearLayout         contentLayoutWrap;
59  
60    private ImageView           imageViewAvatar;
61  
62    public TextView             textViewName;
63  
64    public TextView             textViewMessage;
65  
66    public TextView             textViewTempMessage;
67  
68    private TextView            textViewCommnet;
69  
70    private Button              buttonComment;
71  
72    private Button              buttonLike;
73  
74    private ImageView           typeImageView;
75  
76    private TextView            textViewTime;
77  
78    private View                attachStubView;
79  
80    private String              domain;
81  
82    private String              userName;
83  
84    private Context             mContext;
85  
86    private SocialActivityInfo  activityInfo;
87  
88    private boolean             isDetail;
89  
90    private Resources           resource;
91  
92    private static final String TAG            = "eXo____SocialItem____";
93  
94    public SocialActivityStreamItem(Context context, StandardArrayAdapter.ViewHolder holder, SocialActivityInfo info, boolean is) {
95      mContext = context;
96      resource = mContext.getResources();
97      activityInfo = info;
98      isDetail = is;
99      domain = SocialActivityUtil.getDomain();
100     imageViewAvatar = holder.imageViewAvatar;
101     contentLayoutWrap = holder.contentLayoutWrap;
102     textViewName = holder.textViewName;
103     textViewName.setLinkTextColor(Color.rgb(21, 94, 173));
104     textViewMessage = holder.textViewMessage;
105     textViewTempMessage = holder.textViewTempMessage;
106     textViewCommnet = holder.textViewCommnet;
107     buttonComment = holder.buttonComment;
108     buttonLike = holder.buttonLike;
109     typeImageView = holder.typeImageView;
110     textViewTime = holder.textViewTime;
111     attachStubView = holder.attachStubView;
112   }
113 
114   public void initCommonInfo() {
115 
116     String avatarUrl = activityInfo.getImageUrl();
117     if (avatarUrl != null) {
118       ExoPicasso.picasso(mContext)
119                 .load(Uri.parse(avatarUrl))
120                 .transform(new RoundedCornersTranformer(mContext))
121                 .placeholder(R.drawable.default_avatar)
122                 .error(R.drawable.default_avatar)
123                 .fit()
124                 .into(imageViewAvatar);
125     }
126 
127     userName = activityInfo.getUserName();
128     textViewName.setText(Html.fromHtml(userName));
129     textViewMessage.setText(Html.fromHtml(activityInfo.getTitle(), new EmptyImageGetter(mContext), null),
130                             TextView.BufferType.SPANNABLE);
131     textViewMessage.setVisibility(View.VISIBLE);
132     textViewTime.setText(SocialActivityUtil.getPostedTimeString(mContext,
133                             activityInfo.getUpdatedTime() != 0 ? activityInfo.getUpdatedTime()
134                                                                : activityInfo.getPostedTime()));
135 
136     buttonComment.setText("" + activityInfo.getCommentNumber());
137     buttonLike.setText("" + activityInfo.getLikeNumber());
138     textViewTempMessage.setVisibility(View.GONE);
139     textViewCommnet.setVisibility(View.GONE);
140     attachStubView.setVisibility(View.GONE);
141 
142     int imageId = SocialActivityUtil.getActivityTypeId(activityInfo.getType());
143     SocialActivityUtil.setImageType(imageId, typeImageView);
144     setViewByType(imageId);
145     setDetailView();
146   }
147 
148   private void setDetailView() {
149     if (isDetail) {
150       contentLayoutWrap.setBackgroundDrawable(null);
151       contentLayoutWrap.setPadding(5, -2, 5, 5);
152       buttonComment.setVisibility(View.GONE);
153       buttonLike.setVisibility(View.GONE);
154 
155       textViewName.setMaxLines(100);
156       textViewMessage.setMaxLines(100);
157       textViewTempMessage.setMaxLines(100);
158       textViewCommnet.setMaxLines(100);
159     }
160   }
161 
162   private void setViewByType(int typeId) {
163     String spaceInfo = null;
164     switch (typeId) {
165 
166     case SocialActivityUtil.ACTIVITY_TYPE_FORUM:
167       setActivityTypeForum();
168       break;
169 
170     case SocialActivityUtil.ACTIVITY_TYPE_WIKI:
171       setActivityTypeWiki();
172       break;
173 
174     case SocialActivityUtil.ACTIVITY_TYPE_SPACE:
175       /* add space information */
176       spaceInfo = SocialActivityUtil.getHeaderWithSpaceInfo(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
177       if (spaceInfo != null)
178         textViewName.setText(Html.fromHtml(spaceInfo), TextView.BufferType.SPANNABLE);
179       break;
180 
181     case SocialActivityUtil.ACTIVITY_TYPE_DOC:
182       /* add space information */
183       spaceInfo = SocialActivityUtil.getHeaderWithSpaceInfo(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
184       if (spaceInfo != null)
185         textViewName.setText(Html.fromHtml(spaceInfo), TextView.BufferType.SPANNABLE);
186       /* add document info */
187       String tempMessage = activityInfo.templateParams.get("MESSAGE");
188       if (tempMessage != null)
189         textViewMessage.setText(tempMessage.trim());
190 
191       String docLink = activityInfo.templateParams.get("DOCLINK");
192       if (docLink != null) {
193         String docName = activityInfo.templateParams.get("DOCNAME");
194         String url = domain + docLink;
195         /** get mimetype from url */
196         String mimeTypeExtension = URLConnection.guessContentTypeFromName(url);
197         displayAttachImage(url, docName, null, mimeTypeExtension, false);
198       }
199       break;
200 
201     case SocialActivityUtil.ACTIVITY_TYPE_NORMAL:
202       /* add space information */
203       spaceInfo = SocialActivityUtil.getHeaderWithSpaceInfo(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
204       if (spaceInfo != null) {
205         textViewName.setText(Html.fromHtml(spaceInfo), TextView.BufferType.SPANNABLE);
206       }
207       break;
208 
209     case SocialActivityUtil.ACTIVITY_TYPE_LINK:
210       setActivityTypeLink();
211       break;
212 
213     case SocialActivityUtil.ACTIVITY_TYPE_RELATIONSHIP:
214       break;
215 
216     case SocialActivityUtil.ACTIVITY_TYPE_PEOPLE:
217       break;
218 
219     case SocialActivityUtil.ACTIVITY_TYPE_CONTENT:
220       /* add space information */
221       spaceInfo = SocialActivityUtil.getHeaderWithSpaceInfo(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
222       if (spaceInfo != null)
223         textViewName.setText(Html.fromHtml(spaceInfo), TextView.BufferType.SPANNABLE);
224       /* add content info */
225       String contentLink = activityInfo.templateParams.get("contenLink");
226       if (contentLink != null) {
227 
228         String contentType = activityInfo.templateParams.get("mimeType");
229         if (contentType != null) {
230           String contentName = activityInfo.templateParams.get("contentName");
231           StringBuffer buffer = new StringBuffer();
232           buffer.append(domain);
233           buffer.append("/portal/rest/jcr/");
234           buffer.append(contentLink);
235           displayAttachImage(buffer.toString(), contentName, null, contentType, true);
236         }
237 
238       }
239       break;
240     case SocialActivityUtil.ACTIVITY_TYPE_ANSWER:
241       setActivityTypeAnswer();
242       break;
243 
244     case SocialActivityUtil.ACTIVITY_TYPE_CALENDAR:
245       setActivityTypeCalendar();
246       break;
247 
248     default:
249       break;
250     }
251   }
252 
253   private void setActivityTypeForum() {
254     String forumBuffer = SocialActivityUtil.getActivityTypeForum(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
255 
256     textViewName.setText(Html.fromHtml(forumBuffer), TextView.BufferType.SPANNABLE);
257     String forumBody = activityInfo.getBody();
258 
259     textViewMessage.setText(Html.fromHtml(forumBody), TextView.BufferType.SPANNABLE);
260 
261   }
262 
263   private void setActivityTypeWiki() {
264     String wikiBuffer = SocialActivityUtil.getActivityTypeWiki(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
265     textViewName.setText(Html.fromHtml(wikiBuffer), TextView.BufferType.SPANNABLE);
266     String wikiBody = activityInfo.getBody();
267     if (wikiBody == null || wikiBody.equalsIgnoreCase("body")) {
268       // use the page excerpt if no body is available
269       wikiBody = activityInfo.templateParams != null ? activityInfo.templateParams.get("page_exceprt") : null;
270     }
271     if (wikiBody == null) {
272       textViewMessage.setVisibility(View.GONE);
273     } else {
274       textViewMessage.setText(Html.fromHtml(wikiBody), TextView.BufferType.SPANNABLE);
275     }
276   }
277 
278   private void setActivityTypeAnswer() {
279 
280     String answerBuffer = SocialActivityUtil.getActivityTypeAnswer(userName, activityInfo, resource, FONT_COLOR, IS_HOME_STREAM);
281 
282     textViewName.setText(Html.fromHtml(answerBuffer), TextView.BufferType.SPANNABLE);
283 
284     String answerBody = activityInfo.getBody();
285     if (answerBody != null) {
286       textViewMessage.setText(Html.fromHtml(answerBody), TextView.BufferType.SPANNABLE);
287     }
288   }
289 
290   private void setActivityTypeCalendar() {
291     String calendarBuffer = SocialActivityUtil.getActivityTypeCalendar(userName,
292                                                                        activityInfo,
293                                                                        resource,
294                                                                        FONT_COLOR,
295                                                                        IS_HOME_STREAM);
296 
297     textViewName.setText(Html.fromHtml(calendarBuffer), TextView.BufferType.SPANNABLE);
298     SocialActivityUtil.setCalendarContent(textViewMessage, activityInfo, resource);
299 
300   }
301 
302   private void setActivityTypeLink() {
303 
304     String info = SocialActivityUtil.getLinkActivityInfo(activityInfo, FONT_COLOR, resource);
305     textViewName.setText(Html.fromHtml(info), TextView.BufferType.SPANNABLE);
306 
307     String templateComment = activityInfo.templateParams.get("comment");
308     String description = activityInfo.templateParams.get("description");
309 
310     if (templateComment != null && !templateComment.equalsIgnoreCase("")) {
311       textViewMessage.setText(Html.fromHtml(templateComment, new EmptyImageGetter(mContext), null),
312                               TextView.BufferType.SPANNABLE);
313     }
314     if (description != null) {
315       textViewCommnet.setText(Html.fromHtml(description.trim()), TextView.BufferType.SPANNABLE);
316       textViewCommnet.setVisibility(View.VISIBLE);
317     }
318 
319     String linkBuffer = SocialActivityUtil.getActivityTypeLink(description, activityInfo, FONT_COLOR, IS_HOME_STREAM);
320 
321     String imageParams = activityInfo.templateParams.get("image");
322     if ((imageParams != null) && (imageParams.toLowerCase(Locale.US).contains(ExoConstants.HTTP_PROTOCOL))) {
323       displayAttachImage(imageParams, "", linkBuffer, "image", true);
324     } else {
325       textViewTempMessage.setText(Html.fromHtml(linkBuffer), TextView.BufferType.SPANNABLE);
326       textViewTempMessage.setVisibility(View.VISIBLE);
327     }
328   }
329 
330   private void displayAttachImage(String url, String name, String description, String fileType, boolean isLinkType) {
331     initAttachStubView(url, name, description, fileType, isLinkType);
332     attachStubView.setVisibility(View.VISIBLE);
333   }
334 
335   private void initAttachStubView(final String url,
336                                   final String fileName,
337                                   String description,
338                                   final String fileType,
339                                   boolean isLinkType) {
340     ImageView attachImage = (ImageView) attachStubView.findViewById(R.id.attached_image_view);
341     TextView txtViewFileName = (TextView) attachStubView.findViewById(R.id.textView_file_name);
342     if (description == null)
343       txtViewFileName.setText(fileName);
344     else {
345       txtViewFileName.setText(Html.fromHtml(description), TextView.BufferType.SPANNABLE);
346 
347       if (isDetail) {
348         SocialActivityUtil.setTextLinkify(txtViewFileName);
349         txtViewFileName.setMaxLines(100);
350       }
351     }
352 
353     if (fileType != null && fileType.startsWith(ExoDocumentUtils.IMAGE_TYPE)) {
354       int errorDrawable = isLinkType ? R.drawable.icon_for_unreadable_link : R.drawable.icon_for_placeholder_image;
355       int maxHeight = resource.getDimensionPixelSize(R.dimen.attachment_max_height);
356       ExoPicasso.picasso(mContext)
357                 .load(Uri.parse(ExoUtils.encodeDocumentUrl(url)))
358                 .placeholder(R.drawable.loading_rect)
359                 .error(errorDrawable)
360                 .resize(maxHeight, maxHeight)
361                 .centerCrop()
362                 .onlyScaleDown()
363                 .into(attachImage);
364     } else {
365       attachImage.setImageResource(ExoDocumentUtils.getIconFromType(fileType));
366     }
367     if (isDetail) {
368       /*
369        * Open file with compatible application
370        */
371       attachImage.setOnClickListener(new OnClickListener() {
372 
373         @Override
374         public void onClick(View v) {
375 
376           ExoDocumentUtils.fileOpen(mContext, fileType, url, fileName);
377         }
378       });
379     }
380   }
381 }