package de.stephanlindauer.criticalmaps.handler;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import de.stephanlindauer.criticalmaps.Main;
import de.stephanlindauer.criticalmaps.R;
import de.stephanlindauer.criticalmaps.model.OwnLocationModel;
import de.stephanlindauer.criticalmaps.utils.ImageUtils;
import de.stephanlindauer.criticalmaps.vo.RequestCodes;
import de.stephanlindauer.criticalmaps.vo.ResultType;

import java.io.File;

public class StartCameraHandler extends AsyncTask<Void, Void, ResultType> {

    private Main activity;

    public StartCameraHandler(Main mainActivity) {
        this.activity = mainActivity;
    }

    @Override
    protected void onPreExecute() {
        if (OwnLocationModel.getInstance().ownLocation == null) {
            new AlertDialog.Builder(activity)
                    .setMessage(R.string.camera_no_location_no_camera)
                    .setPositiveButton(R.string.ok, null)
                    .show();
            cancel(true);
        }
        if (!activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            new AlertDialog.Builder(activity)
                    .setMessage(R.string.camera_no_camera_no_camera)
                    .setPositiveButton(R.string.ok, null)
                    .show();
            cancel(true);
        }
    }

    @Override
    protected ResultType doInBackground(Void... voids) {
        final File outputFile = ImageUtils.getNewOutputImageFile();
        activity.setNewCameraOutputFile(outputFile); // FIXME: 07.09.2015

        PackageManager packageManager = activity.getPackageManager();

        if (!packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            return ResultType.FAILED;
        }

        Uri imageCaptureUri = Uri.fromFile(outputFile);

        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageCaptureUri);
        activity.startActivityForResult(cameraIntent, RequestCodes.CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
        return ResultType.SUCCEEDED;
    }

    @Override
    protected void onPostExecute(ResultType resultType) {
        if (resultType == ResultType.FAILED) {
            new AlertDialog.Builder(activity)
                    .setMessage(R.string.camera_no_camera)
                    .setPositiveButton(R.string.ok, null)
                    .show();
        }
    }
}

@@ -398,16 +398,75 @@
 nModel;%0A
+import de.stephanlindauer.criticalmaps.utils.AlertBuilder;%0A
 import d
@@ -976,39 +976,33 @@
         
-new 
 Alert
-Dialog.
 Builder
+.show
 (activit
@@ -1006,42 +1006,39 @@
 vity
-)%0A                    .setMessage(
+,R.string.something_went_wrong,
 R.st
@@ -1075,120 +1075,8 @@
 era)
-%0A                    .setPositiveButton(R.string.ok, null)%0A                    .show();%0A            cancel(true)
 ;%0A  
@@ -1188,39 +1188,33 @@
         
-new 
 Alert
-Dialog.
 Builder
+.show
 (activit
@@ -1218,42 +1218,39 @@
 vity
-)%0A                    .setMessage(
+,R.string.something_went_wrong,
 R.st
@@ -1285,120 +1285,8 @@
 era)
-%0A                    .setPositiveButton(R.string.ok, null)%0A                    .show();%0A            cancel(true)
 ;%0A  
@@ -2217,31 +2217,25 @@
     
-new 
 Alert
-Dialog.
 Builder
+.show
 (act
@@ -2243,153 +2243,64 @@
 vity
-)%0A                    .setMessage(R.string.camera_no_camera)%0A                    .setPositiveButton(R.string.ok, null)%0A                    .show(
+,R.string.something_went_wrong,R.string.camera_no_camera
 );%0A 

Use AlertBuilder in StartCameraHandler to DRY code