blob: 2ae424dd4b1b5a3a3a235df3628226977bf84858 [file] [log] [blame]
Alan Stokesbe9ec972018-12-10 14:07:47 +00001/*
2 * Copyright 2018 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.server.pm;
18
19import android.app.job.JobInfo;
20import android.app.job.JobParameters;
21import android.app.job.JobScheduler;
22import android.app.job.JobService;
23import android.content.ComponentName;
24import android.content.Context;
25import android.os.ServiceManager;
26import android.util.Log;
27
28import com.android.server.pm.dex.DexLogger;
29
30import java.util.concurrent.TimeUnit;
31
32/**
33 * Scheduled job to trigger logging of app dynamic code loading. This runs daily while idle and
34 * charging. The actual logging is performed by {@link DexLogger}.
35 * {@hide}
36 */
37public class DynamicCodeLoggingService extends JobService {
38 private static final String TAG = DynamicCodeLoggingService.class.getName();
39
40 private static final int JOB_ID = 2030028;
41 private static final long PERIOD_MILLIS = TimeUnit.DAYS.toMillis(1);
42
43 private volatile boolean mStopRequested = false;
44
45 private static final boolean DEBUG = false;
46
47 /**
48 * Schedule our job with the {@link JobScheduler}.
49 */
50 public static void schedule(Context context) {
51 ComponentName serviceName = new ComponentName(
52 "android", DynamicCodeLoggingService.class.getName());
53
54 JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
55 js.schedule(new JobInfo.Builder(JOB_ID, serviceName)
56 .setRequiresDeviceIdle(true)
57 .setRequiresCharging(true)
58 .setPeriodic(PERIOD_MILLIS)
59 .build());
60 if (DEBUG) {
61 Log.d(TAG, "Job scheduled");
62 }
63 }
64
65 @Override
66 public boolean onStartJob(JobParameters params) {
67 if (DEBUG) {
68 Log.d(TAG, "onStartJob");
69 }
70 mStopRequested = false;
71 new IdleLoggingThread(params).start();
72 return true; // Job is running on another thread
73 }
74
75 @Override
76 public boolean onStopJob(JobParameters params) {
77 if (DEBUG) {
78 Log.d(TAG, "onStopJob");
79 }
80 mStopRequested = true;
81 return true; // Requests job be re-scheduled.
82 }
83
84 private class IdleLoggingThread extends Thread {
85 private final JobParameters mParams;
86
87 IdleLoggingThread(JobParameters params) {
88 super("DynamicCodeLoggingService_IdleLoggingJob");
89 mParams = params;
90 }
91
92 @Override
93 public void run() {
94 if (DEBUG) {
95 Log.d(TAG, "Starting logging run");
96 }
97
98 PackageManagerService pm = (PackageManagerService) ServiceManager.getService("package");
99 DexLogger dexLogger = pm.getDexManager().getDexLogger();
100 for (String packageName : dexLogger.getAllPackagesWithDynamicCodeLoading()) {
101 if (mStopRequested) {
102 Log.w(TAG, "Stopping logging run at scheduler request");
103 return;
104 }
105
106 dexLogger.logDynamicCodeLoading(packageName);
107 }
108
109 jobFinished(mParams, /* reschedule */ false);
110 if (DEBUG) {
111 Log.d(TAG, "Finished logging run");
112 }
113 }
114 }
115}