de1a32076e60f10a92c991af7ff31e50f099e7d0
[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 public static String getParentPath(String remotePath) {
98 String parentPath = new File(remotePath).getParent();
99 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
100 return parentPath;
101 }
102
103 /**
104 * Creates and populates a new {@link OCFile} object with the data read from the server.
105 *
106 * @param remote remote file read from the server (remote file or folder).
107 * @return New OCFile instance representing the remote resource described by we.
108 */
109 public static OCFile fillOCFile(RemoteFile remote) {
110 OCFile file = new OCFile(remote.getRemotePath());
111 file.setCreationTimestamp(remote.getCreationTimestamp());
112 file.setFileLength(remote.getLength());
113 file.setMimetype(remote.getMimeType());
114 file.setModificationTimestamp(remote.getModifiedTimestamp());
115 file.setEtag(remote.getEtag());
116 file.setPermissions(remote.getPermissions());
117 file.setRemoteId(remote.getRemoteId());
118 return file;
119 }
120
121 /**
122 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
123 *
124 * @param oCFile OCFile
125 * @return New RemoteFile instance representing the resource described by ocFile.
126 */
127 public static RemoteFile fillRemoteFile(OCFile ocFile){
128 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
129 file.setCreationTimestamp(ocFile.getCreationTimestamp());
130 file.setLength(ocFile.getFileLength());
131 file.setMimeType(ocFile.getMimetype());
132 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
133 file.setEtag(ocFile.getEtag());
134 file.setPermissions(ocFile.getPermissions());
135 file.setRemoteId(ocFile.getRemoteId());
136 return file;
137 }
138
139 /**
140 * Sorts all filenames, regarding last user decision
141 */
142 public static Vector<OCFile> sortDirectory(Vector<OCFile> files){
143 switch (mSortOrder){
144 case 0:
145 files = FileStorageUtils.sortByName(files);
146 break;
147 case 1:
148 files = FileStorageUtils.sortByDate(files);
149 break;
150 case 2:
151 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
152 break;
153 }
154
155 return files;
156 }
157
158 /**
159 * Sorts list by Date
160 * @param sortAscending true: ascending, false: descending
161 */
162 public static Vector<OCFile> sortByDate(Vector<OCFile> files){
163 final Integer val;
164 if (mSortAscending){
165 val = 1;
166 } else {
167 val = -1;
168 }
169
170 Collections.sort(files, new Comparator<OCFile>() {
171 public int compare(OCFile o1, OCFile o2) {
172 if (o1.isFolder() && o2.isFolder()) {
173 Long obj1 = o1.getModificationTimestamp();
174 return val * obj1.compareTo(o2.getModificationTimestamp());
175 }
176 else if (o1.isFolder()) {
177 return -1;
178 } else if (o2.isFolder()) {
179 return 1;
180 } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
181 return 0;
182 } else {
183 Long obj1 = o1.getModificationTimestamp();
184 return val * obj1.compareTo(o2.getModificationTimestamp());
185 }
186 }
187 });
188
189 return files;
190 }
191
192 // /**
193 // * Sorts list by Size
194 // * @param sortAscending true: ascending, false: descending
195 // */
196 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
197 // final Integer val;
198 // if (mSortAscending){
199 // val = 1;
200 // } else {
201 // val = -1;
202 // }
203 //
204 // Collections.sort(files, new Comparator<OCFile>() {
205 // public int compare(OCFile o1, OCFile o2) {
206 // if (o1.isFolder() && o2.isFolder()) {
207 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
208 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
209 // }
210 // else if (o1.isFolder()) {
211 // return -1;
212 // } else if (o2.isFolder()) {
213 // return 1;
214 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
215 // return 0;
216 // } else {
217 // Long obj1 = o1.getFileLength();
218 // return val * obj1.compareTo(o2.getFileLength());
219 // }
220 // }
221 // });
222 //
223 // return files;
224 // }
225
226 /**
227 * Sorts list by Name
228 * @param sortAscending true: ascending, false: descending
229 */
230 public static Vector<OCFile> sortByName(Vector<OCFile> files){
231 final Integer val;
232 if (mSortAscending){
233 val = 1;
234 } else {
235 val = -1;
236 }
237
238 Collections.sort(files, new Comparator<OCFile>() {
239 public int compare(OCFile o1, OCFile o2) {
240 if (o1.isFolder() && o2.isFolder()) {
241 return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
242 } else if (o1.isFolder()) {
243 return -1;
244 } else if (o2.isFolder()) {
245 return 1;
246 }
247 return val * new AlphanumComparator().compare(o1, o2);
248 }
249 });
250
251 return files;
252 }
253
254 /**
255 * Local Folder size
256 * @param dir File
257 * @return Size in bytes
258 */
259 public static long getFolderSize(File dir) {
260 if (dir.exists()) {
261 long result = 0;
262 File[] fileList = dir.listFiles();
263 for(int i = 0; i < fileList.length; i++) {
264 if(fileList[i].isDirectory()) {
265 result += getFolderSize(fileList[i]);
266 } else {
267 result += fileList[i].length();
268 }
269 }
270 return result;
271 }
272 return 0;
273 }
274
275 }