package ch.abertschi.flashonvisit.feedback;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

/**
 * Created by abertschi on 12.02.17.
 */
public class FlashFeedback implements Feedback {

    private static final String TAG = "FlashFeedback";

    public static final int DEFAULT_DURATION = 10;

    private Context context;

    private int duration = DEFAULT_DURATION;

    public FlashFeedback(Context context) {
        this.context = context;
    }

    public boolean isSupported() {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @Override
    public void feedback() {
        doFeedback(duration);
    }

    private void doFeedback(final int duration) {

        new AsyncTask<Void, String, Camera>() {
            @Override
            protected Camera doInBackground(Void... params) {
                final Camera camera = Camera.open();
                Camera.Parameters p = camera.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                return camera;
            }

            @Override
            protected void onPostExecute(final Camera camera) {
                try {
                    camera.startPreview();
                } catch (RuntimeException e) {
                    Log.e(TAG, "Error with flash feedback: " + e.getMessage());
                }

                new Handler().postDelayed(
                        () -> {
                            try {
                                camera.stopPreview();
                                camera.release();
                            } catch (RuntimeException e) {
                                Log.e(TAG, "Error with flash feedback: " + e.getMessage());
                            }
                        },
                        duration);
            }
        }.execute();
    }

    @Override
    public void exampleFeedback() {
        feedback();
    }

    public int getDuration() {
        return duration;
    }

    public FlashFeedback setDuration(int duration) {
        this.duration = duration;
        return this;
    }
}

@@ -1062,28 +1062,79 @@
         
-final 
 Camera
+ camera = null;%0A                try %7B%0A                   
  camera 
@@ -1146,24 +1146,28 @@
 era.open();%0A
+    
             
@@ -1232,16 +1232,20 @@
         
+ 
+   
 p.setFla
@@ -1296,32 +1296,36 @@
                 
+   
+ 
 camera.setParame
@@ -1353,123 +1353,429 @@
     
-return camera;%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(final Camera camera) %7B
+%7D catch (RuntimeException e) %7B%0A                    Log.e(TAG, %22Error with flash feedback: %22 + e.getMessage());%0A                %7D%0A                return camera;%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(final Camera camera) %7B%0A                if (camera == null) %7B%0A                    Log.e(TAG, %22Camera is null, not flashing Camera flash%22);%0A                    return;%0A                %7D
 %0A   

fix camera flash bug