Next-Improvement
[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.content.SharedPreferences;
30 import android.preference.PreferenceManager;
31 import android.net.Uri;
32 import android.os.Environment;
33 import android.os.StatFs;
34
35
36 /**
37 * Static methods to help in access to local file system.
38 *
39 * @author David A. Velasco
40 */
41 public class FileStorageUtils {
42 //private static final String TAG = FileStorageUtils.class.getSimpleName();
43
44 @SuppressLint("NewApi")
45 private static final File getBaseStorePath() {
46 File baseStoragePath = Environment.getExternalStorageDirectory();
47 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
48 File[] dirs = MainApp.getAppContext().getExternalFilesDirs(null);
49 if (dirs.length > 1) {
50 baseStoragePath = dirs[1];
51 }
52 }
53 return baseStoragePath;
54 }
55
56 @SuppressLint("NewApi")
57 private static final String getBaseStorePathString() {
58 File baseStoragePath = Environment.getExternalStorageDirectory();
59 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
60 File[] dirs = MainApp.getAppContext().getExternalFilesDirs(null);
61 if (dirs.length > 1) {
62 baseStoragePath = dirs[1];
63 }
64 return baseStoragePath.getAbsolutePath();
65 } else {
66 return baseStoragePath.getAbsolutePath() + "/" + MainApp.getDataFolder();
67 }
68 }
69
70
71 public static final String getSavePath(String accountName) {
72 //File sdCard = Environment.getExternalStorageDirectory();
73 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + 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
75 //return getBaseStorePath().getAbsolutePath() + "/" + Uri.encode(accountName, "@");
76 return getBaseStorePathString() + "/" + Uri.encode(accountName, "@");
77 }
78
79 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
80 return getSavePath(accountName) + file.getRemotePath();
81 }
82
83 public static final String getTemporalPath(String accountName) {
84 //File sdCard = Environment.getExternalStorageDirectory();
85 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
86 // 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
87 //return getBaseStorePath().getAbsolutePath() + "/tmp/" + Uri.encode(accountName, "@");
88 return getBaseStorePathString() + "/tmp/" + Uri.encode(accountName, "@");
89 }
90
91 @SuppressLint("NewApi")
92 public static final long getUsableSpace(String accountName) {
93 //File savePath = Environment.getExternalStorageDirectory();
94 File savePath = getBaseStorePath();
95 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
96 return savePath.getUsableSpace();
97
98 } else {
99 StatFs stats = new StatFs(savePath.getAbsolutePath());
100 return stats.getAvailableBlocks() * stats.getBlockSize();
101 }
102
103 }
104
105 public static final String getLogPath() {
106 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
107 }
108
109 public static String getInstantUploadFilePath(Context context, String fileName) {
110 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
111 String uploadPathdef = context.getString(R.string.instant_upload_path);
112 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
113 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
114 return value;
115 }
116
117 /**
118 * Gets the composed path when video is or must be stored
119 * @param context
120 * @param fileName: video file name
121 * @return String: video file path composed
122 */
123 public static String getInstantVideoUploadFilePath(Context context, String fileName) {
124 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
125 String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
126 String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
127 String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
128 return value;
129 }
130
131 public static String getParentPath(String remotePath) {
132 String parentPath = new File(remotePath).getParent();
133 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
134 return parentPath;
135 }
136
137 /**
138 * Creates and populates a new {@link OCFile} object with the data read from the server.
139 *
140 * @param remote remote file read from the server (remote file or folder).
141 * @return New OCFile instance representing the remote resource described by we.
142 */
143 public static OCFile fillOCFile(RemoteFile remote) {
144 OCFile file = new OCFile(remote.getRemotePath());
145 file.setCreationTimestamp(remote.getCreationTimestamp());
146 file.setFileLength(remote.getLength());
147 file.setMimetype(remote.getMimeType());
148 file.setModificationTimestamp(remote.getModifiedTimestamp());
149 file.setEtag(remote.getEtag());
150 file.setPermissions(remote.getPermissions());
151 file.setRemoteId(remote.getRemoteId());
152 return file;
153 }
154
155 /**
156 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
157 *
158 * @param oCFile OCFile
159 * @return New RemoteFile instance representing the resource described by ocFile.
160 */
161 public static RemoteFile fillRemoteFile(OCFile ocFile){
162 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
163 file.setCreationTimestamp(ocFile.getCreationTimestamp());
164 file.setLength(ocFile.getFileLength());
165 file.setMimeType(ocFile.getMimetype());
166 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
167 file.setEtag(ocFile.getEtag());
168 file.setPermissions(ocFile.getPermissions());
169 file.setRemoteId(ocFile.getRemoteId());
170 return file;
171 }
172
173 }