2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
21 package com
.owncloud
.android
.utils
;
24 import java
.util
.ArrayList
;
25 import java
.util
.Arrays
;
26 import java
.util
.Collections
;
27 import java
.util
.Comparator
;
28 import java
.util
.List
;
29 import java
.util
.Vector
;
31 import third_parties
.daveKoeller
.AlphanumComparator
;
33 import com
.owncloud
.android
.MainApp
;
34 import com
.owncloud
.android
.R
;
35 import com
.owncloud
.android
.datamodel
.OCFile
;
36 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
38 import android
.annotation
.SuppressLint
;
39 import android
.content
.Context
;
40 import android
.content
.SharedPreferences
;
41 import android
.preference
.PreferenceManager
;
42 import android
.net
.Uri
;
43 import android
.os
.Environment
;
44 import android
.os
.StatFs
;
45 import android
.webkit
.MimeTypeMap
;
49 * Static methods to help in access to local file system.
51 public class FileStorageUtils
{
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 public static Integer mSortOrder
= SORT_NAME
;
56 public static Boolean mSortAscending
= true
;
59 //private static final String LOG_TAG = "FileStorageUtils";
61 public static final String
getSavePath(String accountName
) {
62 File sdCard
= Environment
.getExternalStorageDirectory();
63 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/" + Uri
.encode(accountName
, "@");
64 // 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
67 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
68 return getSavePath(accountName
) + file
.getRemotePath();
71 public static final String
getTemporalPath(String accountName
) {
72 File sdCard
= Environment
.getExternalStorageDirectory();
73 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/tmp/" + Uri
.encode(accountName
, "@");
74 // 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
77 @SuppressLint("NewApi")
78 public static final long getUsableSpace(String accountName
) {
79 File savePath
= Environment
.getExternalStorageDirectory();
80 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD
) {
81 return savePath
.getUsableSpace();
84 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
85 return stats
.getAvailableBlocks() * stats
.getBlockSize();
90 public static final String
getLogPath() {
91 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
94 public static String
getInstantUploadFilePath(Context context
, String fileName
) {
95 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
96 String uploadPathdef
= context
.getString(R
.string
.instant_upload_path
);
97 String uploadPath
= pref
.getString("instant_upload_path", uploadPathdef
);
98 String value
= uploadPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
103 * Gets the composed path when video is or must be stored
105 * @param fileName: video file name
106 * @return String: video file path composed
108 public static String
getInstantVideoUploadFilePath(Context context
, String fileName
) {
109 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
110 String uploadVideoPathdef
= context
.getString(R
.string
.instant_upload_path
);
111 String uploadVideoPath
= pref
.getString("instant_video_upload_path", uploadVideoPathdef
);
112 String value
= uploadVideoPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
116 public static String
getParentPath(String remotePath
) {
117 String parentPath
= new File(remotePath
).getParent();
118 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
123 * Creates and populates a new {@link OCFile} object with the data read from the server.
125 * @param remote remote file read from the server (remote file or folder).
126 * @return New OCFile instance representing the remote resource described by we.
128 public static OCFile
fillOCFile(RemoteFile remote
) {
129 OCFile file
= new OCFile(remote
.getRemotePath());
130 file
.setCreationTimestamp(remote
.getCreationTimestamp());
131 file
.setFileLength(remote
.getLength());
132 file
.setMimetype(remote
.getMimeType());
133 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
134 file
.setEtag(remote
.getEtag());
135 file
.setPermissions(remote
.getPermissions());
136 file
.setRemoteId(remote
.getRemoteId());
141 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
143 * @param ocFile OCFile
144 * @return New RemoteFile instance representing the resource described by ocFile.
146 public static RemoteFile
fillRemoteFile(OCFile ocFile
){
147 RemoteFile file
= new RemoteFile(ocFile
.getRemotePath());
148 file
.setCreationTimestamp(ocFile
.getCreationTimestamp());
149 file
.setLength(ocFile
.getFileLength());
150 file
.setMimeType(ocFile
.getMimetype());
151 file
.setModifiedTimestamp(ocFile
.getModificationTimestamp());
152 file
.setEtag(ocFile
.getEtag());
153 file
.setPermissions(ocFile
.getPermissions());
154 file
.setRemoteId(ocFile
.getRemoteId());
159 * Sorts all filenames, regarding last user decision
161 public static Vector
<OCFile
> sortOcFolder(Vector
<OCFile
> files
){
164 files
= FileStorageUtils
.sortOCFilesByName(files
);
167 files
= FileStorageUtils
.sortOCFilesByDate(files
);
170 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
178 * Sorts all filenames, regarding last user decision
180 public static File
[] sortLocalFolder(File
[] files
){
183 files
= FileStorageUtils
.sortLocalFilesByName(files
);
186 files
= FileStorageUtils
.sortLocalFilesByDate(files
);
189 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
200 public static Vector
<OCFile
> sortOCFilesByDate(Vector
<OCFile
> files
){
208 Collections
.sort(files
, new Comparator
<OCFile
>() {
209 public int compare(OCFile o1
, OCFile o2
) {
210 if (o1
.isFolder() && o2
.isFolder()) {
211 Long obj1
= o1
.getModificationTimestamp();
212 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
214 else if (o1
.isFolder()) {
216 } else if (o2
.isFolder()) {
218 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
221 Long obj1
= o1
.getModificationTimestamp();
222 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
234 public static File
[] sortLocalFilesByDate(File
[] filesArray
){
242 List
<File
> files
= new ArrayList
<File
>(Arrays
.asList(filesArray
));
244 Collections
.sort(files
, new Comparator
<File
>() {
245 public int compare(File o1
, File o2
) {
246 if (o1
.isDirectory() && o2
.isDirectory()) {
247 Long obj1
= o1
.lastModified();
248 return val
* obj1
.compareTo(o2
.lastModified());
250 else if (o1
.isDirectory()) {
252 } else if (o2
.isDirectory()) {
254 } else if (o1
.lastModified() == 0 || o2
.lastModified() == 0){
257 Long obj1
= o1
.lastModified();
258 return val
* obj1
.compareTo(o2
.lastModified());
263 File
[] returnArray
= new File
[1];
264 return files
.toArray(returnArray
);
268 // * Sorts list by Size
269 // * @param sortAscending true: ascending, false: descending
271 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
272 // final Integer val;
273 // if (mSortAscending){
279 // Collections.sort(files, new Comparator<OCFile>() {
280 // public int compare(OCFile o1, OCFile o2) {
281 // if (o1.isFolder() && o2.isFolder()) {
282 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
283 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
285 // else if (o1.isFolder()) {
287 // } else if (o2.isFolder()) {
289 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
292 // Long obj1 = o1.getFileLength();
293 // return val * obj1.compareTo(o2.getFileLength());
303 * @param files files to sort
305 public static Vector
<OCFile
> sortOCFilesByName(Vector
<OCFile
> files
){
313 Collections
.sort(files
, new Comparator
<OCFile
>() {
314 public int compare(OCFile o1
, OCFile o2
) {
315 if (o1
.isFolder() && o2
.isFolder()) {
316 return val
* new AlphanumComparator().compare(o1
, o2
);
317 } else if (o1
.isFolder()) {
319 } else if (o2
.isFolder()) {
322 return val
* new AlphanumComparator().compare(o1
, o2
);
331 * @param filesArray files to sort
333 public static File
[] sortLocalFilesByName(File
[] filesArray
){
341 List
<File
> files
= new ArrayList
<File
>(Arrays
.asList(filesArray
));
343 Collections
.sort(files
, new Comparator
<File
>() {
344 public int compare(File o1
, File o2
) {
345 if (o1
.isDirectory() && o2
.isDirectory()) {
346 return val
* o1
.getPath().toLowerCase().compareTo(o2
.getPath().toLowerCase());
347 } else if (o1
.isDirectory()) {
349 } else if (o2
.isDirectory()) {
352 return val
* new AlphanumComparator().compare(o1
.getPath().toLowerCase(),
353 o2
.getPath().toLowerCase());
357 File
[] returnArray
= new File
[1];
358 return files
.toArray(returnArray
);
364 * @return Size in bytes
366 public static long getFolderSize(File dir
) {
369 File
[] fileList
= dir
.listFiles();
370 for(int i
= 0; i
< fileList
.length
; i
++) {
371 if(fileList
[i
].isDirectory()) {
372 result
+= getFolderSize(fileList
[i
]);
374 result
+= fileList
[i
].length();
383 * Mimetype String of a file
387 public static String
getMimeTypeFromName(String path
) {
388 String extension
= "";
389 int pos
= path
.lastIndexOf('.');
391 extension
= path
.substring(pos
+ 1);
393 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
394 return (result
!= null
) ? result
: "";