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 java.io.File;
22  import java.io.IOException;
23  import java.util.concurrent.atomic.AtomicBoolean;
24  
25  import org.exoplatform.R;
26  import org.exoplatform.model.SocialPostInfo;
27  import org.exoplatform.shareextension.service.Action.ActionListener;
28  import org.exoplatform.shareextension.service.PostAction;
29  import org.exoplatform.shareextension.service.PostAction.PostActionListener;
30  import org.exoplatform.shareextension.service.ShareService;
31  import org.exoplatform.shareextension.service.ShareService.UploadInfo;
32  import org.exoplatform.shareextension.service.UploadAction;
33  import org.exoplatform.singleton.AccountSetting;
34  import org.exoplatform.ui.social.AllUpdatesFragment;
35  import org.exoplatform.ui.social.MyStatusFragment;
36  import org.exoplatform.utils.ExoConstants;
37  import org.exoplatform.utils.ExoDocumentUtils;
38  import org.exoplatform.utils.Log;
39  import org.exoplatform.utils.PhotoUtils;
40  import org.exoplatform.widget.PostWaitingDialog;
41  import org.exoplatform.widget.WarningDialog;
42  
43  import android.app.Activity;
44  import android.content.Context;
45  import android.content.res.Resources;
46  import android.net.Uri;
47  import android.os.AsyncTask;
48  
49  /**
50   * A background task that publishes a status with optional attachment. Uses
51   * {@link UploadAction} and {@link PostAction} to not have duplicate activities
52   * (MOB-1384). <br/>
53   * TODO An even better solution would be to use the {@link ShareService}
54   * directly.
55   */
56  public class PostStatusTask extends AsyncTask<Void, Void, Integer> {
57    private PostWaitingDialog        _progressDialog;
58  
59    private Context                  mContext;
60  
61    private String                   sdcard_temp_dir;
62  
63    private String                   composeMessage;
64  
65    private String                   sendingData;
66  
67    private String                   okString;
68  
69    private String                   errorString;
70  
71    private String                   warningTitle;
72  
73    private String                   uploadUrl;
74  
75    private ComposeMessageController messageController;
76  
77    public PostStatusTask(Context context,
78                          String dir,
79                          String content,
80                          ComposeMessageController controller,
81                          PostWaitingDialog dialog) {
82      mContext = context;
83      messageController = controller;
84      sdcard_temp_dir = dir;
85      composeMessage = content;
86      _progressDialog = dialog;
87      changeLanguage();
88    }
89  
90    @Override
91    public void onPreExecute() {
92      _progressDialog = new PostWaitingDialog(mContext, messageController, null, sendingData);
93      _progressDialog.show();
94    }
95  
96    @Override
97    public Integer doInBackground(Void... params) {
98  
99      try {
100       SocialPostInfo postInfo = new SocialPostInfo();
101       postInfo.destinationSpace = messageController.getPostDestination();
102       postInfo.postMessage = composeMessage;
103       postInfo.ownerAccount = AccountSetting.getInstance().getCurrentAccount();
104 
105       // If the post contains an attached image
106       if (sdcard_temp_dir != null) {
107         postInfo.activityType = SocialPostInfo.TYPE_DOC;
108         UploadInfo uploadInfo = new UploadInfo();
109         uploadInfo.init(postInfo);
110         uploadUrl = uploadInfo.jcrUrl + "/" + uploadInfo.folder;
111 
112         // Create destination folder
113         if (ExoDocumentUtils.createFolder(uploadUrl)) {
114 
115           // Upload file
116           File file = new File(sdcard_temp_dir);
117           if (file != null) {
118             File tempFile = PhotoUtils.reziseFileImage(file);
119             String uploadedFileName = file.getName();
120             if (tempFile != null) {
121               uploadInfo.fileToUpload = ExoDocumentUtils.documentInfoFromUri(Uri.fromFile(tempFile),
122                                                                              mContext.getApplicationContext());
123               uploadInfo.uploadId = Long.toHexString(System.currentTimeMillis());
124               if (uploadInfo.fileToUpload != null) {
125                 uploadInfo.fileToUpload.documentName = uploadedFileName;
126                 uploadInfo.fileToUpload.cleanupFilename(mContext);
127                 final AtomicBoolean uploaded = new AtomicBoolean(false);
128                 // UploadAction.excute is synchronize method
129                 UploadAction.execute(postInfo, uploadInfo, new ActionListener() {
130 
131                   @Override
132                   public boolean onSuccess(String message) {
133                     uploaded.set(true);
134                     return true;
135                   }
136 
137                   @Override
138                   public boolean onError(String error) {
139                     uploaded.set(false);
140                     return false;
141                   }
142                 });
143                 if (uploadInfo.fileToUpload.documentData != null) {
144                   try {
145                     uploadInfo.fileToUpload.documentData.close();
146                   } catch (IOException e) {
147                     Log.d(getClass().getSimpleName(), Log.getStackTraceString(e));
148                   }
149                 }
150                 tempFile.delete();
151                 if (uploaded.get()) {
152                   uploadedFileName = uploadInfo.fileToUpload.documentName;
153                 }
154               }
155             }
156             // build post param
157             postInfo.buildTemplateParams(uploadInfo);
158           }
159         }
160       }
161       final AtomicBoolean posted = new AtomicBoolean(false);
162       // post action execute is synchronize
163       PostAction.execute(postInfo, new PostActionListener() {
164 
165         @Override
166         public boolean onSuccess(String message) {
167           posted.set(true);
168           return true;
169         }
170 
171         @Override
172         public boolean onError(String error) {
173           posted.set(false);
174           return false;
175         }
176       });
177       if (posted.get())
178         return 1;
179       else
180         return 0;
181     } catch (RuntimeException e) {
182       // Cannot replace because SocialClientLib can throw exceptions like ServerException, UnsupportMethod, etc
183       if (Log.LOGD)
184         Log.d(getClass().getSimpleName(), e.getMessage(), Log.getStackTraceString(e));
185       return -2;
186     }
187 
188   }
189 
190   @Override
191   public void onPostExecute(Integer result) {
192     if (result == 1) {
193       ((Activity) mContext).finish();
194       if (AllUpdatesFragment.instance != null)
195         AllUpdatesFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, 0);
196       if (MyStatusFragment.instance != null)
197         MyStatusFragment.instance.onPrepareLoad(ExoConstants.NUMBER_OF_ACTIVITY, true, 0);
198 
199     } else {
200       new WarningDialog(mContext, warningTitle, errorString, okString).show();
201     }
202     _progressDialog.dismiss();
203 
204   }
205 
206   private void changeLanguage() {
207     Resources resource = mContext.getResources();
208     sendingData = resource.getString(R.string.SendingData);
209     okString = resource.getString(R.string.OK);
210     errorString = resource.getString(R.string.PostError);
211     warningTitle = resource.getString(R.string.Warning);
212   }
213 
214 }