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 final Integer SORT_NAME
= 0;
50 public static final Integer SORT_DATE
= 1;
51 public static final Integer SORT_SIZE
= 2;
52 public static Integer mSortOrder
= SORT_NAME
;
53 public static Boolean mSortAscending
= true
;
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();
84 public static final String
getSavePath(String accountName
) {
85 //File sdCard = Environment.getExternalStorageDirectory();
86 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
87 // 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
88 //return getBaseStorePath().getAbsolutePath() + "/" + Uri.encode(accountName, "@");
89 return getBaseStorePathString() + "/" + Uri
.encode(accountName
, "@");
92 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
93 return getSavePath(accountName
) + file
.getRemotePath();
96 public static final String
getTemporalPath(String accountName
) {
97 //File sdCard = Environment.getExternalStorageDirectory();
98 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
99 // 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
100 //return getBaseStorePath().getAbsolutePath() + "/tmp/" + Uri.encode(accountName, "@");
101 return getBaseStorePathString() + "/tmp/" + Uri
.encode(accountName
, "@");
104 @SuppressLint("NewApi")
105 public static final long getUsableSpace(String accountName
) {
106 //File savePath = Environment.getExternalStorageDirectory();
107 File savePath
= getBaseStorePath();
108 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD
) {
109 return savePath
.getUsableSpace();
112 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
113 return stats
.getAvailableBlocks() * stats
.getBlockSize();
118 public static final String
getLogPath() {
119 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
122 public static String
getInstantUploadFilePath(Context context
, String fileName
) {
123 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
124 String uploadPathdef
= context
.getString(R
.string
.instant_upload_path
);
125 String uploadPath
= pref
.getString("instant_upload_path", uploadPathdef
);
126 String value
= uploadPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
131 * Gets the composed path when video is or must be stored
133 * @param fileName: video file name
134 * @return String: video file path composed
136 public static String
getInstantVideoUploadFilePath(Context context
, String fileName
) {
137 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
138 String uploadVideoPathdef
= context
.getString(R
.string
.instant_upload_path
);
139 String uploadVideoPath
= pref
.getString("instant_video_upload_path", uploadVideoPathdef
);
140 String value
= uploadVideoPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
144 public static String
getParentPath(String remotePath
) {
145 String parentPath
= new File(remotePath
).getParent();
146 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
151 * Creates and populates a new {@link OCFile} object with the data read from the server.
153 * @param remote remote file read from the server (remote file or folder).
154 * @return New OCFile instance representing the remote resource described by we.
156 public static OCFile
fillOCFile(RemoteFile remote
) {
157 OCFile file
= new OCFile(remote
.getRemotePath());
158 file
.setCreationTimestamp(remote
.getCreationTimestamp());
159 file
.setFileLength(remote
.getLength());
160 file
.setMimetype(remote
.getMimeType());
161 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
162 file
.setEtag(remote
.getEtag());
163 file
.setPermissions(remote
.getPermissions());
164 file
.setRemoteId(remote
.getRemoteId());
169 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
171 * @param ocFile OCFile
172 * @return New RemoteFile instance representing the resource described by ocFile.
174 public static RemoteFile
fillRemoteFile(OCFile ocFile
){
175 RemoteFile file
= new RemoteFile(ocFile
.getRemotePath());
176 file
.setCreationTimestamp(ocFile
.getCreationTimestamp());
177 file
.setLength(ocFile
.getFileLength());
178 file
.setMimeType(ocFile
.getMimetype());
179 file
.setModifiedTimestamp(ocFile
.getModificationTimestamp());
180 file
.setEtag(ocFile
.getEtag());
181 file
.setPermissions(ocFile
.getPermissions());
182 file
.setRemoteId(ocFile
.getRemoteId());
187 * Sorts all filenames, regarding last user decision
189 public static Vector
<OCFile
> sortFolder(Vector
<OCFile
> files
){
192 files
= FileStorageUtils
.sortByName(files
);
195 files
= FileStorageUtils
.sortByDate(files
);
198 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
209 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
217 Collections
.sort(files
, new Comparator
<OCFile
>() {
218 public int compare(OCFile o1
, OCFile o2
) {
219 if (o1
.isFolder() && o2
.isFolder()) {
220 Long obj1
= o1
.getModificationTimestamp();
221 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
223 else if (o1
.isFolder()) {
225 } else if (o2
.isFolder()) {
227 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
230 Long obj1
= o1
.getModificationTimestamp();
231 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
240 // * Sorts list by Size
241 // * @param sortAscending true: ascending, false: descending
243 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
244 // final Integer val;
245 // if (mSortAscending){
251 // Collections.sort(files, new Comparator<OCFile>() {
252 // public int compare(OCFile o1, OCFile o2) {
253 // if (o1.isFolder() && o2.isFolder()) {
254 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
255 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
257 // else if (o1.isFolder()) {
259 // } else if (o2.isFolder()) {
261 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
264 // Long obj1 = o1.getFileLength();
265 // return val * obj1.compareTo(o2.getFileLength());
275 * @param files files to sort
277 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
285 Collections
.sort(files
, new Comparator
<OCFile
>() {
286 public int compare(OCFile o1
, OCFile o2
) {
287 if (o1
.isFolder() && o2
.isFolder()) {
288 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
289 } else if (o1
.isFolder()) {
291 } else if (o2
.isFolder()) {
294 return val
* new AlphanumComparator().compare(o1
, o2
);
304 * @return Size in bytes
306 public static long getFolderSize(File dir
) {
309 File
[] fileList
= dir
.listFiles();
310 for(int i
= 0; i
< fileList
.length
; i
++) {
311 if(fileList
[i
].isDirectory()) {
312 result
+= getFolderSize(fileList
[i
]);
314 result
+= fileList
[i
].length();
323 * Mimetype String of a file
327 public static String
getMimeTypeFromName(String path
) {
328 String extension
= "";
329 int pos
= path
.lastIndexOf('.');
331 extension
= path
.substring(pos
+ 1);
333 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
334 return (result
!= null
) ? result
: "";