CreatePrintImageThread.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package epson.print.service;
  2. import android.util.Log;
  3. import java.util.concurrent.SynchronousQueue;
  4. import java.util.concurrent.TimeUnit;
  5. import epson.print.EPImageList;
  6. public class CreatePrintImageThread extends Thread {
  7. private static final String TAG = "CreatePrintImageThread";
  8. private Exception mException;
  9. private EPImageList mImageList;
  10. private int mImagePerPage;
  11. private LocalImageCreator mLocalImageCreator;
  12. private boolean mLoopEnd;
  13. private PrintService mPrintService;
  14. private SynchronousQueue<Integer> mQueue = new SynchronousQueue<>();
  15. private RenderingController mRenderingController;
  16. private int mTotalPage;
  17. public CreatePrintImageThread(RenderingController renderingController, PrintService printService, LocalImageCreator localImageCreator, EPImageList ePImageList, int i, int i2) {
  18. super(TAG);
  19. mRenderingController = renderingController;
  20. mPrintService = printService;
  21. mLocalImageCreator = localImageCreator;
  22. mImageList = ePImageList;
  23. mImagePerPage = i;
  24. mTotalPage = i2;
  25. }
  26. public void run() {
  27. mLoopEnd = false;
  28. createImageLoop();
  29. mLoopEnd = true;
  30. Log.v(TAG, "run() end");
  31. }
  32. private void createImageLoop() {
  33. mException = null;
  34. int size = mImageList.size();
  35. int i = 0;
  36. int i2 = 0;
  37. while (i < mTotalPage) {
  38. try {
  39. int i3 = mImagePerPage + i2;
  40. if (i3 > size) {
  41. i3 = size;
  42. }
  43. mRenderingController.waitPage(i2, i3);
  44. if (mPrintService.getCancelPrinting()) {
  45. throw new LocalInterrupt();
  46. } else if (localCreateImage(mImageList, i) != null) {
  47. if (!mPrintService.getCancelPrinting()) {
  48. putSheet(i);
  49. i++;
  50. i2 = i3;
  51. } else {
  52. throw new LocalInterrupt();
  53. }
  54. } else {
  55. return;
  56. }
  57. } catch (Exception e) {
  58. mException = e;
  59. e.printStackTrace();
  60. return;
  61. }
  62. }
  63. }
  64. private String localCreateImage(EPImageList ePImageList, int i) {
  65. return mLocalImageCreator.createImage(mPrintService, ePImageList, i);
  66. }
  67. private void putSheet(int i) throws InterruptedException {
  68. Log.v(TAG, "queue.put " + i);
  69. mQueue.put(Integer.valueOf(i));
  70. }
  71. public int waitePrintImage() throws Exception {
  72. Exception exc = mException;
  73. if (exc != null) {
  74. throw exc;
  75. } else if (mLoopEnd) {
  76. return -1;
  77. } else {
  78. Log.v(TAG, "next take");
  79. Integer poll = mQueue.poll(180, TimeUnit.MINUTES);
  80. Log.v(TAG, "queue.take " + poll);
  81. if (poll == null) {
  82. return -1;
  83. }
  84. return poll.intValue();
  85. }
  86. }
  87. static class LocalImageCreator {
  88. int mColor;
  89. int mDate;
  90. int mDuplex;
  91. int mLayout;
  92. int mLayoutMulti;
  93. int mPaperSize;
  94. int mQuality;
  95. public LocalImageCreator(int i, int i2, int i3, int i4, int i5, int i6, int i7) {
  96. mPaperSize = i;
  97. mColor = i2;
  98. mDuplex = i3;
  99. mLayout = i4;
  100. mLayoutMulti = i5;
  101. mQuality = i6;
  102. mDate = i7;
  103. }
  104. public String createImage(PrintService printService, EPImageList ePImageList, int i) {
  105. return printService.createPrintImage(ePImageList, i, mPaperSize, mColor, mDuplex, mLayout, mLayoutMulti, mQuality, mDate);
  106. }
  107. }
  108. }