package com.example.distmeasapp;

import graphview.GraphView;
import graphview.GraphViewSeries;
import graphview.LineGraphView;
import graphview.GraphView.GraphViewData;
import graphview.GraphViewSeries.GraphViewSeriesStyle;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class LongMeas extends Activity implements SensorEventListener, OnSeekBarChangeListener {

	private static final int M_CAMERA = 0;
	private static final int M_SHORTMEAS = 1;
	private static final int M_LONGMEAS = 2;
	private static final int M_HELP = 3;
	private static final int M_EXIT = 4;
	private boolean magn_rdy = false, beep = false;
    private int bufferSize = 50000, bufferSmall = bufferSize/10, del = 0, nof = 100, sens = 1, state = 0, step = 0, steps = 0;
	private float []magn = new float[3];
	private float []accel = new float[3];
	private float []result = new float[3];
	private float []gravity = new float[3];
	private double []T = new double[bufferSize];
	private double []Xg = new double[bufferSize];
	private double []Yg = new double[bufferSize];
	private double []Zg = new double[bufferSize];
	private double []A = new double[bufferSize];
	private double []maxA = new double[bufferSmall];
	private double []maxR0 = new double[bufferSmall];
	private double []maxR1 = new double[bufferSmall];
	private double []maxR2 = new double[bufferSmall];
	private double []maxT = new double[bufferSmall];
	private double []posunR0 = new double[bufferSmall];
	private double []posunR1 = new double[bufferSmall];
	private double []posunR2 = new double[bufferSmall];
	private double []graph2x = new double[bufferSmall];
	private double []graph2y = new double[bufferSmall];
	private double []tempX = new double[nof];
	private double []tempY = new double[nof];
	private double []tempZ = new double[nof];
	private double []array = new double[7];
	private double delay, fix, lastMin, speed, threshold, threshold2, thrCoef = 0.75f, los; // fix - ustaleni v kapse, delay - sledovani zakmitu
	private float alpha = 0.8f, beta = 0.25f; // konstanta pro low-pass filter
	private long multiply = 1000000; // nasobek pro ustaleni jednotky casu (ms)
	private long res;
	private String string, string1 = "", string2 = "", textSens;
	private SensorManager manager;
	private Sensor grav;
	private GraphViewSeries graph1x, graph1h, graph1t1, graph1t2, graph2;
	private GraphView graphView1, graphView2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.long_meas);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		final SeekBar fixBar = (SeekBar) findViewById(R.id.seekBar1);
		fixBar.setOnSeekBarChangeListener(this);
		final SeekBar thresholdBar = (SeekBar) findViewById(R.id.seekBar2);
		thresholdBar.setOnSeekBarChangeListener(this);
		final SeekBar nofBar = (SeekBar) findViewById(R.id.seekBar3);
		nofBar.setOnSeekBarChangeListener(this);
		final SeekBar losBar = (SeekBar) findViewById(R.id.seekBar4);
		losBar.setOnSeekBarChangeListener(this);
		final SeekBar delayBar = (SeekBar) findViewById(R.id.seekBar5);
		delayBar.setOnSeekBarChangeListener(this);
		final SeekBar sensBar = (SeekBar) findViewById(R.id.seekBar6);
		sensBar.setOnSeekBarChangeListener(this);

		final Button textChange = (Button) findViewById(R.id.button1);
		textChange.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				if (state == 0) {
					state = 1; // zapnuti mereni
					if(del == 0 || del == 3) {
						res = System.nanoTime();
						T[0] = 0;
					}
					fix = fixBar.getProgress();
					nof = nofBar.getProgress() + 1;
					los = losBar.getProgress() / 100.0;
					delay = delayBar.getProgress() / 100.0;
					sens = sensBar.getProgress();
					threshold = thresholdBar.getProgress() / 100.0;
					maxA[0] = 0;
					maxT[0] = 0;
					del = 2; // v prubehu mereni nelze mazat
					textChange.setText("Pozastavit");
				} else {
					state = 0; // pozastaveni mereni
					del = 1;
					textChange.setText("Spustit");
				}
			}
		});

		final Button resetSet = (Button) findViewById(R.id.button3);
		resetSet.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(del!=2) {
					CheckBox save1 = (CheckBox) findViewById(R.id.checkBox3);
					CheckBox save2 = (CheckBox) findViewById(R.id.checkBox4);
					CheckBox choice = (CheckBox) findViewById(R.id.checkBox2);
					save1.setChecked(true);
					save2.setChecked(false);
					choice.setChecked(true);
					fixBar.setProgress(3);
					delayBar.setProgress(20);
					thresholdBar.setProgress(25);
					if (sens > 1) thresholdBar.setProgress(250);
					nofBar.setProgress(1);
					losBar.setProgress(65);
					sensBar.setProgress(1);
				} //else Toast.makeText(this,"V průběhu měření nelze resetovat nastavení.",Toast.LENGTH_SHORT).show();
			}
		});

		// inicializace dat pro oba grafy
		graph1x = new GraphViewSeries("X", new GraphViewSeriesStyle(Color.rgb(255, 0, 0),1), new GraphViewData[]{new GraphViewData(1,0)});
		graph1h = new GraphViewSeries("H", new GraphViewSeriesStyle(Color.rgb(0, 0, 255),1), new GraphViewData[]{new GraphViewData(1,0)});
		graph1t1 = new GraphViewSeries("T1", new GraphViewSeriesStyle(Color.rgb(0, 0, 0),1), new GraphViewData[]{new GraphViewData(1,0)});
		graph1t2 = new GraphViewSeries("T2", new GraphViewSeriesStyle(Color.rgb(0, 0, 0),1), new GraphViewData[]{new GraphViewData(1,0)});
		graph2 = new GraphViewSeries("2", new GraphViewSeriesStyle(Color.rgb(255, 0, 0),1), new GraphViewData[]{new GraphViewData(1,0)});
				
		graphView1 = new LineGraphView(this, "");
		graphView2 = new LineGraphView(this, "");
		
		graphView1.addSeries(graph1x);
		graphView1.addSeries(graph1h);
		graphView1.addSeries(graph1t1);
		graphView1.addSeries(graph1t2);
		graphView1.setViewPort(2 , 20);
		graphView1.setScrollable(true);
		graphView1.setScalable(true);
		LinearLayout layout1 = (LinearLayout) findViewById(R.id.tab2);  
		layout1.addView(graphView1);  

		graphView2.addSeries(graph2);
		graphView2.setViewPort(2 , 40);
		graphView2.setScrollable(true);
		graphView2.setScalable(true);
		LinearLayout layout2 = (LinearLayout) findViewById(R.id.tab3);  
		layout2.addView(graphView2); 
		
		// definice zalozek pro TabView
		TabHost tabHost = (TabHost)findViewById(R.id.tabhost);
		tabHost.setup();

		TabSpec spec1=tabHost.newTabSpec("Tab 1");
		spec1.setContent(R.id.tab1);
		spec1.setIndicator("Výpis");
		tabHost.addTab(spec1);

		TabSpec spec2=tabHost.newTabSpec("Tab 2");
		spec2.setContent(R.id.tab2);
		spec2.setIndicator("Diagnostika");
		tabHost.addTab(spec2);
		
		TabSpec spec3=tabHost.newTabSpec("Tab 3");
		spec3.setContent(R.id.tab3);
		spec3.setIndicator("Monitoring");
		tabHost.addTab(spec3);

		TabSpec spec4=tabHost.newTabSpec("Tab 4");
		spec4.setContent(R.id.tab4);
		spec4.setIndicator("Nastavení");
		tabHost.addTab(spec4);
		
		// zmena vysky pro hlavicku zalozky
		tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 30;
		tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 30;
		tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 30;
		tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 30;
		
		manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		grav = manager.getDefaultSensor(Sensor.TYPE_GRAVITY);

	    manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),manager.SENSOR_DELAY_NORMAL);
	    manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),manager.SENSOR_DELAY_NORMAL);
	    manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_GRAVITY),manager.SENSOR_DELAY_NORMAL);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, M_CAMERA,0, R.string.menu0);
		menu.add(0, M_SHORTMEAS,0, R.string.menu1);
		menu.add(0, M_LONGMEAS,0, R.string.menu2);
		menu.add(0, M_HELP,0, R.string.menu3);
		menu.add(0, M_EXIT,0, R.string.menu4);
		return true;
	}

	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	switch(item.getItemId()) {
        case M_CAMERA:
        	startActivity(new Intent(this, CameraMeas.class));
        	return true;
        case M_SHORTMEAS:
        	startActivity(new Intent(this, ShortMeas.class));
        	return true;
        case M_LONGMEAS:
        	startActivity(new Intent(this, LongMeas.class));
        	return true;
        case M_HELP:
        	startActivity(new Intent(this, Help.class));
        	return true;
        case M_EXIT:
        	finish();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
	    }
    }
	
	public void btnClicked(View view) { // rutina pro resetovaci tlacitko
		CheckBox save1 = (CheckBox) findViewById(R.id.checkBox3);
		CheckBox save2 = (CheckBox) findViewById(R.id.checkBox4);
		CheckBox choice = (CheckBox) findViewById(R.id.checkBox2);
		String textChoice = (choice.isChecked())?"minimy.":"maximy.";
		EditText edit = (EditText) findViewById(R.id.editText1);
		String note = "";
		
		// editovatelnost poznamky k mereni
		try {
			  note = edit.getText().toString();
			} catch (NumberFormatException e) {
			  // pokud se nepodari nasadit uzivatelem zadavanou hodnotu
				edit.setText("Poznámky k měření...");
			}
		
		switch (del) {
		case 0:
			Toast.makeText(this,"Není třeba resetovat před prvním.",Toast.LENGTH_SHORT).show();
			break;
		case 1:
			double dist = ((steps * los)*10000)/10000.0;
			if(save1.isChecked() || save2.isChecked()) {
				string = "* Konfigurace měření *" +
						"\r\nUstálení před měřením: " + fix + " s" +
						"\r\nMinimální práh citlivosti: " + threshold + " m/s^2" +
						"\r\nPočet low-pass filtrů: " + (nof - 1) +
						"\r\nDélka kroku: " + los + " m"  +
						"\r\nCitlivost senzoru: " + textSens +
						"\r\nPracováno je s " + textChoice +
						"\r\n\r\n* Výsledky *" +
						"\r\nProvedeno celkem kroků: " + steps +
						"\r\nDélka měření: " + String.format("\t%.3f s",T[step]) +
						"\r\nDélka trasy: " + String.format("\t%.2f m",dist) +
						"\r\nPrůměrná rychlost pohybu: " + String.format("\t%.4f m/s (%.4f km/hod)",speed, 3.6 * speed) +
						"\r\n\r\nPoznámky k měření:\r\n" + note + "\r\n";
				if (save1.isChecked()) string += string1;
				if (save2.isChecked()) string += string2;
				getOutputExport(string);
			}
			
			step = 0;
			steps = 0;
			res = 0;
			del = 3; // po smazani nelze znova mazat
			beep = false;
			threshold = 0.0;
			lastMin = 0.0;
			string1 = "";
			string2 = "";
			Arrays.fill(T, 0);
			Arrays.fill(Xg, 0);
			Arrays.fill(Yg, 0);
			Arrays.fill(Zg, 0);
			Arrays.fill(A, 0);
			Arrays.fill(maxA, 0);
			Arrays.fill(maxT, 0);
			Arrays.fill(maxR0, 0);
			Arrays.fill(maxR1, 0);
			Arrays.fill(maxR2, 0);
			Arrays.fill(posunR0, 0);
			Arrays.fill(posunR1, 0);
			Arrays.fill(posunR2, 0);
			Arrays.fill(graph2x, 0);
			Arrays.fill(graph2y, 0);
			GraphViewData[] tt = new GraphViewData[1];
			tt[0] = new GraphViewData(0,0);
			graph1x.resetData(tt);
			graph1h.resetData(tt);
			graph1t1.resetData(tt);
			graph1t2.resetData(tt);
			graph2.resetData(tt);
			graphView1.setViewPort(2 , 20);
			graphView2.setViewPort(2 , 40);
			Toast.makeText(this,"Hodnoty zresetovány.",Toast.LENGTH_SHORT).show();
			break;
		case 2:
			Toast.makeText(this,"Nejprve je nutné zastavit měření.",Toast.LENGTH_SHORT).show();
			break;
		case 3:
			Toast.makeText(this,"Není třeba resetovat podruhé.",Toast.LENGTH_SHORT).show();
			break;
		}
	}
	
	public void playSound() {
		try {
			Uri notification = Uri.parse("android.resource://com.example.distmeasapp/"+R.raw.beep5);
	        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
	        r.play();
	    } catch (Exception e) {}
	}
	
	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		TextView view = (TextView) findViewById(R.id.textView1);
		CheckBox save1 = (CheckBox) findViewById(R.id.checkBox3);
		CheckBox save2 = (CheckBox) findViewById(R.id.checkBox4);
		CheckBox choice = (CheckBox) findViewById(R.id.checkBox2);
		String s = "", s2 = "";

		if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
			// kod uprednostnovany pro verze OS Android v2.3 a vyssi
			gravity = event.values.clone();
		}
		
		if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
			magn = event.values.clone();
			magn_rdy = true;
		}
		
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			double cas = ((System.nanoTime() - res) / multiply) / 1000.0f;
			accel = event.values.clone();			
			
			if (grav == null) {
				gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
				gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
				gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
			}
			if (state >= 1) {
				if (magn != null && magn_rdy) {
					float[] rot = new float[16];
			        float[] axis = new float[16];
			        SensorManager.getRotationMatrix(rot, axis, accel, magn);
			        SensorManager.getOrientation(rot, result);
				}

				step = step + 1;
				if (step<bufferSize) {
					T[step] =  cas;
					tempX[0] = accel[0] - gravity[0];
					tempY[0] = accel[1] - gravity[1];
					tempZ[0] = accel[2] - gravity[2];
					
					// realizace nof-1 lowpassfilteru
					for(int i=1;i<nof;i++) { //tempX[i] je minuly krok
						tempX[i] = beta * tempX[i] + (1 - beta) * tempX[i-1];
						tempY[i] = beta * tempY[i] + (1 - beta) * tempY[i-1];
						tempZ[i] = beta * tempZ[i] + (1 - beta) * tempZ[i-1];
					}
					Xg[step] = tempX[nof-1];
					Yg[step] = tempY[nof-1];
					Zg[step] = tempZ[nof-1];
				
					A[step] = Math.abs(Xg[step])+Math.abs(Yg[step])+Math.abs(Zg[step]);
					
					if (cas >= fix && !beep) {
						beep = true;
						playSound();
						maxR0[0] = result[0];
						maxR1[0] = result[1];
						maxR2[0] = result[2];
						graph2x[0] = 0.0;
						graph2y[0] = 0.0;
						maxT[0] = T[step];
						if (choice.isChecked()) lastMin = 0.0;
						else					lastMin = 10000.0;
						string1 = "\r\n\r\nsteps\tT\tA\tthr2\trotX\trotY\trotZ\tposunRotX\tposunRotY\tposunRotZ\tsouradniceX\tsouradniceY\r\n";
						string2 = "\r\n\r\nstep\tT\tA\tXg\tYg\tZg\r\n";
					}
					if(cas >= fix && step>=2)
					{
						if (choice.isChecked()) { // prace s minimy
							threshold2 = lastMin * thrCoef;  // lastmin musi klesnout min o 1-threshold2, jinak nebude krok zapocitan
							array[0] = A[step - 1];
							array[1] = A[step - 2];
							array[2] = A[step];
							array[3] = A[step - 1];
							if (A[step-1] > lastMin)	lastMin = A[step-1];
							array[4] = A[step-1];
							array[5] = threshold2;
							if (sens < 2)	array[6] = A[step -1];
							else 			array[6] = lastMin;
							
						} else { // prace s maximy
							threshold2 = lastMin / thrCoef;  // lastmin musi klesnout min o 1-threshold2, jinak nebude krok zapocitan
							array[0] = A[step - 2];
							array[1] = A[step - 1];
							array[2] = A[step - 1];
							array[3] = A[step];
							if (A[step-1] < lastMin)	lastMin = A[step-1];
							array[4] = threshold2;
							array[5] = A[step-1];
							array[6] = A[step-1];
						}
						
						if (array[0] < array[1] && array[2] > array[3] && array[6] >= threshold && array[4] < array[5]) {
							if((T[step-1] - maxT[steps]) > delay) {
								if (choice.isChecked()) lastMin = 0.0;
								else					lastMin = 10000.0;
								steps += 1;
								maxA[steps] = A[step-1];
								maxT[steps] = T[step-1];
								maxR0[steps] = result[0];
								maxR1[steps] = result[1];
								maxR2[steps] = result[2];
								posunR0[steps] = maxR0[steps] - maxR0[0];   
								posunR1[steps] = maxR1[steps] - maxR1[0];   
								posunR2[steps] = maxR2[steps] - maxR2[0];
								
								graph2x[steps] = graph2x[steps-1] + los*Math.cos(posunR0[steps]);
								graph2y[steps] = graph2y[steps-1] - los*Math.sin(posunR0[steps]);
								graph2.appendData2(new GraphViewData (graph2x[steps], graph2y[steps]));
								graph1h.appendData(new GraphViewData (maxT[steps], maxA[steps]), true);
								graph1t1.appendData(new GraphViewData (maxT[steps], threshold), true);
								graph1t2.appendData(new GraphViewData (maxT[steps], threshold2), true);
								
								string1 += 	steps + String.format("\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\r\n",
											maxT[steps], maxA[steps], threshold2, maxR0[steps], maxR1[steps], maxR2[steps],
											posunR0[steps], posunR1[steps], posunR2[steps], graph2x[steps], graph2y[steps]);
							}
						}
					}
					if(cas >= fix) { // pokud neubehly fix (na ustaleni), nevykresluje
						graph1x.appendData(new GraphViewData (T[step], A[step]), true);
						string2 += step + String.format("\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\r\n", T[step], A[step], Xg[step], Yg[step], Zg[step]);
					}

					if(choice.isChecked())	{s2 = "\nLokální minimum: " + maxA[steps] + " m/s^2\nPoslední maximum:" + lastMin;}
					else					{s2 = "\nLokální maximum: " + maxA[steps] + " m/s^2\nPoslední minimum:" + lastMin;}

					speed = (steps * los) / T[step];
					s ="* Průběh *" +
					"\nKrokování aplikace:" + step +
					"\nAkcelerace: " + A[step] + " m/s^2" + s2 +
					"\nČas měření: " + T[step] + " s" + 
					"\nThreshold2: " + threshold2 + " m/s^2" +
					"\nProvedeno kroků: " + steps +
					"\nDélka trasy: " + String.format("%.2f", steps * los) + " m" +
					"\nPrůměrná rychlost: " + String.format("%.4f m/s (%.4f km/hod)", speed, (speed*3.6)) +
					"\n\n* Konfigurace *" +
					"\nFix: " + fix + " s" +
					"\nThreshold1: " + threshold + " m/s^2" +
					"\nKoef. pro threshold2: " + thrCoef + " m/s^2" +
					"\nPočet low-pass filtrů:" + (nof - 1);
				}
				else {
					s = "Krok: " + step + "\nBuffer byl přeplněn.";
					state = 0;
				}

				view.setText(s);
			} else {
			    Xg[0] = event.values[0] - gravity[0];
				Yg[0] = event.values[1] - gravity[1];
				Zg[0] = event.values[2] - gravity[2];
			}
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	    // sem patri standartne registrovaci a odregistrovavaci rutiny naslouchacich metod senzoru
	  }

	  @Override
	  protected void onPause() {
	    // odregistrace sledovace
	    super.onPause();
	    //manager.unregisterListener(this);
	  }
	  
		public void getOutputExport(String string){
			// Pokud neni dostupna pametova karta, exportovani dat skonci
			if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) return;

			// Vytvoreni adresare pro exporty, pokud neexistuje
	        File folder = new File(Environment.getExternalStorageDirectory(), "DMA-export");
	    	
	        if (! folder.exists()){
	            if (! folder.mkdirs()) {
	    	    	Toast.makeText(this, "Nepodařilo se vytvořit adresář " + folder.getPath() + ".", Toast.LENGTH_SHORT ).show();
	    	    	return;
	            }
		        else Toast.makeText(this, "Vytvořen adresář "+Environment.getExternalStorageDirectory().getPath(), Toast.LENGTH_SHORT).show();
	        }

	        // vytvoreni textoveho souboru
	        String fileName = "export_LM_" + new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(new Date()) + ".txt";
	        try
	        {
	            File textFile = new File(folder, fileName);
	            FileWriter writer = new FileWriter(textFile);
	            writer.append(string);
	            writer.flush();
	            writer.close();

	            Toast.makeText(this, "Export dat do souboru " + fileName + " proběhl úspěšně.", Toast.LENGTH_SHORT).show();
	            //zakomentovat
	        }
	        catch (FileNotFoundException e) {}
	        catch (IOException e) {}
	    }

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			final SeekBar thresholdBar = (SeekBar) findViewById(R.id.seekBar2);
			String r;

			switch (seekBar.getId()) {
			case R.id.seekBar1:
				TextView view6 = (TextView) findViewById(R.id.textView3);
				r = "Ustálení před měřením (s): : " + progress;
				view6.setText(r);
				break;
			case R.id.seekBar2:
				TextView view7 = (TextView) findViewById(R.id.textView4);
				r = "Minimální práh citlivosti: " + (progress/100.0);
				view7.setText(r);
				break;
			case R.id.seekBar3:
				TextView view8 = (TextView) findViewById(R.id.textView5);
				r = "Počet low-pass filtrů: " + progress;
				view8.setText(r);
				break;
			case R.id.seekBar4:
				TextView view9 = (TextView) findViewById(R.id.textView6);
				r = "Délka kroku (m): " + (progress/100.0);
				view9.setText(r);
				break;
			case R.id.seekBar5:
				TextView view10 = (TextView) findViewById(R.id.textView7);
				r = "Minimální interval trvání kroku (s): " + (progress/100.0);
				view10.setText(r);
				break;
			case R.id.seekBar6:
				TextView view11 = (TextView) findViewById(R.id.textView8);
				int i = progress;

				if (i > 1)	thresholdBar.setProgress(250);
				else		thresholdBar.setProgress(25);
				
				switch(i) {
				case 2:
					textSens = "střední (UI)";
					manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),manager.SENSOR_DELAY_UI);
					break;
				case 3:
					textSens = "vyšší (GAME)";
				    manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),manager.SENSOR_DELAY_GAME);
				    break;
				case 4:
					textSens = "vysoká (FASTEST)";
				    manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),manager.SENSOR_DELAY_FASTEST);
				    break;
				case 1:
				default:
					progress = 1;
					textSens = "nízká (NORMAL)";
					manager.registerListener(this,manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),manager.SENSOR_DELAY_NORMAL);
					break;
				}
				r = "Citlivost senzoru: " + textSens;
				view11.setText(r);
				break;
			default:
				break;
			}
		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onDestroy() {
			manager.unregisterListener(this);
			super.onDestroy();
		}
}