PhotoPreview.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. package epson.print.phlayout;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.graphics.Point;
  10. import android.graphics.Rect;
  11. import android.graphics.RectF;
  12. import android.graphics.Region;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.support.annotation.AnyThread;
  16. import android.support.p000v4.view.GestureDetectorCompat;
  17. import android.util.AttributeSet;
  18. import android.util.Log;
  19. import android.view.GestureDetector;
  20. import android.view.MotionEvent;
  21. import android.view.ScaleGestureDetector;
  22. import android.view.View;
  23. import epson.common.Info_paper;
  24. import epson.print.R;
  25. import epson.print.EPImage;
  26. import epson.print.EPImageCreator;
  27. import epson.print.EPImageList;
  28. import epson.print.IprintApplication;
  29. import epson.print.phlayout.PhotoPreviewImageList;
  30. import java.lang.annotation.Retention;
  31. import java.lang.annotation.RetentionPolicy;
  32. import java.util.concurrent.locks.ReentrantLock;
  33. public class PhotoPreview extends View {
  34. private static final int ALL_REFRESH_DRAW_END = 4;
  35. private static final int CREATE_PREVIEW = 1;
  36. static final int FIT_MODE_CIRCUMSCRIBED = 1;
  37. static final int FIT_MODE_FREE = 0;
  38. static final int FIT_MODE_INSCRIBED = 2;
  39. private static final int INVALID_IMAGE_NO = -1;
  40. private static final String LOG_TAG = "previewView";
  41. private static final float MAX_MAGNIFICATION_FACTOR = 8.0f;
  42. private static final float MINI_MAGNIFICATION_FACTOR = 0.25f;
  43. private static final double MOVEMENT_MARGIN_RATE = 0.2d;
  44. public static final int RESET_PREVIEW = 2;
  45. public static final int RESTORE_PREVIEW = 0;
  46. private static final int ZOOM_CONTROL = 2;
  47. private float baseScaleFactor = 1.0f;
  48. private int color = 0;
  49. private Handler drawEndHandler = null;
  50. private PhotoPreviewImageList imageList = null;
  51. private volatile int imageNo = 0;
  52. private final Object lockObj = new Object();
  53. private Bitmap mBitmap = null;
  54. private final Rect mBitmapDrawRect = new Rect();
  55. private final ReentrantLock mDrawLock = new ReentrantLock();
  56. private final FitLayout mFitLayout = new FitLayout();
  57. private volatile int mFitMode;
  58. private GestureDetectorCompat mGestureDetector;
  59. private volatile ILayoutPosition mLayoutPosition = null;
  60. private int mPaperSizeId = 0;
  61. private final Path mPath = new Path();
  62. private volatile PreviewBitmapMaker mPreviewBitmapMaker;
  63. private ScaleGestureDetector mScaleGestureDetector = null;
  64. private int[] mViewSize = new int[2];
  65. private ViewSizeChangeListener mViewSizeChangeListener;
  66. private boolean maskExpandPreview = true;
  67. private int maxBitmapHeight = -1;
  68. private int maxBitmapWidth = -1;
  69. private final Paint paint = new Paint();
  70. private float scaleFactor = 1.0f;
  71. private final Point trans = new Point();
  72. private Handler zoomControlHandler = null;
  73. @Retention(RetentionPolicy.SOURCE)
  74. @interface FitMode {
  75. }
  76. public interface PreviewBitmapMaker {
  77. Bitmap getPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z);
  78. }
  79. public interface ViewSizeChangeListener {
  80. void onViewSizeChanged();
  81. }
  82. public PhotoPreview(Context context) {
  83. super(context);
  84. initLocal(context);
  85. }
  86. public PhotoPreview(Context context, AttributeSet attributeSet) {
  87. super(context, attributeSet);
  88. initLocal(context);
  89. }
  90. private void initLocal(Context context) {
  91. this.mGestureDetector = new GestureDetectorCompat(context, new MyGestureListener(this));
  92. this.mScaleGestureDetector = new ScaleGestureDetector(context, new MyScaledGestureListener(this));
  93. this.mFitMode = 1;
  94. }
  95. private void startScaleChange() {
  96. this.maskExpandPreview = false;
  97. this.baseScaleFactor = this.scaleFactor;
  98. this.mFitMode = 0;
  99. invalidate();
  100. }
  101. private void changeScale(float f) {
  102. this.scaleFactor = f * this.baseScaleFactor;
  103. float f2 = this.scaleFactor;
  104. if (f2 < MINI_MAGNIFICATION_FACTOR) {
  105. this.scaleFactor = MINI_MAGNIFICATION_FACTOR;
  106. } else if (f2 > MAX_MAGNIFICATION_FACTOR) {
  107. this.scaleFactor = MAX_MAGNIFICATION_FACTOR;
  108. }
  109. Handler handler = this.zoomControlHandler;
  110. if (handler != null) {
  111. handler.sendEmptyMessage(2);
  112. }
  113. adjustTrans(this.trans);
  114. invalidate();
  115. }
  116. private void changeFitModeAndRefresh() {
  117. this.maskExpandPreview = true;
  118. changeFitMode();
  119. invalidate();
  120. Handler handler = this.zoomControlHandler;
  121. if (handler != null) {
  122. handler.sendEmptyMessage(2);
  123. }
  124. }
  125. private void changeFitMode() {
  126. if (1 == this.mLayoutPosition.getLayoutId()) {
  127. fitCircumscribed();
  128. } else if (this.mFitMode == 1) {
  129. fitInscribed();
  130. } else {
  131. fitCircumscribed();
  132. }
  133. }
  134. @WorkerThread
  135. private void fitCircumscribed() {
  136. this.scaleFactor = 1.0f;
  137. this.baseScaleFactor = 1.0f;
  138. setPaper(this.mPaperSizeId);
  139. this.mFitMode = 1;
  140. }
  141. private void fitInscribed() {
  142. EPImage currentEpImage;
  143. this.mFitMode = 2;
  144. if (this.mLayoutPosition != null && (currentEpImage = getCurrentEpImage()) != null) {
  145. RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  146. double[] fitInscribed = this.mFitLayout.fitInscribed(new double[]{(double) previewImageCircumscribedTargetSize.left, (double) previewImageCircumscribedTargetSize.top, (double) previewImageCircumscribedTargetSize.right, (double) previewImageCircumscribedTargetSize.bottom}, new double[]{(double) currentEpImage.previewWidth, (double) currentEpImage.previewHeight});
  147. if (fitInscribed != null) {
  148. Point point = this.trans;
  149. point.x = (int) fitInscribed[0];
  150. point.y = (int) fitInscribed[1];
  151. this.scaleFactor = (float) fitInscribed[2];
  152. }
  153. }
  154. }
  155. @AnyThread
  156. private EPImage getCurrentEpImage() {
  157. if (this.imageNo < 0 || this.imageNo >= this.imageList.size()) {
  158. return null;
  159. }
  160. return this.imageList.get(this.imageNo);
  161. }
  162. private PhotoPreviewImageList.Element getCurrentElement() {
  163. return this.imageList.getElement(this.imageNo);
  164. }
  165. @WorkerThread
  166. private static Bitmap getBitmap(EPImage ePImage, int i, int i2, int i3, boolean z) {
  167. new EPImageCreator(IprintApplication.getInstance()).createPreviewImage(ePImage, i, i2, i3, z);
  168. if (ePImage.previewImageFileName != null) {
  169. return BitmapFactory.decodeFile(ePImage.previewImageFileName);
  170. }
  171. return null;
  172. }
  173. public void setViewSizeChangeListener(ViewSizeChangeListener viewSizeChangeListener) {
  174. this.mViewSizeChangeListener = viewSizeChangeListener;
  175. }
  176. public void setPreviewBitmapMaker(PreviewBitmapMaker previewBitmapMaker) {
  177. this.mPreviewBitmapMaker = previewBitmapMaker;
  178. }
  179. public EPImageList getImageList() {
  180. EPImageList ePImageList;
  181. synchronized (this.lockObj) {
  182. saveImage(this.imageNo);
  183. ePImageList = new EPImageList();
  184. ePImageList.setRenderingMode(1);
  185. for (int i = 0; i < this.imageList.size(); i++) {
  186. EPImage ePImage = this.imageList.get(i);
  187. if (ePImage != null) {
  188. RectF calcPreviewImageRect = calcPreviewImageRect(ePImage.previewWidth, ePImage.previewHeight, ePImage.scaleFactor, new Point(ePImage.previewImageRectCenterX, ePImage.previewImageRectCenterY));
  189. EPImage ePImage2 = new EPImage(ePImage);
  190. ePImage2.previewImageRectLeft = calcPreviewImageRect.left;
  191. ePImage2.previewImageRectTop = calcPreviewImageRect.top;
  192. ePImage2.previewImageRectRight = calcPreviewImageRect.right;
  193. ePImage2.previewImageRectBottom = calcPreviewImageRect.bottom;
  194. ePImageList.add(ePImage2);
  195. }
  196. }
  197. }
  198. return ePImageList;
  199. }
  200. public void setImageList(@NonNull PhotoPreviewImageList photoPreviewImageList) {
  201. this.imageList = photoPreviewImageList;
  202. }
  203. public void setLayout(int i, int i2) {
  204. if (i == 1) {
  205. this.mLayoutPosition = new BorderlessLayoutPosition();
  206. } else if (i != 4) {
  207. this.mLayoutPosition = new BorderedLayoutPosition();
  208. } else {
  209. this.mLayoutPosition = new CdLayoutPosition();
  210. }
  211. this.mLayoutPosition.setLayoutId(i, i2);
  212. }
  213. public void setColor(int i) {
  214. this.color = i;
  215. }
  216. public void setZoomControlHandler(Handler handler) {
  217. this.zoomControlHandler = handler;
  218. }
  219. public void setDrawEndHandler(Handler handler) {
  220. this.drawEndHandler = handler;
  221. }
  222. public boolean getIsPaperLandscape() {
  223. return this.mLayoutPosition.getIsPaperLandscape();
  224. }
  225. @WorkerThread
  226. public void setIsPaperLandscape(boolean z, Activity activity) {
  227. synchronized (this.lockObj) {
  228. this.mLayoutPosition.setPaperLandscape(z);
  229. if (this.imageList == null || this.imageList.size() <= 0) {
  230. setPaper(this.mPaperSizeId);
  231. } else {
  232. this.scaleFactor = 1.0f;
  233. this.baseScaleFactor = 1.0f;
  234. this.mFitMode = 1;
  235. setImage(this.imageNo, 1, activity);
  236. }
  237. postInvalidate();
  238. }
  239. }
  240. @AnyThread
  241. public boolean setPaper(int i) {
  242. synchronized (this.lockObj) {
  243. this.mPaperSizeId = i;
  244. Info_paper infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), this.mPaperSizeId);
  245. if (infoPaper == null) {
  246. return false;
  247. }
  248. int[] viewSize = getViewSize();
  249. if (viewSize == null) {
  250. return false;
  251. }
  252. this.mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], this.trans);
  253. return true;
  254. }
  255. }
  256. @Nullable
  257. @AnyThread
  258. private synchronized int[] getViewSize() {
  259. if (this.mViewSize[0] > 0) {
  260. if (this.mViewSize[1] > 0) {
  261. return this.mViewSize;
  262. }
  263. }
  264. return null;
  265. }
  266. @WorkerThread
  267. private void reCalcPaperPosition() {
  268. Info_paper infoPaper;
  269. int[] viewSize = getViewSize();
  270. if (viewSize != null && (infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), this.mPaperSizeId)) != null) {
  271. this.mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], new Point());
  272. }
  273. }
  274. @AnyThread
  275. public void invalidateImageNo() {
  276. if (this.imageList != null) {
  277. this.imageNo = -1;
  278. }
  279. }
  280. public void invalidatePreview() {
  281. if (this.imageList != null) {
  282. synchronized (this.lockObj) {
  283. for (int i = 0; i < this.imageList.size(); i++) {
  284. this.imageList.get(i).previewImageFileName = null;
  285. }
  286. }
  287. }
  288. }
  289. /* JADX WARNING: Code restructure failed: missing block: B:62:0x0099, code lost:
  290. r4.mDrawLock.unlock();
  291. */
  292. /* JADX WARNING: Code restructure failed: missing block: B:63:0x009e, code lost:
  293. return false;
  294. */
  295. /* JADX WARNING: Missing exception handler attribute for start block: B:45:0x007e */
  296. /* JADX WARNING: Missing exception handler attribute for start block: B:50:0x0088 */
  297. @android.support.annotation.WorkerThread
  298. /* Code decompiled incorrectly, please refer to instructions dump. */
  299. public boolean setImage(int r5, int r6, android.app.Activity r7) throws java.lang.OutOfMemoryError {
  300. /*
  301. r4 = this;
  302. java.util.concurrent.locks.ReentrantLock r0 = r4.mDrawLock // Catch:{ all -> 0x00a2 }
  303. r0.lock() // Catch:{ all -> 0x00a2 }
  304. java.lang.Object r0 = r4.lockObj // Catch:{ all -> 0x00a2 }
  305. monitor-enter(r0) // Catch:{ all -> 0x00a2 }
  306. epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList // Catch:{ all -> 0x009f }
  307. r2 = 0
  308. if (r1 == 0) goto L_0x0098
  309. epson.print.phlayout.ILayoutPosition r1 = r4.mLayoutPosition // Catch:{ all -> 0x009f }
  310. if (r1 != 0) goto L_0x0013
  311. goto L_0x0098
  312. L_0x0013:
  313. epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  314. int r1 = r1.size() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  315. r3 = 1
  316. if (r1 > 0) goto L_0x003e
  317. r5 = 0
  318. r4.mBitmap = r5 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  319. r5 = 2
  320. if (r6 != r5) goto L_0x0025
  321. r4.fitCircumscribed() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  322. L_0x0025:
  323. if (r6 == r3) goto L_0x002f
  324. epson.print.phlayout.ILayoutPosition r5 = r4.mLayoutPosition // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  325. boolean r5 = r5.isPreviewImageSizeValid() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  326. if (r5 != 0) goto L_0x0034
  327. L_0x002f:
  328. int r5 = r4.mPaperSizeId // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  329. r4.setPaper(r5) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  330. L_0x0034:
  331. r4.postInvalidate() // Catch:{ all -> 0x009f }
  332. monitor-exit(r0) // Catch:{ all -> 0x009f }
  333. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  334. r5.unlock()
  335. return r2
  336. L_0x003e:
  337. r4.setImageCore(r5, r6) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  338. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  339. if (r5 == 0) goto L_0x0076
  340. int r5 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  341. r6 = -1
  342. if (r5 == r6) goto L_0x0054
  343. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  344. int r5 = r5.getWidth() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  345. int r1 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  346. if (r5 > r1) goto L_0x0062
  347. L_0x0054:
  348. int r5 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  349. if (r5 == r6) goto L_0x006a
  350. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  351. int r5 = r5.getHeight() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  352. int r6 = r4.maxBitmapHeight // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  353. if (r5 <= r6) goto L_0x006a
  354. L_0x0062:
  355. epson.print.phlayout.PhotoPreview$1 r5 = new epson.print.phlayout.PhotoPreview$1 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  356. r5.<init>() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  357. r7.runOnUiThread(r5) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  358. L_0x006a:
  359. r4.maskExpandPreview = r3 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  360. r4.postInvalidate() // Catch:{ all -> 0x009f }
  361. monitor-exit(r0) // Catch:{ all -> 0x009f }
  362. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  363. r5.unlock()
  364. return r3
  365. L_0x0076:
  366. java.lang.OutOfMemoryError r5 = new java.lang.OutOfMemoryError // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  367. r5.<init>() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  368. throw r5 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  369. L_0x007c:
  370. r5 = move-exception
  371. goto L_0x0094
  372. L_0x007e:
  373. r4.postInvalidate() // Catch:{ all -> 0x009f }
  374. monitor-exit(r0) // Catch:{ all -> 0x009f }
  375. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  376. r5.unlock()
  377. return r2
  378. L_0x0088:
  379. r4.postInvalidate() // Catch:{ all -> 0x009f }
  380. monitor-exit(r0) // Catch:{ all -> 0x009f }
  381. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  382. r5.unlock()
  383. return r2
  384. L_0x0092:
  385. r5 = move-exception
  386. throw r5 // Catch:{ all -> 0x007c }
  387. L_0x0094:
  388. r4.postInvalidate() // Catch:{ all -> 0x009f }
  389. throw r5 // Catch:{ all -> 0x009f }
  390. L_0x0098:
  391. monitor-exit(r0) // Catch:{ all -> 0x009f }
  392. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  393. r5.unlock()
  394. return r2
  395. L_0x009f:
  396. r5 = move-exception
  397. monitor-exit(r0) // Catch:{ all -> 0x009f }
  398. throw r5 // Catch:{ all -> 0x00a2 }
  399. L_0x00a2:
  400. r5 = move-exception
  401. java.util.concurrent.locks.ReentrantLock r6 = r4.mDrawLock
  402. r6.unlock()
  403. throw r5
  404. */
  405. throw new UnsupportedOperationException("Method not decompiled: epson.print.phlayout.PhotoPreview.setImage(int, int, android.app.Activity):boolean");
  406. }
  407. @WorkerThread
  408. private void setImageCore(int i, int i2) throws Exception {
  409. if (i < 0 || i >= this.imageList.size()) {
  410. throw new Exception();
  411. } else if (this.imageNo < 0 || this.imageNo >= this.imageList.size() || saveImage(this.imageNo)) {
  412. this.imageNo = i;
  413. PhotoPreviewImageList.Element currentElement = getCurrentElement();
  414. EPImage currentEpImage = getCurrentEpImage();
  415. if (i2 == 0) {
  416. setImageCore_RestorePreview(currentElement);
  417. } else {
  418. setImageCore2(currentEpImage, i2 == 2);
  419. }
  420. } else {
  421. throw new Exception();
  422. }
  423. }
  424. @WorkerThread
  425. private void setImageCore2(EPImage ePImage, boolean z) throws Exception {
  426. if (z) {
  427. this.mLayoutPosition.setPaperLandscape(ePImage.srcWidth > ePImage.srcHeight);
  428. fitCircumscribed();
  429. } else {
  430. setPaper(this.mPaperSizeId);
  431. }
  432. if (this.mLayoutPosition.isPreviewImageSizeValid()) {
  433. RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  434. this.mBitmap = localGetPreviewBitmap(ePImage, (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), this.color, true, !z);
  435. return;
  436. }
  437. throw new Exception();
  438. }
  439. @WorkerThread
  440. private void setImageCore_RestorePreview(@NonNull PhotoPreviewImageList.Element element) throws Exception {
  441. restoreImage(element);
  442. reCalcPaperPosition();
  443. if (this.mLayoutPosition.isPreviewImageSizeValid()) {
  444. RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  445. this.mBitmap = localGetPreviewBitmap(element.getEPImage(), (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), this.color, false, true);
  446. return;
  447. }
  448. throw new Exception();
  449. }
  450. @WorkerThread
  451. private Bitmap localGetPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z, boolean z2) {
  452. if (this.mPreviewBitmapMaker != null && z2) {
  453. this.mBitmap = this.mPreviewBitmapMaker.getPreviewBitmap(ePImage, i, i2, i3, z);
  454. Bitmap bitmap = this.mBitmap;
  455. if (bitmap != null) {
  456. return bitmap;
  457. }
  458. }
  459. this.mBitmap = getBitmap(ePImage, i, i2, i3, z);
  460. if (this.mBitmap == null) {
  461. return null;
  462. }
  463. Log.i(LOG_TAG, "bitmap <" + this.mBitmap.getWidth() + PreferencesConstants.COOKIE_DELIMITER + this.mBitmap.getHeight() + "> preview WxH <" + ePImage.previewWidth + PreferencesConstants.COOKIE_DELIMITER + ePImage.previewHeight + ">");
  464. return this.mBitmap;
  465. }
  466. @WorkerThread
  467. private boolean saveImage(int i) {
  468. try {
  469. PhotoPreviewImageList.Element element = this.imageList.getElement(i);
  470. EPImage ePImage = element.getEPImage();
  471. if (ePImage == null) {
  472. return false;
  473. }
  474. ePImage.previewImageRectCenterX = this.trans.x;
  475. ePImage.previewImageRectCenterY = this.trans.y;
  476. this.mLayoutPosition.copyPreviewPrintAreaToEpImage(ePImage);
  477. RectF calcPreviewImageRect = calcPreviewImageRect(this.mBitmap, this.scaleFactor, this.trans);
  478. ePImage.previewImageRectLeft = calcPreviewImageRect.left;
  479. ePImage.previewImageRectTop = calcPreviewImageRect.top;
  480. ePImage.previewImageRectRight = calcPreviewImageRect.right;
  481. ePImage.previewImageRectBottom = calcPreviewImageRect.bottom;
  482. ePImage.scaleFactor = this.scaleFactor;
  483. this.imageList.set(i, ePImage);
  484. element.setFitMode(this.mFitMode);
  485. return true;
  486. } catch (IndexOutOfBoundsException unused) {
  487. return false;
  488. }
  489. }
  490. @WorkerThread
  491. private void restoreImage(@NonNull PhotoPreviewImageList.Element element) {
  492. EPImage ePImage = element.getEPImage();
  493. this.trans.x = ePImage.previewImageRectCenterX;
  494. this.trans.y = ePImage.previewImageRectCenterY;
  495. this.mLayoutPosition.copyPreviewPrintAreaFromEpImage(ePImage);
  496. this.scaleFactor = ePImage.scaleFactor;
  497. this.mFitMode = element.getFitMode();
  498. }
  499. public boolean onTouchEvent(MotionEvent motionEvent) {
  500. boolean z = this.mGestureDetector.onTouchEvent(motionEvent) || this.mScaleGestureDetector.onTouchEvent(motionEvent);
  501. if ((motionEvent.getAction() & 255) == 1) {
  502. onMoveEventEnd();
  503. }
  504. if (z || super.onTouchEvent(motionEvent)) {
  505. return true;
  506. }
  507. return false;
  508. }
  509. private void onMoveEventEnd() {
  510. invalidate();
  511. }
  512. private void onMoveEventStarted(float f, float f2) {
  513. this.maskExpandPreview = false;
  514. invalidate();
  515. }
  516. private void onMoving(float f, float f2) {
  517. this.mFitMode = 0;
  518. if (this.mBitmap != null) {
  519. Point point = this.trans;
  520. point.x = (int) ((-f) + ((float) point.x));
  521. Point point2 = this.trans;
  522. point2.y = (int) ((-f2) + ((float) point2.y));
  523. adjustTrans(this.trans);
  524. invalidate();
  525. }
  526. }
  527. private void adjustTrans(@NonNull Point point) {
  528. synchronized (this.lockObj) {
  529. if (this.mLayoutPosition != null) {
  530. RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  531. EPImage currentEpImage = getCurrentEpImage();
  532. if (currentEpImage != null) {
  533. adjustPosition(point, MOVEMENT_MARGIN_RATE, previewImageCircumscribedTargetSize, (double) (((float) currentEpImage.previewWidth) * this.scaleFactor), (double) (((float) currentEpImage.previewHeight) * this.scaleFactor));
  534. }
  535. }
  536. }
  537. }
  538. private void adjustPosition(@NonNull Point point, double d, RectF rectF, double d2, double d3) {
  539. Point point2 = point;
  540. RectF rectF2 = rectF;
  541. double d4 = ((double) (rectF2.right - rectF2.left)) * d;
  542. double d5 = ((double) (rectF2.bottom - rectF2.top)) * d;
  543. double d6 = d2 / 2.0d;
  544. if (((double) rectF2.left) + d4 > ((double) point2.x) + d6) {
  545. point2.x = (int) ((((double) rectF2.left) + d4) - d6);
  546. }
  547. double d7 = d3 / 2.0d;
  548. if (((double) rectF2.top) + d5 > ((double) point2.y) + d7) {
  549. point2.y = (int) ((((double) rectF2.top) + d5) - d7);
  550. }
  551. if (((double) rectF2.right) - d4 < ((double) point2.x) - d6) {
  552. point2.x = (int) ((((double) rectF2.right) - d4) + d6);
  553. }
  554. if (((double) rectF2.bottom) - d5 < ((double) point2.y) - d7) {
  555. point2.y = (int) ((((double) rectF2.bottom) - d5) + d7);
  556. }
  557. }
  558. public void onSizeChanged(int i, int i2, int i3, int i4) {
  559. super.onSizeChanged(i, i2, i3, i4);
  560. synchronized (this) {
  561. this.mViewSize[0] = i;
  562. this.mViewSize[1] = i2;
  563. }
  564. ViewSizeChangeListener viewSizeChangeListener = this.mViewSizeChangeListener;
  565. if (viewSizeChangeListener != null) {
  566. viewSizeChangeListener.onViewSizeChanged();
  567. }
  568. }
  569. public void onDraw(Canvas canvas) {
  570. super.onDraw(canvas);
  571. if (this.mDrawLock.tryLock()) {
  572. try {
  573. synchronized (this.lockObj) {
  574. if (!canvas.isHardwareAccelerated()) {
  575. if (this.maxBitmapWidth > 0 && this.maxBitmapHeight > 0) {
  576. this.maxBitmapWidth = -1;
  577. this.maxBitmapHeight = -1;
  578. }
  579. } else if (this.maxBitmapWidth < 0 || this.maxBitmapHeight < 0) {
  580. this.maxBitmapWidth = canvas.getMaximumBitmapWidth();
  581. this.maxBitmapHeight = canvas.getMaximumBitmapHeight();
  582. }
  583. RectF calcPreviewImageRect = this.mBitmap != null ? calcPreviewImageRect(this.mBitmap, this.scaleFactor, this.trans) : null;
  584. if (this.mLayoutPosition.getLayoutId() == 4) {
  585. drawCdPreviewAll(canvas, this.paint, this.mBitmap, this.mLayoutPosition, this.mBitmapDrawRect, this.maskExpandPreview, this.mPath, getResources().getColor(R.color.background_preview), calcPreviewImageRect);
  586. } else {
  587. drawPreview(canvas, this.paint, this.mBitmap, this.mLayoutPosition, this.mBitmapDrawRect, this.maskExpandPreview, getResources().getColor(R.color.background_preview), new int[]{getWidth(), getHeight()}, calcPreviewImageRect);
  588. }
  589. if (this.drawEndHandler != null) {
  590. Message message = new Message();
  591. message.what = 4;
  592. message.arg1 = this.imageNo;
  593. this.drawEndHandler.sendMessage(message);
  594. }
  595. }
  596. this.mDrawLock.unlock();
  597. } catch (Throwable th) {
  598. this.mDrawLock.unlock();
  599. throw th;
  600. }
  601. }
  602. }
  603. private void drawPreview(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, int i, int[] iArr, RectF rectF) {
  604. RectF rectF2;
  605. Canvas canvas2 = canvas;
  606. Paint paint3 = paint2;
  607. Bitmap bitmap2 = bitmap;
  608. Rect rect2 = rect;
  609. int layoutId = iLayoutPosition.getLayoutId();
  610. if (layoutId == 2) {
  611. rectF2 = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
  612. } else {
  613. rectF2 = new RectF(iLayoutPosition.getPreviewPaperRect());
  614. }
  615. paint2.setColor(-1);
  616. canvas.drawRect(rectF2, paint2);
  617. if (bitmap2 != null) {
  618. rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
  619. canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
  620. }
  621. drawBorderedOutsidePrintingArea(canvas, rectF2, layoutId, paint2, z, iLayoutPosition, i, iArr);
  622. }
  623. private static void drawBorderedOutsidePrintingArea(@NonNull Canvas canvas, @NonNull RectF rectF, int i, @NonNull Paint paint2, boolean z, @NonNull ILayoutPosition iLayoutPosition, int i2, @NonNull int[] iArr) {
  624. RectF rectF2 = rectF;
  625. int i3 = i;
  626. Paint paint3 = paint2;
  627. int i4 = i2;
  628. if (rectF2.left != 0.0f && rectF2.top != 0.0f && rectF2.right != 0.0f && rectF2.bottom != 0.0f) {
  629. paint3.setColor(i4);
  630. if (!z) {
  631. paint3.setAlpha(221);
  632. }
  633. int leftMargin = iLayoutPosition.getLeftMargin();
  634. int topMargin = iLayoutPosition.getTopMargin();
  635. int rightMargin = iLayoutPosition.getRightMargin();
  636. int bottomMargin = iLayoutPosition.getBottomMargin();
  637. float f = (float) leftMargin;
  638. Canvas canvas2 = canvas;
  639. Paint paint4 = paint2;
  640. canvas2.drawRect(0.0f, 0.0f, rectF2.left - f, (float) iArr[1], paint4);
  641. float f2 = (float) rightMargin;
  642. float f3 = (float) topMargin;
  643. canvas.drawRect(rectF2.left - f, 0.0f, rectF2.right + f2, rectF2.top - f3, paint4);
  644. canvas.drawRect(rectF2.right + f2, 0.0f, (float) iArr[0], (float) iArr[1], paint4);
  645. float f4 = (float) bottomMargin;
  646. canvas.drawRect(rectF2.left - f, rectF2.bottom + f4, rectF2.right + f2, (float) iArr[1], paint4);
  647. if (i3 == 2) {
  648. paint3.setColor(-1);
  649. } else if (i3 == 1) {
  650. paint3.setColor(i4);
  651. if (!z) {
  652. paint3.setAlpha(221);
  653. }
  654. }
  655. Paint paint5 = paint2;
  656. canvas.drawRect(rectF2.left - f, rectF2.top - f3, rectF2.left, rectF2.bottom + f4, paint5);
  657. canvas.drawRect(rectF2.left, rectF2.top - f3, rectF2.right, rectF2.top, paint5);
  658. canvas.drawRect(rectF2.right, rectF2.top - f3, rectF2.right + f2, rectF2.bottom + f4, paint5);
  659. canvas.drawRect(rectF2.left, rectF2.bottom, rectF2.right, rectF2.bottom + f4, paint2);
  660. }
  661. }
  662. private void drawCdPreviewAll(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, @NonNull Path path, int i, @NonNull RectF rectF) {
  663. float f;
  664. if (bitmap != null) {
  665. rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
  666. canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
  667. }
  668. RectF previewImageCircumscribedTargetSize = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
  669. paint2.setColor(i);
  670. if (!z) {
  671. paint2.setAlpha(221);
  672. }
  673. if (previewImageCircumscribedTargetSize.height() > previewImageCircumscribedTargetSize.width()) {
  674. f = previewImageCircumscribedTargetSize.width() / 2.0f;
  675. } else {
  676. f = previewImageCircumscribedTargetSize.height() / 2.0f;
  677. }
  678. path.reset();
  679. canvas.drawCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), (43.0f * f) / 116.0f, paint2);
  680. path.addCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), f, Path.Direction.CCW);
  681. canvas.clipPath(path, Region.Op.DIFFERENCE);
  682. canvas.drawPaint(paint2);
  683. }
  684. @WorkerThread
  685. @Deprecated
  686. @NonNull
  687. public RectF calcPreviewImageRect(@NonNull Bitmap bitmap, float f, @NonNull Point point) {
  688. return calcPreviewImageRect(bitmap.getWidth(), bitmap.getHeight(), f, point);
  689. }
  690. @WorkerThread
  691. @NonNull
  692. private RectF calcPreviewImageRect(int i, int i2, float f, @NonNull Point point) {
  693. RectF rectF = new RectF();
  694. float f2 = ((float) i) * f;
  695. rectF.left = ((float) point.x) - (f2 / 2.0f);
  696. float f3 = ((float) i2) * f;
  697. rectF.top = ((float) point.y) - (f3 / 2.0f);
  698. rectF.right = rectF.left + f2;
  699. rectF.bottom = rectF.top + f3;
  700. return rectF;
  701. }
  702. static class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
  703. private final PhotoPreview mPreviewView;
  704. public MyGestureListener(PhotoPreview photoPreview) {
  705. this.mPreviewView = photoPreview;
  706. }
  707. public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
  708. this.mPreviewView.onMoving(f, f2);
  709. return true;
  710. }
  711. public boolean onDown(MotionEvent motionEvent) {
  712. this.mPreviewView.onMoveEventStarted(motionEvent.getX(), motionEvent.getY());
  713. return true;
  714. }
  715. public boolean onDoubleTap(MotionEvent motionEvent) {
  716. this.mPreviewView.changeFitModeAndRefresh();
  717. return true;
  718. }
  719. }
  720. static class MyScaledGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  721. private final PhotoPreview mPreviewView;
  722. public MyScaledGestureListener(PhotoPreview photoPreview) {
  723. this.mPreviewView = photoPreview;
  724. }
  725. public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
  726. this.mPreviewView.startScaleChange();
  727. return super.onScaleBegin(scaleGestureDetector);
  728. }
  729. public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
  730. this.mPreviewView.changeScale(scaleGestureDetector.getScaleFactor());
  731. return super.onScale(scaleGestureDetector);
  732. }
  733. }
  734. }