Merge branch 'develop' into rename_with_special_character
[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 /**
86 * Gets the composed path when video is or must be stored
87 * @param context
88 * @param fileName: video file name
89 * @return String: video file path composed
90 */
91 public static String getInstantVideoUploadFilePath(Context context, String fileName) {
92 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
93 String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
94 String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
95 String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
96 return value;
97 }
98
99 public static String getParentPath(String remotePath) {
100 String parentPath = new File(remotePath).getParent();
101 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
102 return parentPath;
103 }
104
105 /**
106 * Creates and populates a new {@link OCFile} object with the data read from the server.
107 *
108 * @param remote remote file read from the server (remote file or folder).
109 * @return New OCFile instance representing the remote resource described by we.
110 */
111 public static OCFile fillOCFile(RemoteFile remote) {
112 OCFile file = new OCFile(remote.getRemotePath());
113 file.setCreationTimestamp(remote.getCreationTimestamp());
114 file.setFileLength(remote.getLength());
115 file.setMimetype(remote.getMimeType());
116 file.setModificationTimestamp(remote.getModifiedTimestamp());
117 file.setEtag(remote.getEtag());
118 file.setPermissions(remote.getPermissions());
119 file.setRemoteId(remote.getRemoteId());
120 return file;
121 }
122
123 /**
124 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
125 *
126 * @param oCFile OCFile
127 * @return New RemoteFile instance representing the resource described by ocFile.
128 */
129 public static RemoteFile fillRemoteFile(OCFile ocFile){
130 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
131 file.setCreationTimestamp(ocFile.getCreationTimestamp());
132 file.setLength(ocFile.getFileLength());
133 file.setMimeType(ocFile.getMimetype());
134 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
135 file.setEtag(ocFile.getEtag());
136 file.setPermissions(ocFile.getPermissions());
137 file.setRemoteId(ocFile.getRemoteId());
138 return file;
139 }
140
141 }