<commit_before>/*
 * Copyright (c) 2014, 2015 Data4All
 * 
 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 * 
 * <p>Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.data4all.activity;

import io.github.data4all.R;
import io.github.data4all.handler.CapturePictureHandler;
import io.github.data4all.logger.Log;
import io.github.data4all.view.AutoFocusCrossHair;
import io.github.data4all.view.CameraPreview;
import io.github.data4all.view.CaptureAssistView;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * Activity for constructing the camera.
 * 
 * This activity is used to to create and handle the lifecycle. It produces the
 * layout. checks for the existence of a camera and generates the animations.
 * This activity stands in connection with the classes {@link CameraPreview
 * } and
 * {@link AutoFocusCrossHair}.
 * 
 * @author <NAME>
 * @CreationDate 09.02.2015
 * @LastUpdate 12.02.2015
 * @version 1.2
 * 
 */

public class CameraActivity extends Activity {

	// Logger Tag
	private static final String TAG = CameraActivity.class.getSimpleName();

	private Camera mCamera;
	private CameraPreview cameraPreview;
	private ImageButton btnCapture;
	private AutoFocusCrossHair mAutoFocusCrossHair;
	private CaptureAssistView mAssistView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate is called");
		super.onCreate(savedInstanceState);

		// remove title and status bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_camera);

		// Checking camera availability
		Log.d(TAG, "check if device support Camera");
		if (!this.isDeviceSupportCamera()) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.noCamSupported), Toast.LENGTH_LONG)
					.show();
			finish();
			Log.d(TAG, "device supports no camera");
			return;
		}

		// Set the capturing button
		btnCapture = (ImageButton) findViewById(R.id.btnCapture);
		btnCapture.setOnClickListener(btnCaptureOnClickListener);

		// Set the Focus animation
		mAutoFocusCrossHair = (AutoFocusCrossHair) findViewById(R.id.af_crosshair);

		mAssistView = (CaptureAssistView) findViewById(R.id.cameraAssistView);
		updateAssistView(0.5);
	}

	private OnClickListener btnCaptureOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mAutoFocusCrossHair.showStart();
			mAutoFocusCrossHair.doAnimation();

			mCamera.autoFocus(new AutoFocusCallback() {
				@Override
				public void onAutoFocus(boolean success, Camera camera) {

					if (success) {
						mCamera.takePicture(shutterCallback, null,
								new CapturePictureHandler(
										getApplicationContext()));
					} else {
						// TODO animate fail of focus
						mCamera.takePicture(shutterCallback, null,
								new CapturePictureHandler(
										getApplicationContext()));
					}
				}
			});
			// After photo is taken, disable button for clicking twice
			btnCapture.setOnClickListener(null);
			btnCapture.setClickable(false);
		}
	};

	/**
	 * This method is called when the position of the mobile phones has changed
	 * and the assist view must be adapted.
	 * 
	 * @param precentage
	 */
	public void updateAssistView(double precentage) {
		mAssistView.setInvalidRegion(precentage);
		mAssistView.invalidate();
	}

	@Override
	protected void onResume() {
		Log.i(TAG, "onResume is called");

		super.onResume();

		if (mCamera == null) {
			Log.d(TAG, "camera is null, so we have to recreate");
			this.createCamera();
			mAutoFocusCrossHair.clear();
		}

		btnCapture.setOnClickListener(btnCaptureOnClickListener);
		btnCapture.setClickable(true);

	}

	@Override
	protected void onPause() {
		Log.i(TAG, "onPause is called");
		super.onPause();

		if (mCamera != null) {
			Log.d(TAG, "camera is not null on pause, so release it");
			cameraPreview.setCamera(null);
			this.releaseCamera();
			mAutoFocusCrossHair.clear();
		}

		btnCapture.setOnClickListener(null);
		btnCapture.setClickable(false);

	}

	/**
	 * This method setup the camera.It calls a method from @CameraPreview and
	 * sets all camera parameters.
	 */
	private void createCamera() {
		Log.i(TAG, "createCamera is called");
<commit_msg>startService for deviceOrientation was deleted in the CameraActivity. is fixed<commit_after>/*
 * Copyright (c) 2014, 2015 Data4All
 * 
 * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * 
 * <p>http://www.apache.org/licenses/LICENSE-2.0
 * 
 * <p>Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.data4all.activity;

import io.github.data4all.R;
import io.github.data4all.handler.CapturePictureHandler;
import io.github.data4all.logger.Log;
import io.github.data4all.service.OrientationListener;
import io.github.data4all.view.AutoFocusCrossHair;
import io.github.data4all.view.CameraPreview;
import io.github.data4all.view.CaptureAssistView;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * Activity for constructing the camera.
 * 
 * This activity is used to to create and handle the lifecycle. It produces the
 * layout. checks for the existence of a camera and generates the animations.
 * This activity stands in connection with the classes {@link CameraPreview
 * } and
 * {@link AutoFocusCrossHair}.
 * 
 * @author <NAME>
 * @CreationDate 09.02.2015
 * @LastUpdate 12.02.2015
 * @version 1.2
 * 
 */

public class CameraActivity extends Activity {

	// Logger Tag
	private static final String TAG = CameraActivity.class.getSimpleName();

	private Camera mCamera;
	private CameraPreview cameraPreview;
	private ImageButton btnCapture;
	private AutoFocusCrossHair mAutoFocusCrossHair;
	private CaptureAssistView mAssistView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate is called");
		super.onCreate(savedInstanceState);

		// remove title and status bar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_camera);

		// Checking camera availability
		Log.d(TAG, "check if device support Camera");
		if (!this.isDeviceSupportCamera()) {
			Toast.makeText(getApplicationContext(),
					getString(R.string.noCamSupported), Toast.LENGTH_LONG)
					.show();
			finish();
			Log.d(TAG, "device supports no camera");
			return;
		}

		// Set the capturing button
		btnCapture = (ImageButton) findViewById(R.id.btnCapture);
		btnCapture.setOnClickListener(btnCaptureOnClickListener);

		// Set the Focus animation
		mAutoFocusCrossHair = (AutoFocusCrossHair) findViewById(R.id.af_crosshair);

		mAssistView = (CaptureAssistView) findViewById(R.id.cameraAssistView);
		updateAssistView(0.5);
	}

	private OnClickListener btnCaptureOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			mAutoFocusCrossHair.showStart();
			mAutoFocusCrossHair.doAnimation();

			mCamera.autoFocus(new AutoFocusCallback() {
				@Override
				public void onAutoFocus(boolean success, Camera camera) {

					if (success) {
						mCamera.takePicture(shutterCallback, null,
								new CapturePictureHandler(
										getApplicationContext()));
					} else {
						// TODO animate fail of focus
						mCamera.takePicture(shutterCallback, null,
								new CapturePictureHandler(
										getApplicationContext()));
					}
				}
			});
			// After photo is taken, disable button for clicking twice
			btnCapture.setOnClickListener(null);
			btnCapture.setClickable(false);
		}
	};

	/**
	 * This method is called when the position of the mobile phones has changed
	 * and the assist view must be adapted.
	 * 
	 * @param precentage
	 */
	public void updateAssistView(double precentage) {
		mAssistView.setInvalidRegion(precentage);
		mAssistView.invalidate();
	}

	@Override
	protected void onResume() {
		Log.i(TAG, "onResume is called");

		super.onResume();

		if (mCamera == null) {
			Log.d(TAG, "camera is null, so we have to recreate");
			this.createCamera();
			mAutoFocusCrossHair.clear();
		}
		
		// Start the Device Orientation listener
		startService(new Intent(this, OrientationListener.class));

		btnCapture.setOnClickListener(btnCaptureOnClickListener);
		btnCapture.setClickable(true);

	}

	@Override
	protected void onPause() {
		Log.i(TAG, "onPause is called");
		super.onPause();

		if (mCamera != null) {
			Log.d(TAG, "camera is not null on pause, so release it");
			cameraPreview.setCamera(null);
			this.releaseCamera();
			mAutoFocusCrossHair.clear();
		}
		
		stopService(new Intent(this, OrientationListener.class));

		btnCapture.setOnClickListener(null);
		btnCapture.setClickable(false);

	}

	/**
	 * This method setup the camera.It calls a method from @CameraPreview and
	 * sets all camera parameters.
	 */
	private void createCamera() {
		Log.i(TAG, "createCamera is called");

{'fraction_non_alphanumeric': 0.09842041312272175, 'fraction_numerical': 0.007570801009440135, 'mean_word_length': 4.3526763381690845, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '136687', 'n_tokens_mistral': 3540, 'n_tokens_neox': 3049, 'n_words': 856}