<commit_before>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Drawing;
using Android.Graphics;
using Android.Content.PM;
using Android.Hardware;

namespace ZXing.Mobile
{
	// based on https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Graphics/CameraPreview.cs
	public class ZXingSurfaceView : SurfaceView, ISurfaceHolderCallback, Android.Hardware.Camera.IPreviewCallback, Android.Hardware.Camera.IAutoFocusCallback
	{
		private const int MIN_FRAME_WIDTH = 240;
		private const int MIN_FRAME_HEIGHT = 240;
		private const int MAX_FRAME_WIDTH = 600;
		private const int MAX_FRAME_HEIGHT = 400;
	
		System.Threading.CancellationTokenSource tokenSource;
		ISurfaceHolder surface_holder;
		Android.Hardware.Camera camera;
		//Android.Hardware.Camera.CameraInfo cameraInfo;
		MobileBarcodeScanningOptions options;
		Action<ZXing.Result> callback;
		Activity activity;

		public ZXingSurfaceView (Activity activity, MobileBarcodeScanningOptions options, Action<ZXing.Result> callback)
			: base (activity)
		{
			this.activity = activity;
			this.callback = callback;
			this.options = options;

			lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

			this.surface_holder = Holder;
			this.surface_holder.AddCallback (this);
			this.surface_holder.SetType (SurfaceType.PushBuffers);
			
			this.tokenSource = new System.Threading.CancellationTokenSource();
		}

	    protected ZXingSurfaceView(IntPtr javaReference, JniHandleOwnership transfer) 
            : base(javaReference, transfer) 
        {
            lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

            this.surface_holder = Holder;
            this.surface_holder.AddCallback(this);
            this.surface_holder.SetType(SurfaceType.PushBuffers);

            this.tokenSource = new System.Threading.CancellationTokenSource();
	    }

	    public void SurfaceCreated (ISurfaceHolder holder)
		{
			try 
			{
				var version = Android.OS.Build.VERSION.SdkInt;

				if (version >= BuildVersionCodes.Gingerbread)
				{
					var numCameras = Android.Hardware.Camera.NumberOfCameras;
					var camInfo = new Android.Hardware.Camera.CameraInfo();
					var found = false;
					
					for (int i = 0; i < numCameras; i++)
					{
						Android.Hardware.Camera.GetCameraInfo(i, camInfo);
						if (camInfo.Facing == CameraFacing.Back)
						{
							camera = Android.Hardware.Camera.Open(i);
							found = true;
							break;
						}
					}
					
					if (!found)
					{
						Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding rear camera failed, opening camera 0...");
						camera = Android.Hardware.Camera.Open(0);
					}
				}
				else
				{
					camera = Android.Hardware.Camera.Open();
				}
				if (camera == null)
					Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");
				
				
				//camera = Android.Hardware.Camera.Open ();
				camera.SetPreviewDisplay (holder);
				camera.SetPreviewCallback (this);
				
			} catch (Exception ex) {
				ShutdownCamera ();
				
				// TODO: log or otherwise handle this exception
				Console.WriteLine("Setup Error: " + ex);
				//throw;
			}
		}
		
		public void SurfaceChanged (ISurfaceHolder holder, global::Android.Graphics.Format format, int w, int h)
		{
			if (camera == null)
				return;
			
			var parameters = camera.GetParameters ();
			parameters.PreviewFormat = ImageFormatType.Nv21;
			
			camera.SetParameters (parameters);
<commit_msg>Added Google Glass workaround for the camera on Android<commit_after>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Drawing;
using Android.Graphics;
using Android.Content.PM;
using Android.Hardware;

namespace ZXing.Mobile
{
	// based on https://github.com/xamarin/monodroid-samples/blob/master/ApiDemo/Graphics/CameraPreview.cs
	public class ZXingSurfaceView : SurfaceView, ISurfaceHolderCallback, Android.Hardware.Camera.IPreviewCallback, Android.Hardware.Camera.IAutoFocusCallback
	{
		private const int MIN_FRAME_WIDTH = 240;
		private const int MIN_FRAME_HEIGHT = 240;
		private const int MAX_FRAME_WIDTH = 600;
		private const int MAX_FRAME_HEIGHT = 400;
	
		System.Threading.CancellationTokenSource tokenSource;
		ISurfaceHolder surface_holder;
		Android.Hardware.Camera camera;
		//Android.Hardware.Camera.CameraInfo cameraInfo;
		MobileBarcodeScanningOptions options;
		Action<ZXing.Result> callback;
		Activity activity;

		public ZXingSurfaceView (Activity activity, MobileBarcodeScanningOptions options, Action<ZXing.Result> callback)
			: base (activity)
		{
			this.activity = activity;
			this.callback = callback;
			this.options = options;

			lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

			this.surface_holder = Holder;
			this.surface_holder.AddCallback (this);
			this.surface_holder.SetType (SurfaceType.PushBuffers);
			
			this.tokenSource = new System.Threading.CancellationTokenSource();
		}

	    protected ZXingSurfaceView(IntPtr javaReference, JniHandleOwnership transfer) 
            : base(javaReference, transfer) 
        {
            lastPreviewAnalysis = DateTime.Now.AddMilliseconds(options.InitialDelayBeforeAnalyzingFrames);

            this.surface_holder = Holder;
            this.surface_holder.AddCallback(this);
            this.surface_holder.SetType(SurfaceType.PushBuffers);

            this.tokenSource = new System.Threading.CancellationTokenSource();
	    }

	    public void SurfaceCreated (ISurfaceHolder holder)
		{
			try 
			{
				var version = Android.OS.Build.VERSION.SdkInt;

				if (version >= BuildVersionCodes.Gingerbread)
				{
					var numCameras = Android.Hardware.Camera.NumberOfCameras;
					var camInfo = new Android.Hardware.Camera.CameraInfo();
					var found = false;
					
					for (int i = 0; i < numCameras; i++)
					{
						Android.Hardware.Camera.GetCameraInfo(i, camInfo);
						if (camInfo.Facing == CameraFacing.Back)
						{
							camera = Android.Hardware.Camera.Open(i);
							found = true;
							break;
						}
					}
					
					if (!found)
					{
						Android.Util.Log.Debug("ZXing.Net.Mobile", "Finding rear camera failed, opening camera 0...");
						camera = Android.Hardware.Camera.Open(0);
					}
				}
				else
				{
					camera = Android.Hardware.Camera.Open();
				}
				if (camera == null)
					Android.Util.Log.Debug("ZXing.Net.Mobile", "Camera is null :(");
				
				
				//camera = Android.Hardware.Camera.Open ();
				camera.SetPreviewDisplay (holder);
				camera.SetPreviewCallback (this);
				
			} catch (Exception ex) {
				ShutdownCamera ();
				
				// TODO: log or otherwise handle this exception
				Console.WriteLine("Setup Error: " + ex);
				//throw;
			}
		}
		
		public void SurfaceChanged (ISurfaceHolder holder, global::Android.Graphics.Format format, int w, int h)
		{
			if (camera == null)
				return;
			
			var parameters = camera.GetParameters ();
			parameters.PreviewFormat = ImageFormatType.Nv21;
			// Google Glass requires this fix to display the camera output correctly
			if (Build.Model.Contains ("Glass")) {
				parameters.SetPreviewFpsRange (30000, 30000);
				parameters.SetPreviewSize (640, 360);
			}
			camera.SetParameters (parameters);

{'fraction_non_alphanumeric': 0.09912767644726407, 'fraction_numerical': 0.006608511763150938, 'mean_word_length': 3.376518218623482, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '1209071', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2176, 'n_words': 472}