Revert "Remove the daily job"

This reverts commit 8f7b83342794cb343feb76adbdf4f9efcaa9f439.

We need to land this again to better understand the failures observed
in T devices where we don't have enough telemetry signals. A follow-up
change will make make the daily job available only for dogfood
populations (userdebug).

Bug: 250929504
Test: m
Change-Id: I4f912afbe0b3ae9dfff70baecfd2468f50a2cf53
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
index 479ae7f..f55b42c 100644
--- a/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
@@ -44,10 +44,26 @@
  */
 public class IsolatedCompilationJobService extends JobService {
     private static final String TAG = IsolatedCompilationJobService.class.getName();
+    private static final int DAILY_JOB_ID = 5132250;
     private static final int STAGED_APEX_JOB_ID = 5132251;
 
     private final AtomicReference<CompilationJob> mCurrentJob = new AtomicReference<>();
 
+    static void scheduleDailyJob(JobScheduler scheduler) {
+        // TODO(b/205296305) Remove this
+        ComponentName serviceName =
+                new ComponentName("android", IsolatedCompilationJobService.class.getName());
+
+        int result = scheduler.schedule(new JobInfo.Builder(DAILY_JOB_ID, serviceName)
+                .setRequiresDeviceIdle(true)
+                .setRequiresCharging(true)
+                .setPeriodic(TimeUnit.DAYS.toMillis(1))
+                .build());
+        if (result != JobScheduler.RESULT_SUCCESS) {
+            Log.e(TAG, "Failed to schedule daily job");
+        }
+    }
+
     static void scheduleStagedApexJob(JobScheduler scheduler) {
         ComponentName serviceName =
                 new ComponentName("android", IsolatedCompilationJobService.class.getName());
@@ -68,6 +84,7 @@
                     IsolatedCompilationMetrics.SCHEDULING_FAILURE);
             Log.e(TAG, "Failed to schedule staged APEX job");
         }
+
     }
 
     static boolean isStagedApexJobScheduled(JobScheduler scheduler) {
@@ -76,7 +93,9 @@
 
     @Override
     public boolean onStartJob(JobParameters params) {
-        Log.i(TAG, "Starting job");
+        int jobId = params.getJobId();
+
+        Log.i(TAG, "Starting job " + jobId);
 
         // This function (and onStopJob) are only ever called on the main thread, so we don't have
         // to worry about two starts at once, or start and stop happening at once. But onCompletion
@@ -90,6 +109,9 @@
         }
 
         IsolatedCompilationMetrics metrics = new IsolatedCompilationMetrics();
+        if (jobId != STAGED_APEX_JOB_ID) {
+            metrics.disable();
+        }
 
         CompilationJob newJob = new CompilationJob(IsolatedCompilationJobService.this::onCompletion,
                 params, metrics);
@@ -102,7 +124,7 @@
             @Override
             public void run() {
                 try {
-                    newJob.start();
+                    newJob.start(jobId);
                 } catch (RuntimeException e) {
                     Log.e(TAG, "Starting CompilationJob failed", e);
                     metrics.onCompilationEnded(IsolatedCompilationMetrics.RESULT_FAILED_TO_START);
@@ -159,7 +181,7 @@
             mMetrics = requireNonNull(metrics);
         }
 
-        void start() {
+        void start(int jobId) {
             IBinder binder = ServiceManager.waitForService("android.system.composd");
             IIsolatedCompilationService composd =
                     IIsolatedCompilationService.Stub.asInterface(binder);
@@ -169,7 +191,13 @@
             }
 
             try {
-                ICompilationTask composTask = composd.startStagedApexCompile(this);
+                ICompilationTask composTask;
+                if (jobId == DAILY_JOB_ID) {
+                    composTask = composd.startTestCompile(
+                            IIsolatedCompilationService.ApexSource.NoStaged, this);
+                } else {
+                    composTask = composd.startStagedApexCompile(this);
+                }
                 mMetrics.onCompilationStarted();
                 mTask.set(composTask);
                 composTask.asBinder().linkToDeath(this, 0);
@@ -191,24 +219,16 @@
 
         private void cancelTask() {
             ICompilationTask task = mTask.getAndSet(null);
-            if (task == null) {
-                return;
-            }
-
-            Log.i(TAG, "Cancelling task");
-            try {
-                task.cancel();
-            } catch (RuntimeException | RemoteException e) {
-                // If canceling failed we'll assume it means that the task has already failed;
-                // there's nothing else we can do anyway.
-                Log.w(TAG, "Failed to cancel CompilationTask", e);
-            }
-
-            mMetrics.onCompilationJobCanceled(mParams.getStopReason());
-            try {
-                task.asBinder().unlinkToDeath(this, 0);
-            } catch (NoSuchElementException e) {
-                // Harmless
+            if (task != null) {
+                Log.i(TAG, "Cancelling task");
+                try {
+                    task.cancel();
+                    mMetrics.onCompilationJobCanceled(mParams.getStopReason());
+                } catch (RuntimeException | RemoteException e) {
+                    // If canceling failed we'll assume it means that the task has already failed;
+                    // there's nothing else we can do anyway.
+                    Log.w(TAG, "Failed to cancel CompilationTask", e);
+                }
             }
         }
 
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationMetrics.java b/compos/service/java/com/android/server/compos/IsolatedCompilationMetrics.java
index e333198..df590f3 100644
--- a/compos/service/java/com/android/server/compos/IsolatedCompilationMetrics.java
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationMetrics.java
@@ -75,14 +75,21 @@
             ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED__SCHEDULING_RESULT__SCHEDULING_SUCCESS;
 
     private long mCompilationStartTimeMs = 0;
+    private boolean mEnabled = true; // TODO(b/205296305) Remove this
 
     public static void onCompilationScheduled(@ScheduleJobResult int result) {
         ArtStatsLog.write(ArtStatsLog.ISOLATED_COMPILATION_SCHEDULED, result);
         Log.i(TAG, "ISOLATED_COMPILATION_SCHEDULED: " + result);
     }
 
+    public void disable() {
+        mEnabled = false;
+    }
+
     public void onCompilationStarted() {
-        mCompilationStartTimeMs = SystemClock.elapsedRealtime();
+        if (mEnabled) {
+            mCompilationStartTimeMs = SystemClock.elapsedRealtime();
+        }
     }
 
     public void onCompilationJobCanceled(@JobParameters.StopReason int jobStopReason) {
@@ -95,6 +102,9 @@
 
     private void statsLogPostCompilation(@CompilationResult int result,
                 @JobParameters.StopReason int jobStopReason) {
+        if (!mEnabled) {
+            return;
+        }
 
         long compilationTime = mCompilationStartTimeMs == 0 ? -1
                 : SystemClock.elapsedRealtime() - mCompilationStartTimeMs;
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationService.java b/compos/service/java/com/android/server/compos/IsolatedCompilationService.java
index b2fcbe0..11e3743 100644
--- a/compos/service/java/com/android/server/compos/IsolatedCompilationService.java
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationService.java
@@ -67,6 +67,7 @@
             return;
         }
 
+        IsolatedCompilationJobService.scheduleDailyJob(scheduler);
         StagedApexObserver.registerForStagedApexUpdates(scheduler);
     }