de1a32076e60f10a92c991af7ff31e50f099e7d0
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.utils
;
21 import java
.util
.Collections
;
22 import java
.util
.Comparator
;
23 import java
.util
.Vector
;
25 import third_parties
.daveKoeller
.AlphanumComparator
;
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
;
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
;
42 * Static methods to help in access to local file system.
44 * @author David A. Velasco
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;
54 //private static final String LOG_TAG = "FileStorageUtils";
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
62 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
63 return getSavePath(accountName
) + file
.getRemotePath();
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
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();
79 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
80 return stats
.getAvailableBlocks() * stats
.getBlockSize();
85 public static final String
getLogPath() {
86 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
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
);
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
;
104 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());
122 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
124 * @param oCFile OCFile
125 * @return New RemoteFile instance representing the resource described by ocFile.
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());
140 * Sorts all filenames, regarding last user decision
142 public static Vector
<OCFile
> sortDirectory(Vector
<OCFile
> files
){
145 files
= FileStorageUtils
.sortByName(files
);
148 files
= FileStorageUtils
.sortByDate(files
);
151 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
160 * @param sortAscending true: ascending, false: descending
162 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
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());
176 else if (o1
.isFolder()) {
178 } else if (o2
.isFolder()) {
180 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
183 Long obj1
= o1
.getModificationTimestamp();
184 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
193 // * Sorts list by Size
194 // * @param sortAscending true: ascending, false: descending
196 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
197 // final Integer val;
198 // if (mSortAscending){
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))));
210 // else if (o1.isFolder()) {
212 // } else if (o2.isFolder()) {
214 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
217 // Long obj1 = o1.getFileLength();
218 // return val * obj1.compareTo(o2.getFileLength());
228 * @param sortAscending true: ascending, false: descending
230 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
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()) {
244 } else if (o2
.isFolder()) {
247 return val
* new AlphanumComparator().compare(o1
, o2
);
257 * @return Size in bytes
259 public static long getFolderSize(File dir
) {
262 File
[] fileList
= dir
.listFiles();
263 for(int i
= 0; i
< fileList
.length
; i
++) {
264 if(fileList
[i
].isDirectory()) {
265 result
+= getFolderSize(fileList
[i
]);
267 result
+= fileList
[i
].length();