package com.flurgle.camerakit;

import android.graphics.SurfaceTexture;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.View;

abstract class PreviewImpl {

    interface Callback {
        void onSurfaceChanged();
    }

    private Callback mCallback;

    private int mWidth;
    private int mHeight;

    protected int mTrueWidth;
    protected int mTrueHeight;

    void setCallback(Callback callback) {
        mCallback = callback;
    }

    abstract Surface getSurface();

    abstract View getView();

    abstract Class getOutputClass();

    abstract void setDisplayOrientation(int displayOrientation);

    abstract boolean isReady();

    protected void dispatchSurfaceChanged() {
        mCallback.onSurfaceChanged();
    }

    SurfaceHolder getSurfaceHolder() {
        return null;
    }

    SurfaceTexture getSurfaceTexture() {
        return null;
    }

    void setSize(int width, int height) {
        mWidth = width;
        mHeight = height;

        // Refresh true preview size to adjust scaling
        setTruePreviewSize(mTrueWidth, mTrueHeight);
    }

    int getWidth() {
        return mWidth;
    }

    int getHeight() {
        return mHeight;
    }

    void setTruePreviewSize(int width, int height) {
        this.mTrueWidth = width;
        this.mTrueHeight = height;

        if (width != 0 && height != 0) {
            AspectRatio aspectRatio = AspectRatio.of(width, height);
            int targetHeight = (int) (getView().getWidth() * aspectRatio.toFloat());
            float scaleY;
            if (getView().getHeight() > 0) {
                scaleY = (float) targetHeight / (float) getView().getHeight();
            } else {
                scaleY = 1;
            }

            if (scaleY > 1) {
                getView().setScaleX(1);
                getView().setScaleY(scaleY);
            } else {
                getView().setScaleX(1 / scaleY);
                getView().setScaleY(1);
            }
        }
    }

    int getTrueWidth() {
        return mTrueWidth;
    }

    int getTrueHeight() {
        return mTrueHeight;
    }

}

@@ -1241,24 +1241,30 @@
 PreviewSize(
+final 
 int width, i
@@ -1253,32 +1253,38 @@
 final int width,
+ final
  int height) %7B%0A 
@@ -1342,33 +1342,169 @@
 eight = height;%0A
-%0A
+        ((Activity) getView().getContext()).runOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         if (widt
@@ -1524,24 +1524,32 @@
 ght != 0) %7B%0A
+        
             
@@ -1613,24 +1613,32 @@
             
+        
 int targetHe
@@ -1710,16 +1710,24 @@
         
+        
 float sc
@@ -1740,24 +1740,32 @@
             
+  
+      
 if (getView(
@@ -1781,24 +1781,32 @@
 ht() %3E 0) %7B%0A
+        
             
@@ -1876,32 +1876,40 @@
 ();%0A            
+    
+    
 %7D else %7B%0A       
@@ -1893,32 +1893,40 @@
        %7D else %7B%0A
+        
                 
@@ -1945,27 +1945,43 @@
             
+      
+  
 %7D%0A%0A
+        
             
@@ -1994,24 +1994,32 @@
 aleY %3E 1) %7B%0A
+        
             
@@ -2054,32 +2054,40 @@
                 
+        
 getView().setSca
@@ -2103,32 +2103,40 @@
 Y);%0A            
+        
 %7D else %7B%0A       
@@ -2136,32 +2136,40 @@
                 
+  
+      
 getView().setSca
@@ -2193,32 +2193,40 @@
                 
+        
 getView().setSca
@@ -2229,24 +2229,64 @@
 tScaleY(1);%0A
+                    %7D%0A                %7D%0A
             
@@ -2292,24 +2292,26 @@
  %7D%0A        %7D
+);
 %0A    %7D%0A%0A    

Update PreviewImpl.java