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
);
98 * Gets the composed path when video is or must be stored
100 * @param fileName: video file name
101 * @return String: video file path composed
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
);
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
;
118 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());
136 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
138 * @param oCFile OCFile
139 * @return New RemoteFile instance representing the resource described by ocFile.
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());
154 * Sorts all filenames, regarding last user decision
156 public static Vector
<OCFile
> sortFolder(Vector
<OCFile
> files
){
159 files
= FileStorageUtils
.sortByName(files
);
162 files
= FileStorageUtils
.sortByDate(files
);
165 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
174 * @param sortAscending true: ascending, false: descending
176 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
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());
190 else if (o1
.isFolder()) {
192 } else if (o2
.isFolder()) {
194 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
197 Long obj1
= o1
.getModificationTimestamp();
198 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
207 // * Sorts list by Size
208 // * @param sortAscending true: ascending, false: descending
210 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
211 // final Integer val;
212 // if (mSortAscending){
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))));
224 // else if (o1.isFolder()) {
226 // } else if (o2.isFolder()) {
228 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
231 // Long obj1 = o1.getFileLength();
232 // return val * obj1.compareTo(o2.getFileLength());
242 * @param sortAscending true: ascending, false: descending
244 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
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()) {
258 } else if (o2
.isFolder()) {
261 return val
* new AlphanumComparator().compare(o1
, o2
);
271 * @return Size in bytes
273 public static long getFolderSize(File dir
) {
276 File
[] fileList
= dir
.listFiles();
277 for(int i
= 0; i
< fileList
.length
; i
++) {
278 if(fileList
[i
].isDirectory()) {
279 result
+= getFolderSize(fileList
[i
]);
281 result
+= fileList
[i
].length();