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 LOG_TAG = "FileStorageUtils";
58 public static final String
getSavePath(String accountName
) {
59 File sdCard
= Environment
.getExternalStorageDirectory();
60 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/" + Uri
.encode(accountName
, "@");
61 // 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
64 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
65 return getSavePath(accountName
) + file
.getRemotePath();
68 public static final String
getTemporalPath(String accountName
) {
69 File sdCard
= Environment
.getExternalStorageDirectory();
70 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/tmp/" + Uri
.encode(accountName
, "@");
71 // 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
74 @SuppressLint("NewApi")
75 public static final long getUsableSpace(String accountName
) {
76 File savePath
= Environment
.getExternalStorageDirectory();
77 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD
) {
78 return savePath
.getUsableSpace();
81 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
82 return stats
.getAvailableBlocks() * stats
.getBlockSize();
87 public static final String
getLogPath() {
88 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
91 public static String
getInstantUploadFilePath(Context context
, String fileName
) {
92 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
93 String uploadPathdef
= context
.getString(R
.string
.instant_upload_path
);
94 String uploadPath
= pref
.getString("instant_upload_path", uploadPathdef
);
95 String value
= uploadPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
100 * Gets the composed path when video is or must be stored
102 * @param fileName: video file name
103 * @return String: video file path composed
105 public static String
getInstantVideoUploadFilePath(Context context
, String fileName
) {
106 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
107 String uploadVideoPathdef
= context
.getString(R
.string
.instant_upload_path
);
108 String uploadVideoPath
= pref
.getString("instant_video_upload_path", uploadVideoPathdef
);
109 String value
= uploadVideoPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
113 public static String
getParentPath(String remotePath
) {
114 String parentPath
= new File(remotePath
).getParent();
115 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
120 * Creates and populates a new {@link OCFile} object with the data read from the server.
122 * @param remote remote file read from the server (remote file or folder).
123 * @return New OCFile instance representing the remote resource described by we.
125 public static OCFile
fillOCFile(RemoteFile remote
) {
126 OCFile file
= new OCFile(remote
.getRemotePath());
127 file
.setCreationTimestamp(remote
.getCreationTimestamp());
128 file
.setFileLength(remote
.getLength());
129 file
.setMimetype(remote
.getMimeType());
130 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
131 file
.setEtag(remote
.getEtag());
132 file
.setPermissions(remote
.getPermissions());
133 file
.setRemoteId(remote
.getRemoteId());
138 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
140 * @param ocFile OCFile
141 * @return New RemoteFile instance representing the resource described by ocFile.
143 public static RemoteFile
fillRemoteFile(OCFile ocFile
){
144 RemoteFile file
= new RemoteFile(ocFile
.getRemotePath());
145 file
.setCreationTimestamp(ocFile
.getCreationTimestamp());
146 file
.setLength(ocFile
.getFileLength());
147 file
.setMimeType(ocFile
.getMimetype());
148 file
.setModifiedTimestamp(ocFile
.getModificationTimestamp());
149 file
.setEtag(ocFile
.getEtag());
150 file
.setPermissions(ocFile
.getPermissions());
151 file
.setRemoteId(ocFile
.getRemoteId());
156 * Sorts all filenames, regarding last user decision
158 public static Vector
<OCFile
> sortFolder(Vector
<OCFile
> files
){
161 files
= FileStorageUtils
.sortByName(files
);
164 files
= FileStorageUtils
.sortByDate(files
);
167 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
178 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
186 Collections
.sort(files
, new Comparator
<OCFile
>() {
187 public int compare(OCFile o1
, OCFile o2
) {
188 if (o1
.isFolder() && o2
.isFolder()) {
189 Long obj1
= o1
.getModificationTimestamp();
190 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
192 else if (o1
.isFolder()) {
194 } else if (o2
.isFolder()) {
196 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
199 Long obj1
= o1
.getModificationTimestamp();
200 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
209 // * Sorts list by Size
210 // * @param sortAscending true: ascending, false: descending
212 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
213 // final Integer val;
214 // if (mSortAscending){
220 // Collections.sort(files, new Comparator<OCFile>() {
221 // public int compare(OCFile o1, OCFile o2) {
222 // if (o1.isFolder() && o2.isFolder()) {
223 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
224 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
226 // else if (o1.isFolder()) {
228 // } else if (o2.isFolder()) {
230 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
233 // Long obj1 = o1.getFileLength();
234 // return val * obj1.compareTo(o2.getFileLength());
244 * @param files files to sort
246 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
254 Collections
.sort(files
, new Comparator
<OCFile
>() {
255 public int compare(OCFile o1
, OCFile o2
) {
256 if (o1
.isFolder() && o2
.isFolder()) {
257 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
258 } else if (o1
.isFolder()) {
260 } else if (o2
.isFolder()) {
263 return val
* new AlphanumComparator().compare(o1
, o2
);
273 * @return Size in bytes
275 public static long getFolderSize(File dir
) {
278 File
[] fileList
= dir
.listFiles();
279 for(int i
= 0; i
< fileList
.length
; i
++) {
280 if(fileList
[i
].isDirectory()) {
281 result
+= getFolderSize(fileList
[i
]);
283 result
+= fileList
[i
].length();
292 * Mimetype String of a file
296 public static String
getMimeTypeFromName(String path
) {
297 String extension
= "";
298 int pos
= path
.lastIndexOf('.');
300 extension
= path
.substring(pos
+ 1);
302 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
303 return (result
!= null
) ? result
: "";