ExternalFileUtils.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package epson.common;
  2. import android.content.Context;
  3. import android.support.annotation.VisibleForTesting;
  4. import epson.print.Util.EPLog;
  5. import java.io.File;
  6. import java.util.Locale;
  7. public class ExternalFileUtils {
  8. private static String AREA_INFO = "area_info.dat";
  9. private static String EPSONIPRINT_DOWNLOAD_FOLDER = "download/";
  10. private static String INFO_FOLDER_IPPRINTER = "ipprinters/";
  11. private static String INFO_FOLDER_REMOTEPRINTER = "printers/";
  12. private static String PREVIEW_FOLDER = "tempPDF";
  13. private static String PRE_AREA_INFO = "pre_area_info.dat";
  14. private static String PRE_SUPPORTED_MEDIA = "pre_supported_media.dat";
  15. private static String PRINT_FOLDER = "print";
  16. private static String PRINT_TEMP_FOLDER = "temp";
  17. private static String SAVE_AREA_INFO = "save_area_info.dat";
  18. private static String SAVE_SUPPORTED_MEDIA = "save_supported_media.dat";
  19. private static String SCAN_SCANNED_IMAGE_PATH = "tempScan/";
  20. private static final String SHARED = "shared";
  21. private static String SUPPORTED_MEDIA = "supported_media.dat";
  22. private static final String TAG = "ExternalFileUtils";
  23. private static String TEMP_CR_FOLDER = "tempCR";
  24. private static String TEMP_VIEW_FOLDER = "tempView";
  25. private static ExternalFileUtils externalFileUtils;
  26. private Context mContext;
  27. public enum TempCacheDirectory {
  28. MEMORY_CARD_ACCESS("tempMemcard"),
  29. PHOTO_FILE_CONVERT("tempPhotoFConv"),
  30. GOOGLE_DRIVE_CONVERT("tempGConvert");
  31. String relativeDirectory;
  32. private TempCacheDirectory(String str) {
  33. relativeDirectory = str;
  34. }
  35. public String getRelativeDirectory() {
  36. return relativeDirectory;
  37. }
  38. }
  39. private ExternalFileUtils(Context context) {
  40. mContext = context;
  41. }
  42. public static ExternalFileUtils getInstance(Context context) {
  43. ExternalFileUtils externalFileUtils2 = externalFileUtils;
  44. if (externalFileUtils2 != null) {
  45. return externalFileUtils2;
  46. }
  47. return new ExternalFileUtils(context);
  48. }
  49. public String getFilesDir() {
  50. File externalFilesDir = mContext.getExternalFilesDir((String) null);
  51. if (externalFilesDir != null) {
  52. return externalFilesDir.getPath();
  53. }
  54. return mContext.getFilesDir().getPath();
  55. }
  56. public String getCacheDir() {
  57. File externalCacheDir = mContext.getExternalCacheDir();
  58. if (externalCacheDir != null) {
  59. return externalCacheDir.getPath();
  60. }
  61. return mContext.getCacheDir().getPath();
  62. }
  63. public boolean createTempFolder(String str) {
  64. File file = new File(str);
  65. if (file.exists()) {
  66. return false;
  67. }
  68. EPLog.m313i(TAG, "createTempFolder = " + str);
  69. return file.mkdirs();
  70. }
  71. public void clearTempFoler(String str) {
  72. File file = new File(str);
  73. File[] listFiles = file.listFiles();
  74. if (listFiles != null) {
  75. for (int i = 0; i < listFiles.length; i++) {
  76. if (listFiles[i].isFile()) {
  77. EPLog.m313i(TAG, "clearTempFoler delete() = " + listFiles[i].getPath());
  78. listFiles[i].delete();
  79. } else if (listFiles[i].isDirectory()) {
  80. clearTempFoler(listFiles[i].toString());
  81. }
  82. }
  83. }
  84. if (file.exists() && file.isFile()) {
  85. EPLog.m313i(TAG, "clearTempFoler delete() = " + file.getPath());
  86. file.delete();
  87. }
  88. }
  89. public String getSupportedMediaDir() {
  90. return getFilesDir();
  91. }
  92. public File getSupportedMedia() {
  93. return new File(getSupportedMediaDir(), SUPPORTED_MEDIA);
  94. }
  95. public File getSavedSupportedMedia() {
  96. return new File(getSupportedMediaDir(), SAVE_SUPPORTED_MEDIA);
  97. }
  98. public File getPreSupportedMedia() {
  99. return new File(getSupportedMediaDir(), PRE_SUPPORTED_MEDIA);
  100. }
  101. public File getAreaInfo() {
  102. return new File(getSupportedMediaDir(), AREA_INFO);
  103. }
  104. public File getSavedAreaInfo() {
  105. return new File(getSupportedMediaDir(), SAVE_AREA_INFO);
  106. }
  107. public File getPreAreaInfo() {
  108. return new File(getSupportedMediaDir(), PRE_AREA_INFO);
  109. }
  110. public void removeAreaInfo() {
  111. File areaInfo = getAreaInfo();
  112. if (areaInfo.exists()) {
  113. areaInfo.delete();
  114. }
  115. File savedAreaInfo = getSavedAreaInfo();
  116. if (savedAreaInfo.exists()) {
  117. savedAreaInfo.delete();
  118. }
  119. }
  120. public String getWorkingDir() {
  121. return new File(getFilesDir()).getParent();
  122. }
  123. public String getRemotePrintersInfo() {
  124. return new File(getWorkingDir(), INFO_FOLDER_REMOTEPRINTER).getPath();
  125. }
  126. public String getIpPrintersInfo() {
  127. return new File(getWorkingDir(), INFO_FOLDER_IPPRINTER).getPath();
  128. }
  129. public void clearRemotePrintersInfo() {
  130. clearTempFoler(getRemotePrintersInfo());
  131. }
  132. public void clearIpPrintersInfo() {
  133. clearTempFoler(getIpPrintersInfo());
  134. }
  135. public String getDownloadDir() {
  136. return new File(getCacheDir(), EPSONIPRINT_DOWNLOAD_FOLDER).getPath();
  137. }
  138. public void initDownloadDir() {
  139. createTempFolder(getDownloadDir());
  140. clearTempFoler(getDownloadDir());
  141. }
  142. public String getPrintDir() {
  143. return new File(getCacheDir(), PRINT_FOLDER).getPath();
  144. }
  145. public String getPrintTmpDir() {
  146. return new File(getPrintDir(), PRINT_TEMP_FOLDER).getPath();
  147. }
  148. public void initPrintDir() {
  149. createTempFolder(getPrintDir());
  150. createTempFolder(getPrintTmpDir());
  151. clearTempFoler(getPrintDir());
  152. }
  153. public String getTempViewDir() {
  154. return new File(getCacheDir(), TEMP_VIEW_FOLDER).getPath();
  155. }
  156. public void initTempViewDir() {
  157. createTempFolder(getTempViewDir());
  158. clearTempFoler(getTempViewDir());
  159. }
  160. public String getTempCRDir() {
  161. return new File(getCacheDir(), TEMP_CR_FOLDER).getPath();
  162. }
  163. public void initTempCRDir() {
  164. createTempFolder(getTempCRDir());
  165. clearTempFoler(getTempCRDir());
  166. }
  167. public String getPdfDir() {
  168. return new File(getCacheDir(), PREVIEW_FOLDER).getPath();
  169. }
  170. public void initPdfDir() {
  171. createTempFolder(getPdfDir());
  172. clearTempFoler(getPdfDir());
  173. }
  174. public String getScannedImageDir() {
  175. return new File(getCacheDir(), SCAN_SCANNED_IMAGE_PATH).getPath();
  176. }
  177. public void initScannedImageDir() {
  178. createTempFolder(getScannedImageDir());
  179. clearTempFoler(getScannedImageDir());
  180. }
  181. public String getTempSharedDir() {
  182. return new File(getCacheDir(), SHARED).getPath();
  183. }
  184. public void initTempSharedDir() {
  185. createTempFolder(getTempSharedDir());
  186. clearTempFoler(getTempSharedDir());
  187. }
  188. @NonNull
  189. public File getTempCacheDir(@NonNull TempCacheDirectory tempCacheDirectory) {
  190. return new File(getCacheDir(), tempCacheDirectory.getRelativeDirectory());
  191. }
  192. @NonNull
  193. public File initTempCacheDirectory(@NonNull TempCacheDirectory tempCacheDirectory) {
  194. File tempCacheDir = getTempCacheDir(tempCacheDirectory);
  195. String path = tempCacheDir.getPath();
  196. createTempFolder(path);
  197. clearTempFoler(path);
  198. return tempCacheDir;
  199. }
  200. @Nullable
  201. public static String getNotDuplicateFilename(@NonNull String str, String str2, int i) {
  202. if (str2 == null) {
  203. return null;
  204. }
  205. int i2 = 0;
  206. do {
  207. String convertFilename = getConvertFilename(str, i2, str2);
  208. if (!new File(convertFilename).exists()) {
  209. return convertFilename;
  210. }
  211. i2++;
  212. } while (i2 < i);
  213. return null;
  214. }
  215. @VisibleForTesting
  216. public static String getConvertFilename(@NonNull String str, int i, @NonNull String str2) {
  217. return new File(str2, String.format(Locale.US, "%s_%02x.jpg", new Object[]{new File(str).getName(), Integer.valueOf(i)})).getPath();
  218. }
  219. }