blob: 6fd0256fcbfdd38dfe2f4a8a3e28a9266812806f [file] [log] [blame]
Fyodor Kupolovd225f0bf2017-03-20 16:04:57 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server;
18
19import android.app.job.JobInfo;
20import android.app.job.JobParameters;
21import android.app.job.JobScheduler;
22import android.app.job.JobService;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.res.Resources;
26import android.os.Environment;
27import android.os.SystemProperties;
28import android.util.Slog;
29import android.util.TimeUtils;
30
31import com.android.internal.R;
32
33import java.util.concurrent.TimeUnit;
34
35/**
36 * {@link JobService} that marks
37 * {@link Environment#getDataPreloadsFileCacheDirectory() preloaded file cache} as expired after a
38 * pre-configured timeout.
39 */
40public class PreloadsFileCacheExpirationJobService extends JobService {
41 private static final boolean DEBUG = false; // Do not submit with true
42 private static final String TAG = "PreloadsFileCacheExpirationJobService";
43
44 // TODO move all JOB_IDs into a single class to avoid collisions
45 private static final int JOB_ID = 100500;
46
47 private static final String PERSIST_SYS_PRELOADS_FILE_CACHE_EXPIRED
48 = "persist.sys.preloads.file_cache_expired";
49
50 public static void schedule(Context context) {
51 int keepPreloadsMinDays = Resources.getSystem().getInteger(
52 R.integer.config_keepPreloadsMinDays); // Default is 1 week
53 long keepPreloadsMinTimeoutMs = DEBUG ? TimeUnit.MINUTES.toMillis(2)
54 : TimeUnit.DAYS.toMillis(keepPreloadsMinDays);
55 long keepPreloadsMaxTimeoutMs = DEBUG ? TimeUnit.MINUTES.toMillis(3)
56 : TimeUnit.DAYS.toMillis(keepPreloadsMinDays + 1);
57
58 if (DEBUG) {
59 StringBuilder sb = new StringBuilder("Scheduling expiration job to run in ");
60 TimeUtils.formatDuration(keepPreloadsMinTimeoutMs, sb);
61 Slog.i(TAG, sb.toString());
62 }
63 JobInfo expirationJob = new JobInfo.Builder(JOB_ID,
64 new ComponentName(context, PreloadsFileCacheExpirationJobService.class))
65 .setPersisted(true)
66 .setMinimumLatency(keepPreloadsMinTimeoutMs)
67 .setOverrideDeadline(keepPreloadsMaxTimeoutMs)
68 .build();
69
70 JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
71 jobScheduler.schedule(expirationJob);
72 }
73
74 @Override
75 public boolean onStartJob(JobParameters params) {
76 SystemProperties.set(PERSIST_SYS_PRELOADS_FILE_CACHE_EXPIRED, "1");
77 Slog.i(TAG, "Set " + PERSIST_SYS_PRELOADS_FILE_CACHE_EXPIRED + "=1");
78 return false;
79 }
80
81 @Override
82 public boolean onStopJob(JobParameters params) {
83 return false;
84 }
85}