package com.android.main;

import java.util.Vector;

import com.android.application.*;
import com.android.Stimuli.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class TimeDomainActivity extends Activity{
	private TimeDomainView tdv;
	protected void onCreate(Bundle bb) {
		super.onCreate(bb);
		tdv = new TimeDomainView(this);
		setContentView(tdv);
	}
	@Override
    public void onStop() {
         super.onStop();
              tdv.dtThread.setStopTag(true);
              
              finish();
              System.gc();
    }
	class TimeDomainView extends View implements View.OnTouchListener, View.OnLongClickListener{
		private Paint paint;
		private double[] ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch8,ch9,ch10,ch11,ch12,ch13,ch14,ch15,ch16;
		private int[]xCorx;
		private int sampleRate,shiftYAxis,shiftXAxis;
		private DrawTimeDomainThread dtThread;
		private Vector tempvector;
		private boolean DEBUG = true;
		private String TAG = "TimeDomainView";
		private float yScale;
		private String msgToDisplay = "810uV";
		private Toast popMsg;
		private float oriDistanceOfFingers;
		private int selectionForNextDisplay;
		private float oriXAxis,oriYAxis;//for one finger, swipe screen
		private Buffer buffer;
		private Activity nextActivity, previousActivity;
		private final int MIN_SLIDE_DISTANCE = 300;
		private final int SECOND_TO_DISPLAY = 10;
		private boolean filterTag;
		private FIRFilterImplementation firCH1,firCH2,firCH3,firCH4,firCH5,firCH6,firCH7,firCH8,firCH9,firCH10,firCH11,firCH12,firCH13,firCH14,firCH15,firCH16;
		private final int SCREEN_HEIGHT =727;//xoom727
		private final int SCREEN_WIDTH =1280;//xoom1280
		private final int EEG_CHANNELS = 16;
		private final int EEG_CHANNEL_HEIGHT = SCREEN_HEIGHT/EEG_CHANNELS;
		public TimeDomainView(Context context){
			super(context);
			setTitle("Time Domain Display");
			sampleRate = Buffer.sRate;
			paint = new Paint();
			paint.setStyle(Paint.Style.FILL);
			paint.setStrokeWidth(1);
			ch1 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch2 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch3 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch4 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch5 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch6 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch7 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch8 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch9 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch10 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch11 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch12 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch13 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch14 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch15 = new double[sampleRate*SECOND_TO_DISPLAY];
			ch16 = new double[sampleRate*SECOND_TO_DISPLAY];
			xCorx = new int[sampleRate*SECOND_TO_DISPLAY];
			for (int i=0;i<sampleRate*SECOND_TO_DISPLAY;i++){
				xCorx[i] = i;
			}
			shiftYAxis = 50;
			shiftXAxis = 0;
			tempvector = new Vector(16);
			yScale = 126;
			setOnTouchListener(this);
			setLongClickable(true);
			setOnLongClickListener(this);
			popMsg = new Toast(getContext());
			oriDistanceOfFingers = 0;
			selectionForNextDisplay = -1;//initial, should be set by longClick
			oriXAxis = 0;
			oriYAxis = 0;
			//buffer = new Buffer();
			nextActivity = null;
			previousActivity = null;
			filterTag = false;
			firCH1 = new FIRFilterImplementation();
			firCH2 = new FIRFilterImplementation();
			firCH3 = new FIRFilterImplementation();
			firCH4 = new FIRFilterImplementation();
			firCH5 = new FIRFilterImplementation();
			firCH6 = new FIRFilterImplementation();
			firCH7 = new FIRFilterImplementation();
			firCH8 = new FIRFilterImplementation();
			firCH9 = new FIRFilterImplementation();
			firCH10 = new FIRFilterImplementation();
			firCH11 = new FIRFilterImplementation();
			firCH12 = new FIRFilterImplementation();
			firCH13 = new FIRFilterImplementation();
			firCH14 = new FIRFilterImplementation();
			firCH15 = new FIRFilterImplementation();
			firCH16 = new FIRFilterImplementation();
			dtThread = new DrawTimeDomainThread(mHandler);
			dtThread.start();
		}

		private void setData(Vector v){
			if(v.size()>=16){
				double []a,b,c,d,e,f,g,h,ii,jj,k,l,m,n,o,p;
				if(filterTag){
					a = firCH1.filter((double[])v.elementAt(0));
					b = firCH2.filter((double[])v.elementAt(1));
					c = firCH3.filter((double[])v.elementAt(2));
					d = firCH4.filter((double[])v.elementAt(3));
					e = firCH5.filter((double[])v.elementAt(4));
					f = firCH6.filter((double[])v.elementAt(5));
					g = firCH7.filter((double[])v.elementAt(6));
					h = firCH8.filter((double[])v.elementAt(7));
					ii = firCH9.filter((double[])v.elementAt(8));
					jj = firCH10.filter((double[])v.elementAt(9));
					k = firCH11.filter((double[])v.elementAt(10));
					l = firCH12.filter((double[])v.elementAt(11));
					m = firCH13.filter((double[])v.elementAt(12));
					n = firCH14.filter((double[])v.elementAt(13));
					o = firCH15.filter((double[])v.elementAt(14));
					p = firCH16.filter((double[])v.elementAt(15));
				}
				else{
					a = (double[])v.elementAt(0);
					b = (double[])v.elementAt(1);
					c = (double[])v.elementAt(2);
					d = (double[])v.elementAt(3);
					e = (double[])v.elementAt(4);
					f = (double[])v.elementAt(5);
					g = (double[])v.elementAt(6);
					h = (double[])v.elementAt(7);
					ii = (double[])v.elementAt(8);
					jj = (double[])v.elementAt(9);
					k = (double[])v.elementAt(10);
					l = (double[])v.elementAt(11);
					m = (double[])v.elementAt(12);
					n = (double[])v.elementAt(13);
					o = (double[])v.elementAt(14);
					p = (double[])v.elementAt(15);
				}
				//buffer.write3array(a, b, c, 1);
				synchronized(this){
					for(int i=shiftXAxis,j=0; j<a.length;i++,j++){
		                /*if(a[j]>0)
		                	a[j]*=(420/2048);
		                else
		                	a[j]*=-(420/2048);*/
						if ((a[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    a[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((a[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    a[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch1[i] = a[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2);
						
						if ((b[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							b[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((b[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            b[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch2[i] = b[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT;
				        
				        if ((c[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    c[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((c[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    c[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch3[i] = c[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*2;
						
						if ((d[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							d[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((d[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            d[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch4[i] = d[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*3;
				        
				        if ((e[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    e[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((e[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    e[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch5[i] = e[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*4;;
						
						if ((f[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							f[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((f[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            f[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch6[i] = f[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*5;
				        
				        if ((g[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    g[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((g[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    g[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch7[i] = g[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*6;
						
						if ((h[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							h[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((h[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            h[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch8[i] = h[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*7;
				        
				        if ((ii[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    ii[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((ii[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    ii[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch9[i] = ii[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*8;
						
						if ((jj[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							jj[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((jj[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            jj[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch10[i] = jj[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*9;
				        
				        if ((k[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    k[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((k[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    k[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch11[i] = k[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*10;
						
						if ((l[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							l[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((l[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            l[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch12[i] = l[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*11;
				        
				        if ((m[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    m[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((m[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    m[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch13[i] = m[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*12;
						
						if ((n[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							n[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((n[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            n[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch14[i] = n[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*13;
				        
				        if ((o[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) > EEG_CHANNEL_HEIGHT*1)
		                    o[j] = yScale * (EEG_CHANNEL_HEIGHT*1 - EEG_CHANNEL_HEIGHT*1/2);
		                else if ((o[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
		                    o[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
						ch15[i] = o[j]/ yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*14;
						
						if ((p[j] / yScale + (EEG_CHANNEL_HEIGHT/2)) > EEG_CHANNEL_HEIGHT)
							p[j] = yScale * (EEG_CHANNEL_HEIGHT - EEG_CHANNEL_HEIGHT/2);
				        else if ((p[j] / yScale + (EEG_CHANNEL_HEIGHT*1/2)) < 0)
				            p[j] = yScale * ( 0-(EEG_CHANNEL_HEIGHT*1/2));
				        ch16[i] = p[j] / yScale+(EEG_CHANNEL_HEIGHT*1/2)+EEG_CHANNEL_HEIGHT*15;			        	        
					}
				}
				shiftXAxis+=128;
				if(shiftXAxis>=(sampleRate*SECOND_TO_DISPLAY))
					shiftXAxis=0;
				invalidate();
			}
			if(DEBUG)Log.i(TAG, "SETTING, x="+shiftXAxis);
			//System.out.println("IN SETTING: "+shiftXAxis+"\r");
		}
		
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);	
			//shiftYAxis = (int) (100*Math.random());
			canvas.drawColor(Color.WHITE);
			//paint.setColor(Color.BLACK);
			for (int i=0;i<ch1.length-1;i++){
				paint.setColor(Color.RED);
				canvas.drawLine((int)xCorx[i],(int)ch1[i],(int)xCorx[i+1],(int)ch1[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch5[i],(int)xCorx[i+1],(int)ch5[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch9[i],(int)xCorx[i+1],(int)ch9[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch13[i],(int)xCorx[i+1],(int)ch13[i+1],paint);
				paint.setColor(Color.GREEN);
				canvas.drawLine((int)xCorx[i],(int)ch2[i],(int)xCorx[i+1],(int)ch2[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch6[i],(int)xCorx[i+1],(int)ch6[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch10[i],(int)xCorx[i+1],(int)ch10[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch14[i],(int)xCorx[i+1],(int)ch14[i+1],paint);
				paint.setColor(Color.BLUE);
				canvas.drawLine((int)xCorx[i],(int)ch3[i],(int)xCorx[i+1],(int)ch3[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch7[i],(int)xCorx[i+1],(int)ch7[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch11[i],(int)xCorx[i+1],(int)ch11[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch15[i],(int)xCorx[i+1],(int)ch15[i+1],paint);
				paint.setColor(Color.YELLOW);
				canvas.drawLine((int)xCorx[i],(int)ch4[i],(int)xCorx[i+1],(int)ch4[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch8[i],(int)xCorx[i+1],(int)ch8[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch12[i],(int)xCorx[i+1],(int)ch12[i+1],paint);
				canvas.drawLine((int)xCorx[i],(int)ch16[i],(int)xCorx[i+1],(int)ch16[i+1],paint);
			}
			paint.setColor(Color.GRAY);
			canvas.drawLine((float)shiftXAxis,(float)0,(float)shiftXAxis,(float)getHeight(),paint);
			//canvas.drawText(Integer.toString(SCREEN_HEIGHT)+"/"+Integer.toString(SCREEN_WIDTH),0, 100, paint);
			setTitle("Time Domain Display, "+msgToDisplay+", "+buffer.getDataIndex()/60+":"+buffer.getDataIndex()%60);
			if(DEBUG)Log.i(TAG, "PAINTING!");
			//Log.e("onDraw", "******************"+Integer.toString(getHeight())+"**"+Integer.toString(getWidth()));
			
		}
		private final Handler mHandler = new Handler() {
	        public void handleMessage(Message msg) {
	            switch (msg.what) {
	            case 0://repaint            	
	            	invalidate();
	           	 break;
	            case 1://fill in data
	            	tempvector = (Vector)msg.obj;
	            	setData(tempvector);
	           	 break;
	            case 2:
	           	 break;
	            }
	        }
	    };
		public boolean onTouch(View v, MotionEvent e) {
			handleTwoFingers(e);
			handleOneFingerSwipe(e);
			/*if(e.getX()>400 ){
				msgToDisplay = "222222";
			}
			else if (e.getX()<400 ){
				msgToDisplay = Float.toString(e.getX());
			}
			//msgToDisplay = Float.toString(event.getX());*/
			return false;//if return true, the onLongClick can't work.
		}
		public boolean onLongClick(View v) {
			//msgToDisplay = "11111111111";
/*			new AlertDialog.Builder(this.getContext())
	        //.setIcon(R.drawable.alert_dialog_icon)
	        .setTitle(R.string.app_name)
	        .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	switch(whichButton){
	            	case 0:
	            		selectionForNextDisplay = 0;//button 1 pressed, Time mode display
	            		break;
	            	case 1:
	            		selectionForNextDisplay = 1;//button 2 pressed, Frequency mode display
	            		break;
	            	case 2:
	            		selectionForNextDisplay = 2;//button 3 pressed, CCA display
	            		break;
	            	case 3:
	            		selectionForNextDisplay = 3;//button 4 pressed, Debug mode
	            		break;
	            	}
	            }
	        })
	        .setPositiveButton(R.string.timeDomainDialog_button_ok, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	if(selectionForNextDisplay !=-1){
	            		msgToDisplay = "selectionForNextDisplay"+"/pressed";
	            	}
	            	selectionForNextDisplay =-1;//in case user didn't press any of mode to display, and press OK/CANCEL only.
	            }
	        })
	        .setNegativeButton(R.string.timeDomainDialog_button_cancel, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int whichButton) {
	            	msgToDisplay += "3/";	
	                // User clicked No so do some stuff 
	            }
	        })
	       .show();
			if(DEBUG)Log.e(TAG, "onLongClick!\n");*/
			return true;
		}
		private void handleOneFingerSwipe(MotionEvent event){
				
			int action = event.getAction();
			int actionCode = action & MotionEvent.ACTION_MASK;
			if(actionCode == MotionEvent.ACTION_DOWN && event.getPointerCount()==1){
				oriXAxis = event.getX();
				oriYAxis = event.getY();
				if(DEBUG)Log.e(TAG, "oriXAxis= "+oriXAxis+"\n");
			}
			if(actionCode == MotionEvent.ACTION_MOVE &&event.getPointerCount()==1){
				if(event.getX()>oriXAxis && (event.getX()-oriXAxis >MIN_SLIDE_DISTANCE)){//swipe to right
					/*setContentView(R.layout.slideclockmain);
					myflipper.setInAnimation(inFromRightAnimation());	       
					myflipper.setOutAnimation(outToLeftAnimation());	        
					myflipper.showNext();*/ 
					if(DEBUG)Log.e(TAG, "SWIPE TO RIGHT! /oriXAxis= "+oriXAxis+" / "+event.getX()+"\n");
				}
				else if(event.getX()<oriXAxis && (oriXAxis-event.getX() >MIN_SLIDE_DISTANCE)){//swipe to left. to FFT
					/*setContentView(R.layout.slideclockmain);
					myflipper.setInAnimation(inFromLeftAnimation());     
					myflipper.setOutAnimation(outToRightAnimation());      
					myflipper.showPrevious(); */
					//setContentView(R.layout.canvas);
					finish();
					startActivity(new Intent(TimeDomainActivity.this, StimuliActivity.class));
					if(DEBUG)Log.e(TAG, "SWIPE TO LEFT! /oriXAxis= "+oriXAxis+" / "+event.getX()+"\n");
				}
				//swipe up, turn the Filter
				else if(event.getY()<oriYAxis &&(oriYAxis-event.getY()>MIN_SLIDE_DISTANCE)){
					filterTag = true;
					popMsg.makeText( this.getContext(), R.string.Enable_Band_Pass_Filter,popMsg.LENGTH_SHORT).show();
				}
				//swipe down, turn off the filter
				else if(event.getY()>oriYAxis &&(event.getY()-oriYAxis>MIN_SLIDE_DISTANCE)){
					filterTag = false;
					popMsg.makeText( this.getContext(), R.string.Disable_Band_Pass_Filter,popMsg.LENGTH_SHORT).show();
				}
				
			}
		}
		private void handleTwoFingers(MotionEvent event) {
			float newDistanceOfFingers;
			int action = event.getAction();
			int actionCode = action & MotionEvent.ACTION_MASK;
			if(actionCode == MotionEvent.ACTION_POINTER_DOWN && event.getPointerCount()==2){
				oriDistanceOfFingers = FloatMath.sqrt((event.getX(0)-event.getX(1)) * (event.getX(0)-event.getX(1)) + 
						(event.getY(0)-event.getY(1)) * (event.getY(0)-event.getY(1)));
				//Log.e("******","ori= "+oriDistanceOfFingers);
			}
			if(actionCode == MotionEvent.ACTION_MOVE &&event.getPointerCount()==2){
				newDistanceOfFingers = FloatMath.sqrt((event.getX(0)-event.getX(1)) * (event.getX(0)-event.getX(1)) + 
						(event.getY(0)-event.getY(1)) * (event.getY(0)-event.getY(1)));
				//Log.e("******","new= "+newDistanceOfFingers);
				if(newDistanceOfFingers < oriDistanceOfFingers){
					if(yScale<126){
						yScale +=1;
						msgToDisplay="";
						if(810*(126/yScale) >1000)
							msgToDisplay = Double.toString(810*(126/yScale)/1000)+"mV";
						else
							msgToDisplay = Double.toString(810*(126/yScale))+"uV";
						setTitle("Time Domain Display, " + msgToDisplay);
					}
					//msgToDisplay = "Zoom Out";	
					//popMsg.makeText( this.getContext(), R.string.ZoomOut_text,popMsg.LENGTH_SHORT).show();
					Log.e(TAG,"Zoom Out, yScale= "+yScale);
				}
				else{
					if(yScale>10){
						yScale -=1;
						msgToDisplay="";
						if(810*(126/yScale) >1000)
							msgToDisplay = Double.toString(810*(126/yScale)/1000)+"mV";
						else
							msgToDisplay = Double.toString(810*(126/yScale))+"uV";
						setTitle("Time Domain Display, " + msgToDisplay);
					}
					
					//msgToDisplay = "Zoom In";
					//popMsg.makeText(getContext(), R.string.ZoomIn_text,popMsg.LENGTH_SHORT).show();
					Log.e(TAG,"Zoom In, yScale= "+yScale);
				}
			}
		}
		private void dumpEvent2(MotionEvent event) {
			   String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
			      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
			   StringBuilder sb = new StringBuilder();
			   int action = event.getAction();
			   int actionCode = action & MotionEvent.ACTION_MASK;
			   sb.append("event ACTION_" ).append(names[actionCode]);//DOWN/UP/MOVE....
			   if (actionCode == MotionEvent.ACTION_POINTER_DOWN
			         || actionCode == MotionEvent.ACTION_POINTER_UP) {
			      sb.append("(pid " ).append(
			      action >> MotionEvent.ACTION_POINTER_ID_SHIFT);//ID
			      sb.append(")" );
			   }
			   sb.append("[" );
			   for (int i = 0; i < event.getPointerCount(); i++) {
			      sb.append("#" ).append(i);
			      sb.append("(pid " ).append(event.getPointerId(i));
			      sb.append(")=" ).append((int) event.getX(i));
			      sb.append("," ).append((int) event.getY(i));
			      if (i + 1 < event.getPointerCount())
			         sb.append(";" );
			   }
			   sb.append("]" );
			   Log.e(TAG, sb.toString());
		}
	}
}
