gt: 		int writeMode = ((RadioGroup) getView().findViewById(
context: /**
   * 
   */
  package it.unibo.cs.jonus.waidrec;
  
  import it.unibo.cs.jonus.waidrec.MainActivity.ModelGenRunnable;
  
  import java.util.ArrayList;
  
  import android.support.v4.app.Fragment;
  import android.app.AlertDialog;
  import android.app.ProgressDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.CountDownTimer;
  import android.preference.PreferenceManager;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.RadioButton;
  import android.widget.RadioGroup;
  import android.widget.Spinner;
  import android.widget.Toast;
  
  /**
   * @author jei
   * 
   */
  public class TrainingFragment extends Fragment {
  
  	private static final int MODE_AVAILABLE = 0;
  	private static final int MODE_TRAINING = 1;
  	private static final String KEY_TRAINING_ISRUNNING = "training_isrunning";
  	private static final String KEY_TRAINING_CURRENT_VEHICLE = "training_current_vehicle";
  	private static final String KEY_TRAINING_CURRENT_APPEND = "training_current_append";
  	private static final String KEY_RECOGNIZER_ISRUNNING = "recognizer_isrunning";
  	private static final String KEY_TRN_START_DELAY = "pref_training_start_delay";
  
  	private MainActivity mActivity;
  
  	private SharedPreferences mSharedPrefs;
  
  	private ArrayList<String> mVehicles = new ArrayList<String>();
  
  	private ImageView onView;
  	private ImageView offView;
  	private RadioGroup writeModeGroup;
  	private Spinner vehicleSpinner;
  	private ArrayAdapter<String> mAdapter;
  	private RadioButton overwriteRadio;
  	private RadioButton appendRadio;
  
  	private ProgressDialog progressDialog = null;
  
  	private Thread asyncThread = null;
  
  	private OnClickListener startTrnListener = new OnClickListener() {
  		@Override
  		public void onClick(View v) {
  			startTraining(v);
  		}
  	};
  	private OnClickListener stopTrnListener = new OnClickListener() {
  		@Override
  		public void onClick(View v) {
  			stopTraining(v);
  		}
  	};
  
  	public TrainingFragment() {
  
  	}
  
  	@Override
  	public View onCreateView(LayoutInflater inflater, ViewGroup container,
  			Bundle savedInstanceState) {
  		View rootView = inflater.inflate(R.layout.fragment_training, container,
  				false);
  
  		return rootView;
  	}
  
  	@Override
  	public void onViewCreated(View view, Bundle savedInstanceState) {
  		super.onViewCreated(view, savedInstanceState);
  
  		mActivity = (MainActivity) getActivity();
  		// Get shared preferences
  		mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
  
  		writeModeGroup = (RadioGroup) view.findViewById(R.id.modeRadioGroup);
  		vehicleSpinner = (Spinner) view.findViewById(R.id.classesSpinner);
  		overwriteRadio = (RadioButton) view
  				.findViewById(R.id.overwriteRadioButton);
  		appendRadio = (RadioButton) view.findViewById(R.id.appendRadioButton);
  
  		// Prepare the spinner for the vehicles
  		mAdapter = new ArrayAdapter<String>(mActivity,
  				android.R.layout.simple_spinner_item, mVehicles);
  		// Specify the layout to use when the list of choices appears
  		mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  		// Apply the adapter to the spinner
  		vehicleSpinner.setAdapter(mAdapter);
  
  		// Restore UI elements from the saved state
  		if (savedInstanceState != null) {
  			vehicleSpinner.setSelection(savedInstanceState
  					.getInt("selectedVehicle"));
  			writeModeGroup
  					.check(savedInstanceState.getInt("selectedWriteMode"));
  		}
  
  		// Set on/off images
  		offView = (ImageView) view.findViewById(R.id.trainingOffView);
  		onView = (ImageView) view.findViewById(R.id.trainingOnView);
  
  		// Set click listeners
  		offView.setOnClickListener(startTrnListener);
  		onView.setOnClickListener(stopTrnListener);
  
  	}
  
  	@Override
  	public void onResume() {
  		super.onResume();
  
  		// Get the list of VehicleItem from the db
  		Uri uri = Uri.parse(EvaluationsProvider.VEHICLES_URI
  				+ EvaluationsProvider.PATH_ALL_VEHICLES);
  		Cursor cursor = getActivity().getContentResolver().query(uri,
  				MainActivity.vehicleColumnsProjection, null, null, null);
  		ArrayList<VehicleItem> items = EvaluationsProvider
  				.cursorToVehicleItemArray(cursor);
  		mVehicles.clear();
  		for (VehicleItem i : items) {
  			mVehicles.add(i.getCategory());
  		}
  		mAdapter.notifyDataSetChanged();
  
  		// If the training service is already running, update the UI
  		boolean isServiceRunning = mSharedPrefs.getBoolean(
  				KEY_TRAINING_ISRUNNING, false);
  		if (isServiceRunning) {
  			setUIMode(MODE_TRAINING);
  		} else {
  			setUIMode(MODE_AVAILABLE);
  		}
  		vehicleSpinner.setSelection(mVehicles.indexOf(mSharedPrefs.getString(
  				KEY_TRAINING_CURRENT_VEHICLE, "idle")));
  		if (mSharedPrefs.getBoolean(KEY_TRAINING_CURRENT_APPEND, false)) {
  			writeModeGroup.check(R.id.appendRadioButton);
  		} else {
  			writeModeGroup.check(R.id.overwriteRadioButton);
  		}
  	}
  
  	public void startTraining(View view) {
  		boolean isTrainingRunning = mSharedPrefs.getBoolean(
  				KEY_TRAINING_ISRUNNING, false);
  		boolean isRecognizerRunning = mSharedPrefs.getBoolean(
  				KEY_RECOGNIZER_ISRUNNING, false);
  
  		if (!isTrainingRunning && !isRecognizerRunning) {
  			// Get the selected radio button for write mode
  			int writeMode = ((RadioGroup) getView().findViewById(
  					R.id.modeRadioGroup)).getCheckedRadioButtonId();
  			// Get confirmation from user
  			AlertDialog.Builder confirmDialog = new AlertDialog.Builder(
  					mActivity);
  			confirmDialog.setTitle("Warning");
  			switch (writeMode) {
  			case R.id.overwriteRadioButton:
  				confirmDialog
  						.setMessage(getText(R.string.training_confirm_overwrite));
  				break;
  			case R.id.appendRadioButton:
  				confirmDialog
  						.setMessage(getText(R.string.training_confirm_append));
  				break;
  			}
  			confirmDialog.setPositiveButton(android.R.string.ok,
  					new DialogInterface.OnClickListener() {
  						@Override
  						public void onClick(DialogInterface dialog, int which) {
  							trainingDelayedStart();
  						}
  					});
  			confirmDialog.setNegativeButton(android.R.string.cancel,
  					new DialogInterface.OnClickListener() {
  						@Override
  						public void onClick(DialogInterface dialog, int which) {
  							dialog.dismiss();
  						}
  					});
  			confirmDialog.show();
  		}
  	}
  
  	private void trainingDelayedStart() {
  		String st = mSharedPrefs.getString(KEY_TRN_START_DELAY, "10");
  		long startingTime = Integer.parseInt(st) * 1000;
  
  		final ProgressDialog countdownDialog = new ProgressDialog(mActivity);
  		countdownDialog.setTitle("Starting training");
  		countdownDialog.setMessage(st);
  		countdownDialog.setCancelable(false);
  		countdownDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
  				new DialogInterface.OnClickListener() {
  					@Override
  					public void onClick(DialogInterface dialog, int which) {
  						dialog.dismiss();
  					}
  				});
  		countdownDialog.show();
  
  		new CountDownTimer(startingTime, 1000) {
  			@Override
  			public void onTick(long millisUntilFinished) {
  				countdownDialog.setMessage("" + (millisUntilFinished / 1000));
  			}
  
  			@Override
  			public void onFinish() {
  				trainingStart();
  				countdownDialog.dismiss();
  			}
  		}.start();
  	}
  
  	private void trainingStart() {
  		// Get the selected vehicle
  		String vehicle = mVehicles
  				.get(vehicleSpinner.getSelectedItemPosition());
  		mSharedPrefs.edit().putString(KEY_TRAINING_CURRENT_VEHICLE, vehicle)
  				.commit();
  		// Get the write mode
  