DriveWriter.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package com.epson.iprint.storage.gdrivev3;
  2. import android.content.Context;
  3. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  4. import com.google.api.client.extensions.android.http.AndroidHttp;
  5. import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
  6. import com.google.api.client.http.FileContent;
  7. import com.google.api.client.json.gson.GsonFactory;
  8. import com.google.api.services.drive.Drive;
  9. import com.google.api.services.drive.model.File;
  10. import com.google.api.services.drive.model.FileList;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Collections;
  16. import java.util.List;
  17. import java.util.Locale;
  18. import epson.print.gdconv.ConvertStatus;
  19. public class DriveWriter {
  20. private static final String ROOT_ID = "root";
  21. private static final String TAG = "DriveConverter ";
  22. private Drive mDrive;
  23. public interface ConvertStatusNotifier {
  24. void changeStatus(ConvertStatus convertStatus);
  25. }
  26. @NonNull
  27. public static String getWriteScope() {
  28. return "https://www.googleapis.com/auth/drive";
  29. }
  30. public DriveWriter(@NonNull Drive drive) {
  31. this.mDrive = drive;
  32. }
  33. public void uploadFileToRoot(@NonNull String str, @NonNull String str2, @NonNull String str3) throws IOException {
  34. uploadFile(str, str2, str3, ROOT_ID);
  35. }
  36. public String uploadFile(@NonNull String str, @NonNull String str2, @NonNull String str3, @NonNull String str4) throws IOException {
  37. return ((File) this.mDrive.files().create(new File().setParents(Collections.singletonList(str4)).setMimeType(str3).setName(str2), new FileContent(str3, new java.io.File(str))).setFields("id, parents, mimeType").execute()).getId();
  38. }
  39. @NonNull
  40. public List<String> findFolder(@Nullable String str, String str2) throws IOException {
  41. if (str == null) {
  42. str = ROOT_ID;
  43. }
  44. if (str2 == null) {
  45. return Arrays.asList(new String[]{ROOT_ID});
  46. }
  47. List<File> files = ((FileList) this.mDrive.files().list().setQ(String.format(Locale.US, "name = '%s' and '%s' in parents and trashed = false and mimeType contains '%s'", new Object[]{str2, str, GoogleDriveMimeType.MIME_TYPE_GOOGLE_FOLDER})).execute()).getFiles();
  48. ArrayList arrayList = new ArrayList();
  49. for (File id : files) {
  50. arrayList.add(id.getId());
  51. }
  52. return arrayList;
  53. }
  54. public String createFolderInRoot(@NonNull String str) throws IOException {
  55. return ((File) this.mDrive.files().create(new File().setName(str).setMimeType(GoogleDriveMimeType.MIME_TYPE_GOOGLE_FOLDER)).setFields("id").execute()).getId();
  56. }
  57. public void convertFileToPdf(@NonNull String str, @NonNull String str2, @Nullable String str3, @NonNull java.io.File file, ConvertStatusNotifier convertStatusNotifier) throws IOException {
  58. if (str3 == null) {
  59. str3 = ROOT_ID;
  60. }
  61. String googleDocsConvertMimeType = getGoogleDocsConvertMimeType(str2);
  62. if (googleDocsConvertMimeType != null) {
  63. File file2 = (File) this.mDrive.files().create(new File().setParents(Collections.singletonList(str3)).setMimeType(googleDocsConvertMimeType).setName("iprint_convert_tmp.tmp"), new FileContent(str2, new java.io.File(str))).setFields("id, parents").execute();
  64. if (convertStatusNotifier != null) {
  65. convertStatusNotifier.changeStatus(ConvertStatus.DOWNLOAD);
  66. }
  67. String id = file2.getId();
  68. try {
  69. this.mDrive.files().export(id, "application/pdf").executeMediaAndDownloadTo(new FileOutputStream(file));
  70. } finally {
  71. this.mDrive.files().delete(id).execute();
  72. }
  73. } else {
  74. throw new ConvertMimeTypeException("mime type <" + str2 + "> not supported");
  75. }
  76. }
  77. /* JADX WARNING: Can't fix incorrect switch cases order */
  78. @android.support.annotation.Nullable
  79. @android.support.annotation.VisibleForTesting
  80. /* Code decompiled incorrectly, please refer to instructions dump. */
  81. static java.lang.String getGoogleDocsConvertMimeType(@android.support.annotation.NonNull java.lang.String r1) {
  82. /*
  83. int r0 = r1.hashCode()
  84. switch(r0) {
  85. case -1248332507: goto L_0x0071;
  86. case -1082243251: goto L_0x0067;
  87. case -1073633483: goto L_0x005c;
  88. case -1071817359: goto L_0x0051;
  89. case -1050893613: goto L_0x0047;
  90. case -1004747228: goto L_0x003c;
  91. case -366307023: goto L_0x0032;
  92. case 717553764: goto L_0x0028;
  93. case 817335912: goto L_0x001e;
  94. case 904647503: goto L_0x0014;
  95. case 1993842850: goto L_0x0009;
  96. default: goto L_0x0007;
  97. }
  98. L_0x0007:
  99. goto L_0x007b
  100. L_0x0009:
  101. java.lang.String r0 = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  102. boolean r1 = r1.equals(r0)
  103. if (r1 == 0) goto L_0x007b
  104. r1 = 6
  105. goto L_0x007c
  106. L_0x0014:
  107. java.lang.String r0 = "application/msword"
  108. boolean r1 = r1.equals(r0)
  109. if (r1 == 0) goto L_0x007b
  110. r1 = 1
  111. goto L_0x007c
  112. L_0x001e:
  113. java.lang.String r0 = "text/plain"
  114. boolean r1 = r1.equals(r0)
  115. if (r1 == 0) goto L_0x007b
  116. r1 = 2
  117. goto L_0x007c
  118. L_0x0028:
  119. java.lang.String r0 = "application/vnd.google-apps.document"
  120. boolean r1 = r1.equals(r0)
  121. if (r1 == 0) goto L_0x007b
  122. r1 = 5
  123. goto L_0x007c
  124. L_0x0032:
  125. java.lang.String r0 = "application/vnd.ms-excel"
  126. boolean r1 = r1.equals(r0)
  127. if (r1 == 0) goto L_0x007b
  128. r1 = 7
  129. goto L_0x007c
  130. L_0x003c:
  131. java.lang.String r0 = "text/csv"
  132. boolean r1 = r1.equals(r0)
  133. if (r1 == 0) goto L_0x007b
  134. r1 = 8
  135. goto L_0x007c
  136. L_0x0047:
  137. java.lang.String r0 = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  138. boolean r1 = r1.equals(r0)
  139. if (r1 == 0) goto L_0x007b
  140. r1 = 0
  141. goto L_0x007c
  142. L_0x0051:
  143. java.lang.String r0 = "application/vnd.ms-powerpoint"
  144. boolean r1 = r1.equals(r0)
  145. if (r1 == 0) goto L_0x007b
  146. r1 = 9
  147. goto L_0x007c
  148. L_0x005c:
  149. java.lang.String r0 = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
  150. boolean r1 = r1.equals(r0)
  151. if (r1 == 0) goto L_0x007b
  152. r1 = 10
  153. goto L_0x007c
  154. L_0x0067:
  155. java.lang.String r0 = "text/html"
  156. boolean r1 = r1.equals(r0)
  157. if (r1 == 0) goto L_0x007b
  158. r1 = 3
  159. goto L_0x007c
  160. L_0x0071:
  161. java.lang.String r0 = "application/rtf"
  162. boolean r1 = r1.equals(r0)
  163. if (r1 == 0) goto L_0x007b
  164. r1 = 4
  165. goto L_0x007c
  166. L_0x007b:
  167. r1 = -1
  168. L_0x007c:
  169. switch(r1) {
  170. case 0: goto L_0x0087;
  171. case 1: goto L_0x0087;
  172. case 2: goto L_0x0087;
  173. case 3: goto L_0x0087;
  174. case 4: goto L_0x0087;
  175. case 5: goto L_0x0087;
  176. case 6: goto L_0x0084;
  177. case 7: goto L_0x0084;
  178. case 8: goto L_0x0084;
  179. case 9: goto L_0x0081;
  180. case 10: goto L_0x0081;
  181. default: goto L_0x007f;
  182. }
  183. L_0x007f:
  184. r1 = 0
  185. return r1
  186. L_0x0081:
  187. java.lang.String r1 = "application/vnd.google-apps.presentation"
  188. return r1
  189. L_0x0084:
  190. java.lang.String r1 = "application/vnd.google-apps.spreadsheet"
  191. return r1
  192. L_0x0087:
  193. java.lang.String r1 = "application/vnd.google-apps.document"
  194. return r1
  195. */
  196. throw new UnsupportedOperationException("Method not decompiled: com.epson.iprint.storage.gdrivev3.DriveWriter.getGoogleDocsConvertMimeType(java.lang.String):java.lang.String");
  197. }
  198. public String findOrCreateFolder(@NonNull String str) throws IOException {
  199. List<String> findFolder = findFolder((String) null, str);
  200. if (!findFolder.isEmpty()) {
  201. return findFolder.get(0);
  202. }
  203. return createFolderInRoot(str);
  204. }
  205. @NonNull
  206. public static DriveWriter createDriveWriter(@NonNull Context context) {
  207. GoogleSignInAccount signInAccount = new IprintGoogleSignIn().getSignInAccount(context);
  208. if (signInAccount != null) {
  209. GoogleAccountCredential usingOAuth2 = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(getWriteScope()));
  210. usingOAuth2.setSelectedAccount(signInAccount.getAccount());
  211. return new DriveWriter(new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), usingOAuth2).setApplicationName(GoogleDownloader.getDriveApplicationName(context)).build());
  212. }
  213. throw new IllegalStateException();
  214. }
  215. public static class ConvertMimeTypeException extends IOException {
  216. public ConvertMimeTypeException(String str) {
  217. super(str);
  218. }
  219. }
  220. }