Copyright note fixes
[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 android.annotation.SuppressLint;
23 import android.net.Uri;
24 import android.os.Environment;
25 import android.os.StatFs;
26 import android.util.Log;
27
28 import com.owncloud.android.datamodel.OCFile;
29 import com.owncloud.android.files.services.InstantUploadService;
30
31 /**
32 * Static methods to help in access to local file system.
33 *
34 * @author David A. Velasco
35 */
36 public class FileStorageUtils {
37 private static final String LOG_TAG = "FileStorageUtils";
38
39 public static final String getSavePath(String accountName) {
40 File sdCard = Environment.getExternalStorageDirectory();
41 return sdCard.getAbsolutePath() + "/owncloud/" + Uri.encode(accountName, "@");
42 // 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
43 }
44
45 public static final String getDefaultSavePathFor(String accountName, OCFile file) {
46 return getSavePath(accountName) + file.getRemotePath();
47 }
48
49 public static final String getTemporalPath(String accountName) {
50 File sdCard = Environment.getExternalStorageDirectory();
51 return sdCard.getAbsolutePath() + "/owncloud/tmp/" + Uri.encode(accountName, "@");
52 // 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
53 }
54
55 @SuppressLint("NewApi")
56 public static final long getUsableSpace(String accountName) {
57 File savePath = Environment.getExternalStorageDirectory();
58 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
59 return savePath.getUsableSpace();
60
61 } else {
62 StatFs stats = new StatFs(savePath.getAbsolutePath());
63 return stats.getAvailableBlocks() * stats.getBlockSize();
64 }
65
66 }
67
68 public static final String getLogPath() {
69 return Environment.getExternalStorageDirectory() + File.separator + "owncloud" + File.separator + "log";
70 }
71
72 // to ensure we will not add the slash twice between filename and
73 // folder-name
74 private static String getFileName(String filepath) {
75 if (filepath != null && !"".equals(filepath)) {
76 int psi = filepath.lastIndexOf('/');
77 String filename = filepath;
78 if (psi > -1) {
79 filename = filepath.substring(psi + 1, filepath.length());
80 Log.d(LOG_TAG, "extracted filename :" + filename);
81 }
82 return filename;
83 } else {
84 // Toast
85 Log.w(LOG_TAG, "the given filename was null or empty");
86 return null;
87 }
88 }
89
90 public static String getInstantUploadFilePath(String fileName) {
91 return InstantUploadService.INSTANT_UPLOAD_DIR + "/" + getFileName(fileName);
92 }
93 }