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.Context; 25 import android.util.Slog; 26 27 import com.android.server.LocalServices; 28 29 import java.util.concurrent.TimeUnit; 30 31 /** 32 * A JobService to periodically collect camera usage stats. 33 */ 34 public class CameraStatsJobService extends JobService { 35 private static final String TAG = "CameraStatsJobService"; 36 37 // Must be unique within UID (system service) 38 private static final int CAMERA_REPORTING_JOB_ID = 0xCA3E7A; 39 40 private static ComponentName sCameraStatsJobServiceName = new ComponentName( 41 "android", 42 CameraStatsJobService.class.getName()); 43 44 @Override onStartJob(JobParameters params)45 public boolean onStartJob(JobParameters params) { 46 CameraServiceProxy serviceProxy = LocalServices.getService(CameraServiceProxy.class); 47 if (serviceProxy == null) { 48 Slog.w(TAG, "Can't collect camera usage stats - no camera service proxy found"); 49 return false; 50 } 51 52 serviceProxy.dumpCameraEvents(); 53 return false; 54 } 55 56 @Override onStopJob(JobParameters params)57 public boolean onStopJob(JobParameters params) { 58 // All work is done in onStartJob, so nothing to stop here 59 return false; 60 } 61 schedule(Context context)62 public static void schedule(Context context) { 63 64 JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); 65 if (js == null) { 66 Slog.e(TAG, "Can't collect camera usage stats - no Job Scheduler"); 67 return; 68 } 69 js.schedule(new JobInfo.Builder(CAMERA_REPORTING_JOB_ID, sCameraStatsJobServiceName) 70 .setMinimumLatency(TimeUnit.DAYS.toMillis(1)) 71 .setRequiresDeviceIdle(true) 72 .build()); 73 74 } 75 76 } 77