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
.Collections
;
25 import java
.util
.Comparator
;
26 import java
.util
.Vector
;
28 import third_parties
.daveKoeller
.AlphanumComparator
;
30 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.R
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
35 import android
.annotation
.SuppressLint
;
36 import android
.content
.Context
;
37 import android
.content
.SharedPreferences
;
38 import android
.preference
.PreferenceManager
;
39 import android
.net
.Uri
;
40 import android
.os
.Environment
;
41 import android
.os
.StatFs
;
42 import android
.webkit
.MimeTypeMap
;
46 * Static methods to help in access to local file system.
48 public class FileStorageUtils
{
49 public static Integer mSortOrder
;
50 public static Boolean mSortAscending
;
51 public static final Integer SORT_NAME
= 0;
52 public static final Integer SORT_DATE
= 1;
53 public static final Integer SORT_SIZE
= 2;
56 //private static final String TAG = FileStorageUtils.class.getSimpleName();
58 @SuppressLint("NewApi")
59 private static final File
getBaseStorePath() {
60 File baseStoragePath
= Environment
.getExternalStorageDirectory();
61 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.KITKAT
) {
62 File
[] dirs
= MainApp
.getAppContext().getExternalFilesDirs(null
);
63 if (dirs
.length
> 1) {
64 baseStoragePath
= dirs
[1];
67 return baseStoragePath
;
70 @SuppressLint("NewApi")
71 private static final String
getBaseStorePathString() {
72 File baseStoragePath
= Environment
.getExternalStorageDirectory();
73 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.KITKAT
) {
74 File
[] dirs
= MainApp
.getAppContext().getExternalFilesDirs(null
);
75 if (dirs
.length
> 1) {
76 baseStoragePath
= dirs
[1];
78 return baseStoragePath
.getAbsolutePath();
80 return baseStoragePath
.getAbsolutePath() + "/" + MainApp
.getDataFolder();
85 public static final String
getSavePath(String accountName
) {
86 //File sdCard = Environment.getExternalStorageDirectory();
87 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
88 // 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
89 //return getBaseStorePath().getAbsolutePath() + "/" + Uri.encode(accountName, "@");
90 return getBaseStorePathString() + "/" + Uri
.encode(accountName
, "@");
93 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
94 return getSavePath(accountName
) + file
.getRemotePath();
97 public static final String
getTemporalPath(String accountName
) {
98 //File sdCard = Environment.getExternalStorageDirectory();
99 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
100 // 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
101 //return getBaseStorePath().getAbsolutePath() + "/tmp/" + Uri.encode(accountName, "@");
102 return getBaseStorePathString() + "/tmp/" + Uri
.encode(accountName
, "@");
105 @SuppressLint("NewApi")
106 public static final long getUsableSpace(String accountName
) {
107 //File savePath = Environment.getExternalStorageDirectory();
108 File savePath
= getBaseStorePath();
109 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD
) {
110 return savePath
.getUsableSpace();
113 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
114 return stats
.getAvailableBlocks() * stats
.getBlockSize();
119 public static final String
getLogPath() {
120 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
123 public static String
getInstantUploadFilePath(Context context
, String fileName
) {
124 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
125 String uploadPathdef
= context
.getString(R
.string
.instant_upload_path
);
126 String uploadPath
= pref
.getString("instant_upload_path", uploadPathdef
);
127 String value
= uploadPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
132 * Gets the composed path when video is or must be stored
134 * @param fileName: video file name
135 * @return String: video file path composed
137 public static String
getInstantVideoUploadFilePath(Context context
, String fileName
) {
138 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
139 String uploadVideoPathdef
= context
.getString(R
.string
.instant_upload_path
);
140 String uploadVideoPath
= pref
.getString("instant_video_upload_path", uploadVideoPathdef
);
141 String value
= uploadVideoPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
145 public static String
getParentPath(String remotePath
) {
146 String parentPath
= new File(remotePath
).getParent();
147 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
152 * Creates and populates a new {@link OCFile} object with the data read from the server.
154 * @param remote remote file read from the server (remote file or folder).
155 * @return New OCFile instance representing the remote resource described by we.
157 public static OCFile
fillOCFile(RemoteFile remote
) {
158 OCFile file
= new OCFile(remote
.getRemotePath());
159 file
.setCreationTimestamp(remote
.getCreationTimestamp());
160 file
.setFileLength(remote
.getLength());
161 file
.setMimetype(remote
.getMimeType());
162 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
163 file
.setEtag(remote
.getEtag());
164 file
.setPermissions(remote
.getPermissions());
165 file
.setRemoteId(remote
.getRemoteId());
170 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
172 * @param ocFile OCFile
173 * @return New RemoteFile instance representing the resource described by ocFile.
175 public static RemoteFile
fillRemoteFile(OCFile ocFile
){
176 RemoteFile file
= new RemoteFile(ocFile
.getRemotePath());
177 file
.setCreationTimestamp(ocFile
.getCreationTimestamp());
178 file
.setLength(ocFile
.getFileLength());
179 file
.setMimeType(ocFile
.getMimetype());
180 file
.setModifiedTimestamp(ocFile
.getModificationTimestamp());
181 file
.setEtag(ocFile
.getEtag());
182 file
.setPermissions(ocFile
.getPermissions());
183 file
.setRemoteId(ocFile
.getRemoteId());
188 * Sorts all filenames, regarding last user decision
190 public static Vector
<OCFile
> sortFolder(Vector
<OCFile
> files
){
193 files
= FileStorageUtils
.sortByName(files
);
196 files
= FileStorageUtils
.sortByDate(files
);
199 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
210 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
218 Collections
.sort(files
, new Comparator
<OCFile
>() {
219 public int compare(OCFile o1
, OCFile o2
) {
220 if (o1
.isFolder() && o2
.isFolder()) {
221 Long obj1
= o1
.getModificationTimestamp();
222 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
224 else if (o1
.isFolder()) {
226 } else if (o2
.isFolder()) {
228 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
231 Long obj1
= o1
.getModificationTimestamp();
232 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
241 // * Sorts list by Size
242 // * @param sortAscending true: ascending, false: descending
244 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
245 // final Integer val;
246 // if (mSortAscending){
252 // Collections.sort(files, new Comparator<OCFile>() {
253 // public int compare(OCFile o1, OCFile o2) {
254 // if (o1.isFolder() && o2.isFolder()) {
255 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
256 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
258 // else if (o1.isFolder()) {
260 // } else if (o2.isFolder()) {
262 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
265 // Long obj1 = o1.getFileLength();
266 // return val * obj1.compareTo(o2.getFileLength());
276 * @param files files to sort
278 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
286 Collections
.sort(files
, new Comparator
<OCFile
>() {
287 public int compare(OCFile o1
, OCFile o2
) {
288 if (o1
.isFolder() && o2
.isFolder()) {
289 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
290 } else if (o1
.isFolder()) {
292 } else if (o2
.isFolder()) {
295 return val
* new AlphanumComparator().compare(o1
, o2
);
305 * @return Size in bytes
307 public static long getFolderSize(File dir
) {
310 File
[] fileList
= dir
.listFiles();
311 for(int i
= 0; i
< fileList
.length
; i
++) {
312 if(fileList
[i
].isDirectory()) {
313 result
+= getFolderSize(fileList
[i
]);
315 result
+= fileList
[i
].length();
324 * Mimetype String of a file
328 public static String
getMimeTypeFromName(String path
) {
329 String extension
= "";
330 int pos
= path
.lastIndexOf('.');
332 extension
= path
.substring(pos
+ 1);
334 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
335 return (result
!= null
) ? result
: "";