GoogleDownloader.java 11 KB

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