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.widget;
20  
21  import java.util.ArrayList;
22  
23  import org.exoplatform.R;
24  import org.exoplatform.model.SocialActivityInfo;
25  import org.exoplatform.singleton.SocialDetailHelper;
26  import org.exoplatform.singleton.SocialServiceHelper;
27  import org.exoplatform.social.client.api.SocialClientLibException;
28  import org.exoplatform.social.client.api.model.RestActivity;
29  import org.exoplatform.ui.social.AllUpdatesFragment;
30  import org.exoplatform.ui.social.ComposeMessageActivity;
31  import org.exoplatform.ui.social.MyConnectionsFragment;
32  import org.exoplatform.ui.social.MySpacesFragment;
33  import org.exoplatform.ui.social.MyStatusFragment;
34  import org.exoplatform.ui.social.SocialActivityStreamItem;
35  import org.exoplatform.ui.social.SocialDetailActivity;
36  import org.exoplatform.ui.social.SocialTabsActivity;
37  import org.exoplatform.utils.ExoConnectionUtils;
38  import org.exoplatform.utils.ExoConstants;
39  import org.exoplatform.utils.ExoUtils;
40  import org.exoplatform.utils.Log;
41  
42  import android.content.Context;
43  import android.content.Intent;
44  import android.graphics.Color;
45  import android.os.AsyncTask;
46  import android.view.LayoutInflater;
47  import android.view.MenuItem;
48  import android.view.View;
49  import android.view.View.OnClickListener;
50  import android.view.ViewGroup;
51  import android.view.ViewStub;
52  import android.widget.ArrayAdapter;
53  import android.widget.Button;
54  import android.widget.ImageView;
55  import android.widget.LinearLayout;
56  import android.widget.TextView;
57  
58  public class StandardArrayAdapter extends ArrayAdapter<SocialActivityInfo> {
59  
60    private final ArrayList<SocialActivityInfo> items;
61  
62    private Context                             mContext;
63  
64    private LayoutInflater                      mInflater;
65  
66    private ViewHolder                          holder = null;
67  
68    private LikeLoadTask                        mLoadTask;
69  
70    private static final String                 TAG    = "eXo____StandardArrayAdapter____";
71  
72    public StandardArrayAdapter(Context context, ArrayList<SocialActivityInfo> items) {
73      super(context, R.layout.activitybrowserviewcell, items);
74      mContext = context;
75      this.items = items;
76      mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
77    }
78  
79    @Override
80    public View getView(final int position, View convertView, ViewGroup parent) {
81      final SocialActivityInfo actInfo = items.get(position);
82  
83      if (convertView == null) {
84        convertView = mInflater.inflate(R.layout.activitybrowserviewcell, null);
85        holder = new ViewHolder();
86        holder.imageViewAvatar = (ImageView) convertView.findViewById(R.id.imageView_Avatar);
87        holder.contentLayoutWrap = (LinearLayout) convertView.findViewById(R.id.relativeLayout_Content);
88        holder.textViewName = (TextView) convertView.findViewById(R.id.textView_Name);
89        holder.textViewName.setLinkTextColor(Color.rgb(21, 94, 173));
90        holder.textViewMessage = (TextView) convertView.findViewById(R.id.textView_Message);
91        holder.textViewTempMessage = (TextView) convertView.findViewById(R.id.textview_temp_message);
92        holder.textViewCommnet = (TextView) convertView.findViewById(R.id.activity_comment_view);
93        holder.buttonComment = (Button) convertView.findViewById(R.id.button_Comment);
94        holder.buttonLike = (Button) convertView.findViewById(R.id.button_Like);
95        holder.typeImageView = (ImageView) convertView.findViewById(R.id.activity_image_type);
96        holder.textViewTime = (TextView) convertView.findViewById(R.id.textView_Time);
97        holder.attachStubView = ((ViewStub) convertView.findViewById(R.id.attached_image_stub_activity)).inflate();
98        convertView.setTag(holder);
99      } else {
100       holder = (ViewHolder) convertView.getTag();
101     }
102 
103     SocialActivityStreamItem socialActivityStreamItem = new SocialActivityStreamItem(mContext, holder, actInfo, false);
104     socialActivityStreamItem.initCommonInfo();
105     holder.contentLayoutWrap.setOnClickListener(new OnClickListener() {
106 
107       public void onClick(View v) {
108         // TODO open the image immediately if there is one,
109         // otherwise open the details screen
110         String activityId = actInfo.getActivityId();
111         SocialDetailHelper.getInstance().setActivityId(activityId);
112         SocialDetailHelper.getInstance().setAttachedImageUrl(actInfo.getAttachedImageUrl());
113         Intent intent = new Intent(mContext, SocialDetailActivity.class);
114         intent.putExtra(ExoConstants.ACTIVITY_CURRENT_POSITION, position);
115         intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
116         mContext.startActivity(intent);
117       }
118     });
119 
120     holder.buttonComment.setOnClickListener(new View.OnClickListener() {
121 
122       public void onClick(View v) {
123 
124         SocialDetailHelper.getInstance().setActivityId(actInfo.getActivityId());
125         Intent intent = new Intent(mContext, ComposeMessageActivity.class);
126         intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
127         intent.putExtra(ExoConstants.ACTIVITY_CURRENT_POSITION, position);
128         intent.putExtra(ExoConstants.COMPOSE_TYPE, ExoConstants.COMPOSE_COMMENT_TYPE);
129         mContext.startActivity(intent);
130 
131       }
132     });
133 
134     holder.buttonLike.setOnClickListener(new OnClickListener() {
135 
136       @Override
137       public void onClick(View v) {
138         if (ExoConnectionUtils.isNetworkAvailableExt(mContext)) {
139           onLikeLoad(actInfo, position);
140         } else {
141           new ConnectionErrorDialog(mContext).show();
142         }
143 
144       }
145     });
146 
147     return convertView;
148   }
149 
150   private void onLikeLoad(SocialActivityInfo info, int position) {
151     if (mLoadTask == null || mLoadTask.getStatus() == LikeLoadTask.Status.FINISHED) {
152       mLoadTask = (LikeLoadTask) new LikeLoadTask(position).execute(info);
153     }
154   }
155 
156   public static class ViewHolder {
157     public LinearLayout contentLayoutWrap;
158 
159     public ImageView    imageViewAvatar;
160 
161     public TextView     textViewName;
162 
163     public TextView     textViewMessage;
164 
165     public TextView     textViewTempMessage;
166 
167     public TextView     textViewCommnet;
168 
169     public Button       buttonComment;
170 
171     public Button       buttonLike;
172 
173     public ImageView    typeImageView;
174 
175     public TextView     textViewTime;
176 
177     public View         attachStubView;
178   }
179 
180   private class LikeLoadTask extends AsyncTask<SocialActivityInfo, Void, Boolean> {
181 
182     private MenuItem loaderItem;
183 
184     private int      currentPosition;
185 
186     public LikeLoadTask(int pos) {
187       loaderItem = (SocialTabsActivity.instance != null) ? SocialTabsActivity.instance.loaderItem : null;
188       currentPosition = pos;
189     }
190 
191     @Override
192     protected void onPreExecute() {
193       super.onPreExecute();
194       ExoUtils.setLoadingItem(loaderItem, true);
195     }
196 
197     @Override
198     protected Boolean doInBackground(SocialActivityInfo... params) {
199       SocialActivityInfo actInfo = params[0];
200       try {
201         RestActivity activity = SocialServiceHelper.getInstance().activityService.get(actInfo.getActivityId());
202         if (activity.isLiked())
203           SocialServiceHelper.getInstance().activityService.unlike(activity);
204         else
205           SocialServiceHelper.getInstance().activityService.like(activity);
206 
207         return true;
208 
209       } catch (SocialClientLibException e) {
210         if (Log.LOGD)
211           Log.d(TAG, e.getMessage(), Log.getStackTraceString(e));
212         return false;
213       } catch (RuntimeException e) {
214         // XXX cannot replace because SocialClientLib can throw exceptions like ServerException, UnsupportMethod ,..
215         if (Log.LOGD)
216           Log.d(TAG, e.getMessage(), Log.getStackTraceString(e));
217         return false;
218       }
219     }
220 
221     @Override
222     protected void onCancelled() {
223       ExoUtils.setLoadingItem(loaderItem, false);
224       super.onCancelled();
225     }
226 
227     @Override
228     protected void onPostExecute(Boolean result) {
229       ExoUtils.setLoadingItem(loaderItem, false);
230       if (result) {
231         if (SocialTabsActivity.instance != null) {
232           int tabId = SocialTabsActivity.instance.mPager.getCurrentItem();
233           switch (tabId) {
234           case SocialTabsActivity.ALL_UPDATES:
235 
236             AllUpdatesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
237             if (AllUpdatesFragment.instance.isLoading())
238               holder.buttonLike.setClickable(false);
239             else
240               holder.buttonLike.setClickable(true);
241             break;
242           case SocialTabsActivity.MY_CONNECTIONS:
243             MyConnectionsFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
244             if (MyConnectionsFragment.instance.isLoading())
245               holder.buttonLike.setClickable(false);
246             else
247               holder.buttonLike.setClickable(true);
248             break;
249           case SocialTabsActivity.MY_SPACES:
250             MySpacesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
251             if (MySpacesFragment.instance.isLoading())
252               holder.buttonLike.setClickable(false);
253             else
254               holder.buttonLike.setClickable(true);
255             break;
256           case SocialTabsActivity.MY_STATUS:
257             MyStatusFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
258             if (MyStatusFragment.instance.isLoading())
259               holder.buttonLike.setClickable(false);
260             else
261               holder.buttonLike.setClickable(true);
262             break;
263           }
264         }
265       } else {
266         WarningDialog dialog = new WarningDialog(mContext,
267                                                  mContext.getString(R.string.Warning),
268                                                  mContext.getString(R.string.ErrorOnLike),
269                                                  mContext.getString(R.string.OK));
270         dialog.show();
271       }
272     }
273 
274   }
275 
276 }