1/*
2 * Copyright (C) 2016 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.storagemanager.automatic;
18
19import android.app.job.JobInfo;
20import android.app.job.JobScheduler;
21import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.Intent;
25import android.os.SystemProperties;
26import android.text.format.DateUtils;
27/**
28 * A {@link BroadcastReceiver} listening for {@link Intent#ACTION_BOOT_COMPLETED} broadcasts to
29 * schedule an automatic storage management job. Automatic storage management jobs are only
30 * scheduled once a day for a plugged in device.
31 */
32public class AutomaticStorageBroadcastReceiver extends BroadcastReceiver {
33    private static final int AUTOMATIC_STORAGE_JOB_ID = 0;
34    private static final long PERIOD = DateUtils.DAY_IN_MILLIS;
35    private static final String DEBUG_PERIOD_FLAG = "debug.asm.period";
36
37    @Override
38    public void onReceive(Context context, Intent intent) {
39        // Automatic deletion service
40        JobScheduler jobScheduler =
41                (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
42        ComponentName component = new ComponentName(context,
43                AutomaticStorageManagementJobService.class);
44        long periodicOverride = SystemProperties.getLong(DEBUG_PERIOD_FLAG, PERIOD);
45        JobInfo job = new JobInfo.Builder(AUTOMATIC_STORAGE_JOB_ID, component)
46                .setRequiresCharging(true)
47                .setRequiresDeviceIdle(true)
48                .setPeriodic(periodicOverride)
49                .build();
50        jobScheduler.schedule(job);
51    }
52}