PrintAsyncTask.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2019 Tobias Kaminsky
  6. * Copyright (C) 2019 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.asynctasks;
  22. import android.os.AsyncTask;
  23. import android.os.Build;
  24. import android.print.PrintAttributes;
  25. import android.print.PrintDocumentAdapter;
  26. import android.print.PrintManager;
  27. import com.owncloud.android.R;
  28. import com.owncloud.android.lib.common.utils.Log_OC;
  29. import com.owncloud.android.ui.activity.RichDocumentsWebView;
  30. import com.owncloud.android.ui.adapter.PrintAdapter;
  31. import com.owncloud.android.utils.DisplayUtils;
  32. import org.apache.commons.httpclient.Header;
  33. import org.apache.commons.httpclient.HttpClient;
  34. import org.apache.commons.httpclient.HttpStatus;
  35. import org.apache.commons.httpclient.methods.GetMethod;
  36. import java.io.BufferedInputStream;
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.io.IOException;
  40. import java.lang.ref.WeakReference;
  41. import androidx.annotation.RequiresApi;
  42. import static android.content.Context.PRINT_SERVICE;
  43. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  44. public class PrintAsyncTask extends AsyncTask<Void, Void, Boolean> {
  45. private static final String TAG = PrintAsyncTask.class.getSimpleName();
  46. private static final String JOB_NAME = "Document";
  47. private File file;
  48. private String url;
  49. private WeakReference<RichDocumentsWebView> richDocumentsWebViewWeakReference;
  50. public PrintAsyncTask(File file, String url, WeakReference<RichDocumentsWebView> richDocumentsWebViewWeakReference) {
  51. this.file = file;
  52. this.url = url;
  53. this.richDocumentsWebViewWeakReference = richDocumentsWebViewWeakReference;
  54. }
  55. @Override
  56. protected void onPreExecute() {
  57. richDocumentsWebViewWeakReference.get().runOnUiThread(
  58. () -> richDocumentsWebViewWeakReference.get().showLoadingDialog(
  59. richDocumentsWebViewWeakReference.get().getString(R.string.common_loading)));
  60. super.onPreExecute();
  61. }
  62. @Override
  63. protected Boolean doInBackground(Void... voids) {
  64. HttpClient client = new HttpClient();
  65. GetMethod getMethod = new GetMethod(url);
  66. FileOutputStream fos;
  67. try {
  68. int status = client.executeMethod(getMethod);
  69. if (status == HttpStatus.SC_OK) {
  70. if (file.exists() && !file.delete()) {
  71. return false;
  72. }
  73. file.getParentFile().mkdirs();
  74. if (!file.getParentFile().exists()) {
  75. Log_OC.d(TAG, file.getParentFile().getAbsolutePath() + " does not exist");
  76. return false;
  77. }
  78. if (!file.createNewFile()) {
  79. Log_OC.d(TAG, file.getAbsolutePath() + " could not be created");
  80. return false;
  81. }
  82. BufferedInputStream bis = new BufferedInputStream(getMethod.getResponseBodyAsStream());
  83. fos = new FileOutputStream(file);
  84. long transferred = 0;
  85. Header contentLength = getMethod.getResponseHeader("Content-Length");
  86. long totalToTransfer = contentLength != null && contentLength.getValue().length() > 0 ?
  87. Long.parseLong(contentLength.getValue()) : 0;
  88. byte[] bytes = new byte[4096];
  89. int readResult;
  90. while ((readResult = bis.read(bytes)) != -1) {
  91. fos.write(bytes, 0, readResult);
  92. transferred += readResult;
  93. }
  94. // Check if the file is completed
  95. if (transferred != totalToTransfer) {
  96. return false;
  97. }
  98. if (getMethod.getResponseBodyAsStream() != null) {
  99. getMethod.getResponseBodyAsStream().close();
  100. }
  101. }
  102. } catch (IOException e) {
  103. Log_OC.e(TAG, "Error reading file", e);
  104. }
  105. return true;
  106. }
  107. @Override
  108. protected void onPostExecute(Boolean result) {
  109. RichDocumentsWebView richDocumentsWebView = richDocumentsWebViewWeakReference.get();
  110. richDocumentsWebView.dismissLoadingDialog();
  111. PrintManager printManager = (PrintManager) richDocumentsWebView.getSystemService(PRINT_SERVICE);
  112. if (!result || printManager == null) {
  113. DisplayUtils.showSnackMessage(richDocumentsWebView,
  114. richDocumentsWebView.getString(R.string.failed_to_print));
  115. return;
  116. }
  117. PrintDocumentAdapter printAdapter = new PrintAdapter(file.getAbsolutePath());
  118. printManager.print(JOB_NAME, printAdapter, new PrintAttributes.Builder().build());
  119. }
  120. }