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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.permissioncontroller.permission.service
import android.app.job.JobInfo
import android.app.job.JobParameters
import android.app.job.JobScheduler
import android.app.job.JobService
import android.content.ComponentName
import android.content.Context
import android.provider.DeviceConfig
import com.android.permissioncontroller.Constants
import com.android.permissioncontroller.DumpableLog
import com.android.permissioncontroller.permission.utils.Utils
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
/** A job to clean up old permission events. */
class PermissionEventCleanupJobService : JobService() {
companion object {
const val LOG_TAG = "PermissionEventCleanupJobService"
val DEFAULT_CLEAR_OLD_EVENTS_CHECK_FREQUENCY = TimeUnit.DAYS.toMillis(1)
fun scheduleOldDataCleanupIfNecessary(context: Context, jobScheduler: JobScheduler) {
if (isNewJobScheduleRequired(jobScheduler)) {
val jobInfo =
JobInfo.Builder(
Constants.OLD_PERMISSION_EVENT_CLEANUP_JOB_ID,
ComponentName(context, PermissionEventCleanupJobService::class.java)
)
.setPeriodic(getClearOldEventsCheckFrequencyMs())
// persist this job across boots
.setPersisted(true)
.build()
val status = jobScheduler.schedule(jobInfo)
if (status != JobScheduler.RESULT_SUCCESS) {
DumpableLog.e(LOG_TAG, "Could not schedule job: $status")
}
}
}
/**
* Returns whether a new job needs to be scheduled. A persisted job is used to keep the
* schedule across boots, but that job needs to be scheduled a first time and whenever the
* check frequency changes.
*/
private fun isNewJobScheduleRequired(jobScheduler: JobScheduler): Boolean {
var scheduleNewJob = false
val existingJob: JobInfo? =
jobScheduler.getPendingJob(Constants.OLD_PERMISSION_EVENT_CLEANUP_JOB_ID)
when {
existingJob == null -> {
DumpableLog.i(LOG_TAG, "No existing job, scheduling a new one")
scheduleNewJob = true
}
existingJob.intervalMillis != getClearOldEventsCheckFrequencyMs() -> {
DumpableLog.i(LOG_TAG, "Interval frequency has changed, updating job")
scheduleNewJob = true
}
else -> {
DumpableLog.i(LOG_TAG, "Job already scheduled.")
}
}
return scheduleNewJob
}
private fun getClearOldEventsCheckFrequencyMs() =
DeviceConfig.getLong(
DeviceConfig.NAMESPACE_PERMISSIONS,
Utils.PROPERTY_PERMISSION_EVENTS_CHECK_OLD_FREQUENCY_MILLIS,
DEFAULT_CLEAR_OLD_EVENTS_CHECK_FREQUENCY
)
}
var job: Job? = null
var jobStartTime: Long = -1L
override fun onStartJob(params: JobParameters?): Boolean {
DumpableLog.i(LOG_TAG, "onStartJob")
val storages = PermissionEventStorageImpls.getInstance()
if (storages.isEmpty()) {
return false
}
jobStartTime = System.currentTimeMillis()
job =
GlobalScope.launch(Dispatchers.IO) {
for (storage in storages) {
val success = storage.removeOldData()
if (!success) {
DumpableLog.e(LOG_TAG, "Failed to remove old data for $storage")
}
}
jobFinished(params, false)
}
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
DumpableLog.w(LOG_TAG, "onStopJob after ${System.currentTimeMillis() - jobStartTime}ms")
job?.cancel()
return true
}
}
|