diff --git a/src/com/forfolias/leledroid/details.java b/src/com/forfolias/leledroid/details.java new file mode 100644 index 0000000..6c69f1a --- /dev/null +++ b/src/com/forfolias/leledroid/details.java @@ -0,0 +1,451 @@ +package com.forfolias.leledroid; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +import org.achartengine.ChartFactory; +import org.achartengine.GraphicalView; +import org.achartengine.model.CategorySeries; +import org.achartengine.renderer.DefaultRenderer; +import org.achartengine.renderer.SimpleSeriesRenderer; +import org.joda.time.DateTime; +import org.joda.time.Days; +import org.joda.time.Hours; +import org.joda.time.Minutes; +import org.joda.time.Seconds; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.graphics.Color; +import android.os.Bundle; +import android.os.CountDownTimer; +import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; +import android.widget.Toast; + +public class details extends Activity { + + int selectedItem; + + TextView nameTv; + TextView secondsTv; + TextView minutesTv; + TextView hoursTv; + TextView daysTv; + TextView weeksTv; + TextView monthsTv; + TextView posostoTv; + ProgressBar posostoPB; + Bundle b; + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.details); + b = this.getIntent().getExtras(); + + nameTv = (TextView) findViewById(R.id.onoma); + secondsTv = (TextView) findViewById(R.id.secs); + minutesTv = (TextView) findViewById(R.id.mins); + hoursTv = (TextView) findViewById(R.id.hours); + daysTv = (TextView) findViewById(R.id.days); + weeksTv = (TextView) findViewById(R.id.weeks); + monthsTv = (TextView) findViewById(R.id.months); + posostoPB = (ProgressBar) findViewById(R.id.posostoProgress); + posostoTv = (TextView) findViewById(R.id.pososto); + + setLabels(); + + MyCount counter = new MyCount(Long.parseLong((String) secondsTv + .getText()) * 1000, 1000); + counter.start(); + + } + + public class MyCount extends CountDownTimer { + int secs = 0; + int mins = 0; + + public MyCount(long millisInFuture, long countDownInterval) { + super(millisInFuture, countDownInterval); + } + + public void onFinish() { + secondsTv.setText("0"); + minutesTv.setText("0"); + hoursTv.setText("0"); + daysTv.setText("0"); + weeksTv.setText("0"); + monthsTv.setText("0"); + posostoPB.setProgress(100); + posostoTv.setText("100%"); + } + + public void onTick(long millisUntilFinished) { + secondsTv.setText("" + millisUntilFinished / 1000); + secs++; + if (secs > 59) { + secs = 0; + mins++; + minutesTv.setText("" + + (Integer.parseInt((String) minutesTv.getText()) - 1)); + if (mins > 59){ + mins = 0; + hoursTv.setText("" + + (Integer.parseInt((String) hoursTv.getText()) - 1)); + } + } + } + } + + public void setLabels() { + + DateTime curDate = new DateTime(); + DateTime inDate = setDateFromString(b.getString("indate")); + DateTime outDate = setDateFromString(b.getString("outdate")); + + outDate = setFilakes(inDate, outDate, + Integer.parseInt(b.getString("filaki"))); + + Float totalPososto = getPososto(Seconds.secondsBetween(inDate, curDate) + .getSeconds(), Seconds.secondsBetween(inDate, outDate) + .getSeconds()); + setName(totalPososto); + setPososto(totalPososto); + setRestOfTv(curDate, outDate); + Integer ipolipo = Days.daysBetween(curDate, outDate).getDays() + - Integer.parseInt(b.getString("adeia")); + Integer perasan = Days.daysBetween(inDate, curDate).getDays(); + + setChart(Integer.parseInt(b.getString("adeia")), ipolipo, perasan); + + } + + public void setChart(Integer adeia, Integer ipolipo, Integer perasan) { + if (ipolipo < 0) { + adeia += ipolipo; + ipolipo = 0; + } + if (adeia < 0) { + adeia = 0; + } + if (perasan < 0) { + perasan = 0; + } + String[] labels = { adeia.toString(), ipolipo.toString(), + perasan.toString() }; + GraphicalView mChartView; + LinearLayout layout = (LinearLayout) findViewById(R.id.pieChart); + + double[] values = new double[] { adeia, ipolipo, perasan }; + int[] colors = new int[] { Color.BLUE, Color.RED, Color.GREEN }; + DefaultRenderer renderer = buildCategoryRenderer(colors); + mChartView = ChartFactory.getPieChartView(this, + buildCategoryDataset("Γράφημα", labels, values), renderer); + + layout.addView(mChartView); + layout.setOnClickListener(new View.OnClickListener() { + + public void onClick(View v) { + LayoutInflater inflater = getLayoutInflater(); + View layout = inflater.inflate(R.layout.chart, + (ViewGroup) findViewById(R.id.LL1)); + + Toast toastView = new Toast(getBaseContext()); + toastView.setGravity(Gravity.CENTER_VERTICAL, 0, 0); + toastView.setView(layout); + toastView.setDuration(Toast.LENGTH_LONG); + toastView.show(); + } + }); + } + + public void backButtonClicked(View v) { + finish(); + } + + public void editButtonClicked(View v) { + Intent str = new Intent(this, com.forfolias.leledroid.properties.class); + str.putExtras(b); + startActivity(str); + finish(); + } + + public void deleteButtonClicked(View v) { + DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: + deleteButtonOk(); + break; + + case DialogInterface.BUTTON_NEGATIVE: + return; + } + } + }; + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(getResources().getString(R.string.delete)) + .setPositiveButton("Yes", dialogClickListener) + .setMessage(getResources().getString(R.string.deleteMessage)) + .setNegativeButton("No", dialogClickListener).show(); + + } + + public void deleteButtonOk() { + Toast toast = null; + + try { + OutputStreamWriter out = new OutputStreamWriter(openFileOutput( + "data.txt.tmp", MODE_PRIVATE)); + InputStream instream = openFileInput("data.txt"); + String line = null; + int counter = 0; + + if (instream != null) { + InputStreamReader inputreader = new InputStreamReader(instream); + BufferedReader buffreader = new BufferedReader(inputreader); + + while ((line = buffreader.readLine()) != null) { + if (counter != Integer.parseInt(b.getString("selected"))) { + out.write(line + "\n"); + } + counter++; + } + } + instream.close(); + out.close(); + + if (deleteFile("data.txt") == false) { + toast = Toast.makeText(getApplicationContext(), + "Error deleting data.txt", Toast.LENGTH_SHORT); + toast.show(); + } + + } catch (java.io.FileNotFoundException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + + } + + try { + OutputStreamWriter out = new OutputStreamWriter(openFileOutput( + "data.txt", MODE_PRIVATE)); + InputStream instream = openFileInput("data.txt.tmp"); + String line = null; + + if (instream != null) { + InputStreamReader inputreader = new InputStreamReader(instream); + BufferedReader buffreader = new BufferedReader(inputreader); + + while ((line = buffreader.readLine()) != null) { + out.write(line + "\n"); + } + } + instream.close(); + out.close(); + + if (deleteFile("data.txt.tmp") == false) { + toast = Toast.makeText(getApplicationContext(), + "Error deleting data.txt.tmp", Toast.LENGTH_SHORT); + toast.show(); + } + } catch (java.io.FileNotFoundException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } + finish(); + } + + public void setName(Float totalPososto) { + + String vath = "", oplo = ""; + ImageView vathmosI = (ImageView) findViewById(R.id.vathmosImg); + + if (b.getString("oplo").equals("Πεζικό")) { + oplo = "ΠΖ"; + } else if (b.getString("oplo").equals("Πυροβολικό")) { + oplo = "ΠΒ"; + } else if (b.getString("oplo").equals("Τεθωρακισμένα")) { + oplo = "ΤΘ"; + } else if (b.getString("oplo").equals("Μηχανικό")) { + oplo = "ΜΧ"; + } else if (b.getString("oplo").equals("Διαβιβάσεις")) { + oplo = "ΔΒ"; + } else if (b.getString("oplo").equals("Αεροπορία_Στρατού")) { + oplo = "ΑΣ"; + } else if (b.getString("oplo").equals("Εφοδιασμού_Μεταφορών")) { + oplo = "ΕΜ"; + } else if (b.getString("oplo").equals("Υλικού_Πολέμου")) { + oplo = "ΥΠ"; + } else if (b.getString("oplo").equals("Τεχνικό")) { + oplo = "ΤΧ"; + } else if (b.getString("oplo").equals("Υγειονομικό")) { + oplo = "ΥΓ"; + } + + if (totalPososto < 8) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img1)); + vath = "ΣΤΡ"; + } else if (totalPososto < 14) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img2)); + vath = "ΥΔΝΕΑΣ"; + } else if (totalPososto < 20) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img3)); + vath = "ΔΝΕΑΣ"; + } else if (totalPososto < 22) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img4)); + vath = "ΛΧΙΑΣ"; + } else if (totalPososto < 28) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img5)); + vath = "ΕΠΧΙΑΣ"; + } else if (totalPososto < 34) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img6)); + vath = "ΑΛΧΙΑΣ"; + } else if (totalPososto < 40) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img7)); + vath = "ΑΝΘΣΤΗΣ"; + } else if (totalPososto < 45) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img8)); + vath = "ΑΝΘΛΓΟΣ"; + } else if (totalPososto < 50) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img9)); + vath = "ΥΠΛΓΟΣ"; + } else if (totalPososto < 56) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img10)); + vath = "ΛΓΟΣ"; + } else if (totalPososto < 62) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img11)); + vath = "ΤΧΗΣ"; + } else if (totalPososto < 68) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img12)); + vath = "ΑΝΧΗΣ"; + } else if (totalPososto < 73) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img13)); + vath = "ΣΧΗΣ"; + } else if (totalPososto < 79) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img14)); + vath = "ΤΞΧΟΣ"; + } else if (totalPososto < 85) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img15)); + vath = "ΥΣΤΓΟΣ"; + } else if (totalPososto < 91) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img16)); + vath = "ΑΝΤΓΟΣ"; + } else if (totalPososto < 97) { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img17)); + vath = "ΣΤΓΟΣ"; + } else { + vathmosI.setImageDrawable(getResources().getDrawable( + R.drawable.img18)); + vath = "ΠΟΛΙΤΗΣ"; + oplo = "ΚΟΣΜΟΥ"; + } + nameTv.setText(vath + "(" + oplo + ") " + b.getString("onoma")); + } + + public void setPososto(Float totalPososto) { + if (totalPososto < 0) + totalPososto = (float) 0; + posostoPB.setProgress(totalPososto.intValue()); + posostoTv.setText(totalPososto.intValue() + "%"); + } + + public Float getPososto(Integer a, Integer b) { + return (float) a * 100 / b; + } + + public void setRestOfTv(DateTime curDate, DateTime outDate) { + + Integer meres = Days.daysBetween(curDate, outDate).getDays(); + + secondsTv.setText(Integer.toString(Seconds.secondsBetween(curDate, + outDate).getSeconds())); + minutesTv.setText(Integer.toString(Minutes.minutesBetween(curDate, + outDate).getMinutes())); + hoursTv.setText(Integer.toString(Hours.hoursBetween(curDate, outDate) + .getHours())); + daysTv.setText(Integer.toString(meres)); + weeksTv.setText(String.format("%.2f", (float) meres / 7)); + monthsTv.setText(String.format("%.2f", (float) meres / 30)); + } + + public DateTime setDateFromString(String temp) { + Integer d = Integer.parseInt(temp.split("/")[0]); + Integer m = Integer.parseInt(temp.split("/")[1]); + Integer y = Integer.parseInt(temp.split("/")[2]); + + return new DateTime(y, m, d, 12, 0, 0, 0); + } + + public DateTime setFilakes(DateTime inDate, DateTime outDate, Integer fil) { + if (fil > 0) { + if (fil > 40) { + return outDate.plusDays(fil); + } else if (fil > 20) { + return outDate.plusDays(fil - 20); + } + } + return outDate; + } + + protected DefaultRenderer buildCategoryRenderer(int[] colors) { + DefaultRenderer renderer = new DefaultRenderer(); + for (int color : colors) { + SimpleSeriesRenderer r = new SimpleSeriesRenderer(); + r.setColor(color); + renderer.addSeriesRenderer(r); + } + renderer.setLabelsTextSize(10); + renderer.setShowLegend(false); + return renderer; + } + + protected CategorySeries buildCategoryDataset(String title, + String[] labels, double[] values) { + CategorySeries series = new CategorySeries(title); + series.add(labels[0], values[0]); + series.add(labels[1], values[1]); + series.add(labels[2], values[2]); + return series; + } + +} diff --git a/src/com/forfolias/leledroid/info.java b/src/com/forfolias/leledroid/info.java new file mode 100644 index 0000000..c5b1f0a --- /dev/null +++ b/src/com/forfolias/leledroid/info.java @@ -0,0 +1,73 @@ +package com.forfolias.leledroid; + +import java.util.regex.Pattern; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.text.Html; +import android.text.method.LinkMovementMethod; +import android.text.util.Linkify; +import android.view.Gravity; +import android.view.View; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +public class info extends Activity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.info); + + TextView link = (TextView) findViewById(R.id.site); + TextView cont = (TextView) findViewById(R.id.contact); + + link.setText("" + link.getText() + + ""); + cont.setText(cont.getText() + + " "); + Linkify.addLinks(cont, Pattern.compile("forfolias@gmail.com"), + "http://"); + Linkify.addLinks(link, + Pattern.compile(getResources().getString(R.id.site)), "http://"); + cont.setText(Html.fromHtml(cont.getText() + + "forfolias@gmail.com")); + link.setText(Html.fromHtml((String) link.getText())); + cont.setMovementMethod(LinkMovementMethod.getInstance()); + link.setMovementMethod(LinkMovementMethod.getInstance()); + + ImageView img = (ImageView) findViewById(R.id.infoImage); + img.setOnLongClickListener(new View.OnLongClickListener() { + + public boolean onLongClick(View v) { + Toast toast = Toast.makeText(getApplicationContext(), + "Άκυρο ψαρά!", Toast.LENGTH_SHORT); + toast.setGravity(Gravity.TOP, 75, 35); + toast.show(); + return true; + } + }); + + } + + public void donateButtonClicked(View v) { + Intent browse = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=M5HYBKFQYS84S&lc=GR&item_name=Donation%20to%20LeleDroid%20application¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted") ); + startActivity(browse); + } + + public void licenceButtonClicked(View v) { + AlertDialog.Builder alertbox = new AlertDialog.Builder(this); + alertbox.setMessage(getResources().getString(R.string.licenceText)) + .setTitle(getResources().getString(R.string.licence)); + alertbox.setNeutralButton(getResources().getString(R.string.back), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface arg0, int arg1) { + } + }); + alertbox.show(); + } +} diff --git a/src/com/forfolias/leledroid/leledroid.java b/src/com/forfolias/leledroid/leledroid.java new file mode 100644 index 0000000..67c7780 --- /dev/null +++ b/src/com/forfolias/leledroid/leledroid.java @@ -0,0 +1,112 @@ +package com.forfolias.leledroid; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; + +public class leledroid extends Activity { + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + } + + public void onStart() { + Toast toast = null; + ArrayAdapter adapter; + ListView strList; + super.onStart(); + strList = (ListView) findViewById(R.id.strList); + String line = null; + adapter = new ArrayAdapter(this, + android.R.layout.simple_list_item_1); + + try { + InputStream instream = openFileInput("data.txt"); + if (instream != null) { + Integer c = 0; + InputStreamReader inputreader = new InputStreamReader(instream); + BufferedReader buffreader = new BufferedReader(inputreader); + + while ((line = buffreader.readLine()) != null) { + adapter.add(line.split(" * ")[0].replace('_', ' ')); + c++; + } + if (c == 0) adapter.add(getResources().getString(R.string.emptyList)); + } + instream.close(); + } catch (java.io.FileNotFoundException e) { + adapter.add(getResources().getString(R.string.emptyList)); + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } + strList.setOnItemClickListener(new ListView.OnItemClickListener() { + public void onItemClick(AdapterView a, View v, int i, long l) { + try { + Intent inte = new Intent(leledroid.this, details.class); + Bundle b = new Bundle(); + String line1 = null; + Toast toast; + + try { + InputStream instream = openFileInput("data.txt"); + if (instream != null) { + InputStreamReader inputreader = new InputStreamReader( + instream); + BufferedReader buffreader = new BufferedReader( + inputreader); + for (int k = 0; k < i + 1; k++) { + line1 = buffreader.readLine(); + } + } + instream.close(); + } catch (java.io.FileNotFoundException e) { + ; + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), + e.getMessage(), Toast.LENGTH_SHORT); + toast.show(); + + } + + b.putString("selected", (String) Integer.toString(i)); + b.putString("onoma", (String) line1.split(" * ")[0].replace('_', ' ')); + b.putString("indate", (String) line1.split(" * ")[2]); + b.putString("outdate", (String) line1.split(" * ")[4]); + b.putString("adeia", (String) line1.split(" * ")[6]); + b.putString("filaki", (String) line1.split(" * ")[8]); + b.putString("oplo", (String) line1.split(" * ")[10]); + inte.putExtras(b); + startActivity(inte); + } catch (Exception e) { + ; + } + } + }); + strList.setAdapter(adapter); + } + + public void onAddButtonClick(View v) { + Intent str = new Intent(this, com.forfolias.leledroid.properties.class); + startActivity(str); + } + + public void onInfoButtonClick(View v) { + Intent str = new Intent(this, com.forfolias.leledroid.info.class); + startActivity(str); + } +} diff --git a/src/com/forfolias/leledroid/properties.java b/src/com/forfolias/leledroid/properties.java new file mode 100644 index 0000000..fc21c54 --- /dev/null +++ b/src/com/forfolias/leledroid/properties.java @@ -0,0 +1,319 @@ +package com.forfolias.leledroid; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import android.app.Activity; +import android.app.DatePickerDialog; +import android.app.Dialog; +import android.app.DatePickerDialog.OnDateSetListener; +import android.os.Bundle; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.DatePicker; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.Toast; + +public class properties extends Activity { + + EditText str_name; + EditText str_indate; + EditText str_outdate; + EditText str_adeia; + EditText str_filaki; + Spinner str_oplo; + + static final int DATE_DIALOG_ID = 0; + + private EditText activeDateDisplay; + private Calendar activeDate; + + int selectedItem; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.properties); + Bundle b = this.getIntent().getExtras(); + + str_name = (EditText) findViewById(R.id.str_name_et); + str_indate = (EditText) findViewById(R.id.str_indate_et); + str_outdate = (EditText) findViewById(R.id.str_outdate_et); + str_adeia = (EditText) findViewById(R.id.str_adeia_et); + str_filaki = (EditText) findViewById(R.id.str_filaki_et); + str_oplo = (Spinner) findViewById(R.id.oploSpinner); + + ArrayAdapter adapter = ArrayAdapter.createFromResource( + this, R.array.opla, android.R.layout.simple_spinner_item); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + str_oplo.setAdapter(adapter); + + str_indate.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + showDateDialog(str_indate, Calendar.getInstance()); + } + }); + + str_outdate.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + showDateDialog(str_outdate, Calendar.getInstance()); + } + }); + + updateDisplay(str_indate, Calendar.getInstance()); + updateDisplay(str_outdate, Calendar.getInstance()); + + if (b == null) { + selectedItem = -1; + } else { + selectedItem = Integer.parseInt(b.getString("selected")); + str_name.setText(b.getString("onoma")); + str_indate.setText(b.getString("indate")); + str_outdate.setText(b.getString("outdate")); + str_adeia.setText(b.getString("adeia")); + str_filaki.setText(b.getString("filaki")); + + String[] oplo = getResources().getStringArray(R.array.opla); + + for (int i = 0; i < oplo.length; i++) { + if (oplo[i].equals(b.getString("oplo"))) { + str_oplo.setSelection(i); + } + } + } + } + + private void updateDisplay(EditText dateDisplay, Calendar date) { + dateDisplay.setText(new StringBuilder() + .append(date.get(Calendar.DAY_OF_MONTH)).append("/") + .append(date.get(Calendar.MONTH) + 1).append("/") + .append(date.get(Calendar.YEAR)).append(" ")); + + } + + public void showDateDialog(EditText dateDisplay, Calendar date) { + activeDateDisplay = dateDisplay; + activeDate = date; + showDialog(DATE_DIALOG_ID); + } + + private OnDateSetListener dateSetListener = new OnDateSetListener() { + public void onDateSet(DatePicker view, int year, int monthOfYear, + int dayOfMonth) { + activeDate.set(Calendar.YEAR, year); + activeDate.set(Calendar.MONTH, monthOfYear); + activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); + updateDisplay(activeDateDisplay, activeDate); + unregisterDateDisplay(); + } + }; + + private void unregisterDateDisplay() { + activeDateDisplay = null; + activeDate = null; + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case DATE_DIALOG_ID: + return new DatePickerDialog(this, dateSetListener, + activeDate.get(Calendar.YEAR), + activeDate.get(Calendar.MONTH), + activeDate.get(Calendar.DAY_OF_MONTH)); + } + return null; + } + + @Override + protected void onPrepareDialog(int id, Dialog dialog) { + super.onPrepareDialog(id, dialog); + switch (id) { + case DATE_DIALOG_ID: + ((DatePickerDialog) dialog).updateDate( + activeDate.get(Calendar.YEAR), + activeDate.get(Calendar.MONTH), + activeDate.get(Calendar.DAY_OF_MONTH)); + break; + } + } + + public void okButtonClicked(View v) { + String dataToWrite = null; + Toast toast = null; + + if (!checkOnoma()) + return; + if (!checkAdeia()) + return; + if (!checkFilak()) + return; + if (!checkDates(str_indate.getText().toString(), str_outdate.getText().toString())) + return; + + + dataToWrite = str_name.getText().toString().replace(' ', '_') + " * " + + str_indate.getText().toString() + " * " + + str_outdate.getText().toString() + " * " + + str_adeia.getText().toString() + " * " + + str_filaki.getText().toString() + " * " + + str_oplo.getSelectedItem().toString(); + + try { + OutputStreamWriter out = new OutputStreamWriter(openFileOutput( + "data.txt.tmp", MODE_PRIVATE)); + InputStream instream = openFileInput("data.txt"); + String line = null; + int counter = 0; + + if (instream != null) { + InputStreamReader inputreader = new InputStreamReader(instream); + BufferedReader buffreader = new BufferedReader(inputreader); + + while ((line = buffreader.readLine()) != null) { + if (counter != selectedItem) { + out.write(line + "\n"); + } + counter++; + } + } + instream.close(); + out.close(); + + if (deleteFile("data.txt") == false) { + toast = Toast.makeText(getApplicationContext(), + "Error deleting data.txt", Toast.LENGTH_SHORT); + toast.show(); + } + + } catch (java.io.FileNotFoundException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } + + try { + OutputStreamWriter out = new OutputStreamWriter(openFileOutput( + "data.txt", MODE_PRIVATE)); + InputStream instream = openFileInput("data.txt.tmp"); + String line = null; + + if (instream != null) { + InputStreamReader inputreader = new InputStreamReader(instream); + BufferedReader buffreader = new BufferedReader(inputreader); + + while ((line = buffreader.readLine()) != null) { + out.write(line + "\n"); + } + } + instream.close(); + out.close(); + + if (deleteFile("data.txt.tmp") == false) { + toast = Toast.makeText(getApplicationContext(), + "Error deleting data.txt.tmp", Toast.LENGTH_SHORT); + toast.show(); + } + } catch (java.io.FileNotFoundException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } catch (IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } + + try { + OutputStreamWriter out = new OutputStreamWriter(openFileOutput( + "data.txt", MODE_APPEND)); + out.write(dataToWrite + "\n"); + out.close(); + } catch (java.io.IOException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), + Toast.LENGTH_SHORT); + toast.show(); + } + + finish(); + } + + public void cancelButtonClicked(View v) { + finish(); + } + + public boolean checkAdeia() { + Toast toast = null; + if (str_adeia.getText().toString().equals(new String(""))) { + toast = Toast.makeText(getApplicationContext(), getResources() + .getString(R.string.emptyAdeia), Toast.LENGTH_SHORT); + toast.show(); + return false; + } + return true; + } + + public boolean checkOnoma() { + Toast toast = null; + if (str_name.getText().toString().equals(new String(""))) { + toast = Toast.makeText(getApplicationContext(), getResources() + .getString(R.string.emptyName), Toast.LENGTH_SHORT); + toast.show(); + return false; + } + return true; + } + + public boolean checkFilak() { + Toast toast = null; + if (str_filaki.getText().toString().equals(new String(""))) { + toast = Toast.makeText(getApplicationContext(), getResources() + .getString(R.string.emptyFilaki), Toast.LENGTH_SHORT); + toast.show(); + return false; + } + return true; + } + + public boolean checkDates(String dateIn, String dateOut) { + Toast toast = null; + String myFormatString = "dd/MM/yyyy"; + SimpleDateFormat df = new SimpleDateFormat(myFormatString); + Date date1 = null; + Date date2 = null; + + try { + date1 = df.parse(dateIn); + } catch (ParseException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT); + toast.show(); + } + + try { + date2 = df.parse(dateOut); + } catch (ParseException e) { + toast = Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT); + toast.show(); + } + if (date1.compareTo(date2) >= 0 ){ + toast = Toast.makeText(getApplicationContext(), getResources() + .getString(R.string.dateForward), Toast.LENGTH_LONG); + toast.show(); + return false; + } + return true; + } +}