package pt.fraunhofer.pulse.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Switch;
import org.opencv.android.MyCameraBridgeViewBase;
import pt.fraunhofer.pulse.App;
import pt.fraunhofer.pulse.Pulse;
import pt.fraunhofer.pulse.R;

public class ConfigDialog extends DialogFragment {

    private MyCameraBridgeViewBase camera;
    private Pulse pulse;

    private Switch faceDetectionSwitch;
    private Switch magnificationSwitch;
    private SeekBar magnificationSeekBar;
    private Switch fpsSwitch;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.config);
        builder.setNeutralButton(R.string.done, null);

        View dialogView = getActivity().getLayoutInflater().inflate(R.layout.config, null);
        builder.setView(dialogView);

        faceDetectionSwitch = ((Switch)dialogView.findViewById(R.id.face_detection));
        faceDetectionSwitch.setOnCheckedChangeListener(new FaceDetectionSwitchConfig());

        magnificationSwitch = ((Switch)dialogView.findViewById(R.id.magnification));
        magnificationSwitch.setOnCheckedChangeListener(new MagnificationSwitchConfig());

        magnificationSeekBar = ((SeekBar)dialogView.findViewById(R.id.magnificationFactor));
        magnificationSeekBar.setOnSeekBarChangeListener(new MagnificationSeekBarConfig());

        fpsSwitch = ((Switch)dialogView.findViewById(R.id.fps));
        fpsSwitch.setOnCheckedChangeListener(new FpsSwitchConfig());

        faceDetectionSwitch.setChecked(pulse.hasFaceDetection());
        magnificationSwitch.setChecked(pulse.hasMagnification());
        magnificationSeekBar.setEnabled(pulse.hasMagnification());
        magnificationSeekBar.setProgress(pulse.getMagnificationFactor());
        fpsSwitch.setChecked(camera.isFpsMeterEnabled());

        return builder.create();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        App app = (App)activity;
        camera = app.getCamera();
        pulse = app.getPulse();
    }

    private class FaceDetectionSwitchConfig implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            pulse.setFaceDetection(isChecked);
        }
    }

    private class MagnificationSwitchConfig implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            magnificationSeekBar.setEnabled(isChecked);
            pulse.setMagnification(isChecked);
        }
    }

    private class MagnificationSeekBarConfig implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            pulse.setMagnificationFactor(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
    }

    private class FpsSwitchConfig implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            camera.setFpsMeter(isChecked);
        }
    }
}

@@ -308,58 +308,8 @@
 ch;%0A
-import org.opencv.android.MyCameraBridgeViewBase;%0A
 impo
@@ -457,77 +457,8 @@
  %7B%0A%0A
-    private MyCameraBridgeViewBase camera;%0A    private Pulse pulse;%0A%0A
     
@@ -1652,32 +1652,173 @@
 itchConfig());%0A%0A
+        Pulse pulse = getApp().getPulse();%0A        // on configuration change Pulse may not have loaded yet%0A        if (pulse != null) %7B%0A    
         faceDete
@@ -1867,32 +1867,36 @@
 ion());%0A        
+    
 magnificationSwi
@@ -1937,32 +1937,36 @@
 ion());%0A        
+  
+  
 magnificationSee
@@ -2008,32 +2008,36 @@
 ion());%0A        
+   
+ 
 magnificationSee
@@ -2082,24 +2082,34 @@
 nFactor());%0A
+        %7D%0A
         fpsS
@@ -2125,22 +2125,36 @@
 Checked(
-c
+getApp().getC
 amera
+()
 .isFpsMe
@@ -2217,193 +2217,62 @@
     
-@Override%0A    public void onAttach(Activity activity) %7B%0A        super.onAttach(activity);%0A        App app =
+private App getApp() %7B%0A        return
  (App)
-a
+getA
 ctivity
-;%0A        camera = app.getCamera();%0A        pulse = app.getPulse
 ();%0A
@@ -2485,29 +2485,76 @@
             
-pulse
+if (getApp().getPulse() != null) getApp().getPulse()
 .setFaceDete
@@ -2847,37 +2847,84 @@
 d);%0A            
-pulse
+if (getApp().getPulse() != null) getApp().getPulse()
 .setMagnificatio
@@ -3163,21 +3163,68 @@
         
-pulse
+if (getApp().getPulse() != null) getApp().getPulse()
 .setMagn
@@ -3649,14 +3649,28 @@
     
-c
+getApp().getC
 amera
+()
 .set

Support orientation change in ConfigDialog