DownloadTask.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.epson.iprint.storage.gdrivev3;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.os.AsyncTask;
  5. import android.support.annotation.NonNull;
  6. import android.support.annotation.Nullable;
  7. import epson.common.ExternalFileUtils;
  8. import epson.common.Utils;
  9. import epson.print.CommonDefine;
  10. import java.io.File;
  11. import java.io.IOException;
  12. public class DownloadTask extends AsyncTask<OnlineFile, Void, OnlineFile> implements Canceller {
  13. private static final String DOWNLOAD_TMP_FILE_NAME_BODY = "google_v3";
  14. @SuppressLint({"StaticFieldLeak"})
  15. private final Context mApplicationContext;
  16. private volatile boolean mDownloadCancelled = false;
  17. private final DownloadCompleteListener mDownloadCompleteListener;
  18. private volatile File mDownloadFile;
  19. private final GoogleDownloader mMyDrive;
  20. private Exception mThreadException;
  21. public interface DownloadCompleteListener {
  22. void onDownloadComplete(boolean z, @Nullable String str);
  23. }
  24. DownloadTask(@NonNull Context context, @NonNull GoogleDownloader googleDownloader, @NonNull DownloadCompleteListener downloadCompleteListener) {
  25. this.mApplicationContext = context;
  26. this.mMyDrive = googleDownloader;
  27. this.mDownloadCompleteListener = downloadCompleteListener;
  28. this.mDownloadFile = null;
  29. }
  30. /* access modifiers changed from: protected */
  31. public OnlineFile doInBackground(OnlineFile... onlineFileArr) {
  32. GoogleDriveFile googleDriveFile;
  33. if (onlineFileArr == null || (googleDriveFile = onlineFileArr[0]) == null) {
  34. return null;
  35. }
  36. try {
  37. if (!(googleDriveFile instanceof GoogleDriveFile) || !googleDriveFile.isGoogleDocuments()) {
  38. File outFile = getOutFile(this.mApplicationContext, getExtensionFromMimeType((OnlineFile) googleDriveFile));
  39. this.mDownloadFile = outFile;
  40. if (outFile == null) {
  41. return null;
  42. }
  43. return this.mMyDrive.downloadFile(googleDriveFile, outFile, this);
  44. }
  45. File outFile2 = getOutFile(this.mApplicationContext, CommonDefine.FileType_PDF);
  46. this.mDownloadFile = outFile2;
  47. if (outFile2 == null) {
  48. return null;
  49. }
  50. return this.mMyDrive.downloadPdf(googleDriveFile, outFile2);
  51. } catch (Exception e) {
  52. this.mThreadException = e;
  53. return null;
  54. }
  55. }
  56. @VisibleForTesting
  57. @NonNull
  58. static String getExtensionFromMimeType(OnlineFile onlineFile) {
  59. if (onlineFile == null) {
  60. return "";
  61. }
  62. String extensionFromMimeType = getExtensionFromMimeType(onlineFile.getMimeType());
  63. return extensionFromMimeType.isEmpty() ? getExtensionFromMimeType(Utils.getMimeType(onlineFile.getName())) : extensionFromMimeType;
  64. }
  65. @NonNull
  66. private static String getExtensionFromMimeType(String str) {
  67. String mimeExt = Utils.getMimeExt(str);
  68. return mimeExt != null ? mimeExt : "";
  69. }
  70. /* access modifiers changed from: protected */
  71. public void onPostExecute(OnlineFile onlineFile) {
  72. if (this.mDownloadCancelled) {
  73. this.mDownloadCompleteListener.onDownloadComplete(false, (String) null);
  74. }
  75. if (onlineFile != null) {
  76. String name = onlineFile.getName();
  77. if (name != null) {
  78. this.mDownloadCompleteListener.onDownloadComplete(false, name);
  79. } else {
  80. this.mDownloadCompleteListener.onDownloadComplete(true, (String) null);
  81. }
  82. } else {
  83. Exception exc = this.mThreadException;
  84. deleteDownloadFile();
  85. this.mDownloadCompleteListener.onDownloadComplete(true, (String) null);
  86. }
  87. }
  88. /* access modifiers changed from: protected */
  89. public void onCancelled() {
  90. deleteDownloadFile();
  91. this.mDownloadCompleteListener.onDownloadComplete(false, (String) null);
  92. }
  93. public boolean canceled() {
  94. return this.mDownloadCancelled;
  95. }
  96. public void cancelTask() {
  97. this.mDownloadCancelled = true;
  98. cancel(true);
  99. }
  100. @Nullable
  101. private File getOutFile(@NonNull Context context, @NonNull String str) {
  102. try {
  103. File prepareOutDirectory = prepareOutDirectory(context);
  104. return new File(prepareOutDirectory, DOWNLOAD_TMP_FILE_NAME_BODY + str);
  105. } catch (IOException | SecurityException unused) {
  106. return null;
  107. }
  108. }
  109. @NonNull
  110. private File prepareOutDirectory(@NonNull Context context) throws IOException {
  111. File file = new File(ExternalFileUtils.getInstance(context).getDownloadDir());
  112. if (file.exists() || file.mkdirs()) {
  113. File[] listFiles = file.listFiles();
  114. if (listFiles != null) {
  115. for (File delete : listFiles) {
  116. delete.delete();
  117. }
  118. }
  119. return file;
  120. }
  121. throw new IOException();
  122. }
  123. public void deleteDownloadFile() {
  124. if (this.mDownloadFile != null && this.mDownloadFile.exists()) {
  125. this.mDownloadFile.delete();
  126. this.mDownloadFile = null;
  127. }
  128. }
  129. }