123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- package epson.common;
- import android.content.Context;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.annotation.VisibleForTesting;
- import java.io.File;
- import java.util.Locale;
- import epson.print.Util.EPLog;
- public class ExternalFileUtils {
- private static String AREA_INFO = "area_info.dat";
- private static String EPSONIPRINT_DOWNLOAD_FOLDER = "download/";
- private static String INFO_FOLDER_IPPRINTER = "ipprinters/";
- private static String INFO_FOLDER_REMOTEPRINTER = "printers/";
- private static String PREVIEW_FOLDER = "tempPDF";
- private static String PRE_AREA_INFO = "pre_area_info.dat";
- private static String PRE_SUPPORTED_MEDIA = "pre_supported_media.dat";
- private static String PRINT_FOLDER = "print";
- private static String PRINT_TEMP_FOLDER = "temp";
- private static String SAVE_AREA_INFO = "save_area_info.dat";
- private static String SAVE_SUPPORTED_MEDIA = "save_supported_media.dat";
- private static String SCAN_SCANNED_IMAGE_PATH = "tempScan/";
- private static final String SHARED = "shared";
- private static String SUPPORTED_MEDIA = "supported_media.dat";
- private static final String TAG = "ExternalFileUtils";
- private static String TEMP_CR_FOLDER = "tempCR";
- private static String TEMP_VIEW_FOLDER = "tempView";
- private static ExternalFileUtils externalFileUtils;
- private Context mContext;
- public enum TempCacheDirectory {
- MEMORY_CARD_ACCESS("tempMemcard"),
- PHOTO_FILE_CONVERT("tempPhotoFConv"),
- GOOGLE_DRIVE_CONVERT("tempGConvert");
- String relativeDirectory;
- private TempCacheDirectory(String str) {
- relativeDirectory = str;
- }
- public String getRelativeDirectory() {
- return relativeDirectory;
- }
- }
- private ExternalFileUtils(Context context) {
- mContext = context;
- }
- public static ExternalFileUtils getInstance(Context context) {
- ExternalFileUtils externalFileUtils2 = externalFileUtils;
- if (externalFileUtils2 != null) {
- return externalFileUtils2;
- }
- return new ExternalFileUtils(context);
- }
- public String getFilesDir() {
- File externalFilesDir = mContext.getExternalFilesDir((String) null);
- if (externalFilesDir != null) {
- return externalFilesDir.getPath();
- }
- return mContext.getFilesDir().getPath();
- }
- public String getCacheDir() {
- File externalCacheDir = mContext.getExternalCacheDir();
- if (externalCacheDir != null) {
- return externalCacheDir.getPath();
- }
- return mContext.getCacheDir().getPath();
- }
- public boolean createTempFolder(String str) {
- File file = new File(str);
- if (file.exists()) {
- return false;
- }
- EPLog.i(TAG, "createTempFolder = " + str);
- return file.mkdirs();
- }
- /**
- * 清除临时文件夹
- *
- * @param dirPath 文件夹路径
- */
- public void clearTempFoler(String dirPath) {
- File file = new File(dirPath);
- File[] listFiles = file.listFiles();
- if (listFiles != null) {
- for (int i = 0; i < listFiles.length; i++) {
- if (listFiles[i].isFile()) {
- EPLog.i(TAG, "clearTempFoler delete() = " + listFiles[i].getPath());
- listFiles[i].delete();
- } else if (listFiles[i].isDirectory()) {
- clearTempFoler(listFiles[i].toString());
- }
- }
- }
- if (file.exists() && file.isFile()) {
- EPLog.i(TAG, "clearTempFoler delete() = " + file.getPath());
- file.delete();
- }
- }
- public String getSupportedMediaDir() {
- return getFilesDir();
- }
- public File getSupportedMedia() {
- return new File(getSupportedMediaDir(), SUPPORTED_MEDIA);
- }
- public File getSavedSupportedMedia() {
- return new File(getSupportedMediaDir(), SAVE_SUPPORTED_MEDIA);
- }
- public File getPreSupportedMedia() {
- return new File(getSupportedMediaDir(), PRE_SUPPORTED_MEDIA);
- }
- public File getAreaInfo() {
- return new File(getSupportedMediaDir(), AREA_INFO);
- }
- public File getSavedAreaInfo() {
- return new File(getSupportedMediaDir(), SAVE_AREA_INFO);
- }
- public File getPreAreaInfo() {
- return new File(getSupportedMediaDir(), PRE_AREA_INFO);
- }
- public void removeAreaInfo() {
- File areaInfo = getAreaInfo();
- if (areaInfo.exists()) {
- areaInfo.delete();
- }
- File savedAreaInfo = getSavedAreaInfo();
- if (savedAreaInfo.exists()) {
- savedAreaInfo.delete();
- }
- }
- public String getWorkingDir() {
- return new File(getFilesDir()).getParent();
- }
- /**
- * 远程打印机保存路径
- *
- * @return 文件夹路径
- */
- public String getRemotePrintersInfo() {
- return new File(getWorkingDir(), INFO_FOLDER_REMOTEPRINTER).getPath();
- }
- public String getIpPrintersInfo() {
- return new File(getWorkingDir(), INFO_FOLDER_IPPRINTER).getPath();
- }
- /**
- * 清除远程打印机信息,缓存
- */
- public void clearRemotePrintersInfo() {
- clearTempFoler(getRemotePrintersInfo());
- }
- public void clearIpPrintersInfo() {
- clearTempFoler(getIpPrintersInfo());
- }
- public String getDownloadDir() {
- return new File(getCacheDir(), EPSONIPRINT_DOWNLOAD_FOLDER).getPath();
- }
- public void initDownloadDir() {
- createTempFolder(getDownloadDir());
- clearTempFoler(getDownloadDir());
- }
- public String getPrintDir() {
- return new File(getCacheDir(), PRINT_FOLDER).getPath();
- }
- public String getPrintTmpDir() {
- return new File(getPrintDir(), PRINT_TEMP_FOLDER).getPath();
- }
- public void initPrintDir() {
- createTempFolder(getPrintDir());
- createTempFolder(getPrintTmpDir());
- clearTempFoler(getPrintDir());
- }
- public String getTempViewDir() {
- return new File(getCacheDir(), TEMP_VIEW_FOLDER).getPath();
- }
- public void initTempViewDir() {
- createTempFolder(getTempViewDir());
- clearTempFoler(getTempViewDir());
- }
- public String getTempCRDir() {
- return new File(getCacheDir(), TEMP_CR_FOLDER).getPath();
- }
- public void initTempCRDir() {
- createTempFolder(getTempCRDir());
- clearTempFoler(getTempCRDir());
- }
- public String getPdfDir() {
- return new File(getCacheDir(), PREVIEW_FOLDER).getPath();
- }
- public void initPdfDir() {
- createTempFolder(getPdfDir());
- clearTempFoler(getPdfDir());
- }
- public String getScannedImageDir() {
- return new File(getCacheDir(), SCAN_SCANNED_IMAGE_PATH).getPath();
- }
- public void initScannedImageDir() {
- createTempFolder(getScannedImageDir());
- clearTempFoler(getScannedImageDir());
- }
- public String getTempSharedDir() {
- return new File(getCacheDir(), SHARED).getPath();
- }
- public void initTempSharedDir() {
- createTempFolder(getTempSharedDir());
- clearTempFoler(getTempSharedDir());
- }
- @NonNull
- public File getTempCacheDir(@NonNull TempCacheDirectory tempCacheDirectory) {
- return new File(getCacheDir(), tempCacheDirectory.getRelativeDirectory());
- }
- @NonNull
- public File initTempCacheDirectory(@NonNull TempCacheDirectory tempCacheDirectory) {
- File tempCacheDir = getTempCacheDir(tempCacheDirectory);
- String path = tempCacheDir.getPath();
- createTempFolder(path);
- clearTempFoler(path);
- return tempCacheDir;
- }
- @Nullable
- public static String getNotDuplicateFilename(@NonNull String str, String str2, int i) {
- if (str2 == null) {
- return null;
- }
- int i2 = 0;
- do {
- String convertFilename = getConvertFilename(str, i2, str2);
- if (!new File(convertFilename).exists()) {
- return convertFilename;
- }
- i2++;
- } while (i2 < i);
- return null;
- }
- @VisibleForTesting
- public static String getConvertFilename(@NonNull String str, int i, @NonNull String str2) {
- return new File(str2, String.format(Locale.US, "%s_%02x.jpg", new Object[]{new File(str).getName(), Integer.valueOf(i)})).getPath();
- }
- }
|