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