PhotoPreview.java 34 KB

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