From e26b925e3831d725f8a66e73a7af814f0d3b355d Mon Sep 17 00:00:00 2001 From: Jemshit Iskenderov Date: Mon, 10 Sep 2018 11:00:54 +0300 Subject: [PATCH] If same work cancelled and re runs again, deletes old files --- .../background_work/ExportWorker.kt | 45 ++++++++++++++++++- .../data/SharedPreferenceHelper.kt | 2 +- .../sensorlogger/ui/export/ExportViewModel.kt | 9 ++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/jemshit/sensorlogger/background_work/ExportWorker.kt b/app/src/main/java/com/jemshit/sensorlogger/background_work/ExportWorker.kt index bdbe597..21488be 100644 --- a/app/src/main/java/com/jemshit/sensorlogger/background_work/ExportWorker.kt +++ b/app/src/main/java/com/jemshit/sensorlogger/background_work/ExportWorker.kt @@ -13,6 +13,7 @@ import com.jemshit.sensorlogger.helper.createActivityFolder import com.jemshit.sensorlogger.helper.createLogFile import com.jemshit.sensorlogger.helper.random import com.jemshit.sensorlogger.model.* +import java.io.File import java.lang.reflect.Type import java.text.SimpleDateFormat import java.util.* @@ -46,6 +47,7 @@ class ExportWorker : Worker() { override fun doWork(): Result { createExportNotification(applicationContext) + excludedAccuracies = inputData.getStringArray(ARG_EXCLUDED_ACCURACIES) ?: arrayOf() age = inputData.getString(ARG_AGE) ?: "" weight = inputData.getString(ARG_WEIGHT) ?: "" @@ -53,6 +55,8 @@ class ExportWorker : Worker() { gender = inputData.getString(ARG_GENDER) ?: "" userInfo = UserInfoModel(age, weight, height, gender) + deletePreviousFilesByThisWork() + val cursor = sensorValueRepository.getAllSortedCursor() val success = cursor.moveToFirst() return if (success) { @@ -60,6 +64,7 @@ class ExportWorker : Worker() { } else { // Nothing to export createExportNotification(applicationContext, content = applicationContext.getString(R.string.exported_notification_content)) + if (!cursor.isClosed) cursor.close() Result.SUCCESS } } @@ -128,6 +133,7 @@ class ExportWorker : Worker() { return false } else { val file = fileErrorPair.first!! + persistCreatedFilePath(file) try { file.bufferedWriter().use { bufferedWriter -> bufferedWriter.write("USER_INFO: $userInfo\n") @@ -221,10 +227,10 @@ class ExportWorker : Worker() { while (!cursor.isAfterLast) { val success: Boolean = _processCurrentValue(index, getCurrentItemFromCursor(cursor)) if (!success) { - cursor.close() persistLoggedErrors() createExportNotification(applicationContext, content = applicationContext.getString(R.string.export_failed_notification_content)) + if (!cursor.isClosed) cursor.close() return Result.FAILURE } @@ -234,6 +240,7 @@ class ExportWorker : Worker() { persistLoggedErrors() createExportNotification(applicationContext, content = applicationContext.getString(R.string.exported_notification_content)) + if (!cursor.isClosed) cursor.close() return Result.SUCCESS } @@ -253,4 +260,40 @@ class ExportWorker : Worker() { } } } + + private fun persistCreatedFilePath(file: File) { + val previousPersistedData = getDefaultSharedPreference(applicationContext).getString("$id-FilePaths", "")!! + if (previousPersistedData.isBlank()) { + val paths = listOf(file.absolutePath) + + val pathsJson = gson.toJson(paths, stringListType) + getDefaultSharedPreference(applicationContext).edit { + putString("$id-FilePaths", pathsJson) + } + } else { + val paths = gson.fromJson>(previousPersistedData, stringListType) + val newPaths: MutableList = mutableListOf() + newPaths.addAll(paths) + newPaths.add(file.absolutePath) + + val pathsJson = gson.toJson(newPaths, stringListType) + getDefaultSharedPreference(applicationContext).edit { + putString("$id-FilePaths", pathsJson) + } + } + + } + + private fun deletePreviousFilesByThisWork() { + val pathsJson = getDefaultSharedPreference(applicationContext).getString("$id-FilePaths", "") + if (pathsJson.isNullOrBlank()) { + // No unfinished file is created by this work + } else { + val paths = gson.fromJson>(pathsJson, stringListType) + paths.forEach { + val file = File(it) + if (file.exists()) file.delete() + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/jemshit/sensorlogger/data/SharedPreferenceHelper.kt b/app/src/main/java/com/jemshit/sensorlogger/data/SharedPreferenceHelper.kt index 7bc8f79..b798083 100644 --- a/app/src/main/java/com/jemshit/sensorlogger/data/SharedPreferenceHelper.kt +++ b/app/src/main/java/com/jemshit/sensorlogger/data/SharedPreferenceHelper.kt @@ -4,7 +4,7 @@ import android.content.Context import android.content.Context.MODE_PRIVATE import android.content.SharedPreferences -const val PREF_KEY_LAST_WORKERI_ID = "LastWorkerId" +const val PREF_KEY_LAST_WORKER_ID = "LastWorkerId" fun getDefaultSharedPreference(context: Context): SharedPreferences { return context.getSharedPreferences("SensorLoggerPreferences", MODE_PRIVATE) diff --git a/app/src/main/java/com/jemshit/sensorlogger/ui/export/ExportViewModel.kt b/app/src/main/java/com/jemshit/sensorlogger/ui/export/ExportViewModel.kt index a0cf5c8..ea14c73 100644 --- a/app/src/main/java/com/jemshit/sensorlogger/ui/export/ExportViewModel.kt +++ b/app/src/main/java/com/jemshit/sensorlogger/ui/export/ExportViewModel.kt @@ -12,7 +12,7 @@ import com.google.gson.reflect.TypeToken import com.jemshit.sensorlogger.R import com.jemshit.sensorlogger.SensorLoggerApplication import com.jemshit.sensorlogger.background_work.* -import com.jemshit.sensorlogger.data.PREF_KEY_LAST_WORKERI_ID +import com.jemshit.sensorlogger.data.PREF_KEY_LAST_WORKER_ID import com.jemshit.sensorlogger.data.getDefaultSharedPreference import com.jemshit.sensorlogger.data.sensor_value.SensorValueRepository import com.jemshit.sensorlogger.helper.deleteExportFolder @@ -45,7 +45,7 @@ class ExportViewModel(application: Application) : AndroidViewModel(application) _deleteFolderStatus.value = UIWorkStatus.Idle _deleteLocalStatus.value = UIWorkStatus.Idle - val lastWorkId = getDefaultSharedPreference(application.applicationContext).getString(PREF_KEY_LAST_WORKERI_ID, "")!! + val lastWorkId = getDefaultSharedPreference(application.applicationContext).getString(PREF_KEY_LAST_WORKER_ID, "")!! if (lastWorkId.isBlank()) { _exportStatus.value = UIWorkStatus.Idle } else { @@ -72,7 +72,7 @@ class ExportViewModel(application: Application) : AndroidViewModel(application) _exportStatus.value = UIWorkStatus.Success } androidx.work.State.FAILED -> { - val lastWorkId = getDefaultSharedPreference(getApplication().applicationContext).getString(PREF_KEY_LAST_WORKERI_ID, "")!! + val lastWorkId = getDefaultSharedPreference(getApplication().applicationContext).getString(PREF_KEY_LAST_WORKER_ID, "")!! if (lastWorkId.isBlank()) { _exportStatus.postValue(UIWorkStatus.Error("Failed")) } else { @@ -119,6 +119,7 @@ class ExportViewModel(application: Application) : AndroidViewModel(application) val constraints = Constraints.Builder() .setRequiresBatteryNotLow(true) + .setRequiresStorageNotLow(true) .build() val inputData = Data.Builder() @@ -135,7 +136,7 @@ class ExportViewModel(application: Application) : AndroidViewModel(application) .build() getDefaultSharedPreference(getApplication().applicationContext).edit { - putString(PREF_KEY_LAST_WORKERI_ID, workRequest.id.toString()) + putString(PREF_KEY_LAST_WORKER_ID, workRequest.id.toString()) } workManager.enqueue(workRequest)