ETag used to detect changes in remote files (not only in folders)
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / FileStorageUtils.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21 package com.owncloud.android.utils;
22
23 import java.io.File;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.Vector;
27
28 import third_parties.daveKoeller.AlphanumComparator;
29
30 import com.owncloud.android.MainApp;
31 import com.owncloud.android.R;
32 import com.owncloud.android.datamodel.OCFile;
33 import com.owncloud.android.lib.resources.files.RemoteFile;
34
35 import android.accounts.Account;
36 import android.annotation.SuppressLint;
37 import android.content.Context;
38 import android.content.SharedPreferences;
39 import android.preference.PreferenceManager;
40 import android.net.Uri;
41 import android.os.Environment;
42 import android.os.StatFs;
43 import android.webkit.MimeTypeMap;
44
45
46 /**
47 * Static methods to help in access to local file system.
48 */
49 public class FileStorageUtils {
50 public static Integer mSortOrder;
51 public static Boolean mSortAscending;
52 public static final Integer SORT_NAME = 0;
53 public static final Integer SORT_DATE = 1;
54 public static final Integer SORT_SIZE = 2;
55
56
57 //private static final String LOG_TAG = "FileStorageUtils";
58
59 public static final String getSavePath(String accountName) {
60 File sdCard = Environment.getExternalStorageDirectory();
61 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
62 // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
63 }
64
65 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
66 return getSavePath(accountName) + file.getRemotePath();
67 }
68
69 public static final String getTemporalPath(String accountName) {
70 File sdCard = Environment.getExternalStorageDirectory();
71 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
72 // URL encoding is an 'easy fix' to overcome that NTFS and FAT32 don't allow ":" in file names, that can be in the accountName since 0.1.190B
73 }
74
75 @SuppressLint("NewApi")
76 public static final long getUsableSpace(String accountName) {
77 File savePath = Environment.getExternalStorageDirectory();
78 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
79 return savePath.getUsableSpace();
80
81 } else {
82 StatFs stats = new StatFs(savePath.getAbsolutePath());
83 return stats.getAvailableBlocks() * stats.getBlockSize();
84 }
85
86 }
87
88 public static final String getLogPath() {
89 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
90 }
91
92 public static String getInstantUploadFilePath(Context context, String fileName) {
93 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
94 String uploadPathdef = context.getString(R.string.instant_upload_path);
95 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
96 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
97 return value;
98 }
99
100 /**
101 * Gets the composed path when video is or must be stored
102 * @param context
103 * @param fileName: video file name
104 * @return String: video file path composed
105 */
106 public static String getInstantVideoUploadFilePath(Context context, String fileName) {
107 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
108 String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
109 String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
110 String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
111 return value;
112 }
113
114 public static String getParentPath(String remotePath) {
115 String parentPath = new File(remotePath).getParent();
116 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
117 return parentPath;
118 }
119
120 /**
121 * Creates and populates a new {@link OCFile} object with the data read from the server.
122 *
123 * @param remote remote file read from the server (remote file or folder).
124 * @return New OCFile instance representing the remote resource described by remote.
125 */
126 public static OCFile fillOCFile(RemoteFile remote) {
127 OCFile file = new OCFile(remote.getRemotePath());
128 file.setCreationTimestamp(remote.getCreationTimestamp());
129 file.setFileLength(remote.getLength());
130 file.setMimetype(remote.getMimeType());
131 file.setModificationTimestamp(remote.getModifiedTimestamp());
132 file.setEtag(remote.getEtag());
133 file.setPermissions(remote.getPermissions());
134 file.setRemoteId(remote.getRemoteId());
135 return file;
136 }
137
138 /**
139 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
140 *
141 * @param ocFile OCFile
142 * @return New RemoteFile instance representing the resource described by ocFile.
143 */
144 public static RemoteFile fillRemoteFile(OCFile ocFile){
145 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
146 file.setCreationTimestamp(ocFile.getCreationTimestamp());
147 file.setLength(ocFile.getFileLength());
148 file.setMimeType(ocFile.getMimetype());
149 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
150 file.setEtag(ocFile.getEtag());
151 file.setPermissions(ocFile.getPermissions());
152 file.setRemoteId(ocFile.getRemoteId());
153 return file;
154 }
155
156 /**
157 * Sorts all filenames, regarding last user decision
158 */
159 public static Vector<OCFile> sortFolder(Vector<OCFile> files){
160 switch (mSortOrder){
161 case 0:
162 files = FileStorageUtils.sortByName(files);
163 break;
164 case 1:
165 files = FileStorageUtils.sortByDate(files);
166 break;
167 case 2:
168 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
169 break;
170 }
171
172 return files;
173 }
174
175 /**
176 * Sorts list by Date
177 * @param files
178 */
179 public static Vector<OCFile> sortByDate(Vector<OCFile> files){
180 final Integer val;
181 if (mSortAscending){
182 val = 1;
183 } else {
184 val = -1;
185 }
186
187 Collections.sort(files, new Comparator<OCFile>() {
188 public int compare(OCFile o1, OCFile o2) {
189 if (o1.isFolder() && o2.isFolder()) {
190 Long obj1 = o1.getModificationTimestamp();
191 return val * obj1.compareTo(o2.getModificationTimestamp());
192 }
193 else if (o1.isFolder()) {
194 return -1;
195 } else if (o2.isFolder()) {
196 return 1;
197 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
198 return 0;
199 } else {
200 Long obj1 = o1.getModificationTimestamp();
201 return val * obj1.compareTo(o2.getModificationTimestamp());
202 }
203 }
204 });
205
206 return files;
207 }
208
209 // /**
210 // * Sorts list by Size
211 // * @param sortAscending true: ascending, false: descending
212 // */
213 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
214 // final Integer val;
215 // if (mSortAscending){
216 // val = 1;
217 // } else {
218 // val = -1;
219 // }
220 //
221 // Collections.sort(files, new Comparator<OCFile>() {
222 // public int compare(OCFile o1, OCFile o2) {
223 // if (o1.isFolder() && o2.isFolder()) {
224 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
225 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
226 // }
227 // else if (o1.isFolder()) {
228 // return -1;
229 // } else if (o2.isFolder()) {
230 // return 1;
231 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
232 // return 0;
233 // } else {
234 // Long obj1 = o1.getFileLength();
235 // return val * obj1.compareTo(o2.getFileLength());
236 // }
237 // }
238 // });
239 //
240 // return files;
241 // }
242
243 /**
244 * Sorts list by Name
245 * @param files files to sort
246 */
247 public static Vector<OCFile> sortByName(Vector<OCFile> files){
248 final Integer val;
249 if (mSortAscending){
250 val = 1;
251 } else {
252 val = -1;
253 }
254
255 Collections.sort(files, new Comparator<OCFile>() {
256 public int compare(OCFile o1, OCFile o2) {
257 if (o1.isFolder() && o2.isFolder()) {
258 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
259 } else if (o1.isFolder()) {
260 return -1;
261 } else if (o2.isFolder()) {
262 return 1;
263 }
264 return val * new AlphanumComparator().compare(o1, o2);
265 }
266 });
267
268 return files;
269 }
270
271 /**
272 * Local Folder size
273 * @param dir File
274 * @return Size in bytes
275 */
276 public static long getFolderSize(File dir) {
277 if (dir.exists()) {
278 long result = 0;
279 File[] fileList = dir.listFiles();
280 for(int i = 0; i < fileList.length; i++) {
281 if(fileList[i].isDirectory()) {
282 result += getFolderSize(fileList[i]);
283 } else {
284 result += fileList[i].length();
285 }
286 }
287 return result;
288 }
289 return 0;
290 }
291
292 /**
293 * Mimetype String of a file
294 * @param path
295 * @return
296 */
297 public static String getMimeTypeFromName(String path) {
298 String extension = "";
299 int pos = path.lastIndexOf('.');
300 if (pos >= 0) {
301 extension = path.substring(pos + 1);
302 }
303 String result = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
304 return (result != null) ? result : "";
305 }
306
307 /**
308 * Scans the default location for saving local copies of files searching for
309 * a 'lost' file with the same full name as the {@link OCFile} received as
310 * parameter.
311 *
312 * This method helps to keep linked local copies of the files when the app is uninstalled, and then
313 * reinstalled in the device. OR after the cache of the app was deleted in system settings.
314 *
315 * The method is assuming that all the local changes in the file where synchronized in the past. This is dangerous,
316 * but assuming the contrary could lead to massive unnecessary synchronizations of downloaded file after deleting
317 * the app cache.
318 *
319 * This should be changed in the near future to avoid any chance of data loss, but we need to add some options
320 * to limit hard automatic synchronizations to wifi, unless the user wants otherwise.
321 *
322 * @param file File to associate a possible 'lost' local file.
323 * @param account Account holding file.
324 */
325 public static void searchForLocalFileInDefaultPath(OCFile file, Account account) {
326 if (file.getStoragePath() == null && !file.isFolder()) {
327 File f = new File(FileStorageUtils.getDefaultSavePathFor(account.name, file));
328 if (f.exists()) {
329 file.setStoragePath(f.getAbsolutePath());
330 file.setLastSyncDateForData(f.lastModified());
331 }
332 }
333 }
334
335 }