Skip to content

Commit

Permalink
If same work cancelled and re runs again, deletes old files
Browse files Browse the repository at this point in the history
jemshit committed Sep 10, 2018
1 parent 92fcb02 commit e26b925
Showing 3 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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,20 +47,24 @@ 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) ?: ""
height = inputData.getString(ARG_HEIGHT) ?: ""
gender = inputData.getString(ARG_GENDER) ?: ""
userInfo = UserInfoModel(age, weight, height, gender)

deletePreviousFilesByThisWork()

val cursor = sensorValueRepository.getAllSortedCursor()
val success = cursor.moveToFirst()
return if (success) {
export(cursor)
} 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<String>(file.absolutePath)

val pathsJson = gson.toJson(paths, stringListType)
getDefaultSharedPreference(applicationContext).edit {
putString("$id-FilePaths", pathsJson)
}
} else {
val paths = gson.fromJson<List<String>>(previousPersistedData, stringListType)
val newPaths: MutableList<String> = 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<List<String>>(pathsJson, stringListType)
paths.forEach {
val file = File(it)
if (file.exists()) file.delete()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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<SensorLoggerApplication>().applicationContext).getString(PREF_KEY_LAST_WORKERI_ID, "")!!
val lastWorkId = getDefaultSharedPreference(getApplication<SensorLoggerApplication>().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<SensorLoggerApplication>().applicationContext).edit {
putString(PREF_KEY_LAST_WORKERI_ID, workRequest.id.toString())
putString(PREF_KEY_LAST_WORKER_ID, workRequest.id.toString())
}

workManager.enqueue(workRequest)

0 comments on commit e26b925

Please sign in to comment.