1 /*
2  * Copyright (C) 2024 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.ondevicepersonalization.services.serviceflow;
18 
19 import com.android.ondevicepersonalization.services.OnDevicePersonalizationExecutors;
20 
21 /** Orchestrator that handles the scheduling of all service flows. */
22 public class ServiceFlowOrchestrator {
23 
ServiceFlowOrchestrator()24     ServiceFlowOrchestrator() {}
25 
26     private static class ServiceFlowOrchestratorLazyInstanceHolder {
27         static final ServiceFlowOrchestrator LAZY_INSTANCE =
28                 new ServiceFlowOrchestrator();
29     }
30 
31     /** Returns the global ServiceFlowOrchestrator. */
getInstance()32     public static ServiceFlowOrchestrator getInstance() {
33         return ServiceFlowOrchestratorLazyInstanceHolder.LAZY_INSTANCE;
34     }
35 
36     /** Schedules a given service flow task with the orchestrator. */
schedule(ServiceFlowType serviceFlowType, Object... args)37     public void schedule(ServiceFlowType serviceFlowType, Object... args) {
38         ServiceFlow serviceFlow = ServiceFlowFactory.createInstance(serviceFlowType, args);
39 
40         ServiceFlowTask serviceFlowTask =
41                 new ServiceFlowTask(serviceFlowType, serviceFlow);
42 
43         var unused = switch (serviceFlowType.getPriority()) {
44             case HIGH -> OnDevicePersonalizationExecutors.getHighPriorityBackgroundExecutor()
45                     .submit(serviceFlowTask::run);
46             case NORMAL -> OnDevicePersonalizationExecutors.getBackgroundExecutor()
47                     .submit(serviceFlowTask::run);
48             case LOW -> OnDevicePersonalizationExecutors.getLowPriorityBackgroundExecutor()
49                     .submit(serviceFlowTask::run);
50         };
51     }
52 }
53