Fyodor Kupolov | d225f0bf | 2017-03-20 16:04:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.job.JobInfo; |
| 20 | import android.app.job.JobParameters; |
| 21 | import android.app.job.JobScheduler; |
| 22 | import android.app.job.JobService; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.res.Resources; |
| 26 | import android.os.Environment; |
| 27 | import android.os.SystemProperties; |
| 28 | import android.util.Slog; |
| 29 | import android.util.TimeUtils; |
| 30 | |
| 31 | import com.android.internal.R; |
| 32 | |
| 33 | import 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 | */ |
| 40 | public 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 | } |