package flickr.usth.edu.com.file.upload;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import flickr.usth.edu.com.R;


public class CameraActivity extends AppCompatActivity {

    // LogCat tag
    private static final String TAG = CameraActivity.class.getSimpleName();

    // Camera activity request codes
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    private Uri fileUri; // file url to store image/video

    private Button btnCapturePicture, btnRecordVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Changing action bar background color
        // These two lines are not needed
        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));

        btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
        btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);

        /**
         * Capture image button click event
         */
        btnCapturePicture.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // capture picture
                captureImage();
            }
        });

        /**
         * Record video button click event
         */
        btnRecordVideo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // record video
                recordVideo();
            }
        });

    }

    /**
     * Launching camera app to capture image
     */
    private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    /**
     * Launching camera app to record video
     */
    private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        // set video quality
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
        // name

        // start the video capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    }

    /**
     * Here we store the file url as it will be null after returning from camera app
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on screen orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }



    /**
     * Receiving activity result method will be called after closing the camera
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                // successfully captured the image
                // launching upload activity
                launchUploadActivity(true);


            } else if (resultCode == RESULT_CANCELED) {

                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();

            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }

        } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {

                // video successfully recorded
                // launching upload activity
                launchUploadActivity(false);

            } else if (resultCode == RESULT_CANCELED) {

                // user cancelled recording
                Toast.makeText(getApplicationContext(),
                        "User cancelled video recording", Toast.LENGTH_SHORT)
                        .show();

            } else {
                // failed to record video
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }
}

@@ -5627,10 +5627,1543 @@
 %7D%0A    %7D%0A
+%0A%0A    /**%0A     * ------------ Helper Methods ----------------------%0A     * */%0A%0A    /**%0A     * Creating file uri to store image/video%0A     */%0A    public Uri getOutputMediaFileUri(int type) %7B%0A        return Uri.fromFile(getOutputMediaFile(type));%0A    %7D%0A%0A    /**%0A     * returning image / video%0A     */%0A    private static File getOutputMediaFile(int type) %7B%0A%0A        // External sdcard location%0A        File mediaStorageDir = new File(%0A                Environment%0A                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),%0A                Config.IMAGE_DIRECTORY_NAME);%0A%0A        // Create the storage directory if it does not exist%0A        if (!mediaStorageDir.exists()) %7B%0A            if (!mediaStorageDir.mkdirs()) %7B%0A                Log.d(TAG, %22Oops! Failed create %22%0A                        + Config.IMAGE_DIRECTORY_NAME + %22 directory%22);%0A                return null;%0A            %7D%0A        %7D%0A%0A        // Create a media file name%0A        String timeStamp = new SimpleDateFormat(%22yyyyMMdd_HHmmss%22,%0A                Locale.getDefault()).format(new Date());%0A        File mediaFile;%0A        if (type == MEDIA_TYPE_IMAGE) %7B%0A            mediaFile = new File(mediaStorageDir.getPath() + File.separator%0A                    + %22IMG_%22 + timeStamp + %22.jpg%22);%0A        %7D else if (type == MEDIA_TYPE_VIDEO) %7B%0A            mediaFile = new File(mediaStorageDir.getPath() + File.separator%0A                    + %22VID_%22 + timeStamp + %22.mp4%22);%0A        %7D else %7B%0A            return null;%0A        %7D%0A%0A        return mediaFile;%0A    %7D%0A
 %7D%0A

Create method to get the media file