1 /*
2  * Copyright (C) 2019 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.managedprovisioning.analytics;
18 
19 import com.android.managedprovisioning.DevicePolicyProtos.DevicePolicyEvent;
20 import com.android.managedprovisioning.common.ProvisionLogger;
21 
22 import static com.android.internal.util.Preconditions.checkNotNull;
23 import static com.android.managedprovisioning.analytics.ProcessMetricsJobService.EXTRA_FILE_PATH;
24 
25 import android.app.job.JobInfo;
26 import android.app.job.JobScheduler;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.os.PersistableBundle;
30 
31 import java.io.File;
32 
33 /**
34  * Schedules the reading of the metrics written by {@link DeferredMetricsWriter}.
35  */
36 public class DeferredMetricsReader {
37 
38     private static final ComponentName PROCESS_METRICS_SERVICE_COMPONENT = new ComponentName(
39             "com.android.managedprovisioning", ProcessMetricsJobService.class.getName());
40     private static final int JOB_ID = 1;
41     private static final long MINIMUM_LATENCY = 10 * 60 * 1000;
42     private final File mFile;
43 
44     /**
45      * Constructs a new {@link DeferredMetricsReader}.
46      *
47      * <p>The specified {@link File} is deleted after everything has been read from it.
48      */
DeferredMetricsReader(File file)49     public DeferredMetricsReader(File file) {
50         mFile = checkNotNull(file);
51     }
52 
scheduleDumpMetrics(Context context)53     public void scheduleDumpMetrics(Context context) {
54         final JobInfo jobInfo = new JobInfo.Builder(JOB_ID, PROCESS_METRICS_SERVICE_COMPONENT)
55                 .setExtras(PersistableBundle.forPair(EXTRA_FILE_PATH, mFile.getAbsolutePath()))
56                 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
57                 .setMinimumLatency(MINIMUM_LATENCY)
58                 .setPersisted(true)
59                 .build();
60         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
61         if (jobScheduler != null) {
62             jobScheduler.schedule(jobInfo);
63         } else {
64             ProvisionLogger.logv("JobScheduler is null.");
65         }
66     }
67 }
68