GoogleDownloader.java 11 KB

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