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