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.controller.social;
20  
21  import org.exoplatform.R;
22  import org.exoplatform.model.SocialSpaceInfo;
23  import org.exoplatform.singleton.SocialDetailHelper;
24  import org.exoplatform.singleton.SocialServiceHelper;
25  import org.exoplatform.social.client.api.SocialClientLibException;
26  import org.exoplatform.social.client.api.model.RestActivity;
27  import org.exoplatform.social.client.api.model.RestComment;
28  import org.exoplatform.social.client.api.service.ActivityService;
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.SocialDetailActivity;
35  import org.exoplatform.ui.social.SocialTabsActivity;
36  import org.exoplatform.utils.ExoConnectionUtils;
37  import org.exoplatform.utils.ExoConstants;
38  import org.exoplatform.utils.Log;
39  import org.exoplatform.utils.PhotoUtils;
40  import org.exoplatform.widget.ConnectionErrorDialog;
41  import org.exoplatform.widget.PostWaitingDialog;
42  import org.exoplatform.widget.WarningDialog;
43  
44  import android.app.Activity;
45  import android.content.res.Resources;
46  import android.os.AsyncTask;
47  import android.view.Gravity;
48  import android.widget.Toast;
49  
50  public class ComposeMessageController {
51  
52    private PostWaitingDialog _progressDialog;
53  
54    private int               composeType;
55  
56    private ComposeMessageActivity mComposeActivity;
57  
58    private PostStatusTask    mPostTask;
59  
60    private CommentTask       mCommetnTask;
61  
62    private String            sdcard_temp_dir = null;
63  
64    private String            inputTextWarning;
65  
66    private String            okString;
67  
68    private String            titleString;
69  
70    private String            contentString;
71  
72    /**
73     * Either null (public) or the space name
74     */
75    private SocialSpaceInfo   postDestination;
76  
77    public ComposeMessageController(ComposeMessageActivity activity, int type, PostWaitingDialog dialog) {
78      mComposeActivity = activity;
79      composeType = type;
80      postDestination = null;
81      _progressDialog = dialog;
82      changeLanguage();
83  
84    }
85  
86    /**
87     * Set this post's destination.
88     * 
89     * @param destination null (public) or the SocialSpaceInfo object
90     */
91    public void setPostDestination(SocialSpaceInfo destination) {
92      this.postDestination = destination;
93    }
94  
95    /**
96     * @return This post's destination:<br/>
97     *         - null: public<br/>
98     *         - SocialSpaceInfo: the destination space
99     */
100   public SocialSpaceInfo getPostDestination() {
101     return this.postDestination;
102   }
103 
104   /*
105    * Take a photo and store it into /sdcard/eXo/DocumentCache
106    */
107   public void initCamera() {
108     sdcard_temp_dir = PhotoUtils.startImageCapture(mComposeActivity);
109   }
110 
111   public void onSendMessage(String composeMessage, String sdcard, int position) {
112     if ((composeMessage != null) && (composeMessage.length() > 0)) {
113       if (ExoConnectionUtils.isNetworkAvailableExt(mComposeActivity)) {
114         if (composeType == ExoConstants.COMPOSE_POST_TYPE) {
115           onPostTask(composeMessage, sdcard);
116         } else {
117           onCommentTask(composeMessage, position);
118         }
119       } else {
120         new ConnectionErrorDialog(mComposeActivity).show();
121       }
122     } else {
123       Toast toast = Toast.makeText(mComposeActivity, inputTextWarning, Toast.LENGTH_LONG);
124       toast.setGravity(Gravity.BOTTOM, 0, 0);
125       toast.show();
126     }
127   }
128 
129   private void onPostTask(String composeMessage, String sdcard) {
130     if (mPostTask == null || mPostTask.getStatus() == PostStatusTask.Status.FINISHED) {
131       mPostTask = (PostStatusTask) new PostStatusTask(mComposeActivity, sdcard, composeMessage, this, _progressDialog).execute();
132     }
133   }
134 
135   public void onCancelPostTask() {
136     if (mPostTask != null && mPostTask.getStatus() == PostStatusTask.Status.RUNNING) {
137       mPostTask.cancel(true);
138       mPostTask = null;
139     }
140   }
141 
142   private void onCommentTask(String composeMessage, int position) {
143     if (mCommetnTask == null || mCommetnTask.getStatus() == CommentTask.Status.FINISHED) {
144       mCommetnTask = (CommentTask) new CommentTask(position).execute(composeMessage);
145     }
146   }
147 
148   public String getSdCardTempDir() {
149     return sdcard_temp_dir;
150   }
151 
152   private void changeLanguage() {
153     Resources resource = mComposeActivity.getResources();
154     inputTextWarning = resource.getString(R.string.InputTextWarning);
155     okString = resource.getString(R.string.OK);
156     titleString = resource.getString(R.string.Warning);
157     contentString = resource.getString(R.string.ErrorOnComment);
158   }
159 
160   private class CommentTask extends AsyncTask<String, Void, Boolean> {
161 
162     private int currentPosition;
163 
164     public CommentTask(int position) {
165       currentPosition = position;
166     }
167 
168     @Override
169     protected Boolean doInBackground(String... params) {
170       try {
171         String composeMessage = params[0];
172         RestComment comment = new RestComment();
173         comment.setText(composeMessage);
174         String selectedId = SocialDetailHelper.getInstance().getActivityId();
175         ActivityService<RestActivity> activityService = SocialServiceHelper.getInstance().activityService;
176         RestActivity restActivity = (RestActivity) activityService.get(selectedId);
177 
178         activityService.createComment(restActivity, comment);
179         return true;
180       } catch (SocialClientLibException e) {
181         if (Log.LOGD)
182           Log.d(getClass().getSimpleName(), e.getMessage(), Log.getStackTraceString(e));
183         return false;
184       }
185 
186     }
187 
188     @Override
189     protected void onPostExecute(Boolean result) {
190       if (result) {
191         ((Activity) mComposeActivity).finish();
192         if (SocialDetailActivity.socialDetailActivity != null) {
193           SocialDetailActivity.socialDetailActivity.onLoad();
194         }
195 
196         if (SocialTabsActivity.instance != null) {
197           int tabId = SocialTabsActivity.instance.mPager.getCurrentItem();
198           switch (tabId) {
199           case SocialTabsActivity.ALL_UPDATES:
200             AllUpdatesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
201             break;
202           case SocialTabsActivity.MY_CONNECTIONS:
203             MyConnectionsFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
204             break;
205           case SocialTabsActivity.MY_SPACES:
206             MySpacesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
207             break;
208           case SocialTabsActivity.MY_STATUS:
209             MyStatusFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, currentPosition);
210             break;
211           }
212         }
213       } else {
214         WarningDialog dialog = new WarningDialog(mComposeActivity, titleString, contentString, okString);
215         dialog.show();
216       }
217     }
218 
219   }
220 }