package xyz.pinaki.android.camera;

import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/**
 * Created by pinaki on 3/6/17.
 */
// singleton manager class
public class CameraController {
    private static String TAG = CameraController.class.getSimpleName();
    private static class SingletonHolder {
        private static final CameraController INSTANCE = new CameraController();
    }
    private CameraController() {
    }

    public static CameraController getInstance() {
        return SingletonHolder.INSTANCE;
    }
    public void launch(AppCompatActivity activity, int containerID) {
        if (isCamera2Supported(activity)) {
            Log.i(TAG, "Camera2 Supported");
        } else {
            Log.i(TAG, "Camera2 NOT Supported");
        }
        activity.getSupportFragmentManager().beginTransaction().replace(
                containerID, CameraFragment.newInstance(), "CameraFragment").commit();
    }


    private static boolean isCamera2Supported(Context context) {
        if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ) {
            return false;
        }
        CameraManager cameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE);
        try {
            if (cameraManager.getCameraIdList().length == 0) {
                return false;
            }
            for (String cameraIdStr : cameraManager.getCameraIdList()) {
                CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraIdStr);
                Integer hardwareSupport = cameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
                if ( hardwareSupport == null ) {
                    return false;
                }
                if (hardwareSupport == CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY || hardwareSupport ==
                        CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED) {
                    return false;
                }
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        return true;
    }
}

@@ -61,16 +61,98 @@
 ontext;%0A
+import android.content.pm.ActivityInfo;%0Aimport android.content.res.Configuration;%0A
 import a
@@ -194,24 +194,24 @@
 sException;%0A
-
 import andro
@@ -615,24 +615,79 @@
 mpleName();%0A
+    private final boolean shouldFixOrientation = true;%0A
     private 
@@ -1118,16 +1118,16 @@
  else %7B%0A
-
         
@@ -1169,32 +1169,465 @@
 ed%22);%0A        %7D%0A
+        if (shouldFixOrientation) %7B%0A            int orientation = activity.getResources().getConfiguration().orientation;%0A            if (orientation == Configuration.ORIENTATION_PORTRAIT) %7B%0A                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);%0A            %7D%0A            else %7B%0A                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);%0A            %7D%0A        %7D%0A
         activity

fix orientation condiitonal -- based on initial orientation