1243825f0f353a34c400a6df6b4d2ea957e0b594
[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.net.Uri;
31 import android.os.Environment;
32 import android.os.StatFs;
33
34
35 /**
36 * Static methods to help in access to local file system.
37 *
38 * @author David A. Velasco
39 */
40 public class FileStorageUtils {
41 //private static final String LOG_TAG = "FileStorageUtils";
42
43 public static final String getSavePath(String accountName) {
44 File sdCard = Environment.getExternalStorageDirectory();
45 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
46 // 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
47 }
48
49 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
50 return getSavePath(accountName) + file.getRemotePath();
51 }
52
53 public static final String getTemporalPath(String accountName) {
54 File sdCard = Environment.getExternalStorageDirectory();
55 return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
56 // 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
57 }
58
59 @SuppressLint("NewApi")
60 public static final long getUsableSpace(String accountName) {
61 File savePath = Environment.getExternalStorageDirectory();
62 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
63 return savePath.getUsableSpace();
64
65 } else {
66 StatFs stats = new StatFs(savePath.getAbsolutePath());
67 return stats.getAvailableBlocks() * stats.getBlockSize();
68 }
69
70 }
71
72 public static final String getLogPath() {
73 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
74 }
75
76 public static String getInstantUploadFilePath(Context context, String fileName) {
77 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
78 String uploadPathdef = context.getString(R.string.instant_upload_path);
79 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
80 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
81 return value;
82 }
83
84 public static String getParentPath(String remotePath) {
85 String parentPath = new File(remotePath).getParent();
86 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
87 return parentPath;
88 }
89
90 /**
91 * Creates and populates a new {@link OCFile} object with the data read from the server.
92 *
93 * @param remote remote file read from the server (remote file or folder).
94 * @return New OCFile instance representing the remote resource described by we.
95 */
96 public static OCFile fillOCFile(RemoteFile remote) {
97 OCFile file = new OCFile(remote.getRemotePath());
98 file.setCreationTimestamp(remote.getCreationTimestamp());
99 file.setFileLength(remote.getLength());
100 file.setMimetype(remote.getMimeType());
101 file.setModificationTimestamp(remote.getModifiedTimestamp());
102 file.setEtag(remote.getEtag());
103 file.setPermissions(remote.getPermissions());
104 file.setRemoteId(remote.getRemoteId());
105 return file;
106 }
107
108 /**
109 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
110 *
111 * @param oCFile OCFile
112 * @return New RemoteFile instance representing the resource described by ocFile.
113 */
114 public static RemoteFile fillRemoteFile(OCFile ocFile){
115 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
116 file.setCreationTimestamp(ocFile.getCreationTimestamp());
117 file.setLength(ocFile.getFileLength());
118 file.setMimeType(ocFile.getMimetype());
119 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
120 file.setEtag(ocFile.getEtag());
121 file.setPermissions(ocFile.getPermissions());
122 file.setRemoteId(ocFile.getRemoteId());
123 return file;
124 }
125
126 }