blob: 3d66f1ee5f4acef3a780c45339b04b63d1abf06b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*
* Copyright 2018 Allan Wang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.pitchedapps.frost.services
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.os.PersistableBundle
import com.pitchedapps.frost.activities.ImageActivity
import com.pitchedapps.frost.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.FileFilter
class LocalService : BaseJobService() {
enum class Flag {
PURGE_IMAGE
}
companion object {
private const val FLAG = "extra_local_flag"
/**
* Launches a local service with the provided flag
*/
fun schedule(context: Context, flag: Flag): Boolean {
val scheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
val serviceComponent = ComponentName(context, LocalService::class.java)
val bundle = PersistableBundle()
bundle.putString(FLAG, flag.name)
val builder = JobInfo.Builder(LOCAL_SERVICE_BASE + flag.ordinal, serviceComponent)
.setMinimumLatency(0L)
.setExtras(bundle)
.setOverrideDeadline(2000L)
val result = scheduler.schedule(builder.build())
if (result <= 0) {
L.eThrow("FrostRequestService scheduler failed for ${flag.name}")
return false
}
L.d { "Scheduled ${flag.name}" }
return true
}
}
override fun onStartJob(params: JobParameters?): Boolean {
super.onStartJob(params)
val flagString = params?.extras?.getString(FLAG)
val flag: Flag = try {
Flag.valueOf(flagString!!)
} catch (e: Exception) {
L.e { "Local service with invalid flag $flagString" }
return true
}
launch {
when (flag) {
Flag.PURGE_IMAGE -> purgeImages()
}
}
return false
}
private suspend fun purgeImages() {
withContext(Dispatchers.IO) {
val purge = System.currentTimeMillis() - ImageActivity.PURGE_TIME
ImageActivity.cacheDir(this@LocalService)
.listFiles(FileFilter { it.isFile && it.lastModified() < purge })
?.forEach { it.delete() }
}
}
}
|