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.document;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.exoplatform.R;
25  import org.exoplatform.model.ExoFile;
26  import org.exoplatform.singleton.DocumentHelper;
27  import org.exoplatform.ui.DocumentActivity;
28  import org.exoplatform.utils.ExoConnectionUtils;
29  import org.exoplatform.utils.ExoConstants;
30  import org.exoplatform.utils.ExoDocumentUtils;
31  import org.exoplatform.utils.Log;
32  import org.exoplatform.utils.PhotoUtils;
33  import org.exoplatform.utils.SocialActivityUtil;
34  import org.exoplatform.widget.ConnTimeOutDialog;
35  import org.exoplatform.widget.DocumentWaitingDialog;
36  import org.exoplatform.widget.WarningDialog;
37  
38  import android.content.res.Resources;
39  import android.os.AsyncTask;
40  import android.view.animation.AnimationUtils;
41  
42  public class DocumentLoadTask extends AsyncTask<Integer, Void, Integer> {
43  
44    /*
45     * Result status
46     */
47    private static final int      RESULT_OK      = 1;
48  
49    private static final int      RESULT_ERROR   = 2;
50  
51    private static final int      RESULT_TIMEOUT = 3;
52  
53    private static final int      RESULT_FALSE   = 4;
54    
55    private static final int      RESULT_CANCELED   = 5;
56  
57    // LOG TAG
58    private static final String   LOG_TAG        = "eXo____DocumentLoadTask____";
59  
60    private DocumentWaitingDialog _progressDialog;
61  
62    private String                loadingData;
63  
64    private String                okString;
65  
66    private String                titleString;
67  
68    /*
69     * This @contentWarningString is for display the error/warning message when
70     * retrieving document
71     */
72    private String                contentWarningString;
73  
74    private int                   actionID;
75  
76    private String                strSourceUrl;
77  
78    private String                strDestinationUrl;
79  
80    private DocumentActivity      documentActivity;
81  
82    private ExoFile               sourceFile;
83  
84    private ExoFile               loadedFolder;
85  
86    private Resources             resource;
87  
88    public DocumentLoadTask(DocumentActivity activity, ExoFile source, String destination, int action) {
89      resource = activity.getResources();
90      documentActivity = activity;
91      sourceFile = source;
92      strSourceUrl = source.path;
93      strDestinationUrl = destination;
94      actionID = action;
95      changeLanguage();
96    }
97  
98    private void changeLanguage() {
99      loadingData = resource.getString(R.string.LoadingData);
100     okString = resource.getString(R.string.OK);
101     titleString = resource.getString(R.string.Warning);
102     contentWarningString = resource.getString(R.string.LoadingDataError);
103   }
104 
105   @Override
106   public void onPreExecute() {
107     _progressDialog = new DocumentWaitingDialog(documentActivity, null, loadingData);
108     _progressDialog.show();
109   }
110 
111   @Override
112   public Integer doInBackground(Integer... params) {
113     boolean result = true;
114     try {
115       // Check the session status each time we retrieve files/folders.
116       // If timeout, try to re-login. If re-logging fails, pop-up an error
117       String versionUrl = SocialActivityUtil.getDomain() + ExoConstants.DOMAIN_PLATFORM_VERSION;
118       if (ExoConnectionUtils.checkTimeout(versionUrl) != ExoConnectionUtils.LOGIN_SUCCESS)
119         return RESULT_TIMEOUT;
120 
121       switch (actionID) {
122       case DocumentActivity.ACTION_DELETE:
123         contentWarningString = resource.getString(R.string.DocumentCannotDelete);
124         result = ExoDocumentUtils.deleteFile(strSourceUrl);
125         sourceFile = documentActivity._fileForCurrentActionBar;
126         break;
127       case DocumentActivity.ACTION_COPY:
128         contentWarningString = resource.getString(R.string.DocumentCopyPasteError);
129         result = ExoDocumentUtils.copyFile(strSourceUrl, strDestinationUrl);
130         sourceFile = documentActivity._fileForCurrentActionBar;
131         break;
132       case DocumentActivity.ACTION_MOVE:
133         contentWarningString = resource.getString(R.string.DocumentCopyPasteError);
134         result = ExoDocumentUtils.moveFile(strSourceUrl, strDestinationUrl);
135         sourceFile = documentActivity._fileForCurrentActionBar;
136         break;
137       case DocumentActivity.ACTION_ADD_PHOTO:
138         File file = new File(documentActivity._sdcard_temp_dir);
139         contentWarningString = resource.getString(R.string.DocumentUploadError);
140         File tempFile = PhotoUtils.reziseFileImage(file);
141         if (tempFile != null) {
142           result = ExoDocumentUtils.putFileToServerFromLocal(strSourceUrl + "/" + file.getName(),
143                                                              tempFile,
144                                                              ExoConstants.IMAGE_TYPE);
145         }
146         break;
147       case DocumentActivity.ACTION_RENAME:
148         contentWarningString = resource.getString(R.string.DocumentRenameError);
149         result = ExoDocumentUtils.renameFolder(strSourceUrl, strDestinationUrl);
150         if (result) {
151           // prepare to reload the current folder if the action was successful
152           if (sourceFile == documentActivity._fileForCurrentActionBar) {
153             // folder renamed from within
154             // update the key in folderToParentMap to link to the parent folder
155             ExoFile parent = (ExoFile) DocumentHelper.getInstance().folderToParentMap.getParcelable(strSourceUrl);
156             DocumentHelper.getInstance().folderToParentMap.remove(strSourceUrl);
157             DocumentHelper.getInstance().folderToParentMap.putParcelable(strDestinationUrl, parent);
158           } else {
159             // file/folder renamed from parent
160             // we will reload the current folder
161             sourceFile = documentActivity._fileForCurrentActionBar;
162           }
163         }
164         break;
165       case DocumentActivity.ACTION_CREATE:
166         contentWarningString = resource.getString(R.string.DocumentCreateFolderError);
167         result = ExoDocumentUtils.createFolder(strDestinationUrl);
168         if (result)
169           // prepare to reload the current folder if the action was successful
170           sourceFile = documentActivity._fileForCurrentActionBar;
171         break;
172 
173       }
174 
175 //    Stop execution if the task was canceled
176       if (isCancelled())
177         return RESULT_CANCELED;
178       
179 //    Get folder content
180       if (result == true) {
181         loadedFolder = ExoDocumentUtils.getPersonalDriveContent(documentActivity, sourceFile);
182         return RESULT_OK;
183       } else {
184         return RESULT_FALSE;
185       }
186     } catch (IOException e) {
187       if (Log.LOGE)
188         Log.e(LOG_TAG, e.getMessage(), e);
189       return RESULT_ERROR;
190     }
191   }
192 
193   @Override
194   public void onCancelled() {
195     super.onCancelled();
196     if (_progressDialog.isAttachedToWindow())
197       _progressDialog.dismiss();
198   }
199 
200   @Override
201   public void onPostExecute(Integer result) {
202     if (result == RESULT_OK) {
203       // Map the new current folder path (sourceFile.path)
204       // with its parent (former current folder)
205       if (!DocumentHelper.getInstance().folderToParentMap.containsKey(sourceFile.path))
206         DocumentHelper.getInstance().folderToParentMap.putParcelable(sourceFile.path, documentActivity._fileForCurrentActionBar);
207 
208       documentActivity.updateContent(loadedFolder);
209 
210       /*
211        * Set animation for listview when access to folder
212        */
213       documentActivity._listViewDocument.setAnimation(AnimationUtils.loadAnimation(documentActivity, R.anim.anim_right_to_left));
214 
215     } else if (result == RESULT_ERROR) {
216       new WarningDialog(documentActivity, titleString, contentWarningString, okString).show();
217     } else if (result == RESULT_TIMEOUT) {
218       new ConnTimeOutDialog(documentActivity, titleString, okString).show();
219     } else if (result == RESULT_FALSE) {
220       new WarningDialog(documentActivity, titleString, contentWarningString, okString).show();
221     }
222     if (_progressDialog.isAttachedToWindow())
223       _progressDialog.dismiss();
224   }
225 
226 }