DriveWriter.java 9.5 KB

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