1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
70
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
116
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
152 if (sourceFile == documentActivity._fileForCurrentActionBar) {
153
154
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
160
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
170 sourceFile = documentActivity._fileForCurrentActionBar;
171 break;
172
173 }
174
175
176 if (isCancelled())
177 return RESULT_CANCELED;
178
179
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
204
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
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 }