FileStorageUtils.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. package com.owncloud.android.utils;
  18. import java.io.File;
  19. import java.util.Collections;
  20. import java.util.Comparator;
  21. import java.util.Vector;
  22. import third_parties.daveKoeller.AlphanumComparator;
  23. import com.owncloud.android.MainApp;
  24. import com.owncloud.android.R;
  25. import com.owncloud.android.datamodel.OCFile;
  26. import com.owncloud.android.lib.resources.files.RemoteFile;
  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. * Static methods to help in access to local file system.
  36. *
  37. * @author David A. Velasco
  38. */
  39. public class FileStorageUtils {
  40. public static Integer mSortOrder;
  41. public static Boolean mSortAscending;
  42. public static final Integer SORT_NAME = 0;
  43. public static final Integer SORT_DATE = 1;
  44. public static final Integer SORT_SIZE = 2;
  45. //private static final String LOG_TAG = "FileStorageUtils";
  46. public static final String getSavePath(String accountName) {
  47. File sdCard = Environment.getExternalStorageDirectory();
  48. return sdCard.getAbsolutePath() + "/" + MainApp.getDataFolder() + "/" + Uri.encode(accountName, "@");
  49. // 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
  50. }
  51. public static final String getDefaultSavePathFor(String accountName, OCFile file) {
  52. return getSavePath(accountName) + file.getRemotePath();
  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. @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. } else {
  65. StatFs stats = new StatFs(savePath.getAbsolutePath());
  66. return stats.getAvailableBlocks() * stats.getBlockSize();
  67. }
  68. }
  69. public static final String getLogPath() {
  70. return Environment.getExternalStorageDirectory() + File.separator + MainApp.getDataFolder() + File.separator + "log";
  71. }
  72. public static String getInstantUploadFilePath(Context context, String fileName) {
  73. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  74. String uploadPathdef = context.getString(R.string.instant_upload_path);
  75. String uploadPath = pref.getString("instant_upload_path", uploadPathdef);
  76. String value = uploadPath + OCFile.PATH_SEPARATOR + (fileName == null ? "" : fileName);
  77. return value;
  78. }
  79. public static String getParentPath(String remotePath) {
  80. String parentPath = new File(remotePath).getParent();
  81. parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
  82. return parentPath;
  83. }
  84. /**
  85. * Creates and populates a new {@link OCFile} object with the data read from the server.
  86. *
  87. * @param remote remote file read from the server (remote file or folder).
  88. * @return New OCFile instance representing the remote resource described by we.
  89. */
  90. public static OCFile fillOCFile(RemoteFile remote) {
  91. OCFile file = new OCFile(remote.getRemotePath());
  92. file.setCreationTimestamp(remote.getCreationTimestamp());
  93. file.setFileLength(remote.getLength());
  94. file.setMimetype(remote.getMimeType());
  95. file.setModificationTimestamp(remote.getModifiedTimestamp());
  96. file.setEtag(remote.getEtag());
  97. file.setPermissions(remote.getPermissions());
  98. file.setRemoteId(remote.getRemoteId());
  99. return file;
  100. }
  101. /**
  102. * Creates and populates a new {@link RemoteFile} object with the data read from an {@link OCFile}.
  103. *
  104. * @param oCFile OCFile
  105. * @return New RemoteFile instance representing the resource described by ocFile.
  106. */
  107. public static RemoteFile fillRemoteFile(OCFile ocFile){
  108. RemoteFile file = new RemoteFile(ocFile.getRemotePath());
  109. file.setCreationTimestamp(ocFile.getCreationTimestamp());
  110. file.setLength(ocFile.getFileLength());
  111. file.setMimeType(ocFile.getMimetype());
  112. file.setModifiedTimestamp(ocFile.getModificationTimestamp());
  113. file.setEtag(ocFile.getEtag());
  114. file.setPermissions(ocFile.getPermissions());
  115. file.setRemoteId(ocFile.getRemoteId());
  116. return file;
  117. }
  118. /**
  119. * Sorts all filenames, regarding last user decision
  120. */
  121. public static Vector<OCFile> sortDirectory(Vector<OCFile> files){
  122. switch (mSortOrder){
  123. case 0:
  124. files = FileStorageUtils.sortByName(files);
  125. break;
  126. case 1:
  127. files = FileStorageUtils.sortByDate(files);
  128. break;
  129. case 2:
  130. // mFiles = FileStorageUtils.sortBySize(mSortAscending);
  131. break;
  132. }
  133. return files;
  134. }
  135. /**
  136. * Sorts list by Date
  137. * @param sortAscending true: ascending, false: descending
  138. */
  139. public static Vector<OCFile> sortByDate(Vector<OCFile> files){
  140. final Integer val;
  141. if (mSortAscending){
  142. val = 1;
  143. } else {
  144. val = -1;
  145. }
  146. Collections.sort(files, new Comparator<OCFile>() {
  147. public int compare(OCFile o1, OCFile o2) {
  148. if (o1.isFolder() && o2.isFolder()) {
  149. Long obj1 = o1.getModificationTimestamp();
  150. return val * obj1.compareTo(o2.getModificationTimestamp());
  151. }
  152. else if (o1.isFolder()) {
  153. return -1;
  154. } else if (o2.isFolder()) {
  155. return 1;
  156. } else if (o1.getModificationTimestamp() == 0 || o2.getModificationTimestamp() == 0){
  157. return 0;
  158. } else {
  159. Long obj1 = o1.getModificationTimestamp();
  160. return val * obj1.compareTo(o2.getModificationTimestamp());
  161. }
  162. }
  163. });
  164. return files;
  165. }
  166. // /**
  167. // * Sorts list by Size
  168. // * @param sortAscending true: ascending, false: descending
  169. // */
  170. // public static Vector<OCFile> sortBySize(Vector<OCFile> files){
  171. // final Integer val;
  172. // if (mSortAscending){
  173. // val = 1;
  174. // } else {
  175. // val = -1;
  176. // }
  177. //
  178. // Collections.sort(files, new Comparator<OCFile>() {
  179. // public int compare(OCFile o1, OCFile o2) {
  180. // if (o1.isFolder() && o2.isFolder()) {
  181. // Long obj1 = getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o1)));
  182. // return val * obj1.compareTo(getFolderSize(new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, o2))));
  183. // }
  184. // else if (o1.isFolder()) {
  185. // return -1;
  186. // } else if (o2.isFolder()) {
  187. // return 1;
  188. // } else if (o1.getFileLength() == 0 || o2.getFileLength() == 0){
  189. // return 0;
  190. // } else {
  191. // Long obj1 = o1.getFileLength();
  192. // return val * obj1.compareTo(o2.getFileLength());
  193. // }
  194. // }
  195. // });
  196. //
  197. // return files;
  198. // }
  199. /**
  200. * Sorts list by Name
  201. * @param sortAscending true: ascending, false: descending
  202. */
  203. public static Vector<OCFile> sortByName(Vector<OCFile> files){
  204. final Integer val;
  205. if (mSortAscending){
  206. val = 1;
  207. } else {
  208. val = -1;
  209. }
  210. Collections.sort(files, new Comparator<OCFile>() {
  211. public int compare(OCFile o1, OCFile o2) {
  212. if (o1.isFolder() && o2.isFolder()) {
  213. return val * o1.getRemotePath().toLowerCase().compareTo(o2.getRemotePath().toLowerCase());
  214. } else if (o1.isFolder()) {
  215. return -1;
  216. } else if (o2.isFolder()) {
  217. return 1;
  218. }
  219. return val * new AlphanumComparator().compare(o1, o2);
  220. }
  221. });
  222. return files;
  223. }
  224. /**
  225. * Local Folder size
  226. * @param dir File
  227. * @return Size in bytes
  228. */
  229. public static long getFolderSize(File dir) {
  230. if (dir.exists()) {
  231. long result = 0;
  232. File[] fileList = dir.listFiles();
  233. for(int i = 0; i < fileList.length; i++) {
  234. if(fileList[i].isDirectory()) {
  235. result += getFolderSize(fileList[i]);
  236. } else {
  237. result += fileList[i].length();
  238. }
  239. }
  240. return result;
  241. }
  242. return 0;
  243. }
  244. }