ExternalFileUtils.java 8.3 KB

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