-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to android-job from JobDispatcher for cleanup service
- this move removes the dependency from Google Play service Signed-off-by: Arka Prava Basu <[email protected]>
- Loading branch information
Showing
13 changed files
with
78 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/havenapp/main/service/HavenJobCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.havenapp.main.service | ||
|
||
import com.evernote.android.job.Job | ||
import com.evernote.android.job.JobCreator | ||
|
||
/** | ||
* Created by Arka Prava Basu <[email protected]> on 04/11/18. | ||
*/ | ||
class HavenJobCreator : JobCreator { | ||
override fun create(tag: String): Job? { | ||
return when (tag) { | ||
SERVICE_TAG -> { | ||
RemoveDeletedFilesJob() | ||
} | ||
else -> { | ||
null | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,41 +2,46 @@ package org.havenapp.main.service | |
|
||
import android.os.Environment | ||
import android.util.Log | ||
import com.firebase.jobdispatcher.JobParameters | ||
import com.firebase.jobdispatcher.JobService | ||
import com.evernote.android.job.Job | ||
import com.evernote.android.job.JobRequest | ||
import org.havenapp.main.HavenApp | ||
import org.havenapp.main.PreferenceManager | ||
import org.havenapp.main.database.HavenEventDB | ||
import org.havenapp.main.model.EventTrigger | ||
import java.io.File | ||
|
||
/** | ||
* A [JobService] to delete files related to deleted logs. | ||
* A [Job] to delete files related to deleted logs. | ||
* <p> | ||
* Created by Arka Prava Basu <[email protected]> on 28/10/18. | ||
*/ | ||
|
||
const val SERVICE_TAG = "HavenCleanupService" | ||
const val SERVICE_TAG = "HavenCleanupJob" | ||
|
||
class RemoveDeletedFilesService: JobService() { | ||
override fun onStopJob(job: JobParameters?): Boolean { | ||
Log.d(SERVICE_TAG, "Cleanup Service interrupted") | ||
return false | ||
class RemoveDeletedFilesJob: Job() { | ||
|
||
companion object { | ||
fun schedule(): Int { | ||
return JobRequest.Builder(SERVICE_TAG) | ||
.setPeriodic(24 * 60 * 60 * 1000L) // run once every 24 hrs | ||
.setRequiresCharging(true) | ||
.setUpdateCurrent(true) | ||
.build() | ||
.schedule() | ||
} | ||
} | ||
|
||
override fun onStartJob(job: JobParameters): Boolean { | ||
Log.d(SERVICE_TAG, "Starting Cleanup Service") | ||
Thread { | ||
// remove all deleted logs from disk and reschedule this task | ||
removeDeletedLogsFromDisk() | ||
jobFinished(job, true) | ||
Log.d(SERVICE_TAG, "Stopping Cleanup service") | ||
}.start() | ||
override fun onRunJob(params: Params): Result { | ||
Log.d(SERVICE_TAG, "Starting Cleanup. Job Id: ${params.id}") | ||
|
||
// remove all deleted logs from disk and reschedule this task | ||
removeDeletedLogsFromDisk() | ||
|
||
return true | ||
Log.d(SERVICE_TAG, "Stopping Cleanup. Job Id: ${params.id}") | ||
return Result.SUCCESS | ||
} | ||
|
||
private fun removeDeletedLogsFromDisk() { | ||
val database = HavenEventDB.getDatabase(this) | ||
val database = HavenApp.getDataBaseInstance() | ||
|
||
val eventList = database.getEventDAO().getAllEvent() | ||
val eventTriggerList = database.getEventTriggerDAO().getAllEventTriggers() | ||
|
@@ -60,7 +65,7 @@ class RemoveDeletedFilesService: JobService() { | |
|
||
private fun getAllFilesToBeDeleted(targetFileList: MutableList<File>) { | ||
val storageDir = File(Environment.getExternalStorageDirectory(), | ||
PreferenceManager(this).baseStoragePath) | ||
PreferenceManager(HavenApp.getInstance()).baseStoragePath) | ||
|
||
if (!storageDir.exists()) | ||
return | ||
|
@@ -73,7 +78,7 @@ class RemoveDeletedFilesService: JobService() { | |
|
||
private fun deleteEmptyDirs() { | ||
val storageDir = File(Environment.getExternalStorageDirectory(), | ||
PreferenceManager(this).baseStoragePath) | ||
PreferenceManager(HavenApp.getInstance()).baseStoragePath) | ||
|
||
if (!storageDir.exists()) | ||
return | ||
|
@@ -112,7 +117,7 @@ class RemoveDeletedFilesService: JobService() { | |
} | ||
|
||
private fun getAllEventTriggerPath(): List<String> { | ||
val database = HavenEventDB.getDatabase(this) | ||
val database = HavenApp.getDataBaseInstance() | ||
val eventTriggerPathList = mutableListOf<String>() | ||
database.getEventTriggerDAO().getAllEventTriggers().filter { it.mPath != null } | ||
.mapTo(eventTriggerPathList) { it.mPath!! } | ||
|