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
.io
.FileInputStream
;
25 import java
.io
.FileOutputStream
;
26 import java
.io
.IOException
;
27 import java
.io
.InputStream
;
28 import java
.io
.OutputStream
;
29 import java
.util
.Collections
;
30 import java
.util
.Comparator
;
31 import java
.util
.Vector
;
33 import third_parties
.daveKoeller
.AlphanumComparator
;
35 import com
.owncloud
.android
.MainApp
;
36 import com
.owncloud
.android
.R
;
37 import com
.owncloud
.android
.datamodel
.OCFile
;
38 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
40 import android
.accounts
.Account
;
41 import android
.annotation
.SuppressLint
;
42 import android
.content
.Context
;
43 import android
.content
.SharedPreferences
;
44 import android
.preference
.PreferenceManager
;
45 import android
.net
.Uri
;
46 import android
.os
.StatFs
;
47 import android
.webkit
.MimeTypeMap
;
51 * Static methods to help in access to local file system.
53 public class FileStorageUtils
{
54 public static final Integer SORT_NAME
= 0;
55 public static final Integer SORT_DATE
= 1;
56 public static final Integer SORT_SIZE
= 2;
57 public static Integer mSortOrder
= SORT_NAME
;
58 public static Boolean mSortAscending
= true
;
61 public static final String
getSavePath(String accountName
) {
62 // File sdCard = Environment.getExternalStorageDirectory();
64 return MainApp
.getStoragePath() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ Uri
.encode(accountName
, "@");
65 // 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
68 public static final String
getDefaultSavePathFor(String accountName
, OCFile file
) {
69 return getSavePath(accountName
) + file
.getRemotePath();
72 public static final String
getTemporalPath(String accountName
) {
73 return MainApp
.getStoragePath() + File
.separator
+ MainApp
.getDataFolder() + File
.separator
+ "tmp" + File
.separator
+ 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
= new File(MainApp
.getStoragePath());
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 MainApp
.getStoragePath() + 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 remote.
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
> sortFolder(Vector
<OCFile
> files
){
164 files
= FileStorageUtils
.sortByName(files
);
167 files
= FileStorageUtils
.sortByDate(files
);
170 // mFiles = FileStorageUtils.sortBySize(mSortAscending);
181 public static Vector
<OCFile
> sortByDate(Vector
<OCFile
> files
){
189 Collections
.sort(files
, new Comparator
<OCFile
>() {
190 public int compare(OCFile o1
, OCFile o2
) {
191 if (o1
.isFolder() && o2
.isFolder()) {
192 Long obj1
= o1
.getModificationTimestamp();
193 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
195 else if (o1
.isFolder()) {
197 } else if (o2
.isFolder()) {
199 } else if (o1
.getModificationTimestamp() == 0 || o2
.getModificationTimestamp() == 0){
202 Long obj1
= o1
.getModificationTimestamp();
203 return val
* obj1
.compareTo(o2
.getModificationTimestamp());
212 // * Sorts list by Size
213 // * @param sortAscending true: ascending, false: descending
215 // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
216 // final Integer val;
217 // if (mSortAscending){
223 // Collections.sort(files, new Comparator<OCFile>() {
224 // public int compare(OCFile o1, OCFile o2) {
225 // if (o1.isFolder() && o2.isFolder()) {
226 // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
227 // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
229 // else if (o1.isFolder()) {
231 // } else if (o2.isFolder()) {
233 // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
236 // Long obj1 = o1.getFileLength();
237 // return val * obj1.compareTo(o2.getFileLength());
247 * @param files files to sort
249 public static Vector
<OCFile
> sortByName(Vector
<OCFile
> files
){
257 Collections
.sort(files
, new Comparator
<OCFile
>() {
258 public int compare(OCFile o1
, OCFile o2
) {
259 if (o1
.isFolder() && o2
.isFolder()) {
260 return val
* new AlphanumComparator().compare(o1
, o2
);
261 } else if (o1
.isFolder()) {
263 } else if (o2
.isFolder()) {
266 return val
* new AlphanumComparator().compare(o1
, o2
);
276 * @return Size in bytes
278 public static long getFolderSize(File dir
) {
281 for (File f
: dir
.listFiles()) {
283 result
+= getFolderSize(f
);
285 result
+= f
.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());
335 public static boolean copyFile(File src
, File target
) {
338 InputStream
in = null
;
339 OutputStream out
= null
;
342 in = new FileInputStream(src
);
343 out
= new FileOutputStream(target
);
344 byte[] buf
= new byte[1024];
346 while ((len
= in.read(buf
)) > 0) {
347 out
.write(buf
, 0, len
);
349 } catch (IOException ex
) {
352 if (in != null
) try {
354 } catch (IOException e
) {
355 e
.printStackTrace(System
.err
);
357 if (out
!= null
) try {
359 } catch (IOException e
) {
360 e
.printStackTrace(System
.err
);