package com.android.main;

import java.util.Vector;
import com.android.Algorithm.FastFourierTransform;
import com.android.application.*;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.opengl.Matrix;
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.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class FrequencyDomainActivity extends Activity{
	private FrequencyDomainView fdv;
	protected void onCreate(Bundle bb) {
		super.onCreate(bb);
		fdv = new FrequencyDomainView(this);
		setContentView(fdv);
	}
	@Override
    public void onStop() {
         super.onStop();
         fdv.dfftthread.setStopTag(true);
         
         finish();
         System.gc();
    }
	class FrequencyDomainView extends View implements View.OnTouchListener, View.OnLongClickListener{
		private Paint paint;
		private int sampleRate;
		private Buffer buffer;
		private boolean DEBUG = false;
		private final int secondForFFT = 4;//once modify here, modify in Channel.class as well.
		private String TAG = "FrequencyDomainView";
		private float oriDistanceOfFingers;
		private int selectionForNextDisplay;
		private float oriXAxis,oriYAxis;//for one finger, swipe screen
		private String msgToDisplay = "";
		private Channel [] element;
		private DrawFFTThread dfftthread;
		private int indexForDataBuffer;
		private FastFourierTransform fft;
		//frames to display FFT result
		private int signalFrameWidth, signalFrameHeight;
		private int skullMapWidth, skullMapHeight;
		private final int picChannelWidth=50,picChannelHeight= 50;
		private final int topBottomGap=50, leftSideGap=15, rightSideGap = 20, skullSignalGap=10;
		//private int ch1MaxIndex = 0,ch2MaxIndex = 0,ch3MaxIndex = 0,ch4MaxIndex = 0;//Max index after FFT.
		private final int MIN_SLIDE_DISTANCE = 200;
		private boolean filterTag;
		private Toast popMsg;
		private int [] selectedChannelTable;
		private int nextIndexSelectedChannelTable;
		private Bitmap bitmapOrg,bitmapAfter;
		private RectF rectForBitmap ;
		public FrequencyDomainView(Context context){
			super(context);
			setTitle("FFT Domain Display");
			sampleRate = buffer.sRate;
			paint = new Paint();
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(1);
			setOnTouchListener(this);
			setLongClickable(false);
			setOnLongClickListener(this);
			oriDistanceOfFingers = 0;
			selectionForNextDisplay = -1;//initial, should be set by longClick
			oriXAxis = 0;
			oriYAxis = 0;
			element = new Channel[buffer.channels];
			
			for (int i=0;i<buffer.channels;i++){
				element[i] = new Channel(Integer.toString(i));
			}
			dfftthread = new DrawFFTThread(mHandler, "DrawFreqThread");
			dfftthread.start();
			indexForDataBuffer = 0;//0,1,2,...,secondForFFT-1
			fft = new FastFourierTransform();
			filterTag = false;
			popMsg = new Toast(getContext());
			selectedChannelTable = new int[6];//0:first row, 1:second row, 2:third row, 3:fourth row
			skullMapWidth = 500;
			skullMapHeight = (520-topBottomGap);
			signalFrameWidth = 1024-leftSideGap-rightSideGap-skullMapWidth;
			signalFrameHeight = (520-topBottomGap*2)/selectedChannelTable.length; // 450/2
			nextIndexSelectedChannelTable = 0;
			
			initial();//initial the table and FFT channel
			
			bitmapOrg = BitmapFactory.decodeResource(getResources(), 
		               R.drawable.eeglayout);
			//bitmapOrg = bitmapOrg.copy(bitmapOrg.getConfig(), true);
			bitmapAfter = bitmapOrg.copy(bitmapOrg.getConfig(), true);
			rectForBitmap = new RectF(leftSideGap,topBottomGap,leftSideGap+skullMapWidth,topBottomGap+skullMapHeight);
		}
		private void initial2(){
			for(int i=0;i<selectedChannelTable.length;i++){
				selectedChannelTable[i]= i+1;
				element[i].isSelected = true;
			}
		}

		/*public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {         
			Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());         
			Canvas canvas = new Canvas(bmOverlay);         
			canvas.drawBitmap(bmp1, new Matrix(), null);         
			canvas.drawBitmap(bmp2, 0, 0, null);         
			return bmOverlay;     
		}*/
		private void setData(Vector v){
			if(DEBUG)Log.e(TAG, "In setData()");
			if(v.size()>0){
				//if(DEBUG)buffer.writeData(2,TAG+", in setData()"+v.size());
				putDataIntoDataBuffer(v);//put data into dataBuffer, and filter
				FFT();//FFT
				modifyScale();//modify scale
				invalidate();//paint
			}
		}
		private void putDataIntoDataBuffer(Vector v2){
			//double [][] temp;
			if(DEBUG)Log.e(TAG, "in putDataIntoDataBuffer()");
			if(DEBUG)Log.e(TAG, "in putDataIntoDataBuffer(),vector.size= "+v2.size());
			if(filterTag){
				for(int channel=0;channel<buffer.channels;channel++){
					element[channel].oneSecData = element[channel].filter.filter((double[])v2.elementAt(channel));
				}
			}
			else{
				for(int channel=0;channel<buffer.channels;channel++){
					element[channel].oneSecData = (double[])v2.elementAt(channel);
				}
			}
			if(indexForDataBuffer>=secondForFFT){//no more place to fill in the data
				//move data to previous one sec.
				for(int channel=0;channel<buffer.channels;channel++){
					for (int i = 0; i < (secondForFFT-1); i++) {
	                    for (int j = 0; j < sampleRate; j++) {
	                    	element[channel].oriData[i * sampleRate +j] = element[channel].oriData[i * sampleRate + j + sampleRate];
	                    }
	                }
					//insert data to last sec.
					System.arraycopy(element[channel].oneSecData, 0, element[channel].oriData, (indexForDataBuffer-1)*sampleRate, sampleRate);
				}
				
			}
			else{
				//insert data to last sec.
				for(int channel=0;channel<buffer.channels;channel++){
					System.arraycopy(element[channel].oneSecData, 0, element[channel].oriData, indexForDataBuffer*sampleRate, sampleRate);
				}
			}
			indexForDataBuffer++;
			if(indexForDataBuffer==(secondForFFT+1))
				indexForDataBuffer = secondForFFT;
			//msgToDisplay = Integer.toString(indexForDataBuffer);
			if(DEBUG)Log.e(TAG, "in putDataIntoDataBuffer(), DONE");
		}
		private void FFT(){
			if(DEBUG)Log.e(TAG, "in FFT()");
			for(int channel=0;channel<buffer.channels;channel++){
				if(element[channel].isSelected)
					element[channel].FFTResultData = fft.fftMag2(element[channel].oriData);	
			}
			if(DEBUG)Log.e(TAG, "in FFT(), DONE");
		}
		private void modifyScale(){
			//seek the maximum
			if(DEBUG)Log.e(TAG, "in modifyScale()");
			double [][] tempMax= new double[selectedChannelTable.length][2];
			/**
			 *          0        1
			 *  0    AmaxValue  Atemp
			 *  1    BmaxValue  Btemp
			 *  .		.		  .
			 *  .		.		  .
			 *  .
			 */
	        for(int times=0;times<selectedChannelTable.length;times++){
	        	if(element[selectedChannelTable[times]-1].isSelected){
	        		//seek maximum
		        	for (int i = 16; i < 120;i++){//element[selectedChannelTable[times]-1].FFTResultData.length; i++) {
			            tempMax[times][0] = Math.max(tempMax[times][0], (element[selectedChannelTable[times]-1].FFTResultData[i]));
			           /* if (tempMax[times][0] != tempMax[times][1]) {
			            	if(i<100 && times==0)
			            		msgToDisplay = Double.toString(i/4);
			            	tempMax[times][1] = tempMax[times][0];
			            }*/
			        }
		        	for(int i=0;i<element[0].FFTResultData.length;i++){
			        	if(element[0].FFTResultData[i]==tempMax[0][0]){
			        		msgToDisplay = Double.toString(i);
			        		break;
			        	}
			        }
		        	//modify scale
		        	for (int i = 0; i < element[selectedChannelTable[times]-1].FFTResultData.length; i++) {
		        		element[selectedChannelTable[times]-1].XAxis[i] = i+skullMapWidth+50;//the same x axis
		        		//different y axis
		        		element[selectedChannelTable[times]-1].FFTResultData[i] = 
		        			element[selectedChannelTable[times]-1].FFTResultData[i]*(signalFrameHeight/tempMax[times][0])*(-1)+signalFrameHeight*((times+1))+topBottomGap;
		        	}
	        	}
	        }
	        
	        if(DEBUG)Log.e(TAG, "in modifyScale(), done");
		}
		protected void onDraw(Canvas canvas){
			super.onDraw(canvas);
			if(DEBUG)Log.e(TAG, "in onDraw!");
			//clear old canvas
			canvas.drawColor(Color.WHITE);
			/*paint.setStyle(Paint.Style.STROKE);
			paint.setTextSize(1);
			paint.setColor(Color.BLACK);*/
			for(int times=0;times<selectedChannelTable.length;times++){
				if(times%4==0)
					paint.setColor(Color.RED);
				else if(times%4 ==1)
					paint.setColor(Color.GREEN);
				else if(times%4 ==2)
					paint.setColor(Color.BLUE);
				else if(times%4 ==3)
					paint.setColor(Color.YELLOW);
				for(int i=0;i<element[selectedChannelTable[times]-1].FFTResultData.length-1;i++){
					canvas.drawLine((int)element[selectedChannelTable[times]-1].XAxis[i],(int)element[selectedChannelTable[times]-1].FFTResultData[i],
							(int)element[selectedChannelTable[times]-1].XAxis[i+1],(int)element[selectedChannelTable[times]-1].FFTResultData[i+1],paint);
				}
				paint.setStyle(Style.FILL);
				paint.setColor(Color.GRAY);
				paint.setTextSize(15);
				canvas.drawText(element[selectedChannelTable[times]-1].channelName, 
						element[selectedChannelTable[0]-1].XAxis[0]-40, signalFrameHeight*(times+1)+topBottomGap, paint);
			}
			//canvas.drawText(msgToDisplay, 250,300, paint);
			paint.setColor(Color.YELLOW);
			paint.setTextSize(15);
			//ch1 and ch2 x axis symbol
			canvas.drawText("10", element[selectedChannelTable[0]-1].XAxis[(10-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("20", element[selectedChannelTable[0]-1].XAxis[(20-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("30", element[selectedChannelTable[0]-1].XAxis[(30-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("40", element[selectedChannelTable[0]-1].XAxis[(40-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("50", element[selectedChannelTable[0]-1].XAxis[(50-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("60", element[selectedChannelTable[0]-1].XAxis[(60-1)*4], signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawText("Hz", element[selectedChannelTable[0]-1].XAxis[(60-1)*4]+40, signalFrameHeight*(selectedChannelTable.length)+skullSignalGap+topBottomGap, paint);
			canvas.drawBitmap(bitmapAfter, null, rectForBitmap, paint);
			//if(DEBUG){
			{	paint.setColor(Color.RED);
				canvas.drawText(msgToDisplay, 400, 400, paint);
			}
			//paint.setColor(Color.rgb(180, 180, 180));
			for(int channel=0;channel<buffer.channels;channel++){
				if(element[channel].isSelected){
					//canvas.drawOval(element[channel].rectf, paint);
					canvas.drawBitmap(element[channel].picWhite,null,element[channel].rectf,paint);
				}
				else
					canvas.drawBitmap(element[channel].picGray,null,element[channel].rectf,paint);
			}
			setTitle("FFT Domain Display, "+buffer.getDataIndex()/60+":"+buffer.getDataIndex()%60);
		}
		private int getSignalFrameHeightScale(int channelNumber){
			int result=1000;
			for(int i=0;i<selectedChannelTable.length;i++){
				if((selectedChannelTable[i]-1) == channelNumber){
					result = i;
					break;
				}
			}
			return result;
		}
		private void initial(){
			for(int i=0;i<selectedChannelTable.length;i++){
				selectedChannelTable[i] = i+1;
				element[i].isSelected = true;
			}
			for(int i=0;i<buffer.channels;i++){
				//selectedChannelTable[i]= i+1;
				//element[i].isSelected = true;
				if(i==0){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fpzg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fpzw);
					element[i].setPicXY(259-25, 119-25,259-25+picChannelWidth, 119-25+picChannelHeight);
				}
				else if(i==1){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fzg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fzw);
					element[i].setPicXY(259-25, 171-25,259-25+picChannelWidth, 171-25+picChannelHeight);
				}
				else if(i==2){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f3g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f3w);
					element[i].setPicXY(190-25, 209-25,190-25+picChannelWidth, 209-25+picChannelHeight);
				}
				else if(i==3){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f4g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f4w);
					element[i].setPicXY(328-25,209-25,328-25+picChannelWidth, 209-25+picChannelHeight);;
				}
				else if(i==4){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f7g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f7w);
					element[i].setPicXY(142-25, 182-25,142-25+picChannelWidth, 182-25+picChannelHeight);
				}
				else if(i==5){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f8g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.f8w);
					element[i].setPicXY(380-25, 182-25,380-25+picChannelWidth, 182-25+picChannelHeight);
				}
				else if(i==6){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fczg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.fczw);
					element[i].setPicXY(259-25, 231-25,259-25+picChannelWidth, 231-25+picChannelHeight);
				}
				else if(i==7){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.czg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.czw);
					element[i].setPicXY(259-25, 290-25,259-25+picChannelWidth, 290-25+picChannelHeight);
				}
				else if(i==8){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.c3g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.c3w);
					element[i].setPicXY(190-25, 290-25,190-25+picChannelWidth, 290-25+picChannelHeight);
				}
				else if(i==9){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.c4g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.c4w);
					element[i].setPicXY(328-25, 290-25,328-25+picChannelWidth, 290-25+picChannelHeight);
				}
				else if(i==10){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.pzg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.pzw);
					element[i].setPicXY(259-25, 361-25,259-25+picChannelWidth, 361-25+picChannelHeight);
				}
				else if(i==11){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.p3g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.p3w);
					element[i].setPicXY(190-25, 361-25,190-25+picChannelWidth, 361-25+picChannelHeight);
				}
				else if(i==12){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.p4g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.p4w);
					element[i].setPicXY(328-25, 361-25,328-25+picChannelWidth, 361-25+picChannelHeight);
				}
				else if(i==13){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.t5g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.t5w);
					element[i].setPicXY(149-25, 405-25,149-25+picChannelWidth, 405-25+picChannelHeight);
				}
				else if(i==14){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.t6g);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.t6w);
					element[i].setPicXY(380-25, 405-25,380-25+picChannelWidth, 405-25+picChannelHeight);
				}
				else if(i==15){
					element[i].picGray = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.ozg);
					element[i].picWhite = BitmapFactory.decodeResource(getResources(), 
				               R.drawable.ozw);
					element[i].setPicXY(259-25, 455-25,259-25+picChannelWidth, 455-25+picChannelHeight);
				}
			}
		}
		private final Handler mHandler = new Handler() {
	        public void handleMessage(Message msg) {
	            switch (msg.what) {
	            case 0://repaint            	
	            	invalidate();
	           	 break;
	            case 1://fill in data
	            	setData((Vector)msg.obj);
	           	 break;
	            case 2:
	           	 break;
	            }
	        }
	    };
		public boolean onTouch(View v, MotionEvent e) {
			handleTwoFingers(e);
			handleOneFingerSwipe(e);
			return false;//if return true, the onLongClick can't work.
		}
		
		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");
				//msgToDisplay = "";
				//msgToDisplay = oriXAxis+"/"+oriYAxis;
				handleSelectedChannel(oriXAxis, oriYAxis);				
			}
			if(actionCode == MotionEvent.ACTION_MOVE &&event.getPointerCount()==1){
				if(event.getX()>oriXAxis && (event.getX()-oriXAxis >MIN_SLIDE_DISTANCE)){//swipe to right, to TimeDomain
					finish();
					startActivity(new Intent(FrequencyDomainActivity.this, TimeDomainActivity.class));	
					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 PowerDensityandSkullmapActivity
					finish();
					startActivity(new Intent(FrequencyDomainActivity.this, PowerDensityandSkullmapActivity.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){
					//msgToDisplay = "Zoom Out";	
					//popMsg.makeText( this.getContext(), R.string.ZoomOut_text,popMsg.LENGTH_SHORT).show();
					//Log.e("******","CCCCCCCCCc");
				}
				else{
					//msgToDisplay = "Zoom In";
					//popMsg.makeText(getContext(), R.string.ZoomIn_text,popMsg.LENGTH_SHORT).show();
					//Log.e("******","DDDDDDDDDD");
				}
			}
		}
		private void handleSelectedChannel(float x, float y){
			synchronized(this){
				//extract which channel was selected
				int selectedChannel = -1;// = ((int) (x%buffer.channels))+1;
				for(int i=0;i<buffer.channels;i++){
					if(element[i].rectf.left<x && element[i].rectf.right>x && element[i].rectf.top<y && element[i].rectf.bottom>y){
						//msgToDisplay +="##"+element[0].rectf.left+"/"+element[0].rectf.right+"/"+element[0].rectf.top+"/"+element[0].rectf.bottom;
						selectedChannel = i+1;
						break;
					}
				}
				
				/*if(DEBUG){
					Log.e(TAG, "///////////ORI////////////");
					for(int i=0;i<4;i++){
						Log.e(TAG, "selectedChannelTable["+i+"]= "+selectedChannelTable[i]);
					}
					for(int i=0;i<buffer.channels;i++){
						if(element[i].isSelected)
							Log.e(TAG, "selected channel= "+(i+1));
					}
					Log.e(TAG, "///////////ORI////////////////");
				}*/
				if(selectedChannel!=-1){
					if(!element[selectedChannel-1].isSelected){
						nextIndexSelectedChannelTable++;
						
						if(DEBUG)Log.e(TAG, "selectedChannel= "+selectedChannel+", nextIndexSelectedChannelTable= "+nextIndexSelectedChannelTable);
						
						//update color on skull map
						
						//update the selected channel table and element[].isSelected
						if(nextIndexSelectedChannelTable>selectedChannelTable.length){
							element[selectedChannelTable[0]-1].isSelected = false;
							element[selectedChannel-1].isSelected = true;
							nextIndexSelectedChannelTable=1;
							selectedChannelTable[nextIndexSelectedChannelTable-1] = selectedChannel;
						}
						else{
							if(selectedChannelTable[nextIndexSelectedChannelTable-1]>0)
								if(element[selectedChannelTable[nextIndexSelectedChannelTable-1]-1].isSelected)
									element[selectedChannelTable[nextIndexSelectedChannelTable-1]-1].isSelected = false;
							selectedChannelTable[nextIndexSelectedChannelTable-1] = selectedChannel;
							element[selectedChannel-1].isSelected = true;
						}
					}
					
					if(DEBUG){
						Log.e(TAG, "///////////AFTER////////////////");
						for(int i=0;i<4;i++){
							Log.e(TAG, "selectedChannelTable["+i+"]= "+selectedChannelTable[i]);
						}
						for(int i=0;i<buffer.channels;i++){
							if(element[i].isSelected)
								Log.e(TAG, "selected channel= "+(i+1));
						}
						Log.e(TAG, "///////////AFTER////////////////");
					}
				}
			}
		}
		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;
		}
	}
}
