설명 없음

DirectoryManager.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova.file;
  18. import android.os.Environment;
  19. import android.os.StatFs;
  20. import java.io.File;
  21. /**
  22. * This class provides file directory utilities.
  23. * All file operations are performed on the SD card.
  24. *
  25. * It is used by the FileUtils class.
  26. */
  27. public class DirectoryManager {
  28. @SuppressWarnings("unused")
  29. private static final String LOG_TAG = "DirectoryManager";
  30. /**
  31. * Determine if a file or directory exists.
  32. * @param name The name of the file to check.
  33. * @return T=exists, F=not found
  34. */
  35. public static boolean testFileExists(String name) {
  36. boolean status;
  37. // If SD card exists
  38. if ((testSaveLocationExists()) && (!name.equals(""))) {
  39. File path = Environment.getExternalStorageDirectory();
  40. File newPath = constructFilePaths(path.toString(), name);
  41. status = newPath.exists();
  42. }
  43. // If no SD card
  44. else {
  45. status = false;
  46. }
  47. return status;
  48. }
  49. /**
  50. * Get the free space in external storage
  51. *
  52. * @return Size in KB or -1 if not available
  53. */
  54. public static long getFreeExternalStorageSpace() {
  55. String status = Environment.getExternalStorageState();
  56. long freeSpaceInBytes = 0;
  57. // Check if external storage exists
  58. if (status.equals(Environment.MEDIA_MOUNTED)) {
  59. freeSpaceInBytes = getFreeSpaceInBytes(Environment.getExternalStorageDirectory().getPath());
  60. } else {
  61. // If no external storage then return -1
  62. return -1;
  63. }
  64. return freeSpaceInBytes / 1024;
  65. }
  66. /**
  67. * Given a path return the number of free bytes in the filesystem containing the path.
  68. *
  69. * @param path to the file system
  70. * @return free space in bytes
  71. */
  72. public static long getFreeSpaceInBytes(String path) {
  73. try {
  74. StatFs stat = new StatFs(path);
  75. long blockSize = stat.getBlockSize();
  76. long availableBlocks = stat.getAvailableBlocks();
  77. return availableBlocks * blockSize;
  78. } catch (IllegalArgumentException e) {
  79. // The path was invalid. Just return 0 free bytes.
  80. return 0;
  81. }
  82. }
  83. /**
  84. * Determine if SD card exists.
  85. *
  86. * @return T=exists, F=not found
  87. */
  88. public static boolean testSaveLocationExists() {
  89. String sDCardStatus = Environment.getExternalStorageState();
  90. boolean status;
  91. // If SD card is mounted
  92. if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
  93. status = true;
  94. }
  95. // If no SD card
  96. else {
  97. status = false;
  98. }
  99. return status;
  100. }
  101. /**
  102. * Create a new file object from two file paths.
  103. *
  104. * @param file1 Base file path
  105. * @param file2 Remaining file path
  106. * @return File object
  107. */
  108. private static File constructFilePaths (String file1, String file2) {
  109. File newPath;
  110. if (file2.startsWith(file1)) {
  111. newPath = new File(file2);
  112. }
  113. else {
  114. newPath = new File(file1 + "/" + file2);
  115. }
  116. return newPath;
  117. }
  118. }