package com.commonsensenet.realfarm.actions; import java.util.Calendar; import java.util.List; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.view.MenuItem; import com.commonsensenet.realfarm.DataFormActivity; import com.commonsensenet.realfarm.Global; import com.commonsensenet.realfarm.R; import com.commonsensenet.realfarm.dataaccess.RealFarmDatabase; import com.commonsensenet.realfarm.model.Resource; import com.commonsensenet.realfarm.utils.ApplicationTracker; import com.commonsensenet.realfarm.utils.ApplicationTracker.EventType; public class HarvestActionActivity extends DataFormActivity { public static final String AMOUNT = "amount"; public static final String DAY = "day"; public static final String MONTH = "month"; public static final String SATISFACTION = "satisfaction"; public static final String UNIT = "unit"; public static final String VARIETY = "variety"; private int mAmount; private int mDay; private int mMonth; private int mSatisfaction; private int mUnit; private int mVariety; private int defaultVariety = -1; private int defaultMonth = -1; private int defaultUnit = -1; private int defaultSatisfaction = -1; private String defaultAmount = "0"; private String defaultDay = "0"; private List<Resource> varietyList; private List<Resource> monthList; private List<Resource> unitList; private List<Resource> satisfactionList; @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.equals(mHelpItem)) { // tracks the application usage ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), item.getTitle()); playAudio(R.raw.harvest_help, true); return true; } else { // asks the parent. return super.onOptionsItemSelected(item); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState, R.layout.act_harvest_action); varietyList = mDataProvider.getVarietiesByPlotAndSeason(Global.plotId); monthList = mDataProvider .getResources(RealFarmDatabase.RESOURCE_TYPE_MONTH); unitList = mDataProvider .getUnits(RealFarmDatabase.ACTION_TYPE_HARVEST_ID); satisfactionList = mDataProvider .getResources(RealFarmDatabase.RESOURCE_TYPE_SATISFACTION); playAudio(R.raw.clickingharvest); // adds the fields that need to be validated. mResultsMap.put(VARIETY, defaultVariety); mResultsMap.put(DAY, defaultDay); mResultsMap.put(MONTH, defaultMonth); mResultsMap.put(AMOUNT, defaultAmount); mResultsMap.put(UNIT, defaultUnit); mResultsMap.put(SATISFACTION, defaultSatisfaction); View item1 = findViewById(R.id.dlg_lbl_harvest_crop); View item2 = findViewById(R.id.dlg_lbl_day_harvest); View item3 = findViewById(R.id.dlg_lbl_month_harvest); View item4 = findViewById(R.id.dlg_lbl_unit_no_harvest); View item5 = findViewById(R.id.dlg_lbl_units_harvest); View item6 = findViewById(R.id.dlg_lbl_satisfaction_harvest); item1.setOnLongClickListener(this); item2.setOnLongClickListener(this); item3.setOnLongClickListener(this); item4.setOnLongClickListener(this); item5.setOnLongClickListener(this); item6.setOnLongClickListener(this); View harvest_crop = findViewById(R.id.var_harvest_crop); View harvest_date = findViewById(R.id.harvest_date_tr); View Amount = findViewById(R.id.units_harvest_tr); View harvest_satisfaction = findViewById(R.id.satisfaction_harvest_tr); harvest_crop.setOnLongClickListener(this); harvest_date.setOnLongClickListener(this); Amount.setOnLongClickListener(this); harvest_satisfaction.setOnLongClickListener(this); item1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialog(v, varietyList, VARIETY, "Select the variety", R.raw.select_the_variety, R.id.dlg_lbl_harvest_crop, R.id.var_harvest_crop, 0); } }); item2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialogNP("Choose the day", DAY, R.raw.dateinfo, 1, 31, Calendar.getInstance().get(Calendar.DAY_OF_MONTH), 1, 0, R.id.dlg_lbl_day_harvest, R.id.harvest_date_tr, R.raw.ok, R.raw.cancel, R.raw.day_ok, R.raw.day_cancel); } }); item3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialog(v, monthList, MONTH, "Select the month", R.raw.choosethemonth, R.id.dlg_lbl_month_harvest, R.id.harvest_date_tr, 0); } }); item4.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialogNP("Choose the number of bags", AMOUNT, R.raw.noofbags, 1, 200, 0, 1, 0, R.id.dlg_lbl_unit_no_harvest, R.id.units_harvest_tr, R.raw.ok, R.raw.cancel, R.raw.bag_ok, R.raw.bag_cancel); } }); item5.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialog(v, unitList, UNIT, "Select the unit", R.raw.selecttheunits, R.id.dlg_lbl_units_harvest, R.id.units_harvest_tr, 2); } }); item6.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { stopAudio(); ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); displayDialog(v, satisfactionList, SATISFACTION, "Are you satisfied?", R.raw.are_you_satisfied, R.id.dlg_lbl_satisfaction_harvest, R.id.satisfaction_harvest_tr, 1); } }); } @Override public boolean onLongClick(View v) { ApplicationTracker.getInstance().logEvent(EventType.LONG_CLICK, Global.userId, getLogTag(), getResources().getResourceEntryName(v.getId())); // all long click sounds override the sound enabled flag. if (v.getId() == R.id.dlg_lbl_harvest_crop) { if ((Integer) mResultsMap.get(VARIETY) == defaultVariety) playAudio(R.raw.select_the_variety, true); else playAudio(varietyList.get(((Integer) mResultsMap.get(VARIETY))) .getAudio()); } else if (v.getId() == R.id.dlg_lbl_unit_no_harvest) { if (mResultsMap.get(AMOUNT).equals(defaultAmount)) playAudio(R.raw.select_unit_number, true); // TODO AUDIO: Say the number // Integer.valueOf(mResultsMap.get(DAY).toString()); else playAudio(R.raw.problems, true); } else if (v.getId() == R.id.dlg_lbl_units_harvest) { if ((Integer) mResultsMap.get(UNIT) == defaultUnit) playAudio(R.raw.selecttheunits, true); else playAudio(unitList.get(((Integer) mResultsMap.get(UNIT))) .getAudio()); } else if (v.getId() == R.id.dlg_lbl_day_harvest) { if (mResultsMap.get(DAY).equals(defaultDay)) playAudio(R.raw.selectthedate, true); else { playInteger(Integer.valueOf(mResultsMap.get(DAY).toString())); playSound(); } } else if (v.getId() == R.id.dlg_lbl_month_harvest) { if ((Integer) mResultsMap.get(MONTH) == defaultMonth) playAudio(R.raw.choosethemonthwhenharvested, true); else playAudio(monthList.get(((Integer) mResultsMap.get(MONTH))) .getAudio()); } else if (v.getId() == R.id.dlg_lbl_satisfaction_harvest) { if ((Integer) mResultsMap.get(SATISFACTION) == defaultSatisfaction) playAudio(R.raw.are_you_satisfied, true); else playAudio(satisfactionList.get( ((Integer) mResultsMap.get(SATISFACTION))).getAudio()); } else if (v.getId() == R.id.harvest_date_tr) { playAudio(R.raw.harvestyear, true); } else if (v.getId() == R.id.units_harvest_tr) { playAudio(R.raw.amount, true); } else if (v.getId() == R.id.var_harvest_crop) { playAudio(R.raw.variety, true); } else if (v.getId() == R.id.satisfaction_harvest_tr) { playAudio(R.raw.satisfaction, true); } else { return super.onLongClick(v); } // shows the help icon. showHelpIcon(v); return true; } @Override protected Boolean validateForm() { mDay = Integer.valueOf(mResultsMap.get(DAY).toString()); mAmount = Integer.valueOf(mResultsMap.get(AMOUNT).toString()); boolean isValid = true; if ((Integer) mResultsMap.get(VARIETY) != defaultVariety) { highlightField(R.id.var_harvest_crop, false); } else { ApplicationTracker.getInstance().logEvent(EventType.ERROR, Global.userId, VARIETY); isValid = false; highlightField(R.id.var_harvest_crop, true); } if ((Integer) mResultsMap.get(MONTH) != defaultMonth && mDay > Integer.parseInt(defaultDay) && validDate(mDay, monthList.get((Integer) mResultsMap.get(MONTH)).getId())) { highlightField(R.id.harvest_date_tr, false); } else { ApplicationTracker.getInstance().logEvent(EventType.ERROR, Global.userId, MONTH, DAY); isValid = false; highlightField(R.id.harvest_date_tr, true); } if ((Integer) mResultsMap.get(UNIT) != defaultUnit && mAmount > Integer.parseInt(defaultAmount)) { highlightField(R.id.units_harvest_tr, false); } else { ApplicationTracker.getInstance().logEvent(EventType.ERROR, Global.userId, UNIT, UNIT); isValid = false; highlightField(R.id.units_harvest_tr, true); } if ((Integer) mResultsMap.get(SATISFACTION) != defaultSatisfaction) { highlightField(R.id.satisfaction_harvest_tr, false); } else { ApplicationTracker.getInstance().logEvent(EventType.ERROR, Global.userId, SATISFACTION); isValid = false; highlightField(R.id.satisfaction_harvest_tr, true); } // if all fields are valid the data is inserted in the database. if (isValid) { ApplicationTracker.getInstance().logEvent(EventType.CLICK, Global.userId, getLogTag(), "data entered"); mVariety = varietyList.get((Integer) mResultsMap.get(VARIETY)) .getId(); mMonth = monthList.get((Integer) mResultsMap.get(MONTH)).getId(); mUnit = unitList.get((Integer) mResultsMap.get(UNIT)).getId(); mSatisfaction = satisfactionList.get( (Integer) mResultsMap.get(SATISFACTION)).getId(); long result = mDataProvider.addHarvestAction(Global.userId, Global.plotId, mVariety, mAmount, mUnit, mSatisfaction, getDate(mDay, mMonth), Global.isAdmin); return result != -1; } return false; } }