Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
58 kB
5
Indexable
Never
package com.epson.mobilephone.creative.variety.collageprint.draw;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.net.Uri;
import android.os.AsyncTask;

import androidx.core.view.ViewCompat;

import com.epson.R;
import com.epson.mobilephone.creative.common.util.EPCommonUtil;
import com.epson.mobilephone.creative.main.EpApp;
import com.epson.mobilephone.creative.variety.collageprint.CollagePrint;
import com.epson.mobilephone.creative.variety.collageprint.data.CollagePageData;
import com.epson.mobilephone.creative.variety.collageprint.data.TYPE_POINT;
import com.epson.mobilephone.creative.variety.collageprint.data.TYPE_RECT;
import com.epson.mobilephone.creative.variety.collageprint.data.contentitem.CollageItemBackgroundOption;
import com.epson.mobilephone.creative.variety.collageprint.data.contentitem.CollageItemInfo;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.CollageLayoutData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutBackgroundData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutDiscLabelData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutDrawData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutImageData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutPhotoData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutStampData;
import com.epson.mobilephone.creative.variety.collageprint.data.layout.LayoutTextData;
import com.epson.mobilephone.creative.variety.collageprint.data.paper.CollagePaperData;
import com.epson.mobilephone.creative.variety.collageprint.data.paper.CollagePaperInfo;
import com.epson.mobilephone.creative.variety.collageprint.data.task.CollageTaskLoadContents;
import com.epson.mobilephone.creative.variety.collageprint.util.CollageCache;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class CollagePrintDrawEngine {
    private static final float ZOOM_LIMIT_MAX = 5.0f;
    private static final float ZOOM_LIMIT_MAX_LOW_SPEC_TEXT = 3.6f;
    private static final float ZOOM_LIMIT_MAX_STAMP = 4.0f;
    private static final float ZOOM_LIMIT_MIN = 0.5f;
    private static final float ZOOM_LIMIT_MIN_STAMP = 0.5f;
    private final float ZOOM_LIMIT_MAX_TEXT;
    private final float mAngleZoomDefX;
    private final float mAngleZoomDefY;
    private final int mAngleZoomS;
    private EFFECT_APF mApf;
    private final Bitmap mBitmapAngleZoom;
    private final CollageCache mCache;
    private final CANVAS_CLIP mCanvasClip;
    private final RectF mCanvasRect;
    private float mChangeScaleX;
    private float mChangeScaleY;
    private final RectF mClipBackgroundRect;
    private final Path mClipPath;
    private final CollagePrint mCollagePrint;
    private Rect mColorPatternRect;
    private final Context mContext;
    private final COORDINATE_ORIGIN mCoordinateOrigin;
    private final Paint mDebugPaint;
    private final float mDensity;
    private final Paint mDisclabelPaint;
    private final String mDocumentName;
    private final DrawBackground mDrawBackground;
    private DrawEngineCallback mDrawEngineCallback;
    private final ArrayList<DrawFrame> mDrawFrameList;
    private final DRAW_MODE mDrawMode;
    private final ArrayList<DrawObject> mDrawObjectList;
    private RectF mDrawObjectMovableRect;
    private final DRAW_PAPER mDrawPaper;
    private final DrawPaper mDrawPaperBL;
    private final DrawPaper mDrawPaperPA;
    private final DrawPaper mDrawPaperPS;
    private final ArrayList<DrawPhoto> mDrawPhotoList;
    private final DrawPhoto[] mDrawPhotoStackList;
    private CollageLayoutData mLayoutData;
    private Bitmap mLocationPhoto;
    private final float mMaximumTextureSize;
    private CollagePageData mPagedata;
    private CollagePaperData mPaperData;
    private final float mRadius;
    private float mScaleRate;
    private DrawFrame mSelectDrawFrame;
    private DrawObject mSelectDrawObject;
    private DrawPhoto mSelectDrawPhoto;
    private String mSelectedObjectId;
    private final Paint mTranslucentPaint;
    private final ArrayList<String> mUpdateIdList;
    private final Paint mViewMaskPaint;
    String DRAW_ENGINE_NAME = "AOKAGE";
    String DRAW_ENGINE_VERSION = "001.00.00";
    private final int TRANSLUCENT_COLOR = Color.argb(128, 255, 255, 255);


    public enum CANVAS_CLIP {
        CLIP_NON,
        CLIP_BL,
        CLIP_PS,
        CLIP_PA
    }


    public enum COORDINATE_ORIGIN {
        ORIGIN_CV,
        ORIGIN_BL,
        ORIGIN_PS,
        ORIGIN_PA
    }


    public enum DRAW_MODE {
        MODE_DRAW_ONLY,
        MODE_EDIT_AND_DRAW
    }


    public enum DRAW_PAPER {
        DARW_NON,
        DARW_BL,
        DARW_PS,
        DARW_PA
    }

    public interface DrawEngineCallback {
        void notifyDrawEngine(int i);
    }

    public enum EFFECT_APF {
        APF_OFF,
        APF_ON
    }

    public enum SELECT_STATE {
        STATE_NO_SELECT,
        STATE_PHOTO_AND_TOUCH_OUT_FRAME,
        STATE_PHOTO_AND_TOUCH_IN_FRAME
    }



    public CollagePrintDrawEngine(Context context, DRAW_MODE draw_mode, DRAW_PAPER draw_paper, CANVAS_CLIP canvas_clip, COORDINATE_ORIGIN coordinate_origin, CollagePageData collagePageData, String str, CollagePrint collagePrint, CollageCache collageCache) {
        this.mContext = context;
        this.mDrawMode = draw_mode;
        this.mDrawPaper = draw_paper;
        this.mCanvasClip = canvas_clip;
        this.mCoordinateOrigin = coordinate_origin;
        Resources resources = context.getResources();
        this.mDensity = resources.getDisplayMetrics().density;
        this.mPagedata = collagePageData;
        this.mDocumentName = str;
        this.mCollagePrint = collagePrint;
        this.mCache = collageCache;
        this.mApf = EFFECT_APF.APF_OFF;
        this.mCanvasRect = new RectF();
        this.mCanvasRect.setEmpty();
        this.mClipBackgroundRect = new RectF();
        this.mClipBackgroundRect.setEmpty();
        this.mDrawPaperBL = new DrawPaper(this.mDensity);
        this.mDrawPaperPS = new DrawPaper(this.mDensity);
        this.mDrawPaperPA = new DrawPaper(this.mDensity);
        this.mDrawBackground = new DrawBackground(this.mDensity, CollagePaperInfo.getBackgroundPatternPixel(this.mCollagePrint.getCurrentPaperSize()));
        this.mDrawFrameList = new ArrayList<>();
        this.mDrawPhotoList = new ArrayList<>();
        this.mDrawPhotoStackList = new DrawPhoto[12];
        this.mDrawObjectList = new ArrayList<>();
        this.mSelectDrawObject = null;
        this.mSelectedObjectId = "";
        this.mViewMaskPaint = new Paint();
        this.mViewMaskPaint.setStyle(Paint.Style.FILL);
        this.mViewMaskPaint.setColor(Color.argb(141, 0, 0, 0));
        this.mDebugPaint = new Paint();
        this.mDebugPaint.setStyle(Paint.Style.STROKE);
        this.mDebugPaint.setColor(0xffff0000);
        this.mTranslucentPaint = new Paint();
        this.mTranslucentPaint.setColor(this.TRANSLUCENT_COLOR);
        this.mTranslucentPaint.setStyle(Paint.Style.FILL);
        this.mTranslucentPaint.setAntiAlias(true);
        if (!this.mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
            this.mLocationPhoto = BitmapFactory.decodeResource(resources, R.drawable.location_photo);
        }
        this.mClipPath = new Path();
        this.mDisclabelPaint = new Paint();
        this.mDisclabelPaint.setStyle(Paint.Style.FILL);
        this.mDisclabelPaint.setColor(-16711681);
        this.mUpdateIdList = new ArrayList<>();
        this.mRadius = 16.0f * this.mDensity;
        Bitmap decodeResource = BitmapFactory.decodeResource(resources, R.drawable.photo_handle);
        this.mAngleZoomS = (int) (this.mRadius * 2.0f);
        if (decodeResource.getWidth() == this.mAngleZoomS && decodeResource.getHeight() == this.mAngleZoomS) {
            this.mBitmapAngleZoom = decodeResource;
        } else {
            this.mBitmapAngleZoom = Bitmap.createScaledBitmap(decodeResource, this.mAngleZoomS, this.mAngleZoomS, true);
            decodeResource.recycle();
        }
        this.mAngleZoomDefX = (-this.mBitmapAngleZoom.getWidth()) / 2.0f;
        this.mAngleZoomDefY = (-this.mBitmapAngleZoom.getHeight()) / 2.0f;
        this.mMaximumTextureSize = EPCommonUtil.getMaximumTextureSize();
        this.ZOOM_LIMIT_MAX_TEXT = (this.mMaximumTextureSize > 4096.0f || this.mPagedata.getPaperSizeId() != 62) ? ZOOM_LIMIT_MAX : ZOOM_LIMIT_MAX_LOW_SPEC_TEXT;
        setDrawElement(collagePageData, false);
    }

    public void setDrawLayoutCanvasSize(float f, float f2) {
        if (mLayoutData == null || mPaperData == null || !mCanvasRect.isEmpty()) {
            return;
        }
        mCanvasRect.set(0.0f, 0.0f, f, f2);
        initObjects();
    }

    public void drawContentsCanvas(Canvas canvas) {
        float outerPixel;
        if (mLayoutData == null || mPaperData == null) {
            return;
        }

        if (mCanvasRect.isEmpty()) {
            mCanvasRect.set(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight());
            initObjects();
        }

        if (mCollagePrint.isModeDiscLabel()) {
            mLayoutData.getDiscLabelData().getInnerPixel();
            outerPixel = mLayoutData.getDiscLabelData().getOuterPixel();
        } else {
            outerPixel = 0.0f;
        }

        if (outerPixel > 0.0f) {
            clipCircle(canvas, outerPixel, mDensity / 2.0f);
        } else if (mCanvasClip != CANVAS_CLIP.CLIP_NON) {
            clipCanvas(canvas, mCanvasClip);
        }

        drawContents(canvas);
    }

    private void drawFrameTranslucent(Canvas canvas, float f) {
        canvas.save();
        clipCircle(canvas, f, mDensity / 2.0f, true);
        TYPE_RECT paperScaleRect = mDrawPaperPS.getPaperScaleRect();
        canvas.drawRect(paperScaleRect.rl, paperScaleRect.rt, paperScaleRect.rr, paperScaleRect.rb, mTranslucentPaint);
        canvas.restore();
    }

    private void drawContents(Canvas canvas) {
        if (mDrawObjectList == null || mDrawObjectList.isEmpty()) {
            return;
        }
        for (DrawObject next : mDrawObjectList) {
            if (next instanceof DrawText) {
                ((DrawText) next).drawObject(canvas, false);
            } else if (next instanceof DrawStamp) {
                next.drawObject(canvas);
            } else if (next instanceof DrawImage) {
                ((DrawImage) next).drawObject(canvas, mApf == EFFECT_APF.APF_ON);
            }
        }
    }

    public void drawLayoutCanvas(Canvas canvas) {
        float innerPixel;
        float outerPixel;
        float width = canvas.getWidth();
        float height = canvas.getHeight();

        if (mLayoutData == null || mPaperData == null) {
            return;
        }

        if (mCanvasRect.isEmpty() || mCanvasRect.width() != width || mCanvasRect.height() != height) {
            mCanvasRect.set(0.0f, 0.0f, width, height);
            initObjects();
        }

        if (mCollagePrint.isModeDiscLabel()) {
            innerPixel = mLayoutData.getDiscLabelData().getInnerPixel();
            outerPixel = mLayoutData.getDiscLabelData().getOuterPixel();
        } else {
            outerPixel = 0.0f;
            innerPixel = 0.0f;
        }

        if (outerPixel > 0.0f) {
            if (mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
                clipCircleDoubleCircle(canvas, outerPixel, innerPixel, mDensity / 2.0f);
            }
        } else if (mCanvasClip != CANVAS_CLIP.CLIP_NON) {
            clipCanvas(canvas, mCanvasClip);
        }

        if (mDrawPaper != DRAW_PAPER.DARW_NON) {
            drawPaper(canvas, mDrawPaper);
        }

        drawBackgroundPattern(canvas, mClipBackgroundRect);

        if (innerPixel > 0.0f && mDrawMode.equals(DRAW_MODE.MODE_EDIT_AND_DRAW)) {
            drawCircle(canvas, innerPixel, -1, 0.0f, Paint.Style.FILL);
        }

        drawPhoto(canvas);

        if (!mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
            drawLayout(canvas);
            if (innerPixel > 0.0f) {
                TYPE_RECT paperScaleRect = mDrawPaperPS.getPaperScaleRect();
                canvas.clipRect(paperScaleRect.rl, paperScaleRect.rt, paperScaleRect.rr, paperScaleRect.rb);
                drawFrameTranslucent(canvas, outerPixel);
            }
        }

        DrawPhoto selectedPhoto = isSelectedPhoto();

        if (selectedPhoto == null) {
            drawBackgroundImage(canvas, mClipBackgroundRect);
        }

        if (outerPixel > 0.0f) {
            if (!mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
                drawCircle(canvas, outerPixel, ViewCompat.MEASURED_STATE_MASK, mDensity, Paint.Style.STROKE);
            }
            canvas.clipRect(mCanvasRect);
        }

        if (selectedPhoto != null) {
            drawSelectedPhoto(canvas, isBackgroundImage());

            if (outerPixel > 0.0f) {
                drawBackgroundImage(canvas, outerPixel);
            } else {
                drawBackgroundImage(canvas, mClipBackgroundRect);
            }

            if (isBackgroundImage()) {
                drawContents(canvas);
                drawSelectedPhotoLine(canvas, false);
            } else {
                drawSelectedPhoto(canvas, true);
                drawSelectedFrame(canvas);
                drawContents(canvas);
            }

            if (!isBackgroundImage()) {
                canvas.save();
                TYPE_RECT enableRect = mSelectDrawFrame.getEnableRect();
                canvas.clipRect(mCanvasRect);
                canvas.clipRect(enableRect.rl, enableRect.rt, enableRect.rr, enableRect.rb, Region.Op.DIFFERENCE);
                canvas.drawRect(mCanvasRect, mViewMaskPaint);
                canvas.restore();
            }

            if (outerPixel > 0.0f) {
                drawFrameTranslucent(canvas, outerPixel);
                if (!mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
                    drawCircle(canvas, outerPixel, ViewCompat.MEASURED_STATE_MASK, mDensity, Paint.Style.STROKE);
                }
            }
        }

        if (outerPixel > 0.0f && mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
            clipCircleDoubleCircle(canvas, outerPixel, innerPixel, mDensity / 2.0f);
        }

        if (selectedPhoto == null) {
            drawContents(canvas);
        }

        if (innerPixel > 0.0f) {
            drawCircle(canvas, innerPixel, TRANSLUCENT_COLOR, 0.0f, Paint.Style.FILL);
            if (!mDrawMode.equals(DRAW_MODE.MODE_DRAW_ONLY)) {
                drawCircle(canvas, innerPixel, ViewCompat.MEASURED_STATE_MASK, mDensity, Paint.Style.STROKE);
            }
        }

        if (selectedPhoto != null) {
            drawAngleZoomHandle(canvas);
        }
    }

    private void clipCircle(Canvas canvas, float radius, float offset) {
        clipCircle(canvas, radius, offset, false);
    }

    private void clipCircle(Canvas canvas, float radius, float offset, boolean useDifference) {
        float scaledRadius = ((radius * mScaleRate) / 2.0f) + offset;
        mClipPath.reset();
        mClipPath.addCircle(mDrawPaperBL.getPaperScaleRect().centerX(), mDrawPaperBL.getPaperScaleRect().centerY(), scaledRadius, Path.Direction.CCW);
        if (useDifference) {
            canvas.clipRect(mCanvasRect);
            canvas.clipPath(mClipPath, Region.Op.DIFFERENCE);
        } else {
            canvas.clipPath(mClipPath);
        }
    }

    private void clipCircleDoubleCircle(Canvas canvas, float outerRadius, float innerRadius, float offset) {
        clipCircle(canvas, outerRadius, offset, false);
        clipCircle(canvas, innerRadius, offset, true);
    }

    private void drawCircle(Canvas canvas, float radius, int color, float strokeWidth, Paint.Style style) {
        float scaledRadius = (radius * mScaleRate) / 2.0f;
        mDisclabelPaint.setColor(color);
        mDisclabelPaint.setStyle(style);
        mDisclabelPaint.setStrokeWidth(strokeWidth);
        mDisclabelPaint.setAntiAlias(true);
        canvas.drawCircle(mDrawPaperBL.getPaperScaleRect().centerX(), mDrawPaperBL.getPaperScaleRect().centerY(), scaledRadius, mDisclabelPaint);
    }

    public void setDrawElementverge(CollagePageData collagePageData) {
        setDrawElement(collagePageData);
        setupObjectsFramePhoto(mDrawPaperPS.getScaleOffset());
    }

    public void setDrawElement(CollagePageData collagePageData, boolean z) {
        setDrawElement(collagePageData);
        mDrawObjectList.clear();
        if (z) {
            initObjects();
        } else {
            mCanvasRect.setEmpty();
        }
    }

    public void setDrawElement(CollagePageData collagePageData) {
        mPagedata = collagePageData;

        // Clear and update the draw photo stack
        if (!mDrawPhotoList.isEmpty()) {
            for (int i = 0; i < mDrawPhotoList.size(); i++) {
                mDrawPhotoStackList[i] = null;
                DrawPhoto drawPhoto = getDrawPhoto(i + 1);
                if (drawPhoto != null) {
                    mDrawPhotoStackList[i] = drawPhoto;
                }
            }
        }

        // Clear existing draw frames and photos
        mDrawFrameList.clear();
        mDrawPhotoList.clear();

        // Update layout and paper data
        mLayoutData = collagePageData.getLayoutData();
        mPaperData = collagePageData.getPaperData();
        Bitmap bitmap = mCollagePrint.isModeDesignPaper() ? null : mLocationPhoto;

        // Add new draw frames and photos based on layout data
        for (LayoutPhotoData photoData : mLayoutData.getPhotoDataList()) {
            DrawFrame drawFrame = new DrawFrame(mDensity, photoData);
            mDrawFrameList.add(drawFrame);

            DrawPhoto drawPhoto = new DrawPhoto(mDensity, photoData, drawFrame, bitmap);
            drawPhoto.setAngleZoomHandle(mBitmapAngleZoom, mAngleZoomDefX, mAngleZoomDefY, mRadius);
            mDrawPhotoList.add(drawPhoto);
        }
    }

    public void setDrawObjectStamp(String str, String str2) {
        LayoutStampData layoutStampData = new LayoutStampData();
        layoutStampData.setStampNo(str);
        layoutStampData.setCategoryNo(str2);
        mLayoutData.getStampDataList().add(layoutStampData);
        mLayoutData.getDrawObjectIdList().add(layoutStampData.getId());
        mSelectedObjectId = layoutStampData.getId();
    }

    public void setDrawObjectImage(LayoutImageData layoutImageData) {
        mLayoutData.getImageDataList().add(layoutImageData);
        mLayoutData.getDrawObjectIdList().add(layoutImageData.getId());
        mSelectedObjectId = layoutImageData.getId();
    }

    public void updateDrawObjectImage(LayoutImageData layoutImageData, String str) {
        ArrayList<LayoutImageData> imageDataList = mLayoutData.getImageDataList();
        for (int i = 0; i < imageDataList.size(); i++) {
            LayoutImageData layoutImageData2 = imageDataList.get(i);
            if (layoutImageData2.getId().equalsIgnoreCase(str)) {
                mCache.remove(layoutImageData2.getCustomImageThumbnailName(), false);
                layoutImageData2.setCropEditAngle(layoutImageData.getCropEditAngle());
                layoutImageData2.setCropEditScale(layoutImageData.getCropEditScale());
                layoutImageData2.setCropEditMaskId(layoutImageData.getCropEditMaskId());
                layoutImageData2.setCropEditMove(layoutImageData.getCropEditMove());
                layoutImageData2.setCropEditFrameRate(layoutImageData.getCropEditFrameRate());
                mUpdateIdList.add(layoutImageData2.getId());
            }
        }
    }

    public void setDrawObjectText(String str, float f, int i, String str2, int i2) {
        LayoutTextData layoutTextData = new LayoutTextData();
        layoutTextData.setText(str);
        layoutTextData.setFontSize(f);
        layoutTextData.setTextColor(i);
        layoutTextData.setFontName(str2);
        layoutTextData.setTextAlignmentValue(i2);
        mLayoutData.getTextDataList().add(layoutTextData);
        mLayoutData.getDrawObjectIdList().add(layoutTextData.getId());
        mSelectedObjectId = layoutTextData.getId();
    }

    public void updateDrawObjectText(String str, float f, int i, String str2, int i2) {
        String str3 = "";
        DrawObject selectDrawObject = getSelectDrawObject();
        if (selectDrawObject != null && (selectDrawObject instanceof DrawText)) {
            str3 = selectDrawObject.getId();
            DrawText drawText = (DrawText) selectDrawObject;
            drawText.setText(str);
            drawText.setTextColor(i);
            drawText.setFontName(str2);
            drawText.setTextAlignmentValue(i2);
        }
        if (str3.isEmpty()) {
            return;
        }
        ArrayList<LayoutTextData> textDataList = mLayoutData.getTextDataList();
        for (int i3 = 0; i3 < textDataList.size(); i3++) {
            LayoutTextData layoutTextData = textDataList.get(i3);
            if (layoutTextData.getId().equalsIgnoreCase(str3)) {
                layoutTextData.setText(str);
                layoutTextData.setTextColor(i);
                layoutTextData.setFontName(str2);
                layoutTextData.setTextAlignmentValue(i2);
                mSelectedObjectId = layoutTextData.getId();
                return;
            }
        }
    }

    public void setDrawObjectPhoto(ArrayList<String> arrayList) {
        for (String next : arrayList) {
            LayoutPhotoData emptyLayoutPhotoData = getEmptyLayoutPhotoData();
            if (emptyLayoutPhotoData == null) {
                return;
            }
            emptyLayoutPhotoData.resetImageParam();
            emptyLayoutPhotoData.setImagePath(next);
            mCollagePrint.putSelectedFramePhotoPath(emptyLayoutPhotoData.clip_index, next);
        }
    }

    public void setDrawObjectPhotoUriList(ArrayList<String> arrayList, ArrayList<Uri> arrayList2) {
        String path = EpApp.getAppContext().getExternalFilesDir(null).getPath();
        String path2 = EpApp.getAppContext().getExternalCacheDir().getPath();
        for (int i = 0; i < arrayList2.size(); i++) {
            Uri uri = arrayList2.get(i);
            String str = arrayList.get(i);
            LayoutPhotoData emptyLayoutPhotoData = getEmptyLayoutPhotoData();
            if (emptyLayoutPhotoData == null) {
                return;
            }
            emptyLayoutPhotoData.resetImageParam();
            if (str.contains(path) || str.contains(path2)) {
                emptyLayoutPhotoData.setImagePath(str);
                mCollagePrint.putSelectedFramePhotoPath(emptyLayoutPhotoData.clip_index, str);
            }
            emptyLayoutPhotoData.setImageUri(uri);
            mCollagePrint.putSelectedFramePhotoUri(emptyLayoutPhotoData.clip_index, uri);
        }
    }

    public void setDrawObjectPhoto(String str, int i) {
        LayoutPhotoData layoutPhotoData = getLayoutPhotoData(i);
        if (layoutPhotoData != null) {
            layoutPhotoData.resetImageParam();
            layoutPhotoData.setImagePath(str);
            mCollagePrint.putSelectedFramePhotoPath(layoutPhotoData.clip_index, str);
        }
    }

    public void setDrawObjectPhoto(String str, Uri uri, int i) {
        String path = EpApp.getAppContext().getExternalFilesDir(null).getPath();
        String path2 = EpApp.getAppContext().getExternalCacheDir().getPath();
        LayoutPhotoData layoutPhotoData = getLayoutPhotoData(i);
        if (layoutPhotoData != null) {
            layoutPhotoData.resetImageParam();
            if (str.contains(path) || str.contains(path2)) {
                layoutPhotoData.setImagePath(str);
                mCollagePrint.putSelectedFramePhotoPath(layoutPhotoData.clip_index, str);
            }
            layoutPhotoData.setImageUri(uri);
            mCollagePrint.putSelectedFramePhotoUri(layoutPhotoData.clip_index, uri);
        }
    }

    private LayoutPhotoData getEmptyLayoutPhotoData() {
        for (LayoutPhotoData next : mLayoutData.getPhotoDataList()) {
            String imagePath = next.getImagePath();
            Uri imageUri = next.getImageUri();
            if (imagePath.isEmpty() && imageUri == null) {
                return next;
            }
        }
        return null;
    }

    public void setDrawObjectBackground(CollageItemInfo collageItemInfo, String str, String str2) {
        LayoutBackgroundData backgroundData = mLayoutData.getBackgroundData();
        if (collageItemInfo.isColor()) {
            backgroundData.setColor(collageItemInfo.getColor());
            backgroundData.setBackgroundFileName("");
            backgroundData.setTypeColor();
            backgroundData.setCategoryFolderName("");
        } else if (collageItemInfo.isCustomBackground()) {
            backgroundData.resetColor();
            backgroundData.setBackgroundPatternSize(str2);
            backgroundData.setBackgroundFileName(collageItemInfo.getName());
            backgroundData.setDownloadFolder(str);
            backgroundData.setCategoryFolderName(collageItemInfo.getKey());
            backgroundData.setTypeCustom();
        } else {
            backgroundData.resetColor();
            backgroundData.setBackgroundPatternSize(str2);
            backgroundData.setBackgroundFileName(collageItemInfo.getName());
            backgroundData.setDownloadFolder(str);
            backgroundData.setCategoryFolderName(collageItemInfo.getKey());
            backgroundData.setTypePattern();
        }
        backgroundData.setBackgroundOption(collageItemInfo.getItemOption());
        mDrawBackground.setBackgroundData(backgroundData);
    }

    public void setDrawObjectBackgroundOption(CollageItemBackgroundOption collageItemBackgroundOption) {
        LayoutBackgroundData backgroundData = mLayoutData.getBackgroundData();
        backgroundData.setBackgroundOption(collageItemBackgroundOption);
        mDrawBackground.setBackgroundData(backgroundData);
    }

    public void setDisclabelOption(float outer, float inner) {
        LayoutDiscLabelData discLabelData = mLayoutData.getDiscLabelData();
        if (discLabelData != null) {
            discLabelData.setInner(inner);
            discLabelData.setOuter(outer);
        }
    }

    public DrawObject containsSelectDrawObject(float x, float y) {
        if (mSelectDrawObject == null && mSelectDrawFrame == null) {
            for (DrawObject obj : mDrawObjectList) {
                if (obj.contains(x, y)) {
                    mSelectDrawObject = obj;
                    return obj;
                }
            }
        }
        return mSelectDrawObject;
    }

    public DrawFrame containsSelectDrawFrame(float x, float y) {
        if (mSelectDrawFrame == null && mSelectDrawObject == null) {
            for (DrawFrame frame : mDrawFrameList) {
                if (frame.contains(x, y)) {
                    mSelectDrawPhoto = getDrawPhoto(frame.getIndex());
                    mSelectDrawFrame = mSelectDrawPhoto != null ? frame : null;
                    return frame;
                }
            }
        }
        return null;
    }

    public DrawObject containsDrawObject(float x, float y) {
        for (DrawObject obj : mDrawObjectList) {
            if (obj.contains(x, y)) {
                return obj;
            }
        }
        return null;
    }

    public boolean containsObject(DrawObject drawObject, float f, float f2) {
        return drawObject.contains(f, f2);
    }

    public DrawFrame containsDrawFrame(float f, float f2) {
        for (DrawFrame next : mDrawFrameList) {
            if (next.contains(f, f2)) {
                return next;
            }
        }
        return null;
    }

    public DrawFrame getSelectDrawFrame() {
        return mSelectDrawFrame;
    }

    public DrawObject getSelectDrawObject() {
        return mSelectDrawObject;
    }

    public DrawPhoto getSelectDrawPhoto() {
        return mSelectDrawPhoto;
    }

    public DrawPhoto getDragDrawPhoto() {
        if (mSelectDrawPhoto != null) {
            return mSelectDrawPhoto.getDragDrawPhoto(mApf == EFFECT_APF.APF_ON);
        }
        return null;
    }

    public int getPhotoIndex(DrawFrame drawFrame) {
        for (DrawPhoto next : mDrawPhotoList) {
            if (drawFrame.getIndex() == next.getIndex() && next.isPhoto()) {
                return next.getIndex();
            }
        }
        return -1;
    }

    public void swapFramePhoto(int i, int i2) {
        mLayoutData.swapFramePhoto(i, getImageAspectRate(i), i2);
        mCollagePrint.swapSelectedFramePhotoPath(i, i2);
        mCollagePrint.swapSelectedFramePhotoUri(i, i2);
        for (DrawPhoto next : mDrawPhotoList) {
            next.mUserPhoto = null;
            next.mEffectPhoto = null;
        }
    }

    private float getImageAspectRate(int i) {
        Bitmap userPhoto;
        DrawPhoto drawPhoto = getDrawPhoto(i);
        if (drawPhoto == null || (userPhoto = drawPhoto.getUserPhoto()) == null) {
            return 0.0f;
        }
        float width = userPhoto.getWidth();
        float height = userPhoto.getHeight();
        if (width <= 0.0f || height <= 0.0f) {
            return 0.0f;
        }
        return height / width;
    }

    public void setSelectDrawObject(DrawObject drawObject) {
        mSelectDrawObject = drawObject;
    }

    public void setSelectDrawFrame(DrawFrame drawFrame) {
        mSelectDrawFrame = drawFrame;
    }

    public boolean isSelectedFrame() {
        return mSelectDrawFrame != null;
    }

    public DrawPhoto isSelectedPhoto() {
        return mSelectDrawPhoto;
    }

    public void updateStateDrawObject() {
        if (mSelectDrawObject != null) {
            for (DrawObject next : mDrawObjectList) {
                next.setSelect(mSelectDrawObject.getId().equals(next.getId()));
            }
            return;
        }
        for (DrawObject drawObject : mDrawObjectList) {
            drawObject.setSelect(false);
        }
    }

    public void updateStateDrawFrame() {
        if (mSelectDrawFrame != null) {
            for (DrawFrame next : mDrawFrameList) {
                next.setSelect(mSelectDrawFrame.getIndex() == next.getIndex());
            }
            return;
        }
        for (DrawFrame drawFrame : mDrawFrameList) {
            drawFrame.setSelect(false);
        }
    }

    private boolean isOverlapFrame(Region region, float f, float f2, float f3, float f4) {
        if (region == null || region.isEmpty()) {
            return false;
        }
        return region.op((int) f, (int) f2, (int) f3, (int) f4, Region.Op.INTERSECT);
    }

    private boolean isOverlapPaper(Region region) {
        if (region == null || region.isEmpty()) {
            return false;
        }
        return region.op((int) mDrawObjectMovableRect.left, (int) mDrawObjectMovableRect.top, (int) mDrawObjectMovableRect.right, (int) mDrawObjectMovableRect.bottom, Region.Op.INTERSECT);
    }

    public boolean moveDrawObject(float f, float f2) {
        if (mSelectDrawObject == null || !isOverlapPaper(mSelectDrawObject.getObjectRegionMove(f, f2))) {
            return false;
        }
        mSelectDrawObject.setMovedPoint(f, f2);
        return true;
    }

    public Bitmap getSelectDrawPhotoBitmap() {
        if (mSelectDrawPhoto != null) {
            return mSelectDrawPhoto.getBitmap(mApf == EFFECT_APF.APF_ON);
        }
        return null;
    }

    public boolean moveFramePhoto(float f, float f2) {
        if (mSelectDrawFrame != null) {
            mSelectDrawPhoto = getDrawPhoto(mSelectDrawFrame.getIndex());
            if (mSelectDrawPhoto != null) {
                Region objectRegionMove = mSelectDrawPhoto.getObjectRegionMove(f, f2);
                TYPE_RECT movableRect = mSelectDrawPhoto.getMovableRect();
                if (isOverlapFrame(objectRegionMove, movableRect.rl, movableRect.rt, movableRect.rr, movableRect.rb)) {
                    mSelectDrawPhoto.setMovedPoint(f, f2);
                    return true;
                }
            }
        }
        return false;
    }

    public DrawPhoto getDrawPhoto(int i) {
        for (DrawPhoto next : mDrawPhotoList) {
            if (next.getIndex() == i && next.getUserPhoto() != null) {
                return next;
            }
        }
        return null;
    }

    public float getScaleDrawObject() {
        if (mSelectDrawObject != null) {
            return mSelectDrawObject.getEditScale();
        }
        return 1.0f;
    }

    public boolean setScaleDrawObject(float f) {
        boolean z = false;
        if (mSelectDrawObject != null) {
            isOverlapPaper(mSelectDrawObject.getObjectRegionScaled(f));
        }
        return z;
    }

    public float getScaleFramePhoto() {
        if (mSelectDrawPhoto != null) {
            return mSelectDrawPhoto.getEditScale();
        }
        return 1.0f;
    }

    public boolean setScaleFramePhoto(float f) {
        if (mSelectDrawPhoto == null) {
            return false;
        }
        Region objectRegionScaled = mSelectDrawPhoto.getObjectRegionScaled(f);
        TYPE_RECT movableRect = mSelectDrawPhoto.getMovableRect();
        if (!isOverlapFrame(objectRegionScaled, movableRect.rl, movableRect.rt, movableRect.rr, movableRect.rb) || f < 0.5f || f > ZOOM_LIMIT_MAX) {
            return false;
        }
        mSelectDrawPhoto.setEditScale(f);
        return true;
    }

    public boolean getSelectObject() {
        return mSelectDrawObject != null || mSelectDrawFrame != null;
    }

    public float getAngleDrawObject() {
        return mSelectDrawObject.getEditAngle();
    }

    public boolean setAngleDrawObject(float f) {
        if (mSelectDrawObject == null || !isOverlapPaper(mSelectDrawObject.getObjectRegionAngle(f))) {
            return false;
        }
        float editAngle = mSelectDrawObject.getEditAngle() + f;
        if (editAngle < 0.0f) {
            editAngle += 360.0f;
        }
        if (editAngle > 360.0f) {
            editAngle -= 360.0f;
        }
        mSelectDrawObject.setEditAngle(editAngle);
        return true;
    }

    public void setEditAngleDrawObject(float f) {
        if (mSelectDrawObject != null) {
            setAngleDrawObject(f);
        }
    }

    public void setEditAngleFramePhoto(float f) {
        if (mSelectDrawPhoto != null) {
            setAngleFramePhoto(f);
        }
    }

    public float getAngleFramePhoto() {
        return mSelectDrawPhoto.getEditAngle();
    }

    public boolean setAngleFramePhoto(float f) {
        if (mSelectDrawPhoto != null) {
            Region objectRegionAngle = mSelectDrawPhoto.getObjectRegionAngle(f);
            TYPE_RECT movableRect = mSelectDrawPhoto.getMovableRect();
            if (isOverlapFrame(objectRegionAngle, movableRect.rl, movableRect.rt, movableRect.rr, movableRect.rb)) {
                float editAngle = mSelectDrawPhoto.getEditAngle() + f;
                if (editAngle < 0.0f) {
                    editAngle += 360.0f;
                }
                if (editAngle > 360.0f) {
                    editAngle -= 360.0f;
                }
                mSelectDrawPhoto.setEditAngle(editAngle);
                return true;
            }
        }
        return false;
    }

    public void removeSelectDrawObject() {
        if (mSelectDrawObject == null) {
            return;
        }

        String id = mSelectDrawObject.getId();
        DrawObject drawObjectToRemove = null;

        // Remove from mDrawObjectList
        Iterator<DrawObject> iterator = mDrawObjectList.iterator();
        while (iterator.hasNext()) {
            DrawObject drawObject = iterator.next();
            if (id.equalsIgnoreCase(drawObject.getId())) {
                drawObjectToRemove = drawObject;
                iterator.remove();
                break;
            }
        }

        if (drawObjectToRemove == null) {
            return;
        }

        // Remove from drawObjectIdList
        Iterator<String> idIterator = mLayoutData.getDrawObjectIdList().iterator();
        while (idIterator.hasNext()) {
            if (id.equalsIgnoreCase(idIterator.next())) {
                idIterator.remove();
                break;
            }
        }

        // Remove from specific data lists based on type
        if (drawObjectToRemove instanceof DrawText) {
            removeFromList(mLayoutData.getTextDataList(), id);
        } else if (drawObjectToRemove instanceof DrawStamp) {
            removeFromList(mLayoutData.getStampDataList(), id);
        } else if (drawObjectToRemove instanceof DrawImage) {
            removeFromList(mLayoutData.getImageDataList(), id);
        }

        mSelectDrawObject = null;
    }

    private <T extends LayoutDrawData> void removeFromList(ArrayList<T> list, String id) {
        Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            if (id.equalsIgnoreCase(iterator.next().getId())) {
                iterator.remove();
                return;
            }
        }
    }

    public void removeSelectDrawPhoto() {
        if (mSelectDrawPhoto != null) {
            removeFramePhoto(mSelectDrawPhoto);
            mSelectDrawPhoto = null;
        }
    }

    public void removeFramePhoto(DrawPhoto drawPhoto) {
        if (drawPhoto == null) {
            return;
        }

        int index = drawPhoto.getIndex();

        // Find and update the DrawPhoto in mDrawPhotoList
        for (DrawPhoto drawPhoto2 : mDrawPhotoList) {
            if (index == drawPhoto2.getIndex()) {
                drawPhoto2.setBitmap(null, null);
                resetAllObject();
                break;
            }
        }

        // Reset layout photo data
        LayoutPhotoData layoutPhotoData = getLayoutPhotoData(index);
        if (layoutPhotoData != null) {
            layoutPhotoData.resetImageParam();
        }

        // Remove selected photo paths and URIs
        mCollagePrint.removeSelectedFramePhotoPath(index);
        mCollagePrint.removeSelectedFramePhotoUri(index);
    }

    public void resetAllObject() {
        resetDrawObject();
        resetDrawFrame();
    }

    private void resetDrawObject() {
        for (DrawObject drawObject : mDrawObjectList) {
            drawObject.setSelect(false);
        }
        mSelectDrawObject = null;
    }

    private void resetDrawFrame() {
        for (DrawFrame drawFrame : mDrawFrameList) {
            drawFrame.setSelect(false);
        }
        mSelectDrawFrame = null;
        mSelectDrawPhoto = null;
    }

    public void resetSelectedPhoto() {
        mSelectDrawPhoto.resetEditParam();
    }

    private LayoutPhotoData getLayoutPhotoData(int i) {
        for (LayoutPhotoData next : mLayoutData.getPhotoDataList()) {
            if (next.clip_index == i) {
                return next;
            }
        }
        return null;
    }

    private LayoutTextData getLayoutTextData(String str) {
        for (LayoutTextData next : mLayoutData.getTextDataList()) {
            if (str.equals(next.getId())) {
                return next;
            }
        }
        return null;
    }

    private LayoutStampData getLayoutStampData(String str) {
        for (LayoutStampData next : mLayoutData.getStampDataList()) {
            if (str.equals(next.getId())) {
                return next;
            }
        }
        return null;
    }

    private LayoutImageData getLayoutImageData(String str) {
        for (LayoutImageData next : mLayoutData.getImageDataList()) {
            if (str.equals(next.getId())) {
                return next;
            }
        }
        return null;
    }

    private void initObjects() {
        String orientation = mLayoutData.getOrient();

        // Initialize paper rectangles
        mDrawPaperBL.setPaperRect(mPaperData.getPaperBL(orientation), mCanvasRect);
        mScaleRate = mDrawPaperBL.getScale();

        TYPE_POINT scaleOffset = mDrawPaperBL.getScaleOffset();
        mDrawPaperPS.setPaperRect(mPaperData.getPaperPS(orientation), mScaleRate, scaleOffset);
        mDrawPaperPA.setPaperRect(mPaperData.getPaperPA(orientation), mScaleRate, scaleOffset);

        // Calculate paper scale rectangle and movable rectangle
        TYPE_RECT paperScaleRect = mDrawPaperPS.getPaperScaleRect();
        float offsetX = paperScaleRect.width() * 0.05f;
        float offsetY = paperScaleRect.height() * 0.05f;
        mDrawObjectMovableRect = new RectF(
                paperScaleRect.rl + offsetX,
                paperScaleRect.rt + offsetY,
//                (float) Math.ceil(paperScaleRect.rr - r1),
//                (float) Math.ceil(paperScaleRect.rb - r3)
                (float) Math.ceil(paperScaleRect.rr - 10.0f),
                (float) Math.ceil(paperScaleRect.rb - 10.0f)
        );

        // Set the clip background rectangle based on the type of paper
        TYPE_RECT currentPaperScaleRect;
        switch (mDrawPaper) {
            case DARW_BL:
                currentPaperScaleRect = mDrawPaperBL.getPaperScaleRect();
                break;
            case DARW_PS:
                currentPaperScaleRect = mDrawPaperPS.getPaperScaleRect();
                break;
            default:
                currentPaperScaleRect = mDrawPaperPA.getPaperScaleRect();
                break;
        }
        mClipBackgroundRect.set(
                currentPaperScaleRect.rl,
                currentPaperScaleRect.rt,
                currentPaperScaleRect.rr,
                currentPaperScaleRect.rb
        );

        // Set background data
        mDrawBackground.setBackgroundData(mLayoutData.getBackgroundData(), mDrawPaperBL);

        setupObjects();
    }

    private void setupObjects() {
        TYPE_POINT scaleOffset = mDrawPaperPS.getScaleOffset();
        setupObjectsFramePhoto(scaleOffset);
        setupObjectsDrawObject(scaleOffset);
    }

    private void setupObjectsFramePhoto(TYPE_POINT type_point) {
        for (DrawFrame next : mDrawFrameList) {
            next.setScale(mScaleRate, type_point);
            next.setPaperScaleRect(mDrawPaperPS.getPaperScaleRect());
            next.setStrokeWidth(mDensity);
        }
        for (DrawPhoto next2 : mDrawPhotoList) {
            next2.setScale(mScaleRate, type_point);
            int index = next2.getIndex() - 1;
            DrawPhoto drawPhoto = mDrawPhotoStackList[index];
            if (drawPhoto != null) {
                next2.setBitmap(drawPhoto.mUserPhoto, drawPhoto.mEffectPhoto);
                LayoutPhotoData layoutPhotoData = getLayoutPhotoData(next2.getIndex());
                if (layoutPhotoData != null) {
                    next2.setEditAngle(layoutPhotoData.getImageAngle());
                    next2.setEditScale(layoutPhotoData.getImageScale());
                    next2.setEditMoveSV(layoutPhotoData.getImageOffset());
                }
                mDrawPhotoStackList[index] = null;
            }
        }
    }

    private void setupObjectsDrawObject(TYPE_POINT type_point) {
        LayoutImageData layoutImageData;
        for (DrawObject next : mDrawObjectList) {
            next.setScale(mScaleRate, type_point);
            if (next instanceof DrawText) {
                LayoutTextData layoutTextData = getLayoutTextData(next.getId());
                if (layoutTextData != null) {
                    next.setEditAngle(layoutTextData.getAngle());
                    next.setEditScale(layoutTextData.getScale());
                    next.setEditMoveSV(layoutTextData.getOffset());
                }
            } else if (next instanceof DrawStamp) {
                LayoutStampData layoutStampData = getLayoutStampData(next.getId());
                if (layoutStampData != null) {
                    next.setEditAngle(layoutStampData.getAngle());
                    next.setEditScale(layoutStampData.getScale());
                    next.setEditMoveSV(layoutStampData.getOffset());
                }
            } else if ((next instanceof DrawImage) && (layoutImageData = getLayoutImageData(next.getId())) != null) {
                next.setEditAngle(layoutImageData.getAngle());
                next.setEditScale(layoutImageData.getScale());
                next.setEditMoveSV(layoutImageData.getOffset());
            }
        }
    }

    public void setApfMode(boolean z) {
        mApf = z ? EFFECT_APF.APF_ON : EFFECT_APF.APF_OFF;
    }

    private void drawSelectedPhoto(Canvas canvas, boolean z) {
        mSelectDrawPhoto.draw(canvas, mApf == EFFECT_APF.APF_ON, z);
    }

    private void drawSelectedPhotoLine(Canvas canvas, boolean z) {
        mSelectDrawPhoto.drawLine(canvas, z);
    }

    private void drawSelectedFrame(Canvas canvas) {
        mSelectDrawFrame.draw(canvas);
    }

    private void drawAngleZoomHandle(Canvas canvas) {
        DrawPhoto isSelectedPhoto = isSelectedPhoto();
        if (isSelectedPhoto != null) {
            isSelectedPhoto.drawHandle(canvas);
        }
    }

    private void drawPaper(Canvas canvas, DRAW_PAPER draw_paper) {
        if (draw_paper == DRAW_PAPER.DARW_BL) {
            mDrawPaperBL.draw(canvas);
        } else if (draw_paper == DRAW_PAPER.DARW_PS) {
            mDrawPaperPS.draw(canvas);
        } else if (draw_paper == DRAW_PAPER.DARW_PA) {
            mDrawPaperPA.draw(canvas);
        }
    }

    private void clipCanvas(Canvas canvas, CANVAS_CLIP canvas_clip) {
        if (canvas_clip == CANVAS_CLIP.CLIP_BL) {
            mDrawPaperBL.clip(canvas);
        } else if (canvas_clip == CANVAS_CLIP.CLIP_PS) {
            mDrawPaperPS.clip(canvas);
        } else if (canvas_clip == CANVAS_CLIP.CLIP_PA) {
            mDrawPaperPA.clip(canvas);
        }
    }

    private boolean isBackgroundImage() {
        if (mDrawBackground != null) {
            return mDrawBackground.isBackgroundImaged();
        }
        return false;
    }

    private void drawBackgroundPattern(Canvas canvas, RectF rectF) {
        if (mDrawBackground == null || mDrawBackground.isBackgroundImaged()) {
            return;
        }
        canvas.save();
        if (!rectF.isEmpty()) {
            canvas.clipRect(rectF);
        }
        mDrawBackground.drawBackgroundObject(mContext, mCollagePrint, canvas);
        canvas.restore();
    }

    public Bitmap getBackgroundPatternBitmap(Bitmap bitmap) {
        return mDrawBackground.getBackgroundPatternBitmap(bitmap);
    }

    private void drawBackgroundImage(Canvas canvas, RectF rectF) {
        if (isBackgroundImage()) {
            canvas.save();
            if (!rectF.isEmpty()) {
                canvas.clipRect(rectF);
            }
            mDrawBackground.drawObject(canvas);
            canvas.restore();
        }
    }

    private void drawBackgroundImage(Canvas canvas, float f) {
        if (isBackgroundImage()) {
            canvas.save();
            mDrawBackground.drawObject(canvas);
            canvas.restore();
        }
    }

    private void drawLayout(Canvas canvas) {
        if (mCollagePrint.isModeDesignPaper() || mDrawFrameList == null) {
            return;
        }
        for (int i = 0; i < mDrawFrameList.size(); i++) {
            mDrawFrameList.get(i).draw(canvas);
        }
    }

    private void drawPhoto(Canvas canvas) {
        if (mDrawPhotoList != null) {
            for (int i = 0; i < mDrawPhotoList.size(); i++) {
                mDrawPhotoList.get(i).draw(canvas, mApf == EFFECT_APF.APF_ON, true);
            }
        }
    }

    public void loadContents(DrawEngineCallback drawEngineCallback) {
        mDrawEngineCallback = drawEngineCallback;
        new CollageTaskLoadContents(mContext, mPagedata, mDocumentName, mCollagePrint, true, mCache, getLoadContentsCallback()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }

    public void loadContents() {
        setContents(new CollageTaskLoadContents(mContext, mPagedata, mDocumentName, mCollagePrint, true, mCache, null).loadBitmapSyncMode());
    }

    private CollageTaskLoadContents.CollageTaskLoadContentsCallback getLoadContentsCallback() {
        return (i, hashMap) -> {
            setContents(hashMap);
            if (mDrawEngineCallback != null) {
                mDrawEngineCallback.notifyDrawEngine(i);
            }
        };
    }

    public void setContents(HashMap<String, Bitmap> hashMap) {
        if (hashMap != null) {
            TYPE_RECT paperScaleRect = mDrawPaperBL.getPaperScaleRect();
            TYPE_RECT paperScaleRect2 = mDrawPaperPS.getPaperScaleRect();
            LayoutBackgroundData backgroundData = mLayoutData.getBackgroundData();
            Bitmap mapBitmap = getMapBitmap(hashMap, backgroundData.getId());
            if (mapBitmap != null) {
                mDrawBackground.setId(backgroundData.getId());
                mDrawBackground.setBackgroundImaged(backgroundData.isBackgroundImage());
            }
            mDrawBackground.setBitmap(mapBitmap);

            for (LayoutPhotoData next : mLayoutData.getPhotoDataList()) {
                Bitmap mapBitmap2 = getMapBitmap(hashMap, next.getId() + "_STD");
                Bitmap mapBitmap3 = getMapBitmap(hashMap, next.getId() + "_APF");
                for (DrawPhoto next2 : mDrawPhotoList) {
                    if (next.clip_index == next2.getIndex()) {
                        if (mapBitmap2 != null) {
                            next2.setId(next.getId());
                            next2.setBitmap(mapBitmap2, mapBitmap3);
                            next2.setScale(mScaleRate);
                            next2.setEditAngle(next.getImageAngle());
                            next2.setEditScale(next.getImageScale());
                            next2.setEditMoveSV(next.getImageOffset());
                        }
                    }
                }
            }

            for (String next3 : mLayoutData.getDrawObjectIdList()) {
                if (getDrawObject(next3) == null) {
                    LayoutDrawData layoutDrawObject = mLayoutData.getLayoutDrawObject(next3);
                    if (layoutDrawObject instanceof LayoutTextData) {
                        DrawText drawText = new DrawText(mDensity, paperScaleRect, paperScaleRect2, mScaleRate, (LayoutTextData) layoutDrawObject);
                        drawText.setAngleZoomHandle(mBitmapAngleZoom, mAngleZoomDefX, mAngleZoomDefY, mRadius);
                        drawText.setId(next3);
                        mDrawObjectList.add(drawText);
                    } else if (layoutDrawObject instanceof LayoutStampData) {
                        Bitmap mapBitmap4 = getMapBitmap(hashMap, next3);
                        if (mapBitmap4 != null) {
                            DrawStamp drawStamp = new DrawStamp(mDensity, paperScaleRect, paperScaleRect2, mScaleRate, (LayoutStampData) layoutDrawObject, mapBitmap4);
                            drawStamp.setAngleZoomHandle(mBitmapAngleZoom, mAngleZoomDefX, mAngleZoomDefY, mRadius);
                            drawStamp.setId(next3);
                            mDrawObjectList.add(drawStamp);
                        }
                    } else if (layoutDrawObject instanceof LayoutImageData) {
                        LayoutImageData layoutImageData = (LayoutImageData) layoutDrawObject;
                        String customImageThumbnailName = layoutImageData.getCustomImageThumbnailName();
                        String customImageThumbnailNameApf = layoutImageData.getCustomImageThumbnailNameApf();
                        Bitmap mapBitmap5 = getMapBitmap(hashMap, customImageThumbnailName);
                        Bitmap mapBitmap6 = getMapBitmap(hashMap, customImageThumbnailNameApf);
                        if (mapBitmap5 != null) {
                            DrawImage drawImage = new DrawImage(mDensity, paperScaleRect, paperScaleRect2, mScaleRate, layoutImageData, mapBitmap5, mapBitmap6);
                            drawImage.setAngleZoomHandle(mBitmapAngleZoom, mAngleZoomDefX, mAngleZoomDefY, mRadius);
                            drawImage.setId(next3);
                            mDrawObjectList.add(drawImage);
                        }
                    }
                }
            }

            if (mUpdateIdList != null && !mUpdateIdList.isEmpty()) {
                for (String s : mUpdateIdList) {
                    DrawObject drawObject = getDrawObject(s);
                    if (drawObject != null && (drawObject instanceof DrawImage)) {
                        DrawImage drawImage2 = (DrawImage) drawObject;
                        String keyT = drawImage2.getKeyT();
                        String keyA = drawImage2.getKeyA();
                        Bitmap mapBitmap7 = getMapBitmap(hashMap, keyT);
                        Bitmap mapBitmap8 = getMapBitmap(hashMap, keyA);
                        drawImage2.setBitmap(mapBitmap7);
                        drawImage2.setBitmapApf(mapBitmap8);
                        break;
                    }
                }
                mUpdateIdList.clear();
            }

            DrawObject selectDrawObject = getSelectDrawObject();
            if (selectDrawObject != null) {
                mSelectDrawObject = selectDrawObject;
                mSelectDrawFrame = null;
                mSelectDrawPhoto = null;
                updateStateDrawObject();
                updateStateDrawFrame();
            }
        }
    }

    private Bitmap getMapBitmap(HashMap<String, Bitmap> hashMap, String str) {
        return hashMap.get(str);
    }

    private DrawObject getDrawObject(String str) {
        for (DrawObject next : mDrawObjectList) {
            if (next.getId().equals(str)) {
                return next;
            }
        }
        return null;
    }

    public void feedbackContents() {
        ArrayList<LayoutImageData> imageDataList;
        feedbackFramePhoto();
        if (mDrawObjectList == null || mDrawObjectList.isEmpty()) {
            return;
        }
        for (DrawObject next : mDrawObjectList) {
            String id = next.getId();
            float editAngle = next.getEditAngle();
            float editScale = next.getEditScale();
            float editEditMoveCV_x = next.getEditEditMoveCV_x();
            float editEditMoveCV_y = next.getEditEditMoveCV_y();
            if (next instanceof DrawText) {
                ArrayList<LayoutTextData> textDataList = mLayoutData.getTextDataList();
                if (textDataList != null && !textDataList.isEmpty()) {
                    Iterator<LayoutTextData> it2 = textDataList.iterator();
                    while (true) {
                        if (it2.hasNext()) {
                            LayoutTextData next2 = it2.next();
                            if (id.equals(next2.getId())) {
                                next2.setAngle(editAngle);
                                next2.setScale(editScale);
                                next2.setOffset(editEditMoveCV_x, editEditMoveCV_y);
                                break;
                            }
                        }
                    }
                }
            } else if (next instanceof DrawStamp) {
                ArrayList<LayoutStampData> stampDataList = mLayoutData.getStampDataList();
                if (stampDataList != null && !stampDataList.isEmpty()) {
                    Iterator<LayoutStampData> it3 = stampDataList.iterator();
                    while (true) {
                        if (it3.hasNext()) {
                            LayoutStampData next3 = it3.next();
                            if (id.equals(next3.getId())) {
                                next3.setAngle(editAngle);
                                next3.setScale(editScale);
                                next3.setOffset(editEditMoveCV_x, editEditMoveCV_y);
                                break;
                            }
                        }
                    }
                }
            } else if ((next instanceof DrawImage) && (imageDataList = mLayoutData.getImageDataList()) != null && !imageDataList.isEmpty()) {
                Iterator<LayoutImageData> it4 = imageDataList.iterator();
                while (true) {
                    if (it4.hasNext()) {
                        LayoutImageData next4 = it4.next();
                        if (id.equals(next4.getId())) {
                            next4.setAngle(editAngle);
                            next4.setScale(editScale);
                            next4.setOffset(editEditMoveCV_x, editEditMoveCV_y);
                            break;
                        }
                    }
                }
            }
        }
    }

    public void feedbackFramePhoto() {
        if (mDrawPhotoList == null || mDrawPhotoList.isEmpty()) {
            return;
        }

        ArrayList<LayoutPhotoData> photoDataList = mLayoutData.getPhotoDataList();
        if (photoDataList == null || photoDataList.isEmpty()) {
            return;
        }

        for (DrawPhoto drawPhoto : mDrawPhotoList) {
            String id = drawPhoto.getId();
            float editAngle = drawPhoto.getEditAngle();
            float editScale = drawPhoto.getEditScale();
            float editOffsetX = drawPhoto.getEditEditMoveCV_x();
            float editOffsetY = drawPhoto.getEditEditMoveCV_y();

            for (LayoutPhotoData layoutPhotoData : photoDataList) {
                if (id.equals(layoutPhotoData.getId())) {
                    layoutPhotoData.setImageAngle(editAngle);
                    layoutPhotoData.setImageScale(editScale);
                    layoutPhotoData.setImageOffset(editOffsetX, editOffsetY);
                    break; // Exit loop once the matching item is updated
                }
            }
        }
    }

    public ArrayList<DrawPhoto> getDrawPhotoList() {
        return mDrawPhotoList;
    }

    public ArrayList<DrawObject> getDrawObjectList() {
        return mDrawObjectList;
    }

    public DrawBackground getDrawBackground() {
        return mDrawBackground;
    }

    public void deviceConfigurationChangedOrientation(int i) {
        feedbackContents();
        mCanvasRect.setEmpty();
    }
}
Leave a Comment