diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/ExportDataActivity.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/ExportDataActivity.java index d0c18fd..7fe4555 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/ExportDataActivity.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/ExportDataActivity.java @@ -3,6 +3,8 @@ package cc.pogoda.mobile.pogodacc.activity; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import android.app.AlertDialog; +import android.content.DialogInterface; import android.content.Intent; import android.content.UriPermission; import android.database.Cursor; @@ -59,13 +61,13 @@ public class ExportDataActivity extends AppCompatActivity { WeatherStation stationToExport = null; - Uri exportUri; + Uri exportUri = null; public WeatherStation getStationToExport() { return stationToExport; } - public void openDirectory(Uri uriToLoad) { + public void openDirectory(String fn, int outputFormat) { // // Choose a directory using the system's file picker. // Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); // @@ -77,12 +79,15 @@ public class ExportDataActivity extends AppCompatActivity { Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); - intent.setType("application/vnd.ms-excel"); - intent.putExtra(Intent.EXTRA_TITLE, "text.xls"); + if (outputFormat == 1) { + intent.setType("text/csv"); + intent.putExtra(Intent.EXTRA_TITLE, fn + ".csv"); - // Optionally, specify a URI for the directory that should be opened in - // the system file picker when your app creates the document. - //intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri); + } + if (outputFormat == 2) { + intent.setType("application/vnd.ms-excel"); + intent.putExtra(Intent.EXTRA_TITLE, fn + ".xls"); + } startActivityForResult(intent, 123); @@ -194,7 +199,17 @@ public class ExportDataActivity extends AppCompatActivity { outputFileButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - openDirectory(null); + + String fn; + + if (stationToExport != null) { + fn = stationToExport.getSystemName() + "_" + datePicker.getDayOfMonth() + String.format("%02d", datePicker.getMonth()) + "_ln_" + act.getExportLnInHours() + "hrs"; + } + else { + fn = "export"; + } + + openDirectory(fn, act.getOutputFormat()); } }); @@ -223,7 +238,7 @@ public class ExportDataActivity extends AppCompatActivity { WeatherStation toExport = act.getStationToExport(); - if (toExport != null) { + if (toExport != null && exportUri != null) { long timestampStart = act.getStartTimestamp(); long timestampStop = timestampStart + act.getExportLnInHours() * 3600; @@ -244,6 +259,15 @@ public class ExportDataActivity extends AppCompatActivity { } + else { + AlertDialog.Builder builder = new AlertDialog.Builder(act); + builder.setMessage(R.string.select_station_export); + builder.setPositiveButton(R.string.ok, (DialogInterface var1, int var2) -> { + var1.dismiss(); + }); + builder.create(); + builder.show(); + } } }); diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/MainActivity.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/MainActivity.java index 263cb8b..d94850e 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/MainActivity.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/MainActivity.java @@ -94,6 +94,7 @@ public class MainActivity extends AppCompatActivity { fromFavs.setLat(fromAllStations.getLat()); fromFavs.setDisplayedName(fromAllStations.getDisplayedName()); fromFavs.setDisplayedLocation(fromAllStations.getDisplayedLocation()); + fromFavs.setTimezone(fromAllStations.getTimezone()); // there is no need to delete and put object on the list once again // as a list does not make a copy of the object. It (ArrayList) keeps diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/TrendActivity.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/TrendActivity.java index 293c6d2..3fb25a6 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/TrendActivity.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/TrendActivity.java @@ -55,20 +55,8 @@ public class TrendActivity extends AppCompatActivity { NavDirections wind = WindTrendFragmentDirections.actionNavigationWindToNavigationTemperature(stationName); NavDirections pressure = PressureTrendFragmentDirections.actionNavigationPressureToNavigationWind(stationName); - //NavHostFragment.create(R.navigation.mobile_navigation, bundle); - -// NavGraph navGraph = navController.getNavInflater().inflate(R.navigation.mobile_navigation); -// builder.setDefaultValue(stationName); -// navGraph.addArgument("station", builder.build()); -// navController.setGraph(navGraph, bundle); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); - //navController.navigate(temperature); -// -// NavArgument.Builder builder = new NavArgument.Builder(); -// builder.setDefaultValue(stationName); -// navGraph.addArgument("station", builder.build()); -// navController.setGraph(navGraph); } diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/AllStationsDao.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/AllStationsDao.java index 1f13c7d..2c13731 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/AllStationsDao.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/AllStationsDao.java @@ -66,6 +66,7 @@ public class AllStationsDao { elem.setStationNameTextColor(def.stationNameTextColour); elem.setImageAlign(def.backgroundJpgAlign); elem.setMoreInfo(def.moreInfo); + elem.setTimezone(def.timezone); AvailableParameters availableParameters = AvailableParameters.fromStation(def); elem.setAvailableParameters(availableParameters); diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java index 481e9d8..8f30bd8 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java @@ -7,21 +7,40 @@ import android.provider.DocumentsContract; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.threeten.bp.Instant; +import org.threeten.bp.LocalDate; +import org.threeten.bp.ZoneId; +import org.threeten.bp.ZonedDateTime; +import org.threeten.bp.format.DateTimeFormatter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.text.SimpleDateFormat; import cc.pogoda.mobile.pogodacc.type.WeatherStation; import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData; +import cc.pogoda.mobile.pogodacc.type.web.StationData; public class ExcelExport { + private static final SimpleDateFormat SDF = new SimpleDateFormat("dd-M-yyyy HH:mm:ss"); + + private static double round (double in) { + double out = 0; + + long temp = (long)(in * 10); + + out = temp / 10.0d; + + return out; + } public static boolean exportToExcel(ListOfStationData data, WeatherStation station, Context context, OutputStream out) { @@ -29,38 +48,152 @@ public class ExcelExport { return false; } + if (data == null || data.list_of_station_data.length == 0) { + return false; + } + + int rowNumber = 0; + Cell cell; + ZonedDateTime first = ZonedDateTime.ofInstant(Instant.ofEpochSecond(data.list_of_station_data[0].epoch), ZoneId.of(station.getTimezone())); + String generalTimezone = first.format(DateTimeFormatter.ofPattern("zzzz")); + Workbook workbook = new HSSFWorkbook(); + CellStyle left = workbook.createCellStyle(); + left.setAlignment(CellStyle.ALIGN_LEFT); + Sheet sheet = workbook.createSheet(station.getSystemName()); - Row header = sheet.createRow(3); + { + Row stationame = sheet.createRow(rowNumber++); + Cell name = stationame.createCell(0); + name.setCellValue("Station name:"); + name = stationame.createCell(1); + name.setCellValue(station.getDisplayedName()); + + Row localization = sheet.createRow(rowNumber++); + Cell loc = localization.createCell(0); + loc.setCellValue("Location:"); + loc = localization.createCell(1); + loc.setCellValue(station.getDisplayedLocation()); + + Row coordinates = sheet.createRow(rowNumber++); + Cell coord = coordinates.createCell(0); + coord.setCellValue("Coordinates:"); + coord = coordinates.createCell(1); + coord.setCellValue("Lat " + station.getLat() + " , Lon " + station.getLon()); + + Row timezone = sheet.createRow(rowNumber++); + Cell tz = timezone.createCell(0); + tz.setCellValue("All date and time as in:"); + tz = timezone.createCell(1); + tz.setCellValue(station.getTimezone()); + + Row generaltz = sheet.createRow(rowNumber++); + Cell gtz = generaltz.createCell(0); + gtz.setCellValue("Timezone in this export:"); + gtz = generaltz.createCell(1); + gtz.setCellValue(generalTimezone); + + Row offset = sheet.createRow(rowNumber++); + Cell off = offset.createCell(0); + off.setCellValue("Time offset for the timezone:"); + off = offset.createCell(1); + off.setCellValue(first.getOffset().toString()); + +// Row aprscall = sheet.createRow(rowNumber++); +// Cell call = aprscall.createCell(0); +// call.setCellValue("APRS Callsign:"); +// call = aprscall.createCell(1); +// call.setCellValue(station.get); + } + + rowNumber++; + rowNumber++; + rowNumber++; + + Row header = sheet.createRow(rowNumber++); cell = header.createCell(0); cell.setCellValue("UNIX Epoch Timestamp"); + cell.setCellStyle(left); cell = header.createCell(1); - cell.setCellValue("Date / Time"); + cell.setCellValue("Date / Time [DD-MM-YYYY 24HH:MM]"); + cell.setCellStyle(left); cell = header.createCell(2); - cell.setCellValue("Temperature"); + cell.setCellValue("Temperature [C]"); + cell.setCellStyle(left); cell = header.createCell(3); - cell.setCellValue("QNH pressure"); + cell.setCellValue("QNH pressure [hPa]"); + cell.setCellStyle(left); cell = header.createCell(4); - cell.setCellValue("Humidity"); + cell.setCellValue("Humidity [%]"); + cell.setCellStyle(left); cell = header.createCell(5); cell.setCellValue("Wind Direction"); + cell.setCellStyle(left); cell = header.createCell(6); - cell.setCellValue("Wind Average Speed"); + cell.setCellValue("Wind Average Speed [m/s]"); + cell.setCellStyle(left); cell = header.createCell(7); - cell.setCellValue("Wind Gusts"); + cell.setCellValue("Wind Gusts [m/s]"); + cell.setCellStyle(left); + // put data into output file + for (StationData d : data.list_of_station_data) { + Row r = sheet.createRow(rowNumber++); + + Cell epoch = r.createCell(0); + epoch.setCellValue(d.epoch); + epoch.setCellStyle(left); + + ZonedDateTime zoneddatetime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(d.epoch), ZoneId.of(station.getTimezone())); + Cell datetime = r.createCell(1); + datetime.setCellValue(zoneddatetime.format(DateTimeFormatter.ofPattern("dd-M-yyyy HH:mm:ss"))); + datetime.setCellStyle(left); + + Cell temperature = r.createCell(2); + temperature.setCellValue(round(d.temperature)); + temperature.setCellStyle(left); + + Cell pressure = r.createCell(3); + pressure.setCellValue(d.pressure); + pressure.setCellStyle(left); + + Cell humidity = r.createCell(4); + humidity.setCellValue(d.humidity); + humidity.setCellStyle(left); + + Cell winddir = r.createCell(5); + winddir.setCellValue(d.winddir); + winddir.setCellStyle(left); + + Cell windspeed = r.createCell(6); + windspeed.setCellValue(round(d.windspeed)); + windspeed.setCellStyle(left); + + Cell windgust = r.createCell(7); + windgust.setCellValue(round(d.windgusts)); + windgust.setCellStyle(left); + } + + sheet.setColumnWidth(0, 6600); + sheet.setColumnWidth(1, 5900); + sheet.setColumnWidth(2, 3900); + sheet.setColumnWidth(3, 4600); + sheet.setColumnWidth(4, 2900); + sheet.setColumnWidth(5, 3200); + sheet.setColumnWidth(6, 3700); + sheet.setColumnWidth(7, 3200); try { workbook.write(out); diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/ParceableStationsList.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/ParceableStationsList.java index 5176e1e..e4b349c 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/ParceableStationsList.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/ParceableStationsList.java @@ -56,6 +56,7 @@ public class ParceableStationsList implements Parcelable { parcel.writeString(s.getDisplayedLocation()); parcel.writeString(s.getSponsorUrl()); parcel.writeString(s.getImageUrl()); + parcel.writeString(s.getTimezone()); parcel.writeInt(s.getImageAlign()); parcel.writeInt(s.getStationNameTextColor()); @@ -90,6 +91,7 @@ public class ParceableStationsList implements Parcelable { wx.displayedLocation = in.readString(); wx.sponsorUrl = in.readString(); wx.imageUrl = in.readString(); + wx.timezone = in.readString(); wx.imageAlign = in.readInt(); wx.stationNameTextColor = in.readInt(); diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/WeatherStation.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/WeatherStation.java index cb0dbe8..fc999a4 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/WeatherStation.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/WeatherStation.java @@ -130,6 +130,16 @@ public class WeatherStation implements Serializable { String imageUrl; + public String getTimezone() { + return timezone.trim(); + } + + public void setTimezone(String timezone) { + this.timezone = timezone; + } + + String timezone; + int imageAlign; int stationNameTextColor; diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/web/StationDefinition.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/web/StationDefinition.java index 40aa631..b2748e0 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/type/web/StationDefinition.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/type/web/StationDefinition.java @@ -26,6 +26,8 @@ public class StationDefinition { public String moreInfo; + public String timezone; + public float lat; public float lon; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0247563..d686fad 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -123,7 +123,6 @@ CSV MS Excel XLS - @string/email @string/csv @string/xlsx