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