From dac1a2a0b85c583d6a186bd2cde4077f50524271 Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Tue, 29 Aug 2023 15:37:46 +0100 Subject: [PATCH] Moved export data functionality to an AsyncTask to avoid ApplicationNotResponding warnings when exporting large amounts of data. --- app/src/main/AndroidManifest.xml | 4 +- .../org/openseizuredetector/LogManager.java | 250 +++++++++++------- 2 files changed, 160 insertions(+), 94 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 16b0fa6..a74697c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="127" + android:versionName="4.1.12"> diff --git a/app/src/main/java/uk/org/openseizuredetector/LogManager.java b/app/src/main/java/uk/org/openseizuredetector/LogManager.java index f3c3fb0..02c6577 100644 --- a/app/src/main/java/uk/org/openseizuredetector/LogManager.java +++ b/app/src/main/java/uk/org/openseizuredetector/LogManager.java @@ -105,7 +105,7 @@ public class LogManager { public double mNDATimeRemaining; // hours public double mNDALogPeriodHours = 24.0; // hours private static Context mContext; - private OsdUtil mUtil; + private static OsdUtil mUtil; public static WebApiConnection mWac; public static final boolean USE_FIREBASE_BACKEND = false; @@ -493,109 +493,23 @@ public class LogManager { } /** - * exportToFile - export datapoints data to a csv file on the android device. + * exportToCsvFile - export datapoints data to a csv file on the android device. * * @param endDate end date of period to export (Date type) * @param duration duration in hours of period to export (double) * @param uri uri of file to save. + * @param callback function to be called on completion of the task (returns true on success, false on error) */ public void exportToCsvFile(Date endDate, double duration, Uri uri, BooleanCallback callback) { Log.v(TAG, "exportToCsvFile(): uri=" + uri.toString()); - long endDateMillis = endDate.getTime(); - long durationMillis = (long) (duration * 3600. * 1000); - long startDateMillis = endDateMillis - durationMillis; - Log.v(TAG, "exportToCsvFile() - endDateMillis=" + endDateMillis + ", startDateMillis=" + startDateMillis + ", durationMillis=" + durationMillis); - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String sDateStr = dateFormat.format(new Date(startDateMillis)); - String eDateStr = dateFormat.format(new Date(endDateMillis)); - Log.v(TAG, "exportToFile() - sDateStr=" + sDateStr + " eDateStr=" + eDateStr); - String[] columns = {"*"}; - String whereClause = "DataTime>? AND DataTime { - Log.v(TAG, "exportToCsvFile - returned " + cursor); - if (cursor != null) { - Log.d(TAG, "we got a cursor!"); - try { - ParcelFileDescriptor pfd = mContext.getContentResolver(). - openFileDescriptor(uri, "w"); - FileOutputStream fileOutputStream = - new FileOutputStream(pfd.getFileDescriptor()); - fileOutputStream.write(("# dataTime, alarmState, hr, o2sat, accel*125\n").getBytes()); - writeDatapointsToFile(cursor, fileOutputStream); - // Let the document provider know you're done by closing the stream. - fileOutputStream.close(); - pfd.close(); - callback.accept(true); - } catch (FileNotFoundException e) { - e.printStackTrace(); - mUtil.showToast(mContext.getString(R.string.error_exporting_data)); - Log.e(TAG, "exportToFile() - FileNotFoundException: " + e.toString()); - callback.accept(false); - } catch (IOException e) { - e.printStackTrace(); - mUtil.showToast(mContext.getString(R.string.error_exporting_data)); - Log.e(TAG, "exportToFile() - IOException: " + e.toString()); - callback.accept(false); - } - - } else { - Log.w(TAG, "exportToCsvFile() - returned null result"); - callback.accept(false); - } + new ExportDataTask(endDate, duration, uri, (boolean retVal) -> { + Log.v(TAG, "exportToCsvFile - returned " + retVal); + callback.accept(retVal); }).execute(); return; } - private void writeDatapointsToFile(Cursor c, FileOutputStream fileOutputStream) { - Log.v(TAG, "writeDatapointsToFile()"); - JSONArray dataObj; - String dataJsonStr; - JSONObject dataJsonObj; - JSONArray rawDataArr; - Log.d(TAG,"writeDatapointsToFile()" + c.getColumnNames()); - //for (int i=0;i { + BooleanCallback mCallback; + Date mEndDate; + double mDuration; + Uri mUri; + + + ExportDataTask(Date endDate, double duration, Uri uri, BooleanCallback callback) { + Log.i(TAG,"ExportDataTask constructor()"); + this.mCallback = callback; + mEndDate = endDate; + mDuration = duration; + mUri = uri; + } + + @Override + protected Boolean doInBackground(Void... params) { + Log.v(TAG, "ExportDataTask.doInBackground()"); + long endDateMillis = mEndDate.getTime(); + long durationMillis = (long) (mDuration * 3600. * 1000); + long startDateMillis = endDateMillis - durationMillis; + Log.v(TAG, "exportDataTask() - endDateMillis=" + endDateMillis + ", startDateMillis=" + startDateMillis + ", durationMillis=" + durationMillis); + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String sDateStr = dateFormat.format(new Date(startDateMillis)); + String eDateStr = dateFormat.format(new Date(endDateMillis)); + Log.v(TAG, "ExportDataTask.doInBackground - sDateStr=" + sDateStr + " eDateStr=" + eDateStr); + String[] columns = {"*"}; + String whereClause = "DataTime>? AND DataTime