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
.accounts
.Account
;
36 import android
.annotation
.SuppressLint
;
37 import android
.content
.Context
;
38 import android
.content
.SharedPreferences
;
39 import android
.preference
.PreferenceManager
;
40 import android
.net
.Uri
;
41 import android
.os
.Environment
;
42 import android
.os
.StatFs
;
43 import android
.webkit
.MimeTypeMap
;
47 * Static methods to help in access to local file system.
49 public class FileStorageUtils
{
50 public static Integer mSortOrder
;
51 public static Boolean mSortAscending
;
52 public static final Integer SORT_NAME
= 0;
53 public static final Integer SORT_DATE
= 1;
54 public static final Integer SORT_SIZE
= 2;
57 //private static final String LOG_TAG = "FileStorageUtils";
59 public static final String
getSavePath(String accountName
) {
60 File sdCard
= Environment
.getExternalStorageDirectory();
61 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/" + Uri
.encode(accountName
, "@");
62 // 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
65 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
66 return getSavePath(accountName
) + file
.getRemotePath();
69 public static final String
getTemporalPath(String accountName
) {
70 File sdCard
= Environment
.getExternalStorageDirectory();
71 return sdCard
.getAbsolutePath() + "/" + MainApp
.getDataFolder() + "/tmp/" + Uri
.encode(accountName
, "@");
72 // 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
75 @SuppressLint("NewApi")
76 public static final long getUsableSpace(String accountName
) {
77 File savePath
= Environment
.getExternalStorageDirectory();
78 if (android
.os
.Build
.VERSION
.SDK_INT
>= android
.os
.Build
.VERSION_CODES
.GINGERBREAD
) {
79 return savePath
.getUsableSpace();
82 StatFs stats
= new StatFs(savePath
.getAbsolutePath());
83 return stats
.getAvailableBlocks() * stats
.getBlockSize();
88 public static final String
getLogPath() {
89 return Environment
.getExternalStorageDirectory() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "log";
92 public static String
getInstantUploadFilePath(Context context
, String fileName
) {
93 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
94 String uploadPathdef
= context
.getString(R
.string
.instant_upload_path
);
95 String uploadPath
= pref
.getString("instant_upload_path", uploadPathdef
);
96 String value
= uploadPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
101 * Gets the composed path when video is or must be stored
103 * @param fileName: video file name
104 * @return String: video file path composed
106 public static String
getInstantVideoUploadFilePath(Context context
, String fileName
) {
107 SharedPreferences pref
= PreferenceManager
.getDefaultSharedPreferences(context
);
108 String uploadVideoPathdef
= context
.getString(R
.string
.instant_upload_path
);
109 String uploadVideoPath
= pref
.getString("instant_video_upload_path", uploadVideoPathdef
);
110 String value
= uploadVideoPath
+ OCFile
.PATH_SEPARATOR
+ (fileName
== null ?
"" : fileName
);
114 public static String
getParentPath(String remotePath
) {
115 String parentPath
= new File(remotePath
).getParent();
116 parentPath
= parentPath
.endsWith(OCFile
.PATH_SEPARATOR
) ? parentPath
: parentPath
+ OCFile
.PATH_SEPARATOR
;
121 * Creates and populates a new {@link OCFile} object with the data read from the server.
123 * @param remote remote file read from the server (remote file or folder).
124 * @return New OCFile instance representing the remote resource described by remote.
126 public static OCFile
fillOCFile(RemoteFile remote
) {
127 OCFile file
= new OCFile(remote
.getRemotePath());
128 file
.setCreationTimestamp(remote
.getCreationTimestamp());
129 file
.setFileLength(remote
.getLength());
130 file
.setMimetype(remote
.getMimeType());
131 file
.setModificationTimestamp(remote
.getModifiedTimestamp());
132 file
.setEtag(remote
.getEtag());
133 file
.setPermissions(remote
.getPermissions());
134 file
.setRemoteId(remote
.getRemoteId());
139 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
141 * @param ocFile OCFile
142 * @return New RemoteFile instance representing the resource described by ocFile.
144 public static RemoteFile
fillRemoteFile(OCFile ocFile
){
145 RemoteFile file
= new RemoteFile(ocFile
.getRemotePath());
146 file
.setCreationTimestamp(ocFile
.getCreationTimestamp());
147 file
.setLength(ocFile
.getFileLength());
148 file
.setMimeType(ocFile
.getMimetype());
149 file
.setModifiedTimestamp(ocFile
.getModificationTimestamp());
150 file
.setEtag(ocFile
.getEtag());
151 file
.setPermissions(ocFile
.getPermissions());
152 file
.setRemoteId(ocFile
.getRemoteId());
157 * Sorts all filenames, regarding last user decision
159 public static Vector
<OCFile
> sortFolder(Vector
<OCFile
> files
){
162 files
= FileStorageUtils
.sortByName(files
);
165 files
= FileStorageUtils
.sortByDate(files
);
168 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
179 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
187 Collections
.sort(files
, new Comparator
<OCFile
>() {
188 public int compare(OCFile o1
, OCFile o2
) {
189 if (o1
.isFolder() && o2
.isFolder()) {
190 Long obj1
= o1
.getModificationTimestamp();
191 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
193 else if (o1
.isFolder()) {
195 } else if (o2
.isFolder()) {
197 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
200 Long obj1
= o1
.getModificationTimestamp();
201 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
210 // * Sorts list by Size
211 // * @param sortAscending true: ascending, false: descending
213 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
214 // final Integer val;
215 // if (mSortAscending){
221 // Collections.sort(files, new Comparator<OCFile>() {
222 // public int compare(OCFile o1, OCFile o2) {
223 // if (o1.isFolder() && o2.isFolder()) {
224 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
225 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
227 // else if (o1.isFolder()) {
229 // } else if (o2.isFolder()) {
231 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
234 // Long obj1 = o1.getFileLength();
235 // return val * obj1.compareTo(o2.getFileLength());
245 * @param files files to sort
247 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
255 Collections
.sort(files
, new Comparator
<OCFile
>() {
256 public int compare(OCFile o1
, OCFile o2
) {
257 if (o1
.isFolder() && o2
.isFolder()) {
258 return val
* o1
.getRemotePath().toLowerCase().compareTo(o2
.getRemotePath().toLowerCase());
259 } else if (o1
.isFolder()) {
261 } else if (o2
.isFolder()) {
264 return val
* new AlphanumComparator().compare(o1
, o2
);
274 * @return Size in bytes
276 public static long getFolderSize(File dir
) {
279 File
[] fileList
= dir
.listFiles();
280 for(int i
= 0; i
< fileList
.length
; i
++) {
281 if(fileList
[i
].isDirectory()) {
282 result
+= getFolderSize(fileList
[i
]);
284 result
+= fileList
[i
].length();
293 * Mimetype String of a file
297 public static String
getMimeTypeFromName(String path
) {
298 String extension
= "";
299 int pos
= path
.lastIndexOf('.');
301 extension
= path
.substring(pos
+ 1);
303 String result
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension(extension
.toLowerCase());
304 return (result
!= null
) ? result
: "";
308 * Scans the default location for saving local copies of files searching for
309 * a 'lost' file with the same full name as the {@link OCFile} received as
312 * This method helps to keep linked local copies of the files when the app is uninstalled, and then
313 * reinstalled in the device. OR after the cache of the app was deleted in system settings.
315 * The method is assuming that all the local changes in the file where synchronized in the past. This is dangerous,
316 * but assuming the contrary could lead to massive unnecessary synchronizations of downloaded file after deleting
319 * This should be changed in the near future to avoid any chance of data loss, but we need to add some options
320 * to limit hard automatic synchronizations to wifi, unless the user wants otherwise.
322 * @param file File to associate a possible 'lost' local file.
323 * @param account Account holding file.
325 public static void searchForLocalFileInDefaultPath(OCFile file
, Account account
) {
326 if (file
.getStoragePath() == null
&& !file
.isFolder()) {
327 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
));
329 file
.setStoragePath(f
.getAbsolutePath());
330 file
.setLastSyncDateForData(f
.lastModified());