Merge branch 'develop2' into imageGrid2
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / FileStorageUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.utils;
19
20 import java.io.File;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Vector;
24
25 import third_parties.daveKoeller.AlphanumComparator;
26
27 import com.owncloud.android.MainApp;
28 import com.owncloud.android.R;
29 import com.owncloud.android.datamodel.OCFile;
30 import com.owncloud.android.lib.resources.files.RemoteFile;
31
32 import android.annotation.SuppressLint;
33 import android.content.Context;
34 import android.content.SharedPreferences;
35 import android.preference.PreferenceManager;
36 import android.net.Uri;
37 import android.os.Environment;
38 import android.os.StatFs;
39
40
41 /**
42 * Static methods to help in access to local file system.
43 *
44 * @author David A. Velasco
45 */
46 public class FileStorageUtils {
47 public static Integer mSortOrder;
48 public static Boolean mSortAscending;
49 public static final Integer SORT_NAME = 0;
50 public static final Integer SORT_DATE = 1;
51 public static final Integer SORT_SIZE = 2;
52
53
54 //private static final String LOG_TAG = "FileStorageUtils";
55
56 public static final String getSavePath(String accountName) {
57 File sdCard = Environment.getExternalStorageDirectory();
58 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
59 // 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
60 }
61
62 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
63 return getSavePath(accountName) + file.getRemotePath();
64 }
65
66 public static final String getTemporalPath(String accountName) {
67 File sdCard = Environment.getExternalStorageDirectory();
68 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
69 // 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
70 }
71
72 @SuppressLint("NewApi")
73 public static final long getUsableSpace(String accountName) {
74 File savePath = Environment.getExternalStorageDirectory();
75 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
76 return savePath.getUsableSpace();
77
78 } else {
79 StatFs stats = new StatFs(savePath.getAbsolutePath());
80 return stats.getAvailableBlocks() * stats.getBlockSize();
81 }
82
83 }
84
85 public static final String getLogPath() {
86 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
87 }
88
89 public static String getInstantUploadFilePath(Context context, String fileName) {
90 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
91 String uploadPathdef = context.getString(R.string.instant_upload_path);
92 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
93 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
94 return value;
95 }
96
97 /**
98 * Gets the composed path when video is or must be stored
99 * @param context
100 * @param fileName: video file name
101 * @return String: video file path composed
102 */
103 public static String getInstantVideoUploadFilePath(Context context, String fileName) {
104 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
105 String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
106 String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
107 String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
108 return value;
109 }
110
111 public static String getParentPath(String remotePath) {
112 String parentPath = new File(remotePath).getParent();
113 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
114 return parentPath;
115 }
116
117 /**
118 * Creates and populates a new {@link OCFile} object with the data read from the server.
119 *
120 * @param remote remote file read from the server (remote file or folder).
121 * @return New OCFile instance representing the remote resource described by we.
122 */
123 public static OCFile fillOCFile(RemoteFile remote) {
124 OCFile file = new OCFile(remote.getRemotePath());
125 file.setCreationTimestamp(remote.getCreationTimestamp());
126 file.setFileLength(remote.getLength());
127 file.setMimetype(remote.getMimeType());
128 file.setModificationTimestamp(remote.getModifiedTimestamp());
129 file.setEtag(remote.getEtag());
130 file.setPermissions(remote.getPermissions());
131 file.setRemoteId(remote.getRemoteId());
132 return file;
133 }
134
135 /**
136 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
137 *
138 * @param oCFile OCFile
139 * @return New RemoteFile instance representing the resource described by ocFile.
140 */
141 public static RemoteFile fillRemoteFile(OCFile ocFile){
142 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
143 file.setCreationTimestamp(ocFile.getCreationTimestamp());
144 file.setLength(ocFile.getFileLength());
145 file.setMimeType(ocFile.getMimetype());
146 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
147 file.setEtag(ocFile.getEtag());
148 file.setPermissions(ocFile.getPermissions());
149 file.setRemoteId(ocFile.getRemoteId());
150 return file;
151 }
152
153 /**
154 * Sorts all filenames, regarding last user decision
155 */
156 public static Vector<OCFile> sortFolder(Vector<OCFile> files){
157 switch (mSortOrder){
158 case 0:
159 files = FileStorageUtils.sortByName(files);
160 break;
161 case 1:
162 files = FileStorageUtils.sortByDate(files);
163 break;
164 case 2:
165 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
166 break;
167 }
168
169 return files;
170 }
171
172 /**
173 * Sorts list by Date
174 * @param sortAscending true: ascending, false: descending
175 */
176 public static Vector<OCFile> sortByDate(Vector<OCFile> files){
177 final Integer val;
178 if (mSortAscending){
179 val = 1;
180 } else {
181 val = -1;
182 }
183
184 Collections.sort(files, new Comparator<OCFile>() {
185 public int compare(OCFile o1, OCFile o2) {
186 if (o1.isFolder() && o2.isFolder()) {
187 Long obj1 = o1.getModificationTimestamp();
188 return val * obj1.compareTo(o2.getModificationTimestamp());
189 }
190 else if (o1.isFolder()) {
191 return -1;
192 } else if (o2.isFolder()) {
193 return 1;
194 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
195 return 0;
196 } else {
197 Long obj1 = o1.getModificationTimestamp();
198 return val * obj1.compareTo(o2.getModificationTimestamp());
199 }
200 }
201 });
202
203 return files;
204 }
205
206 // /**
207 // * Sorts list by Size
208 // * @param sortAscending true: ascending, false: descending
209 // */
210 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
211 // final Integer val;
212 // if (mSortAscending){
213 // val = 1;
214 // } else {
215 // val = -1;
216 // }
217 //
218 // Collections.sort(files, new Comparator<OCFile>() {
219 // public int compare(OCFile o1, OCFile o2) {
220 // if (o1.isFolder() && o2.isFolder()) {
221 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
222 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
223 // }
224 // else if (o1.isFolder()) {
225 // return -1;
226 // } else if (o2.isFolder()) {
227 // return 1;
228 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
229 // return 0;
230 // } else {
231 // Long obj1 = o1.getFileLength();
232 // return val * obj1.compareTo(o2.getFileLength());
233 // }
234 // }
235 // });
236 //
237 // return files;
238 // }
239
240 /**
241 * Sorts list by Name
242 * @param sortAscending true: ascending, false: descending
243 */
244 public static Vector<OCFile> sortByName(Vector<OCFile> files){
245 final Integer val;
246 if (mSortAscending){
247 val = 1;
248 } else {
249 val = -1;
250 }
251
252 Collections.sort(files, new Comparator<OCFile>() {
253 public int compare(OCFile o1, OCFile o2) {
254 if (o1.isFolder() && o2.isFolder()) {
255 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
256 } else if (o1.isFolder()) {
257 return -1;
258 } else if (o2.isFolder()) {
259 return 1;
260 }
261 return val * new AlphanumComparator().compare(o1, o2);
262 }
263 });
264
265 return files;
266 }
267
268 /**
269 * Local Folder size
270 * @param dir File
271 * @return Size in bytes
272 */
273 public static long getFolderSize(File dir) {
274 if (dir.exists()) {
275 long result = 0;
276 File[] fileList = dir.listFiles();
277 for(int i = 0; i < fileList.length; i++) {
278 if(fileList[i].isDirectory()) {
279 result += getFolderSize(fileList[i]);
280 } else {
281 result += fileList[i].length();
282 }
283 }
284 return result;
285 }
286 return 0;
287 }
288
289 }