owncloud-android-external
[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 public static final String getSavePath(String accountName) {
57 //File sdCard = Environment.getExternalStorageDirectory();
58 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
59 // 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
60 return getBaseStorePath().getAbsolutePath() + "/" + Uri.encode(accountName, "@");
61 }
62
63 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
64 return getSavePath(accountName) + file.getRemotePath();
65 }
66
67 public static final String getTemporalPath(String accountName) {
68 //File sdCard = Environment.getExternalStorageDirectory();
69 //return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/tmp/" + Uri.encode(accountName, "@");
70 // 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
71 return getBaseStorePath().getAbsolutePath() + "/tmp/" + Uri.encode(accountName, "@");
72 }
73
74 @SuppressLint("NewApi")
75 public static final long getUsableSpace(String accountName) {
76 //File savePath = Environment.getExternalStorageDirectory();
77 File savePath = getBaseStorePath();
78 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
79 return savePath.getUsableSpace();
80
81 } else {
82 StatFs stats = new StatFs(savePath.getAbsolutePath());
83 return stats.getAvailableBlocks() * stats.getBlockSize();
84 }
85
86 }
87
88 public static final String getLogPath() {
89 return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
90 }
91
92 public static String getInstantUploadFilePath(Context context, String fileName) {
93 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
94 String uploadPathdef = context.getString(R.string.instant_upload_path);
95 String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
96 String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
97 return value;
98 }
99
100 /**
101 * Gets the composed path when video is or must be stored
102 * @param context
103 * @param fileName: video file name
104 * @return String: video file path composed
105 */
106 public static String getInstantVideoUploadFilePath(Context context, String fileName) {
107 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
108 String uploadVideoPathdef = context.getString(R.string.instant_upload_path);
109 String uploadVideoPath = pref.getString("instant_video_upload_path", uploadVideoPathdef);
110 String value = uploadVideoPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
111 return value;
112 }
113
114 public static String getParentPath(String remotePath) {
115 String parentPath = new File(remotePath).getParent();
116 parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
117 return parentPath;
118 }
119
120 /**
121 * Creates and populates a new {@link OCFile} object with the data read from the server.
122 *
123 * @param remote remote file read from the server (remote file or folder).
124 * @return New OCFile instance representing the remote resource described by we.
125 */
126 public static OCFile fillOCFile(RemoteFile remote) {
127 OCFile file = new OCFile(remote.getRemotePath());
128 file.setCreationTimestamp(remote.getCreationTimestamp());
129 file.setFileLength(remote.getLength());
130 file.setMimetype(remote.getMimeType());
131 file.setModificationTimestamp(remote.getModifiedTimestamp());
132 file.setEtag(remote.getEtag());
133 file.setPermissions(remote.getPermissions());
134 file.setRemoteId(remote.getRemoteId());
135 return file;
136 }
137
138 /**
139 * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
140 *
141 * @param oCFile OCFile
142 * @return New RemoteFile instance representing the resource described by ocFile.
143 */
144 public static RemoteFile fillRemoteFile(OCFile ocFile){
145 RemoteFile file = new RemoteFile(ocFile.getRemotePath());
146 file.setCreationTimestamp(ocFile.getCreationTimestamp());
147 file.setLength(ocFile.getFileLength());
148 file.setMimeType(ocFile.getMimetype());
149 file.setModifiedTimestamp(ocFile.getModificationTimestamp());
150 file.setEtag(ocFile.getEtag());
151 file.setPermissions(ocFile.getPermissions());
152 file.setRemoteId(ocFile.getRemoteId());
153 return file;
154 }
155
156 }