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
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.pm.PackageManagerInternal;
26
27import java.util.concurrent.TimeUnit;
28
29public class PruneInstantAppsJobService extends JobService {
30    private static final boolean DEBUG = false;
31
32    private static final int JOB_ID = 765123;
33
34    private static final long PRUNE_INSTANT_APPS_PERIOD_MILLIS = DEBUG
35            ? TimeUnit.MINUTES.toMillis(1) : TimeUnit.DAYS.toMillis(1);
36
37    public static void schedule(Context context) {
38        JobInfo pruneJob = new JobInfo.Builder(JOB_ID, new ComponentName(
39                context.getPackageName(), PruneInstantAppsJobService.class.getName()))
40                .setRequiresDeviceIdle(true)
41                .setPeriodic(PRUNE_INSTANT_APPS_PERIOD_MILLIS)
42                .build();
43
44        JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
45        jobScheduler.schedule(pruneJob);
46    }
47
48    @Override
49    public boolean onStartJob(JobParameters params) {
50        PackageManagerInternal packageManagerInternal = LocalServices.getService(
51                PackageManagerInternal.class);
52        packageManagerInternal.pruneInstantApps();
53        jobFinished(params, false);
54        return true;
55    }
56
57    @Override
58    public boolean onStopJob(JobParameters params) {
59        return false;
60    }
61}