GoogleDownloader.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package com.epson.iprint.storage.gdrivev3;
  2. import android.content.Context;
  3. import android.content.pm.PackageInfo;
  4. import android.content.pm.PackageManager;
  5. import com.epson.iprint.storage.gdrivev3.DownloadTask;
  6. import com.epson.iprint.storage.gdrivev3.DriveListTask;
  7. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  8. import com.google.api.client.extensions.android.http.AndroidHttp;
  9. import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
  10. import com.google.api.client.json.gson.GsonFactory;
  11. import com.google.api.services.drive.Drive;
  12. import com.google.api.services.drive.model.FileList;
  13. import epson.print.CommonDefine;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.LinkedList;
  21. public class GoogleDownloader {
  22. private static final String ROOT_ID = "root";
  23. private static final String TARGET_SCOPE = "https://www.googleapis.com/auth/drive";
  24. private final Drive mDrive;
  25. private final FolderManager mFolderManager = new FolderManager();
  26. public static String getTargetScope() {
  27. return "https://www.googleapis.com/auth/drive";
  28. }
  29. private GoogleDownloader(@NonNull Drive drive) {
  30. this.mDrive = drive;
  31. }
  32. public DownloadTask startDownload(@NonNull ActivityWrapper activityWrapper, @NonNull OnlineFile onlineFile, @NonNull DownloadTask.DownloadCompleteListener downloadCompleteListener) {
  33. DownloadTask downloadTask = new DownloadTask(activityWrapper.getActivity().getApplicationContext(), this, downloadCompleteListener);
  34. downloadTask.execute(new OnlineFile[]{onlineFile});
  35. return downloadTask;
  36. }
  37. public void startListTask(@Nullable OnlineFile onlineFile, @NonNull DriveListTask.ListTaskCompleteListener listTaskCompleteListener) {
  38. new DriveListTask(this, listTaskCompleteListener).execute(new OnlineFile[]{onlineFile});
  39. }
  40. public boolean isRootFolder() {
  41. return this.mFolderManager.isRootFolder();
  42. }
  43. @Nullable
  44. public OnlineFile downloadPdf(GoogleDriveFile googleDriveFile, File file) throws IOException {
  45. Throwable th;
  46. String id = googleDriveFile.getId();
  47. if (id != null) {
  48. FileOutputStream fileOutputStream = new FileOutputStream(file);
  49. try {
  50. this.mDrive.files().export(id, "application/pdf").executeMediaAndDownloadTo(fileOutputStream);
  51. fileOutputStream.close();
  52. return new DownloadFile(file.getPath(), "application/pdf");
  53. } catch (Throwable th2) {
  54. th.addSuppressed(th2);
  55. }
  56. } else {
  57. throw new IllegalStateException();
  58. }
  59. throw th;
  60. }
  61. /* JADX WARNING: Code restructure failed: missing block: B:12:?, code lost:
  62. r2.close();
  63. */
  64. /* JADX WARNING: Code restructure failed: missing block: B:13:0x002f, code lost:
  65. if (r7 == null) goto L_0x0034;
  66. */
  67. /* JADX WARNING: Code restructure failed: missing block: B:14:0x0031, code lost:
  68. r7.close();
  69. */
  70. /* JADX WARNING: Code restructure failed: missing block: B:15:0x0034, code lost:
  71. return r0;
  72. */
  73. /* JADX WARNING: Code restructure failed: missing block: B:16:0x0035, code lost:
  74. r8 = th;
  75. */
  76. /* JADX WARNING: Code restructure failed: missing block: B:17:0x0036, code lost:
  77. r9 = null;
  78. */
  79. /* JADX WARNING: Code restructure failed: missing block: B:21:0x003a, code lost:
  80. r9 = move-exception;
  81. */
  82. /* JADX WARNING: Code restructure failed: missing block: B:22:0x003b, code lost:
  83. r5 = r9;
  84. r9 = r8;
  85. r8 = r5;
  86. */
  87. @android.support.annotation.Nullable
  88. /* Code decompiled incorrectly, please refer to instructions dump. */
  89. public com.epson.iprint.storage.gdrivev3.OnlineFile downloadFile(com.epson.iprint.storage.gdrivev3.OnlineFile r7, java.io.File r8, @android.support.annotation.NonNull com.epson.iprint.storage.gdrivev3.Canceller r9) throws java.io.IOException {
  90. /*
  91. r6 = this;
  92. com.epson.iprint.storage.gdrivev3.DownloadFile r0 = new com.epson.iprint.storage.gdrivev3.DownloadFile
  93. java.lang.String r1 = r8.getPath()
  94. java.lang.String r2 = r7.getMimeType()
  95. r0.<init>(r1, r2)
  96. java.io.InputStream r7 = r6.getFileInputStream(r7)
  97. r1 = 0
  98. java.io.FileOutputStream r2 = new java.io.FileOutputStream // Catch:{ Throwable -> 0x004f }
  99. r2.<init>(r8) // Catch:{ Throwable -> 0x004f }
  100. r8 = 1024(0x400, float:1.435E-42)
  101. byte[] r8 = new byte[r8] // Catch:{ Throwable -> 0x0038, all -> 0x0035 }
  102. L_0x001b:
  103. int r3 = r7.read(r8) // Catch:{ Throwable -> 0x0038, all -> 0x0035 }
  104. if (r3 < 0) goto L_0x002c
  105. boolean r4 = r9.canceled() // Catch:{ Throwable -> 0x0038, all -> 0x0035 }
  106. if (r4 != 0) goto L_0x002c
  107. r4 = 0
  108. r2.write(r8, r4, r3) // Catch:{ Throwable -> 0x0038, all -> 0x0035 }
  109. goto L_0x001b
  110. L_0x002c:
  111. r2.close() // Catch:{ Throwable -> 0x004f }
  112. if (r7 == 0) goto L_0x0034
  113. r7.close()
  114. L_0x0034:
  115. return r0
  116. L_0x0035:
  117. r8 = move-exception
  118. r9 = r1
  119. goto L_0x003e
  120. L_0x0038:
  121. r8 = move-exception
  122. throw r8 // Catch:{ all -> 0x003a }
  123. L_0x003a:
  124. r9 = move-exception
  125. r5 = r9
  126. r9 = r8
  127. r8 = r5
  128. L_0x003e:
  129. if (r9 == 0) goto L_0x0049
  130. r2.close() // Catch:{ Throwable -> 0x0044 }
  131. goto L_0x004c
  132. L_0x0044:
  133. r0 = move-exception
  134. r9.addSuppressed(r0) // Catch:{ Throwable -> 0x004f }
  135. goto L_0x004c
  136. L_0x0049:
  137. r2.close() // Catch:{ Throwable -> 0x004f }
  138. L_0x004c:
  139. throw r8 // Catch:{ Throwable -> 0x004f }
  140. L_0x004d:
  141. r8 = move-exception
  142. goto L_0x0052
  143. L_0x004f:
  144. r8 = move-exception
  145. r1 = r8
  146. throw r1 // Catch:{ all -> 0x004d }
  147. L_0x0052:
  148. if (r7 == 0) goto L_0x0062
  149. if (r1 == 0) goto L_0x005f
  150. r7.close() // Catch:{ Throwable -> 0x005a }
  151. goto L_0x0062
  152. L_0x005a:
  153. r7 = move-exception
  154. r1.addSuppressed(r7)
  155. goto L_0x0062
  156. L_0x005f:
  157. r7.close()
  158. L_0x0062:
  159. throw r8
  160. */
  161. throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.gdrivev3.GoogleDownloader.downloadFile(com.epson.iprint.storage.gdrivev3.OnlineFile, java.io.File, com.epson.iprint.storage.gdrivev3.Canceller):com.epson.iprint.storage.gdrivev3.OnlineFile");
  162. }
  163. @NonNull
  164. public static GoogleDownloader createGoogleDownloader(@NonNull Context context, @NonNull GoogleSignInAccount googleSignInAccount) {
  165. GoogleAccountCredential usingOAuth2 = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(getTargetScope()));
  166. usingOAuth2.setSelectedAccount(googleSignInAccount.getAccount());
  167. return new GoogleDownloader(new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), usingOAuth2).setApplicationName(getDriveApplicationName(context)).build());
  168. }
  169. public static String getDriveApplicationName(@NonNull Context context) {
  170. String appName = getAppName(context);
  171. return appName == null ? context.getPackageName() : appName;
  172. }
  173. private static String getAppName(@NonNull Context context) {
  174. try {
  175. PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 128);
  176. String str = (String) packageInfo.applicationInfo.loadLabel(context.getPackageManager());
  177. if (packageInfo.versionName == null) {
  178. return str;
  179. }
  180. return str + CommonDefine.SLASH + packageInfo.versionName;
  181. } catch (PackageManager.NameNotFoundException unused) {
  182. return null;
  183. }
  184. }
  185. @NonNull
  186. public ArrayList<OnlineFile> listFile(@Nullable OnlineFile onlineFile) throws IOException {
  187. Drive.Files files = this.mDrive.files();
  188. this.mFolderManager.changeCurrentFolder(onlineFile);
  189. String currentFolderId = this.mFolderManager.getCurrentFolderId();
  190. ArrayList<OnlineFile> arrayList = new ArrayList<>();
  191. String str = null;
  192. do {
  193. Drive.Files.List list = files.list();
  194. FileList fileList = (FileList) list.setQ("'" + currentFolderId + "' in parents and trashed = false and (mimeType contains 'image/' or mimeType contains 'application/' or mimeType contains 'text/')").setFields("nextPageToken, files(id, name, mimeType, trashed)").setPageToken(str).execute();
  195. if (!this.mFolderManager.isRootFolder()) {
  196. arrayList.add(new ParentFolder());
  197. }
  198. for (com.google.api.services.drive.model.File googleDriveFile : fileList.getFiles()) {
  199. arrayList.add(new GoogleDriveFile(googleDriveFile));
  200. }
  201. str = fileList.getNextPageToken();
  202. } while (str != null);
  203. return arrayList;
  204. }
  205. private InputStream getFileInputStream(@NonNull OnlineFile onlineFile) throws IOException {
  206. String id = onlineFile.getId();
  207. if (id != null) {
  208. return this.mDrive.files().get(id).executeMediaAsInputStream();
  209. }
  210. throw new IOException("getId() return null");
  211. }
  212. private static class FolderManager {
  213. private OnlineFile mCurrentFolder = null;
  214. private final LinkedList<OnlineFile> mParentDirList = new LinkedList<>();
  215. FolderManager() {
  216. }
  217. private void changeCurrentFolder(@Nullable OnlineFile onlineFile) {
  218. if (onlineFile == null || !onlineFile.isFolder()) {
  219. return;
  220. }
  221. if (onlineFile instanceof ParentFolder) {
  222. gotoParent();
  223. return;
  224. }
  225. OnlineFile onlineFile2 = this.mCurrentFolder;
  226. if (onlineFile2 != null) {
  227. this.mParentDirList.push(onlineFile2);
  228. }
  229. this.mCurrentFolder = onlineFile;
  230. }
  231. private void gotoRoot() {
  232. this.mParentDirList.clear();
  233. this.mCurrentFolder = null;
  234. }
  235. private void gotoParent() {
  236. if (this.mParentDirList.isEmpty()) {
  237. this.mCurrentFolder = null;
  238. gotoRoot();
  239. return;
  240. }
  241. OnlineFile pop = this.mParentDirList.pop();
  242. if (pop == null) {
  243. gotoRoot();
  244. return;
  245. }
  246. this.mCurrentFolder = pop;
  247. this.mCurrentFolder.getId();
  248. }
  249. private String getCurrentFolderId() {
  250. OnlineFile onlineFile = this.mCurrentFolder;
  251. if (onlineFile == null) {
  252. return GoogleDownloader.ROOT_ID;
  253. }
  254. return onlineFile.getId();
  255. }
  256. private boolean isRootFolder() {
  257. return this.mCurrentFolder == null;
  258. }
  259. }
  260. }