1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE2.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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.android.server.camera;
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.ContentResolver;
25 import android.content.Context;
26 import android.util.Slog;
27 
28 import java.util.concurrent.TimeUnit;
29 
30 import com.android.server.LocalServices;
31 
32 /**
33  * A JobService to periodically collect camera usage stats.
34  */
35 public class CameraStatsJobService extends JobService {
36     private static final String TAG = "CameraStatsJobService";
37 
38     // Must be unique within UID (system service)
39     private static final int CAMERA_REPORTING_JOB_ID = 0xCA3E7A;
40 
41     private static ComponentName sCameraStatsJobServiceName = new ComponentName(
42             "android",
43             CameraStatsJobService.class.getName());
44 
45     @Override
onStartJob(JobParameters params)46     public boolean onStartJob(JobParameters params) {
47         CameraServiceProxy serviceProxy = LocalServices.getService(CameraServiceProxy.class);
48         if (serviceProxy == null) {
49             Slog.w(TAG, "Can't collect camera usage stats - no camera service proxy found");
50             return false;
51         }
52 
53         serviceProxy.dumpUsageEvents();
54         return false;
55     }
56 
57     @Override
onStopJob(JobParameters params)58     public boolean onStopJob(JobParameters params) {
59         // All work is done in onStartJob, so nothing to stop here
60         return false;
61     }
62 
schedule(Context context)63     public static void schedule(Context context) {
64 
65         JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
66         if (js == null) {
67             Slog.e(TAG, "Can't collect camera usage stats - no Job Scheduler");
68             return;
69         }
70         js.schedule(new JobInfo.Builder(CAMERA_REPORTING_JOB_ID, sCameraStatsJobServiceName)
71                 .setMinimumLatency(TimeUnit.DAYS.toMillis(1))
72                 .setRequiresDeviceIdle(true)
73                 .build());
74 
75     }
76 
77 }
78