From aa992cb23a2b4ad1b42a212dbff16bd60bd58aa9 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Sat, 16 Oct 2021 22:41:14 +0200 Subject: [PATCH] file chooser --- app/build.gradle | 3 + .../pogodacc/activity/ExportDataActivity.java | 29 +++++++ .../ExportDataActStartButtonClickEvent.java | 24 ++++++ .../pogoda/mobile/pogodacc/dao/TrendDao.java | 8 +- .../mobile/pogodacc/file/ExcelExport.java | 75 +++++++++++++++++++ .../mobile/pogodacc/web/RestClientConfig.java | 11 ++- 6 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java diff --git a/app/build.gradle b/app/build.gradle index 6b131e6..9316440 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -55,5 +55,8 @@ dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' implementation 'org.greenrobot:eventbus:3.0.0' + implementation 'org.apache.poi:poi:3.12' + // implementation 'org.apache.poi:poi-ooxml:3.12' + implementation 'com.fasterxml:aalto-xml:1.0.0' } \ No newline at end of file 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 c97ac8a..a5ca9f3 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 @@ -1,9 +1,12 @@ package cc.pogoda.mobile.pogodacc.activity; +import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; +import android.os.FileUtils; import android.text.Editable; import android.view.View; import android.widget.ArrayAdapter; @@ -90,9 +93,35 @@ public class ExportDataActivity extends AppCompatActivity { } public int getOutputFormat() { + + String selected = outputFormat.getSelectedItem().toString(); + + switch (selected) { + case "CSV" : return 1; + case "MS Excel XLSX": return 2; + } + return -1; } + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + + String s; + + if (requestCode == 123 && resultCode == RESULT_OK) { + Uri uri = data.getData(); + + s = uri.getPath(); + + FileUtils. + + System.out.println(s); + } + + super.onActivityResult(requestCode, resultCode, data); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/handler/ExportDataActStartButtonClickEvent.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/handler/ExportDataActStartButtonClickEvent.java index cf995af..544e617 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/handler/ExportDataActStartButtonClickEvent.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/activity/handler/ExportDataActStartButtonClickEvent.java @@ -1,9 +1,12 @@ package cc.pogoda.mobile.pogodacc.activity.handler; +import android.content.Intent; +import android.net.Uri; import android.view.View; import cc.pogoda.mobile.pogodacc.activity.ExportDataActivity; import cc.pogoda.mobile.pogodacc.dao.StationDataDao; +import cc.pogoda.mobile.pogodacc.file.ExcelExport; import cc.pogoda.mobile.pogodacc.type.WeatherStation; import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData; @@ -15,6 +18,17 @@ public class ExportDataActStartButtonClickEvent implements View.OnClickListener{ activity = act; } + public void openDirectory(Uri uriToLoad) { + // Choose a directory using the system's file picker. + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); + + // Optionally, specify a URI for the directory that should be opened in + // the system file picker when it loads. + //intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uriToLoad); + + activity.startActivityForResult(intent, 123); + } + @Override public void onClick(View view) { @@ -30,6 +44,16 @@ public class ExportDataActStartButtonClickEvent implements View.OnClickListener{ ListOfStationData stationData = stationDataDao.getLastStationData(toExport.getSystemName(), timestampStart, timestampStop); + int format = activity.getOutputFormat(); + + openDirectory(null); + + if (format == 2) { + ExcelExport.exportToExcel(stationData, toExport, activity.getApplicationContext()); + + } + + } } diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/TrendDao.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/TrendDao.java index 90984fe..c79d573 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/TrendDao.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/dao/TrendDao.java @@ -8,7 +8,6 @@ import cc.pogoda.mobile.pogodacc.web.TrendConsumer; import retrofit2.Response; public class TrendDao { - RestClientConfig restClient; Response trend; @@ -16,6 +15,8 @@ public class TrendDao { class Worker implements Runnable { + RestClientConfig restClient; + @Override public void run() { @@ -27,7 +28,7 @@ public class TrendDao { trend = trendConsumer.getTrendForStation(station).execute(); } catch (IOException e) { - + e.printStackTrace(); } } } @@ -47,6 +48,9 @@ public class TrendDao { } catch (InterruptedException e) { e.printStackTrace(); } + catch (NullPointerException e) { + e.printStackTrace(); + } return out; } 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 new file mode 100644 index 0000000..6d2035a --- /dev/null +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/file/ExcelExport.java @@ -0,0 +1,75 @@ +package cc.pogoda.mobile.pogodacc.file; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.provider.DocumentsContract; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +import cc.pogoda.mobile.pogodacc.type.WeatherStation; +import cc.pogoda.mobile.pogodacc.type.web.ListOfStationData; + +public class ExcelExport { + + + public static boolean exportToExcel(ListOfStationData data, WeatherStation station, Context context) { + + Cell cell; + + Workbook workbook = new HSSFWorkbook(); + + Sheet sheet = workbook.createSheet(station.getSystemName()); + + Row header = sheet.createRow(3); + + cell = header.createCell(0); + cell.setCellValue("UNIX Epoch Timestamp"); + + cell = header.createCell(1); + cell.setCellValue("Date / Time"); + + cell = header.createCell(2); + cell.setCellValue("Temperature"); + + cell = header.createCell(3); + cell.setCellValue("QNH pressure"); + + cell = header.createCell(4); + cell.setCellValue("Humidity"); + + cell = header.createCell(5); + cell.setCellValue("Wind Direction"); + + cell = header.createCell(6); + cell.setCellValue("Wind Average Speed"); + + cell = header.createCell(7); + cell.setCellValue("Wind Gusts"); + + File file = new File(context.getExternalFilesDir(null), "test.xls"); + FileOutputStream fileOutputStream = null; + + try { + fileOutputStream = new FileOutputStream(file); + + workbook.write(fileOutputStream); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return true; + } + +} diff --git a/app/src/main/java/cc/pogoda/mobile/pogodacc/web/RestClientConfig.java b/app/src/main/java/cc/pogoda/mobile/pogodacc/web/RestClientConfig.java index dd6c313..9d8d3ca 100644 --- a/app/src/main/java/cc/pogoda/mobile/pogodacc/web/RestClientConfig.java +++ b/app/src/main/java/cc/pogoda/mobile/pogodacc/web/RestClientConfig.java @@ -3,6 +3,8 @@ package cc.pogoda.mobile.pogodacc.web; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.util.concurrent.TimeUnit; + import cc.pogoda.mobile.pogodacc.type.CustomLocalDateTime; import cc.pogoda.mobile.pogodacc.web.deserializer.CustomLocalDateTimeDeserializer; import okhttp3.OkHttpClient; @@ -16,7 +18,14 @@ public class RestClientConfig { Gson gson = new GsonBuilder().registerTypeAdapter(CustomLocalDateTime.class, new CustomLocalDateTimeDeserializer()).setLenient().create(); - OkHttpClient client = new OkHttpClient(); + OkHttpClient.Builder builder = new OkHttpClient.Builder(); + + builder.readTimeout(20, TimeUnit.SECONDS); + builder.writeTimeout(20, TimeUnit.SECONDS); + builder.connectTimeout(20, TimeUnit.SECONDS); + builder.callTimeout(20, TimeUnit.SECONDS); + + OkHttpClient client = builder.build();//new OkHttpClient(builder); out = new Retrofit.Builder().baseUrl("http://pogoda.cc:8080/").addConverterFactory(GsonConverterFactory.create(gson)).client(client).build();