01176ac211011a67323a10f794a64162fde72f33
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / FileStorageUtils.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.utils;
19
20 import java.io.File;
21
22 import com.owncloud.android.MainApp;
23 import com.owncloud.android.R;
24 import com.owncloud.android.datamodel.OCFile;
25 import com.owncloud.android.lib.resources.files.RemoteFile;
26
27 import android.annotation.SuppressLint;
28 import android.content.Context;
29 import android.net.Uri;
30 import android.os.Environment;
31 import android.os.StatFs;
32
33
34 /**
35 * Static methods to help in access to local file system.
36 *
37 * @author David A. Velasco
38 */
39 public class FileStorageUtils {
40 //private static final String LOG_TAG = "FileStorageUtils";
41
42 public static final String getSavePath(String accountName) {
43 File sdCard = Environment.getExternalStorageDirectory();
44 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
45 // 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
46 }
47
48 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
49 return getSavePath(accountName) + file.getRemotePath();
50 }
51
52 public static final String getTemporalPath(String accountName) {
53 File sdCard = Environment.getExternalStorageDirectory();
54 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
55 // 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
56 }
57
58 @SuppressLint("NewApi")
59 public static final long getUsableSpace(String accountName) {
60 File savePath = Environment.getExternalStorageDirectory();
61 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
62 return savePath.getUsableSpace();
63
64 } else {
65 StatFs stats = new StatFs(savePath.getAbsolutePath());
66 return stats.getAvailableBlocks() * stats.getBlockSize();
67 }
68
69 }
70
71 public static final String getLogPath() {
72 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
73 }
74
75 public static String getInstantUploadFilePath(Context context, String fileName) {
76 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
77 String uploadPathdef = context.getString(R.string.instant_upload_path);
78 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
79 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
80 return value;
81 }
82
83 public static String getParentPath(String remotePath) {
84 String parentPath = new File(remotePath).getParent();
85 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
86 return parentPath;
87 }
88
89 /**
90 * Creates and populates a new {@link OCFile} object with the data read from the server.
91 *
92 * @param remote remote file read from the server (remote file or folder).
93 * @return New OCFile instance representing the remote resource described by we.
94 */
95 public static OCFile fillOCFile(RemoteFile remote) {
96 OCFile file = new OCFile(remote.getRemotePath());
97 file.setCreationTimestamp(remote.getCreationTimestamp());
98 file.setFileLength(remote.getLength());
99 file.setMimetype(remote.getMimeType());
100 file.setModificationTimestamp(remote.getModifiedTimestamp());
101 file.setEtag(remote.getEtag());
102 file.setPermissions(remote.getPermissions());
103 file.setRemoteId(remote.getRemoteId());
104 return file;
105 }
106
107 /**
108 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
109 *
110 * @param oCFile OCFile
111 * @return New RemoteFile instance representing the resource described by ocFile.
112 */
113 public static RemoteFile fillRemoteFile(OCFile ocFile){
114 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
115 file.setCreationTimestamp(ocFile.getCreationTimestamp());
116 file.setLength(ocFile.getFileLength());
117 file.setMimeType(ocFile.getMimetype());
118 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
119 file.setEtag(ocFile.getEtag());
120 file.setPermissions(ocFile.getPermissions());
121 file.setRemoteId(ocFile.getRemoteId());
122 return file;
123 }
124
125 }