Commit 7b5479b1 authored by Oscar Megia Lopez's avatar Oscar Megia Lopez
Browse files

version 0.4beta

Updated to save date in a file and read it when app starts (sometimes
android closes app). This way user don't lost lines when android closes
app. Also if user switch to landscape, the screen clean variables. Now when
reloading screen, this app read file and restore lines.
Update menu. Added reset to delete file and clean lines.
parent e7d096d5
......@@ -8,7 +8,7 @@ android {
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "0.3beta"
versionName "0.4beta"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
......
package com.oml.recordtimedroid;
class DiasHorasMinutosSegundosMilisegundos {
private long dias;
private long horas;
private long minutos;
private long segundos;
private long milisegundos;
DiasHorasMinutosSegundosMilisegundos() {
dias = 0;
horas = 0;
minutos = 0;
segundos = 0;
milisegundos = 0;
}
long getDias() {
return dias;
}
void setDias(long dias) {
this.dias = dias;
}
long getHoras() {
return horas;
}
void setHoras(long horas) {
this.horas = horas;
}
long getMinutos() {
return minutos;
}
void setMinutos(long minutos) {
this.minutos = minutos;
}
long getSegundos() {
return segundos;
}
void setSegundos(long segundos) {
this.segundos = segundos;
}
long getMilisegundos() {
return milisegundos;
}
void setMilisegundos(long milisegundos) {
this.milisegundos = milisegundos;
}
}
\ No newline at end of file
......@@ -13,14 +13,22 @@ import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
public class ScrollingActivity extends AppCompatActivity {
private TextView tView;
private static int linea = 1;
private static String text = "";
private static Date dFechaAnterior;
private int linea = 1;
private StringBuffer text;
private Date dFechaAnterior;
private String data;
private String fileName = "lines";
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -29,6 +37,44 @@ public class ScrollingActivity extends AppCompatActivity {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tView = (TextView) findViewById(R.id.textView1);
text = new StringBuffer();
try {
String[] sFileList = this.fileList();
if (Arrays.asList(sFileList).contains(fileName)) {
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS", Locale.ROOT);
DiasHorasMinutosSegundosMilisegundos dhmsmTempo = new DiasHorasMinutosSegundosMilisegundos();
FileInputStream fin = openFileInput(fileName);
int c;
String sFecha = "";
while ((c = fin.read()) != -1) {
if (c == '\n') {
Date d = format.parse(sFecha);
text.append(String.format(Locale.ROOT, "%03d %s ", linea++, sFecha));
if (dFechaAnterior != null) {
calculaDiasHorasMinutosSegundos(dFechaAnterior, d, dhmsmTempo);
text.append(String.format(Locale.ROOT, " %03d %s %02d:%02d:%02d.%03d",
(int) dhmsmTempo.getDias(), getResources().getString(R.string.dias),
(int) dhmsmTempo.getHoras(),
(int) dhmsmTempo.getMinutos(), (int) dhmsmTempo.getSegundos(),
(int) dhmsmTempo.getMilisegundos()));
}
text.append("\n");
dFechaAnterior = format.parse(sFecha);
sFecha = "";
} else {
sFecha += Character.toString((char) c);
}
}
fin.close();
}
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"ERROR " + e.getMessage() +
" reading file",Toast.LENGTH_LONG).show();
}
tView.setText(text);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
......@@ -37,43 +83,71 @@ public class ScrollingActivity extends AppCompatActivity {
public void onClick(View view) {
String notification = getResources().getString(R.string.notificacion) + " " + linea;
Snackbar.make(view, notification, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
text += String.format(Locale.ROOT,"%03d - ", linea++) +
android.text.format.DateFormat.format("dd-MM-yyyy HH:mm:ss",
new java.util.Date()) + " ";
if (dFechaAnterior != null) {
Date dAhora = new Date();
long different = dAhora.getTime() - dFechaAnterior.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
different = different % secondsInMilli;
long elapsedMiliseconds = different;
text += String.format(Locale.ROOT, " - %03d %s %02d:%02d:%02d.%03d ", (int)elapsedDays,
getResources().getString(R.string.dias), (int)elapsedHours,
(int)elapsedMinutes, (int)elapsedSeconds, (int)elapsedMiliseconds);
.setAction("Action", null).show();
Date dFechaHora = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS", Locale.ROOT);
CharSequence csFechaHora = dateFormat.format(dFechaHora);
Date dAhora = new Date();
text.append(String.format(Locale.ROOT, "%03d ", linea));
text.append(csFechaHora);
text.append(" ");
if (linea > 1) {
DiasHorasMinutosSegundosMilisegundos dhmsmTempo =
new DiasHorasMinutosSegundosMilisegundos();
calculaDiasHorasMinutosSegundos(dFechaAnterior, dAhora, dhmsmTempo);
text.append(String.format(Locale.ROOT, " %03d %s %02d:%02d:%02d.%03d",
(int) dhmsmTempo.getDias(), getResources().getString(R.string.dias), (int) dhmsmTempo.getHoras(),
(int) dhmsmTempo.getMinutos(), (int) dhmsmTempo.getSegundos(),
(int) dhmsmTempo.getMilisegundos()));
}
text += "\n";
linea++;
text.append("\n");
tView.setText(text);
dFechaAnterior = new Date();
data = dateFormat.format(dFechaHora) + "\n";
// Save line in file
try {
FileOutputStream fOut = openFileOutput(fileName, MODE_APPEND);
fOut.write(data.getBytes());
fOut.close();
//Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show();
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"ERROR " + e.getMessage() +
" saving file",Toast.LENGTH_SHORT).show();
}
}
});
}
private void calculaDiasHorasMinutosSegundos(Date dIni, Date dFin,
DiasHorasMinutosSegundosMilisegundos dhmsm) {
long different = dFin.getTime() - dIni.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
different = different % secondsInMilli;
dhmsm.setDias(elapsedDays);
dhmsm.setHoras(elapsedHours);
dhmsm.setMinutos(elapsedMinutes);
dhmsm.setSegundos(elapsedSeconds);
dhmsm.setMilisegundos(different);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
......@@ -95,6 +169,33 @@ public class ScrollingActivity extends AppCompatActivity {
return true;
}
if (id == R.id.reset) {
try {
String[] sFileList = this.fileList();
if (Arrays.asList(sFileList).contains(fileName)) {
boolean ret = this.deleteFile(fileName);
if (ret) {
Toast.makeText(getBaseContext(), "file " + fileName + " deleted", Toast.LENGTH_LONG).show();
text.delete(0,text.length());
linea = 1;
tView.setText(text);
} else {
Toast.makeText(getBaseContext(), "file " + fileName + " NOT deleted", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getBaseContext(), "file " + fileName + " NOT exists", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"ERROR " + e.getMessage() +
" deleting file",Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
\ No newline at end of file
......@@ -7,4 +7,9 @@
android:orderInCategory="100"
android:title="@string/action_acerca_de"
app:showAsAction="never" />
<item
android:id="@+id/reset"
android:orderInCategory="200"
android:title="@string/reset"
app:showAsAction="never" />
</menu>
......@@ -2,7 +2,7 @@
<resources>
<string name="app_name">Record Time Droid</string>
<string name="notificacion">Registrada linea</string>
<string name="action_acerca_de">Acerca de</string>
<string name="action_acerca_de">Acerca de ...</string>
<string name="dias">días</string>
<string name="mensaje_acerca_de">
"Desarrollada por Oscar Megía López\n\n"
......@@ -20,4 +20,5 @@
"También puedes donar bitcoins en mi cartera:\n"
"bc1qrldk9a9w8evj7p96aekefng36j6g9hv4xnsn3t"
</string>
<string name="reset">Inicializar</string>
</resources>
\ No newline at end of file
......@@ -19,4 +19,5 @@
"Also you can donate bitcoins in my wallet:\n"
"bc1qrldk9a9w8evj7p96aekefng36j6g9hv4xnsn3t"
</string>
<string name="reset">Reset</string>
</resources>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment